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