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