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