View Javadoc

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