KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > JarFileResource


1 // ========================================================================
2
// $Id: JarFileResource.java,v 1.12 2005/08/13 00:01:28 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15
package org.mortbay.util;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.net.JarURLConnection JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.jar.JarEntry JavaDoc;
24 import java.util.jar.JarFile JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.mortbay.log.LogFactory;
28
29 /* ------------------------------------------------------------ */
30 class JarFileResource extends JarResource
31 {
32     private static Log log = LogFactory.getLog(JarFileResource.class);
33     transient JarFile JavaDoc _jarFile;
34     transient File JavaDoc _file;
35     transient String JavaDoc[] _list;
36     transient JarEntry JavaDoc _entry;
37     transient boolean _directory;
38     transient String JavaDoc _jarUrl;
39     transient String JavaDoc _path;
40     transient boolean _exists;
41     
42     /* -------------------------------------------------------- */
43     JarFileResource(URL JavaDoc url)
44     {
45         super(url);
46     }
47
48     /* ------------------------------------------------------------ */
49     public synchronized void release()
50     {
51         _list=null;
52         _entry=null;
53         _file=null;
54         _jarFile=null;
55         super.release();
56     }
57     
58     /* ------------------------------------------------------------ */
59     protected boolean checkConnection()
60     {
61         try{
62             super.checkConnection();
63         }
64         finally
65         {
66             if (_jarConnection==null)
67             {
68                 _entry=null;
69                 _file=null;
70                 _jarFile=null;
71                 _list=null;
72             }
73         }
74         return _jarFile!=null;
75     }
76
77
78     /* ------------------------------------------------------------ */
79     protected void newConnection()
80         throws IOException JavaDoc
81     {
82         super.newConnection();
83         
84         _entry=null;
85         _file=null;
86         _jarFile=null;
87         _list=null;
88         
89         int sep = _urlString.indexOf("!/");
90         _jarUrl=_urlString.substring(0,sep+2);
91         _path=_urlString.substring(sep+2);
92         if (_path.length()==0)
93             _path=null;
94         _jarFile=_jarConnection.getJarFile();
95         _file=new File JavaDoc(_jarFile.getName());
96     }
97     
98     
99     /* ------------------------------------------------------------ */
100     /**
101      * Returns true if the respresenetd resource exists.
102      */

103     public boolean exists()
104     {
105         if (_exists)
106             return true;
107
108         if (_urlString.endsWith("!/"))
109         {
110             String JavaDoc file_url=_urlString.substring(4,_urlString.length()-2);
111             try{return newResource(file_url).exists();}
112             catch(Exception JavaDoc e) {LogSupport.ignore(log,e); return false;}
113         }
114         
115         boolean check=checkConnection();
116         
117         // Is this a root URL?
118
if (_jarUrl!=null && _path==null)
119         {
120             // Then if it exists it is a directory
121
_directory=check;
122             return true;
123         }
124         else
125         {
126             // Can we find a file for it?
127
JarFile JavaDoc jarFile=null;
128             if (check)
129                 // Yes
130
jarFile=_jarFile;
131             else
132             {
133                 // No - so lets look if the root entry exists.
134
try
135                 {
136                     jarFile=
137                         ((JarURLConnection JavaDoc)
138                          ((new URL JavaDoc(_jarUrl)).openConnection())).getJarFile();
139                 }
140                 catch(Exception JavaDoc e)
141                 {
142                        LogSupport.ignore(log,e);
143                 }
144             }
145
146             // Do we need to look more closely?
147
if (jarFile!=null && _entry==null && !_directory)
148             {
149                 // OK - we have a JarFile, lets look at the entries for our path
150
Enumeration JavaDoc e=jarFile.entries();
151                 while(e.hasMoreElements())
152                 {
153                     JarEntry JavaDoc entry = (JarEntry JavaDoc) e.nextElement();
154                     String JavaDoc name=entry.getName().replace('\\','/');
155                     
156                     // Do we have a match
157
if (name.equals(_path))
158                     {
159                         _entry=entry;
160                         // Is the match a directory
161
_directory=_path.endsWith("/");
162                         break;
163                     }
164                     else if (_path.endsWith("/"))
165                     {
166                         if (name.startsWith(_path))
167                         {
168                             _directory=true;
169                             break;
170                         }
171                     }
172                     else if (name.startsWith(_path) && name.length()>_path.length() && name.charAt(_path.length())=='/')
173                     {
174                         _directory=true;
175                         break;
176                     }
177                 }
178             }
179         }
180         
181         _exists= ( _directory || _entry!=null);
182         return _exists;
183     }
184
185     /* ------------------------------------------------------------ */
186     /**
187      * Returns true if the respresenetd resource is a container/directory.
188      * If the resource is not a file, resources ending with "/" are
189      * considered directories.
190      */

191     public boolean isDirectory()
192     {
193         return _urlString.endsWith("/") || exists() && _directory;
194     }
195     
196     /* ------------------------------------------------------------ */
197     /**
198      * Returns the last modified time
199      */

200     public long lastModified()
201     {
202         if (checkConnection() && _file!=null)
203             return _file.lastModified();
204         return -1;
205     }
206
207     /* ------------------------------------------------------------ */
208     public synchronized String JavaDoc[] list()
209     {
210         if(isDirectory() && _list==null)
211         {
212             ArrayList JavaDoc list = new ArrayList JavaDoc(32);
213
214             checkConnection();
215             
216             JarFile JavaDoc jarFile=_jarFile;
217             if(jarFile==null)
218             {
219                 try
220                 {
221                     jarFile=((JarURLConnection JavaDoc)
222                              ((new URL JavaDoc(_jarUrl)).openConnection())).getJarFile();
223                 }
224                 catch(Exception JavaDoc e)
225                 {
226                      LogSupport.ignore(log,e);
227                 }
228             }
229             
230             Enumeration JavaDoc e=jarFile.entries();
231             String JavaDoc dir=_urlString.substring(_urlString.indexOf("!/")+2);
232             while(e.hasMoreElements())
233             {
234                 JarEntry JavaDoc entry = (JarEntry JavaDoc) e.nextElement();
235                 String JavaDoc name=entry.getName().replace('\\','/');
236                 if(!name.startsWith(dir) || name.length()==dir.length())
237                     continue;
238                 String JavaDoc listName=name.substring(dir.length());
239                 int dash=listName.indexOf('/');
240                 if (dash>=0)
241                 {
242                     listName=listName.substring(0,dash+1);
243                     if (list.contains(listName))
244                         continue;
245                 }
246                 
247                 list.add(listName);
248             }
249             
250             _list=new String JavaDoc[list.size()];
251             list.toArray(_list);
252         }
253         return _list;
254     }
255     
256     /* ------------------------------------------------------------ */
257     /**
258      * Return the length of the resource
259      */

260     public long length()
261     {
262         if (isDirectory())
263             return -1;
264
265         if (_entry!=null)
266             return _entry.getSize();
267         
268         return -1;
269     }
270     
271     /* ------------------------------------------------------------ */
272     /** Encode according to this resource type.
273      * File URIs are not encoded.
274      * @param uri URI to encode.
275      * @return The uri unchanged.
276      */

277     public String JavaDoc encode(String JavaDoc uri)
278     {
279         return uri;
280     }
281 }
282
283
284
285
286
287
288
289
290
Popular Tags