KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > JarPath


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.vfs;
31
32 import com.caucho.util.LruCache;
33
34 import java.io.IOException JavaDoc;
35 import java.security.cert.Certificate JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.jar.Manifest JavaDoc;
38
39 /**
40  * A filesystem for .jar files.
41  */

42 public class JarPath extends FilesystemPath {
43   private static LruCache<Path,JarPath> _jarCache
44     = new LruCache<Path,JarPath>(256);
45   
46   private Path _backing;
47
48   /**
49    * Creates a new jar path for the specific file
50    *
51    * @param root the root of this jar
52    * @param userPath the path specified by the user in the lookup()
53    * @param path the normalized path
54    * @param jarFile the underlying jar
55    */

56   protected JarPath(FilesystemPath root, String JavaDoc userPath,
57             String JavaDoc path, Path backing)
58   {
59     super(root, userPath, path);
60
61     if (_root == null)
62       _root = this;
63     
64     _backing = backing;
65   }
66
67   /**
68    * Creates a new root Jar path.
69    */

70   public static JarPath create(Path backing)
71   {
72     JarPath path = _jarCache.get(backing);
73
74     if (path == null) {
75       path = new JarPath(null, "/", "/", backing);
76       _jarCache.put(backing, path);
77     }
78
79     return path;
80   }
81
82   public Path fsWalk(String JavaDoc userPath,
83              Map JavaDoc<String JavaDoc,Object JavaDoc> attributes,
84              String JavaDoc path)
85   {
86     if ("/".equals(userPath) && "/".equals(path))
87       return _root;
88     else
89       return new JarPath(_root, userPath, path, _backing);
90   }
91
92   /**
93    * Returns the scheme (jar)
94    */

95   public String JavaDoc getScheme()
96   {
97     return "jar";
98   }
99
100   /**
101    * Returns the full url.
102    *
103    * <p>jar:<container-url>!/entry-path
104    */

105   public String JavaDoc getURL()
106   {
107     String JavaDoc path = getFullPath();
108
109     return getScheme() + ":" + getContainer().getURL() + "!" + path;
110   }
111
112   /**
113    * Returns the underlying file below the jar.
114    */

115   public Path getContainer()
116   {
117     return getJar().getBacking();
118   }
119
120   /**
121    * Returns any signing certificates.
122    */

123   @Override JavaDoc
124   public Certificate JavaDoc []getCertificates()
125   {
126     return getJar().getCertificates(getPath());
127   }
128
129   /**
130    * Returns true if the entry exists in the jar file.
131    */

132   public boolean exists()
133   {
134     return getJar().exists(getPath());
135   }
136
137   /**
138    * Returns true if the entry is a directory in the jar file.
139    */

140   public boolean isDirectory()
141   {
142     return getJar().isDirectory(getPath());
143   }
144
145   /**
146    * Returns true if the entry is a file in the jar file.
147    */

148   public boolean isFile()
149   {
150     return getJar().isFile(getPath());
151   }
152
153   public long getLength()
154   {
155     return getJar().getLength(this);
156   }
157
158   public long getLastModified()
159   {
160     return getJar().getLastModified(getPath());
161   }
162   
163   public boolean canRead()
164   {
165     return getJar().canRead(getPath());
166   }
167
168   public boolean canWrite()
169   {
170     return getJar().canWrite(getPath());
171   }
172
173   /**
174    * Returns a list of the directories in the jar.
175    */

176   public String JavaDoc []list() throws IOException JavaDoc
177   {
178     return getJar().list(getPath());
179   }
180
181   /**
182    * Returns the manifest.
183    */

184   public Manifest JavaDoc getManifest() throws IOException JavaDoc
185   {
186     return getJar().getManifest();
187   }
188
189   /**
190    * Returns the dependency checker from the jar.
191    */

192   public PersistentDependency getDepend()
193   {
194     return getJar().getDepend();
195   }
196
197   public StreamImpl openReadImpl() throws IOException JavaDoc
198   {
199     return getJar().openReadImpl(this);
200   }
201
202   protected Jar getJar()
203   {
204     return Jar.create(_backing);
205   }
206
207   public void closeJar()
208   {
209     Jar jar = Jar.getJar(_backing);
210
211     if (jar != null)
212       jar.close();
213   }
214
215   public String JavaDoc toString()
216   {
217     return "jar:(" + _backing + ")" + getPath();
218   }
219
220   public int hashCode()
221   {
222     return 65531 * getFullPath().hashCode() + getContainer().hashCode();
223   }
224
225   /**
226    * Tests for equality.
227    */

228   public boolean equals(Object JavaDoc o)
229   {
230     if (this == o)
231       return true;
232     else if (o == null || ! o.getClass().equals(this.getClass()))
233       return false;
234
235     JarPath jarPath = (JarPath) o;
236
237     return (_backing.equals(jarPath._backing) &&
238         getFullPath().equals(jarPath.getFullPath()));
239   }
240 }
241
Popular Tags