View Javadoc

1   package org.apache.velocity.runtime.resource.loader;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.JarURLConnection;
25  import java.net.URL;
26  import java.util.Enumeration;
27  import java.util.jar.JarEntry;
28  import java.util.jar.JarFile;
29  import java.util.Hashtable;
30  
31  import org.apache.velocity.runtime.RuntimeServices;
32  import org.apache.velocity.runtime.log.Log;
33  import org.apache.velocity.exception.ResourceNotFoundException;
34  
35  /**
36   * A small wrapper around a Jar
37   *
38   * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
39   * @version $Id: JarHolder.java 471259 2006-11-04 20:26:57Z henning $
40   */
41  public class JarHolder
42  {
43      private String urlpath = null;
44      private JarFile theJar = null;
45      private JarURLConnection conn = null;
46  
47      private Log log = null;
48  
49      /**
50       * @param rs
51       * @param urlpath
52       */
53      public JarHolder( RuntimeServices rs, String urlpath )
54      {
55          this.log = rs.getLog();
56  
57          this.urlpath=urlpath;
58          init();
59  
60          if (log.isDebugEnabled())
61          {
62              log.debug("JarHolder: initialized JAR: " + urlpath);
63          }
64      }
65  
66      /**
67       *
68       */
69      public void init()
70      {
71          try
72          {
73              if (log.isDebugEnabled())
74              {
75                  log.debug("JarHolder: attempting to connect to " + urlpath);
76              }
77              URL url = new URL( urlpath );
78              conn = (JarURLConnection) url.openConnection();
79              conn.setAllowUserInteraction(false);
80              conn.setDoInput(true);
81              conn.setDoOutput(false);
82              conn.connect();
83              theJar = conn.getJarFile();
84          }
85          catch (IOException ioe)
86          {
87              log.error("JarHolder: error establishing connection to JAR at \""
88                        + urlpath + "\"", ioe);
89          }
90      }
91  
92      /**
93       *
94       */
95      public void close()
96      {
97          try
98          {
99              theJar.close();
100         }
101         catch ( Exception e )
102         {
103             log.error("JarHolder: error closing the JAR file", e);
104         }
105         theJar = null;
106         conn = null;
107 
108         log.trace("JarHolder: JAR file closed");
109     }
110 
111     /**
112      * @param theentry
113      * @return The requested resource.
114      * @throws ResourceNotFoundException
115      */
116     public InputStream getResource( String theentry )
117      throws ResourceNotFoundException {
118         InputStream data = null;
119 
120         try
121         {
122             JarEntry entry = theJar.getJarEntry( theentry );
123 
124             if ( entry != null )
125             {
126                 data =  theJar.getInputStream( entry );
127             }
128         }
129         catch(Exception fnfe)
130         {
131             log.error("JarHolder: getResource() error", fnfe);
132             throw new ResourceNotFoundException(fnfe);
133         }
134 
135         return data;
136     }
137 
138     /**
139      * @return The entries of the jar as a hashtable.
140      */
141     public Hashtable getEntries()
142     {
143         Hashtable allEntries = new Hashtable(559);
144 
145         Enumeration all  = theJar.entries();
146         while ( all.hasMoreElements() )
147         {
148             JarEntry je = (JarEntry)all.nextElement();
149 
150             // We don't map plain directory entries
151             if ( !je.isDirectory() )
152             {
153                 allEntries.put( je.getName(), this.urlpath );
154             }
155         }
156         return allEntries;
157     }
158 
159     /**
160      * @return The URL path of this jar holder.
161      */
162     public String getUrlPath()
163     {
164         return urlpath;
165     }
166 }
167 
168 
169 
170 
171 
172 
173