001: package org.apache.velocity.runtime.resource.loader;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.net.JarURLConnection;
025: import java.net.URL;
026: import java.util.Enumeration;
027: import java.util.jar.JarEntry;
028: import java.util.jar.JarFile;
029: import java.util.Hashtable;
030:
031: import org.apache.velocity.runtime.RuntimeServices;
032: import org.apache.velocity.runtime.log.Log;
033: import org.apache.velocity.exception.ResourceNotFoundException;
034:
035: /**
036: * A small wrapper around a Jar
037: *
038: * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
039: * @version $Id: JarHolder.java 471259 2006-11-04 20:26:57Z henning $
040: */
041: public class JarHolder {
042: private String urlpath = null;
043: private JarFile theJar = null;
044: private JarURLConnection conn = null;
045:
046: private Log log = null;
047:
048: /**
049: * @param rs
050: * @param urlpath
051: */
052: public JarHolder(RuntimeServices rs, String urlpath) {
053: this .log = rs.getLog();
054:
055: this .urlpath = urlpath;
056: init();
057:
058: if (log.isDebugEnabled()) {
059: log.debug("JarHolder: initialized JAR: " + urlpath);
060: }
061: }
062:
063: /**
064: *
065: */
066: public void init() {
067: try {
068: if (log.isDebugEnabled()) {
069: log.debug("JarHolder: attempting to connect to "
070: + urlpath);
071: }
072: URL url = new URL(urlpath);
073: conn = (JarURLConnection) url.openConnection();
074: conn.setAllowUserInteraction(false);
075: conn.setDoInput(true);
076: conn.setDoOutput(false);
077: conn.connect();
078: theJar = conn.getJarFile();
079: } catch (IOException ioe) {
080: log.error(
081: "JarHolder: error establishing connection to JAR at \""
082: + urlpath + "\"", ioe);
083: }
084: }
085:
086: /**
087: *
088: */
089: public void close() {
090: try {
091: theJar.close();
092: } catch (Exception e) {
093: log.error("JarHolder: error closing the JAR file", e);
094: }
095: theJar = null;
096: conn = null;
097:
098: log.trace("JarHolder: JAR file closed");
099: }
100:
101: /**
102: * @param theentry
103: * @return The requested resource.
104: * @throws ResourceNotFoundException
105: */
106: public InputStream getResource(String theentry)
107: throws ResourceNotFoundException {
108: InputStream data = null;
109:
110: try {
111: JarEntry entry = theJar.getJarEntry(theentry);
112:
113: if (entry != null) {
114: data = theJar.getInputStream(entry);
115: }
116: } catch (Exception fnfe) {
117: log.error("JarHolder: getResource() error", fnfe);
118: throw new ResourceNotFoundException(fnfe);
119: }
120:
121: return data;
122: }
123:
124: /**
125: * @return The entries of the jar as a hashtable.
126: */
127: public Hashtable getEntries() {
128: Hashtable allEntries = new Hashtable(559);
129:
130: Enumeration all = theJar.entries();
131: while (all.hasMoreElements()) {
132: JarEntry je = (JarEntry) all.nextElement();
133:
134: // We don't map plain directory entries
135: if (!je.isDirectory()) {
136: allEntries.put(je.getName(), this .urlpath);
137: }
138: }
139: return allEntries;
140: }
141:
142: /**
143: * @return The URL path of this jar holder.
144: */
145: public String getUrlPath() {
146: return urlpath;
147: }
148: }
|