1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.geronimo.ews.ws4j2ee.utils.packager;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
22
23 import java.io.BufferedInputStream;
24 import java.io.BufferedOutputStream;
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Enumeration;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.jar.JarEntry;
33 import java.util.jar.JarFile;
34 import java.util.jar.JarOutputStream;
35 import java.util.zip.ZipEntry;
36
37 /***
38 * create a jar file with given set of jar entries.
39 * It works like
40 * <ol>
41 * <li>if no extension given the .class is added to the extenstion</li>
42 * <li>then file with the given name is tried</li>
43 * <li>if that failed the name is tired to load as a stream form the
44 * class path</li>
45 * </ol>
46 *
47 * @author Srinath Perera(hemapani@opensource.lk)
48 */
49 public JARFile {/package-summary.html">class JARFile {
50 protected static Log log =
51 LogFactory.getLog(JARFile.class.getName());
52
53 private HashMap jarEntries;
54 private File path;
55
56 public JARFile(String path, HashMap entries) {/package-summary.html">ong> JARFile(String path, HashMap entries) {
57 jarEntries = entries;
58 this.path = new File(path);
59 }
60
61 public JARFile(File path) {/package-summary.html">ong> JARFile(File path) {
62 this.path = path;
63 jarEntries = new HashMap();
64 }
65
66 public void addJarFile(String jarFile) throws GenerationFault {
67 try {
68 log.info(jarFile + " added ....");
69 JarFile file = new JarFile(jarFile);
70 Enumeration e = file.entries();
71 while (e.hasMoreElements()) {
72 ZipEntry entry = (ZipEntry) e.nextElement();
73 JARFileEntry newEntry =/package-summary.html">JARFileEntry newEntry =
74 JARFileEntry(entry/getName(),/package-summary.html">new JARFileEntry(entry.getName(),
75 file.getInputStream(entry));
76 if (!jarEntries.containsKey(entry.getName())) {
77 this.jarEntries.put(entry.getName(), newEntry);
78 }
79 }
80 } catch (IOException e) {
81 e.printStackTrace();
82 throw GenerationFault.createGenerationFault(e);
83 }
84 }
85
86 public void addJarEntry(JARFileEntry entry) {/package-summary.html">ong> void addJarEntry(JARFileEntry entry) {
87 this.jarEntries.put(entry.getJarEntry().getName(), entry);
88 }
89
90 public void createNewJarFile() throws IOException {
91 if (!path.exists())
92 path.createNewFile();
93 BufferedOutputStream bo =
94 new BufferedOutputStream(new FileOutputStream(path));
95 JarOutputStream jo = new JarOutputStream(bo);
96 Iterator it = jarEntries.values().iterator();
97 for (; it.hasNext();) {
98 JARFileEntry jarentry = (JARFileEntry) it/next()/package-summary.html">JARFileEntry jarentry = (JARFileEntry) it.next();
99 System.out.println(jarentry.getJarEntry().getName() + " adding");
100 InputStream instream = null;
101 instream = jarentry.getSource();
102 BufferedInputStream source = new BufferedInputStream(instream);
103 JarEntry je = jarentry.getJarEntry();
104 jo.putNextEntry(je);
105 byte[] buf = new byte[1024];
106 int anz;
107 while ((anz = source.read(buf)) != -1) {
108 jo.write(buf, 0, anz);
109 }
110 jo.flush();
111 jo.closeEntry();
112 instream.close();
113 source.close();
114 }
115 jo.close();
116 bo.close();
117 }
118
119 }