Coverage Report - argos.deploy.JarResource
 
Classes in this File Line Coverage Branch Coverage Complexity
JarResource
0%
0/53
0%
0/18
0
 
 1  
 /*
 2  
 Copyright (c) 2006, University of Tromsø
 3  
 All rights reserved.
 4  
 
 5  
 Redistribution and use in source and binary forms, with or without 
 6  
 modification, are permitted provided that the following conditions are met:
 7  
 
 8  
  * Redistributions of source code must retain the above copyright notice, this list 
 9  
    of conditions and the following disclaimer.
 10  
 
 11  
  * Redistributions in binary form must reproduce the above copyright notice, this 
 12  
    list of conditions and the following disclaimer in the documentation and/or other 
 13  
    materials provided with the distribution.
 14  
 
 15  
  * Neither the name of the University of Tromsø nor the names of its contributors may 
 16  
    be used to endorse or promote products derived from this software without specific 
 17  
    prior written permission.
 18  
 
 19  
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 20  
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 21  
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 22  
 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 23  
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
 24  
 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 25  
 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 26  
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 27  
 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
 28  
 DAMAGE.
 29  
 */
 30  
 
 31  
 package argos.deploy;
 32  
 
 33  
 import java.io.File;
 34  
 import java.io.FileInputStream;
 35  
 import java.io.IOException;
 36  
 import java.util.ArrayList;
 37  
 import java.util.Collections;
 38  
 import java.util.Enumeration;
 39  
 import java.util.List;
 40  
 import java.util.logging.Level;
 41  
 import java.util.logging.Logger;
 42  
 import java.util.zip.ZipEntry;
 43  
 import java.util.zip.ZipFile;
 44  
 import java.util.zip.ZipInputStream;
 45  
 
 46  
 /**
 47  
  * Created on 30.jun.2006
 48  
  * 
 49  
  * JarResource: JarResource maps all resources included in a Zip or Jar file. Additionaly, it
 50  
  * provides a method to extract one as a blob.
 51  
  * 
 52  
  * @author Dan Peder Eriksen
 53  
  */
 54  
 public final class JarResource {
 55  0
         private static final Logger logger = Logger.getLogger(JarResource.class.getName());
 56  
         private List<String> content;
 57  
         private File jarFile;
 58  
         
 59  
         
 60  
         /**
 61  
          * creates a JarResource. It extracts all resources from a Jar into an internal hashtable, keyed
 62  
          * by resource names.
 63  
          * 
 64  
          * @param jarFileName a jar or zip file
 65  
          */
 66  
         public JarResource(File jarFileName) {
 67  0
                 super();
 68  0
                 this.jarFile = jarFileName;
 69  0
                 content = new ArrayList<String>();
 70  0
                 init();
 71  0
         }
 72  
         
 73  
         public synchronized void reload() {
 74  0
                 content.clear();
 75  0
                 init();
 76  0
         }
 77  
         
 78  
         
 79  
         /**
 80  
          * Extracts a jar resource as a blob.
 81  
          * 
 82  
          * @param name  a resource name.
 83  
          */
 84  
         public byte[] getResource(String name) {
 85  0
                 if(hasFile(name)) {
 86  0
                         byte[] b = null;
 87  0
                         ZipEntry ze = null;
 88  0
                         FileInputStream fis = null;
 89  0
                         ZipInputStream zis = null;
 90  
                         try {
 91  0
                                 fis = new FileInputStream(jarFile);
 92  0
                                 zis = new ZipInputStream(fis);
 93  0
                                 while((ze = zis.getNextEntry()) != null) {
 94  0
                                         if(ze.getName().equals(name)) {
 95  0
                                                 int size = (int) ze.getSize();
 96  0
                                                 b = new byte[size];
 97  0
                                                 int rb = 0;
 98  0
                                                 int chunk = 0;
 99  0
                                                 while((size - rb) > 0) {
 100  0
                                                         chunk = zis.read(b, rb, size - rb);
 101  0
                                                         if(chunk == -1) {
 102  0
                                                                 break;
 103  
                                                         }
 104  0
                                                         rb += chunk;
 105  
                                                 }
 106  
                                                 break;
 107  
                                         }
 108  
                                 }
 109  0
                                 zis.close();
 110  0
                                 fis.close();
 111  0
                                 return b;
 112  
                         }
 113  0
                         catch (IOException e) {
 114  0
                                 logger.log(Level.WARNING, "JarResource couldnt read file " + jarFile, e);
 115  
                         }
 116  
                         finally {
 117  0
                                 if(zis != null) {
 118  
                                         try {
 119  0
                                                 zis.close();
 120  0
                                         } catch(IOException ignore) {}
 121  
                                 }
 122  0
                                 if(fis != null) {
 123  
                                         try {
 124  0
                                                 fis.close();
 125  0
                                         } catch(IOException ignore) {}
 126  
                                 }
 127  
                         }
 128  
                 }
 129  0
                 return null;
 130  
         }
 131  
         
 132  
         
 133  
         /**
 134  
          * initializes internal hash tables with Jar file resources.
 135  
          */
 136  
         private void init() {
 137  
                 try {
 138  0
                         ZipFile zf = new ZipFile(jarFile);
 139  0
                         Enumeration<? extends ZipEntry> e = zf.entries();
 140  0
                         while(e.hasMoreElements()) {
 141  0
                                 ZipEntry entry = e.nextElement();
 142  0
                                 content.add(entry.getName());
 143  0
                         }
 144  0
                         zf.close();
 145  0
                 } catch (IOException e) {
 146  0
                         logger.log(Level.WARNING, "JarResource couldnt read file " + jarFile, e);
 147  0
                 }
 148  0
                 Collections.sort(content);
 149  0
         }
 150  
         
 151  
         public boolean hasFile(String filename) {
 152  0
                 return Collections.binarySearch(content, filename) >= 0;
 153  
         }
 154  
         
 155  
         public File getFile() {
 156  0
                 return jarFile;
 157  
         }
 158  
 }