View Javadoc

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  	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  		super();
68  		this.jarFile = jarFileName;
69  		content = new ArrayList<String>();
70  		init();
71  	}
72  	
73  	public synchronized void reload() {
74  		content.clear();
75  		init();
76  	}
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  		if(hasFile(name)) {
86  			byte[] b = null;
87  			ZipEntry ze = null;
88  			FileInputStream fis = null;
89  			ZipInputStream zis = null;
90  			try {
91  				fis = new FileInputStream(jarFile);
92  				zis = new ZipInputStream(fis);
93  				while((ze = zis.getNextEntry()) != null) {
94  					if(ze.getName().equals(name)) {
95  						int size = (int) ze.getSize();
96  						b = new byte[size];
97  						int rb = 0;
98  						int chunk = 0;
99  						while((size - rb) > 0) {
100 							chunk = zis.read(b, rb, size - rb);
101 							if(chunk == -1) {
102 								break;
103 							}
104 							rb += chunk;
105 						}
106 						break;
107 					}
108 				}
109 				zis.close();
110 				fis.close();
111 				return b;
112 			}
113 			catch (IOException e) {
114 				logger.log(Level.WARNING, "JarResource couldnt read file " + jarFile, e);
115 			}
116 			finally {
117 				if(zis != null) {
118 					try {
119 						zis.close();
120 					} catch(IOException ignore) {}
121 				}
122 				if(fis != null) {
123 					try {
124 						fis.close();
125 					} catch(IOException ignore) {}
126 				}
127 			}
128 		}
129 		return null;
130 	}
131 	
132 	
133 	/**
134 	 * initializes internal hash tables with Jar file resources.
135 	 */
136 	private void init() {
137 		try {
138 			ZipFile zf = new ZipFile(jarFile);
139 			Enumeration<? extends ZipEntry> e = zf.entries();
140 			while(e.hasMoreElements()) {
141 				ZipEntry entry = e.nextElement();
142 				content.add(entry.getName());
143 			}
144 			zf.close();
145 		} catch (IOException e) {
146 			logger.log(Level.WARNING, "JarResource couldnt read file " + jarFile, e);
147 		}
148 		Collections.sort(content);
149 	}
150 	
151 	public boolean hasFile(String filename) {
152 		return Collections.binarySearch(content, filename) >= 0;
153 	}
154 	
155 	public File getFile() {
156 		return jarFile;
157 	}
158 }