1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.boot; |
12 | |
|
13 | |
import java.io.File; |
14 | |
import java.io.FileInputStream; |
15 | |
import java.io.FileOutputStream; |
16 | |
import java.io.IOException; |
17 | |
import java.io.InputStream; |
18 | |
import java.io.OutputStream; |
19 | |
import java.lang.reflect.InvocationTargetException; |
20 | |
import java.lang.reflect.Method; |
21 | |
import java.net.URL; |
22 | |
import java.net.URLClassLoader; |
23 | |
import java.security.AccessController; |
24 | |
import java.security.PrivilegedAction; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
|
28 | |
public final class MuleBootstrapUtils |
29 | |
{ |
30 | 0 | private static final String MULE_LIB_FILENAME = "lib" + File.separator + "mule"; |
31 | 0 | private static final String MULE_HOME = System.getProperty("mule.home"); |
32 | |
|
33 | |
public static final String MULE_LOCAL_JAR_FILENAME = "mule-local-install.jar"; |
34 | |
|
35 | |
private MuleBootstrapUtils() |
36 | 0 | { |
37 | |
|
38 | 0 | } |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public static boolean isStandalone() |
45 | |
{ |
46 | |
|
47 | 0 | return MULE_HOME != null; |
48 | |
} |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static File getMuleHomeFile() |
54 | |
{ |
55 | 0 | return isStandalone() ? new File(MULE_HOME) : null; |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public static File getMuleLibDir() |
62 | |
{ |
63 | 0 | return isStandalone() ? new File(MULE_HOME + File.separator + MULE_LIB_FILENAME) : null; |
64 | |
} |
65 | |
|
66 | |
public static File getMuleLocalJarFile() |
67 | |
{ |
68 | 0 | return isStandalone() ? new File(getMuleLibDir(), MULE_LOCAL_JAR_FILENAME) : null; |
69 | |
} |
70 | |
|
71 | |
public static void addLocalJarFilesToClasspath(File muleHome, File muleBase) throws Exception |
72 | |
{ |
73 | 0 | DefaultMuleClassPathConfig classPath = new DefaultMuleClassPathConfig(muleHome, muleBase); |
74 | 0 | addLibrariesToClasspath(classPath.getURLs()); |
75 | 0 | } |
76 | |
|
77 | |
public static void addLibrariesToClasspath(List urls) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException |
78 | |
{ |
79 | 0 | ClassLoader sys = ClassLoader.getSystemClassLoader(); |
80 | 0 | if (!(sys instanceof URLClassLoader)) |
81 | |
{ |
82 | 0 | throw new IllegalArgumentException( |
83 | |
"PANIC: Mule has been started with an unsupported classloader: " + sys.getClass().getName() |
84 | |
+ ". " + "Please report this error to user<at>mule<dot>codehaus<dot>org"); |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | 0 | URLClassLoader sysCl = (URLClassLoader) sys; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | 0 | Class refClass = URLClassLoader.class; |
110 | 0 | Method methodAddUrl = refClass.getDeclaredMethod("addURL", new Class[]{URL.class}); |
111 | 0 | methodAddUrl.setAccessible(true); |
112 | 0 | for (Iterator it = urls.iterator(); it.hasNext();) |
113 | |
{ |
114 | 0 | URL url = (URL) it.next(); |
115 | 0 | methodAddUrl.invoke(sysCl, new Object[]{url}); |
116 | 0 | } |
117 | 0 | } |
118 | |
|
119 | |
public static class ProxyInfo |
120 | |
{ |
121 | |
String host; |
122 | |
String port; |
123 | |
String username; |
124 | |
String password; |
125 | |
|
126 | |
public ProxyInfo(String host, String port) |
127 | |
{ |
128 | 0 | this(host, port, null, null); |
129 | 0 | } |
130 | |
|
131 | |
public ProxyInfo(String host, String port, String username, String password) |
132 | 0 | { |
133 | 0 | this.host = host; |
134 | 0 | this.port = port; |
135 | 0 | this.username = username; |
136 | 0 | this.password = password; |
137 | 0 | } |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public static URL getResource(final String resourceName, final Class callingClass) |
149 | |
{ |
150 | 0 | URL url = (URL) AccessController.doPrivileged(new PrivilegedAction() |
151 | 0 | { |
152 | |
public Object run() |
153 | |
{ |
154 | 0 | final ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
155 | 0 | return cl != null ? cl.getResource(resourceName) : null; |
156 | |
} |
157 | |
}); |
158 | |
|
159 | 0 | if (url == null) |
160 | |
{ |
161 | 0 | url = (URL) AccessController.doPrivileged(new PrivilegedAction() |
162 | 0 | { |
163 | |
public Object run() |
164 | |
{ |
165 | 0 | return MuleBootstrap.class.getClassLoader().getResource(resourceName); |
166 | |
} |
167 | |
}); |
168 | |
} |
169 | |
|
170 | 0 | if (url == null) |
171 | |
{ |
172 | 0 | url = (URL) AccessController.doPrivileged(new PrivilegedAction() |
173 | 0 | { |
174 | |
public Object run() |
175 | |
{ |
176 | 0 | return callingClass.getClassLoader().getResource(resourceName); |
177 | |
} |
178 | |
}); |
179 | |
} |
180 | |
|
181 | 0 | return url; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public static boolean renameFile(File srcFile, File destFile) throws IOException |
188 | |
{ |
189 | 0 | boolean isRenamed = false; |
190 | 0 | if (srcFile != null && destFile != null) |
191 | |
{ |
192 | 0 | if (!destFile.exists()) |
193 | |
{ |
194 | 0 | if (srcFile.isFile()) |
195 | |
{ |
196 | 0 | isRenamed = srcFile.renameTo(destFile); |
197 | 0 | if (!isRenamed && srcFile.exists()) |
198 | |
{ |
199 | 0 | isRenamed = renameFileHard(srcFile, destFile); |
200 | |
} |
201 | |
} |
202 | |
} |
203 | |
} |
204 | 0 | return isRenamed; |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public static boolean renameFileHard(File srcFile, File destFile) throws IOException |
211 | |
{ |
212 | 0 | boolean isRenamed = false; |
213 | 0 | if (srcFile != null && destFile != null) |
214 | |
{ |
215 | 0 | if (!destFile.exists()) |
216 | |
{ |
217 | 0 | if (srcFile.isFile()) |
218 | |
{ |
219 | 0 | FileInputStream in = null; |
220 | 0 | FileOutputStream out = null; |
221 | |
try |
222 | |
{ |
223 | 0 | in = new FileInputStream(srcFile); |
224 | 0 | out = new FileOutputStream(destFile); |
225 | 0 | out.getChannel().transferFrom(in.getChannel(), 0, srcFile.length()); |
226 | 0 | isRenamed = true; |
227 | |
} |
228 | |
finally |
229 | |
{ |
230 | 0 | if (in != null) |
231 | |
{ |
232 | 0 | in.close(); |
233 | |
} |
234 | 0 | if (out != null) |
235 | |
{ |
236 | 0 | out.close(); |
237 | |
} |
238 | |
} |
239 | 0 | if (isRenamed) |
240 | |
{ |
241 | 0 | srcFile.delete(); |
242 | |
} |
243 | |
else |
244 | |
{ |
245 | 0 | destFile.delete(); |
246 | |
} |
247 | |
} |
248 | |
} |
249 | |
} |
250 | 0 | return isRenamed; |
251 | |
} |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
public static int copy(InputStream input, OutputStream output) throws IOException { |
257 | 0 | long count = copyLarge(input, output); |
258 | 0 | if (count > Integer.MAX_VALUE) { |
259 | 0 | return -1; |
260 | |
} |
261 | 0 | return (int) count; |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
public static long copyLarge(InputStream input, OutputStream output) throws IOException { |
268 | 0 | byte[] buffer = new byte[1024 * 4]; |
269 | 0 | long count = 0; |
270 | 0 | int n = 0; |
271 | 0 | while (-1 != (n = input.read(buffer))) { |
272 | 0 | output.write(buffer, 0, n); |
273 | 0 | count += n; |
274 | |
} |
275 | 0 | return count; |
276 | |
} |
277 | |
} |