View Javadoc

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