KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > resource > loader > JarHolder


1 package org.apache.velocity.runtime.resource.loader;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.InputStream JavaDoc;
20 import java.net.JarURLConnection JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.jar.JarEntry JavaDoc;
24 import java.util.jar.JarFile JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 import org.apache.velocity.runtime.RuntimeServices;
28
29 import org.apache.velocity.exception.ResourceNotFoundException;
30
31 /**
32  * A small wrapper around a Jar
33  *
34  * @author <a HREF="mailto:daveb@miceda-data.com">Dave Bryson</a>
35  * @version $Id: JarHolder.java,v 1.8.4.1 2004/03/03 23:23:02 geirm Exp $
36  */

37 public class JarHolder
38 {
39     private String JavaDoc urlpath = null;
40     private JarFile JavaDoc theJar = null;
41     private JarURLConnection JavaDoc conn = null;
42         
43     private RuntimeServices rsvc = null;
44
45     public JarHolder( RuntimeServices rs, String JavaDoc urlpath )
46     {
47         rsvc = rs;
48
49         this.urlpath=urlpath;
50         init();
51         
52         rsvc.info(" JarHolder : initialized JAR: " + urlpath );
53     }
54
55     public void init()
56     {
57         try
58         {
59             rsvc.info(" JarHolder : attempting to connect to "+ urlpath);
60             URL JavaDoc url = new URL JavaDoc( urlpath );
61             conn = (JarURLConnection JavaDoc) url.openConnection();
62             conn.setAllowUserInteraction(false);
63             conn.setDoInput(true);
64             conn.setDoOutput(false);
65             conn.connect();
66             theJar = conn.getJarFile();
67         }
68         catch (Exception JavaDoc e)
69         {
70             rsvc.error(" JarHolder : error establishing connection to JAR "+ e);
71         }
72     }
73
74     public void close()
75     {
76         try
77         {
78             theJar.close();
79         }
80         catch ( Exception JavaDoc e )
81         {
82             rsvc.error(" JarHolder : error Closing JAR the file " + e);
83         }
84         theJar = null;
85         conn = null;
86
87         rsvc.info(" JarHolder : JAR file closed");
88     }
89     
90     public InputStream JavaDoc getResource( String JavaDoc theentry )
91      throws ResourceNotFoundException {
92         InputStream JavaDoc data = null;
93         
94         try
95         {
96             JarEntry JavaDoc entry = theJar.getJarEntry( theentry );
97             
98             if ( entry != null )
99             {
100                 data = theJar.getInputStream( entry );
101             }
102         }
103         catch( Exception JavaDoc fnfe )
104         {
105             rsvc.error(" JarHolder : getResource() error : exception : " + fnfe );
106             throw new ResourceNotFoundException( fnfe.getMessage() );
107         }
108         
109         return data;
110     }
111
112     public Hashtable JavaDoc getEntries()
113     {
114         Hashtable JavaDoc allEntries = new Hashtable JavaDoc(559);
115         
116         Enumeration JavaDoc all = theJar.entries();
117         while ( all.hasMoreElements() )
118         {
119             JarEntry JavaDoc je = (JarEntry JavaDoc)all.nextElement();
120             
121             // We don't map plain directory entries
122
if ( !je.isDirectory() )
123             {
124                 allEntries.put( je.getName(), this.urlpath );
125             }
126         }
127         return allEntries;
128     }
129     
130     public String JavaDoc getUrlPath()
131     {
132         return urlpath;
133     }
134 }
135
136
137
138
139
140
141
142
Popular Tags