1 /* 2 * $Header: /u/cvs/Projects/EnhydraOrg/enhydra3x/Enhydra/modules/Tomcat/src/share/org/apache/tomcat/util/Attic/URLResourceReader.java,v 1.2 2000/02/26 02:32:34 shawn Exp $ 3 * $Revision: 1.2 $ 4 * $Date: 2000/02/26 02:32:34 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.tomcat.util; 65 66 import java.util.zip; 67 import java.net; 68 import java.util; 69 import java.io; 70 71 /** 72 * This implementation of URL Resource Reader assumes 2 types 73 * of base urls. A base url that ends with / is considered a 74 * resource folder, whereas a resource that does not end with 75 * / is considered a zip/jar resource folder. 76 * 77 * If the resource folder happens is a zip/jar archive, the 78 * entries are always cached. 79 * For non-zip base urls, one could specify whether or not it should 80 * be cached. 81 * 82 * @author Harish Prabandham 83 */ 84 public class URLResourceReader { 85 private Hashtable resourceCache = new Hashtable(); 86 private boolean iszip = true; 87 private URL url = null; 88 private boolean cache = true; 89 90 /** 91 * Creates a new URLResourceReader object. You can either give 92 * the URL of the zip/jar file or a base url where to 93 * look for additional resources. If the url ends with 94 * "/" then it is assumed to be a Base URL. 95 * @param The base url to look for the resources. 96 * @param If the base url is not a zip/jar, then true indicates 97 * that entries should be cached, false otherwise. 98 */ 99 public URLResourceReader(URL baseurl, boolean cache) throws IOException { 100 this.url = baseurl; 101 this.cache = cache; 102 this.iszip = !url.getFile().endsWith("/"); 103 if(this.iszip) 104 this.cache = true; 105 initialize(); 106 } 107 108 /** 109 * equivalent to URLResourceReader(baseurl, false) 110 */ 111 public URLResourceReader(URL baseurl) throws IOException { 112 this(baseurl, false); 113 } 114 115 /** 116 * Creates a new URLResourceReader object with the given 117 * input stream. The stream is assumed to be a zip/jar 118 * stream. 119 */ 120 public URLResourceReader(InputStream is) throws IOException { 121 init(is); 122 } 123 124 private void initialize() throws IOException { 125 if(iszip) { 126 InputStream is = url.openStream(); 127 init(is); 128 is.close(); 129 } 130 } 131 132 private byte[] readFully(InputStream is) throws IOException { 133 byte[] buf = new byte[1024]; 134 int num = 0; 135 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 136 137 while( (num = is.read(buf)) != -1) { 138 bout.write(buf, 0, num); 139 } 140 141 return bout.toByteArray(); 142 } 143 144 private void init(InputStream is) throws IOException { 145 ZipInputStream zstream = new ZipInputStream(is); 146 ZipEntry entry; 147 148 while( (entry = zstream.getNextEntry()) != null) { 149 byte[] entryData = readFully(zstream); 150 if(cache) 151 resourceCache.put(entry.getName(), entryData); 152 zstream.closeEntry(); 153 } 154 155 zstream.close(); 156 } 157 158 /** 159 * Returns an Enumeration of all "known" resource names. 160 */ 161 public Enumeration getResourceNames() { 162 return resourceCache.keys(); 163 } 164 165 /** 166 * Returns an array of bytes read for this resource if the 167 * resource exists. This method blocks until the resource 168 * has been fully read. If the resource does not exist, 169 * this method returns null. 170 */ 171 public byte[] getResource(String resource) { 172 // lookup the data in the cache... 173 byte[] data = (byte[]) resourceCache.get(resource); 174 if(data != null) { 175 return data; 176 } 177 178 // if the data was to come from a zip file that we 179 // already read fully & cached , then it is probably 180 // not there. 181 if(iszip) { 182 return null; 183 } 184 185 // Now the only choice left is to make a url connection. 186 try { 187 URL realURL = new URL(url.getProtocol(), url.getHost(), 188 url.getFile() + resource); 189 data = readFully(realURL.openStream()); 190 // add it to cache if needed... 191 if(cache) 192 resourceCache.put(resource, data); 193 return data; 194 } catch(Exception e) { 195 return null; 196 } 197 } 198 199 public void close() { 200 resourceCache.clear(); 201 resourceCache = null; 202 } 203 } 204 205 206 207 208 209 210 211