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