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