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