View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.geronimo.ews.ws4j2ee.utils.packager;
18  
19  import java.io.BufferedOutputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.Enumeration;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.jar.JarFile;
29  import java.util.jar.JarOutputStream;
30  import java.util.zip.ZipEntry;
31  
32  /***
33   * @author hemapani@opensource.lk
34   */
35  public ModulePackager {/package-summary.html">class ModulePackager {
36      private JarOutputStream jarFile;
37      private byte[] buf = new byte[1024];
38      private HashMap entries = new HashMap();
39  
40      publicModulePackager(File outjarName) throws IOException {/package-summary.html">ong> ModulePackager(File outjarName) throws IOException {
41          BufferedOutputStream bo =
42                  new BufferedOutputStream(new FileOutputStream(outjarName));
43          jarFile = new JarOutputStream(bo);
44      }
45  
46      public void addJarFile(File file) throws IOException {
47          JarFile infile = new JarFile(file);
48          Enumeration enu = infile.entries();
49          while (enu.hasMoreElements()) {
50              ZipEntry zipentry = (ZipEntry) enu.nextElement();
51              InputStream in = infile.getInputStream(zipentry);
52              addFileToJar(zipentry.getName(), in);
53          }
54      }
55  
56      public void addClassFiles(File dir) throws IOException {
57          addClassFiles(dir, null);
58      }
59  
60      private void addClassFiles(File file, String path) throws IOException {
61          if (file.isDirectory()) {
62              if (path == null) {
63                  path = "";
64              } else if ("".equals(path)) {
65                  path = file.getName();
66              } else {
67                  path = path + "/" + file.getName();
68              }
69              File[] files = file.listFiles();
70              if (files != null) {
71                  for (int i = 0; i < files.length; i++) {
72                      addClassFiles(files[i], path);
73                  }
74              }
75          } else {
76              path = path + "/" + file.getName();
77              addFileToJar(path, new FileInputStream(file));
78          }
79      }
80  
81      public void addFileToJar(String path, File input) throws IOException {
82          addFileToJar(path, new FileInputStream(input));
83      }
84  
85      private void addFileToJar(String entry, InputStream instream) throws IOException {
86          if (entries.containsKey(entry)) {
87              entries.remove(entry);
88          }
89          entries.put(entry, instream);
90      }
91  
92      public void finalizeJar() throws IOException {
93          Iterator jarentries = entries.keySet().iterator();
94          while (jarentries.hasNext()) {
95              String entryName = (String) jarentries.next();
96              ZipEntry entry = new ZipEntry(entryName);
97              InputStream instream = (InputStream) entries.get(entryName);
98              jarFile.putNextEntry(entry);
99              int anz;
100             while ((anz = instream.read(buf)) != -1) {
101                 jarFile.write(buf, 0, anz);
102             }
103             jarFile.flush();
104             jarFile.closeEntry();
105             instream.close();
106         }
107         jarFile.close();
108     }
109 
110 }