1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.reboot; |
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.net.URL; |
20 | |
import java.security.AccessController; |
21 | |
import java.security.PrivilegedAction; |
22 | |
|
23 | |
public final class MuleContainerBootstrapUtils |
24 | |
{ |
25 | |
private static final String MULE_APPS_FILENAME = "apps"; |
26 | 0 | private static final String MULE_LIB_FILENAME = "lib" + File.separator + "mule"; |
27 | |
|
28 | |
public static final String MULE_LOCAL_JAR_FILENAME = "mule-local-install.jar"; |
29 | |
|
30 | |
private MuleContainerBootstrapUtils() |
31 | 0 | { |
32 | |
|
33 | 0 | } |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public static boolean isStandalone() |
40 | |
{ |
41 | |
|
42 | 0 | return getMuleHome() != null; |
43 | |
} |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public static File getMuleHome() |
49 | |
{ |
50 | 0 | final String muleHome = System.getProperty("mule.home"); |
51 | 0 | return muleHome != null ? new File(muleHome) : null; |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public static File getMuleAppsDir() |
58 | |
{ |
59 | 0 | return isStandalone() ? new File(getMuleHome(), MULE_APPS_FILENAME) : null; |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public static File getMuleLibDir() |
66 | |
{ |
67 | 0 | return isStandalone() ? new File(getMuleHome(), MULE_LIB_FILENAME) : null; |
68 | |
} |
69 | |
|
70 | |
public static File getMuleLocalJarFile() |
71 | |
{ |
72 | 0 | return isStandalone() ? new File(getMuleLibDir(), MULE_LOCAL_JAR_FILENAME) : null; |
73 | |
} |
74 | |
|
75 | |
public static class ProxyInfo |
76 | |
{ |
77 | |
String host; |
78 | |
String port; |
79 | |
String username; |
80 | |
String password; |
81 | |
|
82 | |
public ProxyInfo(String host, String port) |
83 | |
{ |
84 | 0 | this(host, port, null, null); |
85 | 0 | } |
86 | |
|
87 | |
public ProxyInfo(String host, String port, String username, String password) |
88 | 0 | { |
89 | 0 | this.host = host; |
90 | 0 | this.port = port; |
91 | 0 | this.username = username; |
92 | 0 | this.password = password; |
93 | 0 | } |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public static URL getResource(final String resourceName, final Class<?> callingClass) |
105 | |
{ |
106 | 0 | URL url = AccessController.doPrivileged(new PrivilegedAction<URL>() |
107 | 0 | { |
108 | |
public URL run() |
109 | |
{ |
110 | 0 | final ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
111 | 0 | return cl != null ? cl.getResource(resourceName) : null; |
112 | |
} |
113 | |
}); |
114 | |
|
115 | 0 | if (url == null) |
116 | |
{ |
117 | 0 | url = AccessController.doPrivileged(new PrivilegedAction<URL>() |
118 | 0 | { |
119 | |
public URL run() |
120 | |
{ |
121 | 0 | return MuleContainerBootstrap.class.getClassLoader().getResource(resourceName); |
122 | |
} |
123 | |
}); |
124 | |
} |
125 | |
|
126 | 0 | if (url == null) |
127 | |
{ |
128 | 0 | url = AccessController.doPrivileged(new PrivilegedAction<URL>() |
129 | 0 | { |
130 | |
public URL run() |
131 | |
{ |
132 | 0 | return callingClass.getClassLoader().getResource(resourceName); |
133 | |
} |
134 | |
}); |
135 | |
} |
136 | |
|
137 | 0 | return url; |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public static boolean renameFile(File srcFile, File destFile) throws IOException |
144 | |
{ |
145 | 0 | boolean isRenamed = false; |
146 | 0 | if (srcFile != null && destFile != null) |
147 | |
{ |
148 | 0 | if (!destFile.exists()) |
149 | |
{ |
150 | 0 | if (srcFile.isFile()) |
151 | |
{ |
152 | 0 | isRenamed = srcFile.renameTo(destFile); |
153 | 0 | if (!isRenamed && srcFile.exists()) |
154 | |
{ |
155 | 0 | isRenamed = renameFileHard(srcFile, destFile); |
156 | |
} |
157 | |
} |
158 | |
} |
159 | |
} |
160 | 0 | return isRenamed; |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public static boolean renameFileHard(File srcFile, File destFile) throws IOException |
167 | |
{ |
168 | 0 | boolean isRenamed = false; |
169 | 0 | if (srcFile != null && destFile != null) |
170 | |
{ |
171 | 0 | if (!destFile.exists()) |
172 | |
{ |
173 | 0 | if (srcFile.isFile()) |
174 | |
{ |
175 | 0 | FileInputStream in = null; |
176 | 0 | FileOutputStream out = null; |
177 | |
try |
178 | |
{ |
179 | 0 | in = new FileInputStream(srcFile); |
180 | 0 | out = new FileOutputStream(destFile); |
181 | 0 | out.getChannel().transferFrom(in.getChannel(), 0, srcFile.length()); |
182 | 0 | isRenamed = true; |
183 | |
} |
184 | |
finally |
185 | |
{ |
186 | 0 | if (in != null) |
187 | |
{ |
188 | 0 | in.close(); |
189 | |
} |
190 | 0 | if (out != null) |
191 | |
{ |
192 | 0 | out.close(); |
193 | |
} |
194 | |
} |
195 | 0 | if (isRenamed) |
196 | |
{ |
197 | 0 | srcFile.delete(); |
198 | |
} |
199 | |
else |
200 | |
{ |
201 | 0 | destFile.delete(); |
202 | |
} |
203 | |
} |
204 | |
} |
205 | |
} |
206 | 0 | return isRenamed; |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public static int copy(InputStream input, OutputStream output) throws IOException |
213 | |
{ |
214 | 0 | long count = copyLarge(input, output); |
215 | 0 | if (count > Integer.MAX_VALUE) |
216 | |
{ |
217 | 0 | return -1; |
218 | |
} |
219 | 0 | return (int) count; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public static long copyLarge(InputStream input, OutputStream output) throws IOException |
226 | |
{ |
227 | 0 | byte[] buffer = new byte[1024 * 4]; |
228 | 0 | long count = 0; |
229 | 0 | int n = 0; |
230 | 0 | while (-1 != (n = input.read(buffer))) |
231 | |
{ |
232 | 0 | output.write(buffer, 0, n); |
233 | 0 | count += n; |
234 | |
} |
235 | 0 | return count; |
236 | |
} |
237 | |
} |