1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import java.io.ByteArrayOutputStream; |
14 | |
import java.io.File; |
15 | |
import java.io.FileInputStream; |
16 | |
import java.io.FileOutputStream; |
17 | |
import java.io.IOException; |
18 | |
import java.io.InputStream; |
19 | |
import java.io.OutputStream; |
20 | |
import java.util.Enumeration; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.LinkedHashMap; |
23 | |
import java.util.jar.JarEntry; |
24 | |
import java.util.jar.JarFile; |
25 | |
import java.util.jar.JarOutputStream; |
26 | |
import java.util.zip.ZipEntry; |
27 | |
|
28 | |
import org.apache.commons.io.IOUtils; |
29 | |
import org.apache.commons.logging.Log; |
30 | |
import org.apache.commons.logging.LogFactory; |
31 | |
|
32 | |
public final class JarUtils |
33 | |
{ |
34 | 0 | private static final String MULE_MODULE_FILENAME = "lib" + File.separator + "module"; |
35 | 0 | private static final String MULE_LIB_FILENAME = "lib" + File.separator + "mule"; |
36 | 0 | private static final String MULE_HOME = System.getProperty("mule.home"); |
37 | |
|
38 | |
public static final String MULE_LOCAL_JAR_FILENAME = "mule-local-install.jar"; |
39 | |
|
40 | 0 | private static final Log logger = LogFactory.getLog(JarUtils.class); |
41 | |
|
42 | |
private JarUtils() |
43 | 0 | { |
44 | |
|
45 | 0 | } |
46 | |
|
47 | |
public static LinkedHashMap readJarFileEntries(File jarFile) throws Exception |
48 | |
{ |
49 | 0 | LinkedHashMap entries = new LinkedHashMap(); |
50 | 0 | JarFile jarFileWrapper = null; |
51 | 0 | if (jarFile != null) |
52 | |
{ |
53 | 0 | logger.debug("Reading jar entries from " + jarFile.getAbsolutePath()); |
54 | |
try |
55 | |
{ |
56 | 0 | jarFileWrapper = new JarFile(jarFile); |
57 | 0 | Enumeration iter = jarFileWrapper.entries(); |
58 | 0 | while (iter.hasMoreElements()) |
59 | |
{ |
60 | 0 | ZipEntry zipEntry = (ZipEntry) iter.nextElement(); |
61 | 0 | InputStream entryStream = jarFileWrapper.getInputStream(zipEntry); |
62 | 0 | ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(); |
63 | |
try |
64 | |
{ |
65 | 0 | IOUtils.copy(entryStream, byteArrayStream); |
66 | 0 | entries.put(zipEntry.getName(), byteArrayStream.toByteArray()); |
67 | 0 | logger.debug("Read jar entry " + zipEntry.getName() + " from " + jarFile.getAbsolutePath()); |
68 | |
} |
69 | |
finally |
70 | |
{ |
71 | 0 | byteArrayStream.close(); |
72 | 0 | } |
73 | 0 | } |
74 | |
} |
75 | |
finally |
76 | |
{ |
77 | 0 | if (jarFileWrapper != null) |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | 0 | jarFileWrapper.close(); |
82 | |
} |
83 | 0 | catch (Exception ignore) |
84 | |
{ |
85 | 0 | logger.debug(ignore); |
86 | 0 | } |
87 | |
} |
88 | |
} |
89 | |
} |
90 | 0 | return entries; |
91 | |
} |
92 | |
|
93 | |
public static void appendJarFileEntries(File jarFile, LinkedHashMap entries) throws Exception |
94 | |
{ |
95 | 0 | if (entries != null) |
96 | |
{ |
97 | 0 | LinkedHashMap combinedEntries = readJarFileEntries(jarFile); |
98 | 0 | combinedEntries.putAll(entries); |
99 | 0 | File tmpJarFile = File.createTempFile(jarFile.getName(), null); |
100 | 0 | createJarFileEntries(tmpJarFile, combinedEntries); |
101 | 0 | jarFile.delete(); |
102 | 0 | FileUtils.renameFile(tmpJarFile, jarFile); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
public static void createJarFileEntries(File jarFile, LinkedHashMap entries) throws Exception |
107 | |
{ |
108 | 0 | JarOutputStream jarStream = null; |
109 | 0 | FileOutputStream fileStream = null; |
110 | |
|
111 | 0 | if (jarFile != null) |
112 | |
{ |
113 | 0 | logger.debug("Creating jar file " + jarFile.getAbsolutePath()); |
114 | |
|
115 | |
try |
116 | |
{ |
117 | 0 | fileStream = new FileOutputStream(jarFile); |
118 | 0 | jarStream = new JarOutputStream(fileStream); |
119 | |
|
120 | 0 | if (entries != null && !entries.isEmpty()) |
121 | |
{ |
122 | 0 | Iterator iter = entries.keySet().iterator(); |
123 | 0 | while (iter.hasNext()) |
124 | |
{ |
125 | 0 | String jarFilePath = (String) iter.next(); |
126 | 0 | Object content = entries.get(jarFilePath); |
127 | |
|
128 | 0 | JarEntry entry = new JarEntry(jarFilePath); |
129 | 0 | jarStream.putNextEntry(entry); |
130 | |
|
131 | 0 | logger.debug("Adding jar entry " + jarFilePath + " to " + jarFile.getAbsolutePath()); |
132 | |
|
133 | 0 | if (content instanceof String) |
134 | |
{ |
135 | 0 | writeJarEntry(jarStream, ((String) content).getBytes()); |
136 | |
} |
137 | 0 | else if (content instanceof byte[]) |
138 | |
{ |
139 | 0 | writeJarEntry(jarStream, (byte[]) content); |
140 | |
} |
141 | 0 | else if (content instanceof File) |
142 | |
{ |
143 | 0 | writeJarEntry(jarStream, (File) content); |
144 | |
} |
145 | 0 | } |
146 | |
} |
147 | |
|
148 | 0 | jarStream.flush(); |
149 | 0 | fileStream.getFD().sync(); |
150 | |
} |
151 | |
finally |
152 | |
{ |
153 | 0 | if (jarStream != null) |
154 | |
{ |
155 | |
try |
156 | |
{ |
157 | 0 | jarStream.close(); |
158 | |
} |
159 | 0 | catch (Exception jarNotClosed) |
160 | |
{ |
161 | 0 | logger.debug(jarNotClosed); |
162 | 0 | } |
163 | |
} |
164 | 0 | if (fileStream != null) |
165 | |
{ |
166 | |
try |
167 | |
{ |
168 | 0 | fileStream.close(); |
169 | |
} |
170 | 0 | catch (Exception fileNotClosed) |
171 | |
{ |
172 | 0 | logger.debug(fileNotClosed); |
173 | 0 | } |
174 | |
} |
175 | |
} |
176 | |
} |
177 | 0 | } |
178 | |
|
179 | |
private static void writeJarEntry(OutputStream stream, byte[] entry) throws IOException |
180 | |
{ |
181 | 0 | stream.write(entry, 0, entry.length); |
182 | 0 | } |
183 | |
|
184 | |
private static void writeJarEntry(OutputStream stream, File entry) throws IOException |
185 | |
{ |
186 | 0 | FileInputStream fileContentStream = null; |
187 | |
try |
188 | |
{ |
189 | 0 | fileContentStream = new FileInputStream(entry); |
190 | 0 | IOUtils.copy(fileContentStream, stream); |
191 | |
} |
192 | |
finally |
193 | |
{ |
194 | 0 | if (fileContentStream != null) |
195 | |
{ |
196 | |
try |
197 | |
{ |
198 | 0 | fileContentStream.close(); |
199 | |
} |
200 | 0 | catch (Exception fileContentNotClosed) |
201 | |
{ |
202 | 0 | logger.debug(fileContentNotClosed); |
203 | 0 | } |
204 | |
} |
205 | |
} |
206 | 0 | } |
207 | |
|
208 | |
public static File getMuleHomeFile() |
209 | |
{ |
210 | 0 | return new File(MULE_HOME); |
211 | |
} |
212 | |
|
213 | |
public static File getMuleLibDir() |
214 | |
{ |
215 | 0 | return new File(MULE_HOME + File.separator + MULE_LIB_FILENAME); |
216 | |
} |
217 | |
|
218 | |
public static File getMuleModuleDir() |
219 | |
{ |
220 | 0 | return new File(MULE_HOME + File.separator + MULE_MODULE_FILENAME); |
221 | |
} |
222 | |
|
223 | |
public static File getMuleLocalJarFile() |
224 | |
{ |
225 | 0 | return new File(getMuleLibDir(), MULE_LOCAL_JAR_FILENAME); |
226 | |
} |
227 | |
} |