KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > plugins > context > jar > JarEntryHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.virtual.plugins.context.jar;
23
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.jar.JarEntry JavaDoc;
34 import java.util.jar.JarFile JavaDoc;
35
36 import org.jboss.virtual.plugins.context.AbstractURLHandler;
37 import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
38 import org.jboss.virtual.spi.VFSContext;
39 import org.jboss.virtual.spi.VirtualFileHandler;
40
41 /**
42  * JarEntryHandler.
43  *
44  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
45  * @author Scott.Stark@jboss.org
46  * @version $Revision: 1.1 $
47  */

48 public class JarEntryHandler extends AbstractURLHandler
49    implements StructuredVirtualFileHandler
50 {
51    /** serialVersionUID */
52    private static final long serialVersionUID = 1L;
53
54    /** The jar file */
55    private transient final JarFile JavaDoc jar;
56    
57    /** The jar entry */
58    private transient final JarEntry JavaDoc entry;
59    private transient List JavaDoc<VirtualFileHandler> entryChildren;
60    private transient Map JavaDoc<String JavaDoc, VirtualFileHandler> entryMap;
61    
62    /**
63     * Create a new JarHandler.
64     *
65     * @param context the context
66     * @param parent the parent
67     * @param jar the jar file
68     * @param entry the entry
69     * @param url the url
70     * @throws IOException for an error accessing the file system
71     * @throws IllegalArgumentException for a null context, url, jar or entry
72     */

73    public JarEntryHandler(VFSContext context, VirtualFileHandler parent, JarFile JavaDoc jar,
74       JarEntry JavaDoc entry, String JavaDoc entryName, URL JavaDoc url)
75       throws IOException JavaDoc
76    {
77       super(context, parent, url, entryName);
78       if (jar == null)
79          throw new IllegalArgumentException JavaDoc("Null jar");
80       
81       this.jar = jar;
82       this.entry = entry;
83    }
84
85    /**
86     * Add a child to an entry
87     * @param child
88     */

89    public void addChild(VirtualFileHandler child)
90    {
91       if( entryChildren == null )
92          entryChildren = new ArrayList JavaDoc<VirtualFileHandler>();
93       entryChildren.add(child);
94    }
95
96    /**
97     * Get the entry
98     *
99     * @return the file
100     */

101    protected JarEntry JavaDoc getEntry()
102    {
103       checkClosed();
104       return entry;
105    }
106    
107    @Override JavaDoc
108    public long getLastModified()
109    {
110       return getEntry().getTime();
111    }
112
113    @Override JavaDoc
114    public long getSize()
115    {
116       return getEntry().getSize();
117    }
118
119    public boolean isLeaf()
120    {
121       return getEntry().isDirectory() == false;
122    }
123
124    public boolean isHidden()
125    {
126       checkClosed();
127       return false;
128    }
129
130    @Override JavaDoc
131    public InputStream JavaDoc openStream() throws IOException JavaDoc
132    {
133       return jar.getInputStream(getEntry());
134    }
135
136    public List JavaDoc<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException JavaDoc
137    {
138       checkClosed();
139       List JavaDoc<VirtualFileHandler> children = entryChildren;
140       if( entryChildren == null )
141          children = Collections.emptyList();
142       return children;
143    }
144
145    public VirtualFileHandler findChild(String JavaDoc path) throws IOException JavaDoc
146    {
147       return super.structuredFindChild(path);
148    }
149
150    /**
151     * TODO: synchronization on lazy entryMap creation
152     */

153    public VirtualFileHandler createChildHandler(String JavaDoc name) throws IOException JavaDoc
154    {
155       if( entryChildren == null )
156          throw new FileNotFoundException JavaDoc(this+" has no children");
157       if( entryMap == null )
158       {
159          entryMap = new HashMap JavaDoc<String JavaDoc, VirtualFileHandler>();
160          for(VirtualFileHandler child : entryChildren)
161             entryMap.put(child.getName(), child);
162       }
163       VirtualFileHandler child = entryMap.get(name);
164       if( child == null )
165          throw new FileNotFoundException JavaDoc(this+" has no child: "+name);
166       return child;
167    }
168
169 }
170
Popular Tags