1
2
3
4
5
6
7 package org.mule.util;
8
9 import org.mule.module.boot.MuleBootstrapUtils;
10
11 import java.io.ByteArrayOutputStream;
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.util.Enumeration;
19 import java.util.Iterator;
20 import java.util.LinkedHashMap;
21 import java.util.jar.JarEntry;
22 import java.util.jar.JarFile;
23 import java.util.jar.JarOutputStream;
24 import java.util.zip.ZipEntry;
25
26 public final class JarUtils
27 {
28 private JarUtils()
29 {
30
31 }
32
33 public static LinkedHashMap readJarFileEntries(File jarFile) throws Exception
34 {
35 LinkedHashMap entries = new LinkedHashMap();
36 JarFile jarFileWrapper = null;
37 if (jarFile != null)
38 {
39
40 try
41 {
42 jarFileWrapper = new JarFile(jarFile);
43 Enumeration iter = jarFileWrapper.entries();
44 while (iter.hasMoreElements())
45 {
46 ZipEntry zipEntry = (ZipEntry) iter.nextElement();
47 InputStream entryStream = jarFileWrapper.getInputStream(zipEntry);
48 ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
49 try
50 {
51 MuleBootstrapUtils.copy(entryStream, byteArrayStream);
52 entries.put(zipEntry.getName(), byteArrayStream.toByteArray());
53
54 }
55 finally
56 {
57 byteArrayStream.close();
58 }
59 }
60 }
61 finally
62 {
63 if (jarFileWrapper != null)
64 {
65 jarFileWrapper.close();
66 }
67 }
68 }
69 return entries;
70 }
71
72 public static void appendJarFileEntries(File jarFile, LinkedHashMap entries) throws Exception
73 {
74 if (entries != null)
75 {
76 LinkedHashMap combinedEntries = readJarFileEntries(jarFile);
77 combinedEntries.putAll(entries);
78 File tmpJarFile = File.createTempFile(jarFile.getName(), null);
79 createJarFileEntries(tmpJarFile, combinedEntries);
80 jarFile.delete();
81 FileUtils.renameFile(tmpJarFile, jarFile);
82 }
83 }
84
85 public static void createJarFileEntries(File jarFile, LinkedHashMap entries) throws Exception
86 {
87 JarOutputStream jarStream = null;
88 FileOutputStream fileStream = null;
89
90 if (jarFile != null)
91 {
92
93
94 try
95 {
96 fileStream = new FileOutputStream(jarFile);
97 jarStream = new JarOutputStream(fileStream);
98
99 if (entries != null && !entries.isEmpty())
100 {
101 Iterator iter = entries.keySet().iterator();
102 while (iter.hasNext())
103 {
104 String jarFilePath = (String) iter.next();
105 Object content = entries.get(jarFilePath);
106
107 JarEntry entry = new JarEntry(jarFilePath);
108 jarStream.putNextEntry(entry);
109
110
111
112 if (content instanceof String)
113 {
114 writeJarEntry(jarStream, ((String) content).getBytes());
115 }
116 else if (content instanceof byte[])
117 {
118 writeJarEntry(jarStream, (byte[]) content);
119 }
120 else if (content instanceof File)
121 {
122 writeJarEntry(jarStream, (File) content);
123 }
124 }
125 }
126
127 jarStream.flush();
128 fileStream.getFD().sync();
129 }
130 finally
131 {
132 if (jarStream != null)
133 {
134 jarStream.close();
135 }
136 if (fileStream != null)
137 {
138 fileStream.close();
139 }
140 }
141 }
142 }
143
144 private static void writeJarEntry(OutputStream stream, byte[] entry) throws IOException
145 {
146 stream.write(entry, 0, entry.length);
147 }
148
149 private static void writeJarEntry(OutputStream stream, File entry) throws IOException
150 {
151 FileInputStream fileContentStream = null;
152 try
153 {
154 fileContentStream = new FileInputStream(entry);
155 MuleBootstrapUtils.copy(fileContentStream, stream);
156 }
157 finally
158 {
159 if (fileContentStream != null)
160 {
161 fileContentStream.close();
162 }
163 }
164 }
165 }