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