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; |
20 | import java.net.JarURLConnection; |
21 | import java.net.URL; |
22 | import java.util.Enumeration; |
23 | import java.util.jar.JarEntry; |
24 | import java.util.jar.JarFile; |
25 | import java.util.Hashtable; |
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 urlpath = null; |
40 | private JarFile theJar = null; |
41 | private JarURLConnection conn = null; |
42 | |
43 | private RuntimeServices rsvc = null; |
44 | |
45 | public JarHolder( RuntimeServices rs, String 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 url = new URL( urlpath ); |
61 | conn = (JarURLConnection) 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 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 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 getResource( String theentry ) |
91 | throws ResourceNotFoundException { |
92 | InputStream data = null; |
93 | |
94 | try |
95 | { |
96 | JarEntry entry = theJar.getJarEntry( theentry ); |
97 | |
98 | if ( entry != null ) |
99 | { |
100 | data = theJar.getInputStream( entry ); |
101 | } |
102 | } |
103 | catch( Exception 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 getEntries() |
113 | { |
114 | Hashtable allEntries = new Hashtable(559); |
115 | |
116 | Enumeration all = theJar.entries(); |
117 | while ( all.hasMoreElements() ) |
118 | { |
119 | JarEntry je = (JarEntry)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 getUrlPath() |
131 | { |
132 | return urlpath; |
133 | } |
134 | } |
135 | |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | |