1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.launcher; |
8 | |
|
9 | |
import org.mule.api.config.MuleProperties; |
10 | |
import org.mule.util.FileUtils; |
11 | |
import org.mule.util.SystemUtils; |
12 | |
|
13 | |
import java.io.File; |
14 | |
import java.io.IOException; |
15 | |
import java.net.URL; |
16 | |
import java.util.ArrayList; |
17 | |
import java.util.Collection; |
18 | |
import java.util.List; |
19 | |
|
20 | |
import org.apache.commons.logging.Log; |
21 | |
import org.apache.commons.logging.LogFactory; |
22 | |
|
23 | |
public class MuleApplicationClassLoader extends GoodCitizenClassLoader |
24 | |
{ |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public static final String PATH_LIBRARY = "lib"; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public static final String PATH_CLASSES = "classes"; |
35 | |
|
36 | 0 | protected static final URL[] CLASSPATH_EMPTY = new URL[0]; |
37 | 0 | protected final transient Log logger = LogFactory.getLog(getClass()); |
38 | |
|
39 | 0 | protected List<ShutdownListener> shutdownListeners = new ArrayList<ShutdownListener>(); |
40 | |
|
41 | |
private String appName; |
42 | |
|
43 | |
public MuleApplicationClassLoader(String appName, ClassLoader parentCl) |
44 | |
{ |
45 | 0 | super(CLASSPATH_EMPTY, parentCl); |
46 | 0 | this.appName = appName; |
47 | |
try |
48 | |
{ |
49 | |
|
50 | 0 | final String muleHome = System.getProperty(MuleProperties.MULE_HOME_DIRECTORY_PROPERTY); |
51 | 0 | String configPath = String.format("%s/apps/%s", muleHome, appName); |
52 | 0 | File parentFile = new File(configPath); |
53 | 0 | File classesDir = new File(parentFile, PATH_CLASSES); |
54 | 0 | addURL(classesDir.toURI().toURL()); |
55 | |
|
56 | 0 | File libDir = new File(parentFile, PATH_LIBRARY); |
57 | |
|
58 | 0 | if (libDir.exists() && libDir.canRead()) |
59 | |
{ |
60 | |
@SuppressWarnings("unchecked") |
61 | 0 | Collection<File> jars = FileUtils.listFiles(libDir, new String[] {"jar"}, false); |
62 | |
|
63 | 0 | if (!jars.isEmpty() && logger.isInfoEnabled()) |
64 | |
{ |
65 | 0 | StringBuilder sb = new StringBuilder(); |
66 | 0 | sb.append(String.format("[%s] Loading the following jars:%n", appName)); |
67 | 0 | sb.append("=============================").append(SystemUtils.LINE_SEPARATOR); |
68 | |
|
69 | 0 | for (File jar : jars) |
70 | |
{ |
71 | 0 | sb.append(jar.toURI().toURL()).append(SystemUtils.LINE_SEPARATOR); |
72 | |
} |
73 | |
|
74 | 0 | sb.append("=============================").append(SystemUtils.LINE_SEPARATOR); |
75 | |
|
76 | 0 | logger.info(sb.toString()); |
77 | |
} |
78 | |
|
79 | 0 | for (File jar : jars) |
80 | |
{ |
81 | 0 | addURL(jar.toURI().toURL()); |
82 | |
} |
83 | |
} |
84 | |
|
85 | |
} |
86 | 0 | catch (IOException e) |
87 | |
{ |
88 | 0 | if (logger.isDebugEnabled()) |
89 | |
{ |
90 | 0 | logger.debug(String.format("[%s]", appName), e); |
91 | |
} |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | |
@Override |
96 | |
public void close() |
97 | |
{ |
98 | 0 | for (ShutdownListener listener : shutdownListeners) |
99 | |
{ |
100 | |
try |
101 | |
{ |
102 | 0 | listener.execute(); |
103 | |
} |
104 | 0 | catch (Exception e) |
105 | |
{ |
106 | 0 | logger.error(e); |
107 | 0 | } |
108 | |
} |
109 | 0 | super.close(); |
110 | 0 | } |
111 | |
|
112 | |
public void addShutdownListener(ShutdownListener listener) |
113 | |
{ |
114 | 0 | this.shutdownListeners.add(listener); |
115 | 0 | } |
116 | |
|
117 | |
public String getAppName() |
118 | |
{ |
119 | 0 | return appName; |
120 | |
} |
121 | |
|
122 | |
@Override |
123 | |
public String toString() |
124 | |
{ |
125 | 0 | return String.format("%s[%s]@%s", getClass().getName(), |
126 | |
appName, |
127 | |
Integer.toHexString(System.identityHashCode(this))); |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public interface ShutdownListener |
134 | |
{ |
135 | |
void execute(); |
136 | |
} |
137 | |
} |