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