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