KickJava   Java API By Example, From Geeks To Geeks.

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


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
34 import org.jboss.virtual.plugins.context.AbstractURLHandler;
35 import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
36 import org.jboss.virtual.spi.VFSContext;
37 import org.jboss.virtual.spi.VirtualFileHandler;
38
39 /**
40  * SynthenticDirEntryHandler represents non-existent directory jar entry.
41  *
42  * @author Scott.Stark@jboss.org
43  * @version $Revision: 1.1 $
44  */

45 public class SynthenticDirEntryHandler extends AbstractURLHandler
46    implements StructuredVirtualFileHandler
47 {
48    /** serialVersionUID */
49    private static final long serialVersionUID = 1L;
50
51    /** The jar file */
52    private long lastModified;
53    private transient List JavaDoc<VirtualFileHandler> entryChildren;
54    private transient Map JavaDoc<String JavaDoc, VirtualFileHandler> entryMap;
55    
56    /**
57     * Create a new SynthenticDirEntryHandler.
58     *
59     * @param context the context
60     * @param parent the parent
61     * @param entryName - the simple name for the dir
62     * @param lastModified the timestamp for the dir
63     * @param url the full url
64     * @throws IOException for an error accessing the file system
65     * @throws IllegalArgumentException for a null context, url, jar or entry
66     */

67    public SynthenticDirEntryHandler(VFSContext context, VirtualFileHandler parent,
68       String JavaDoc entryName, long lastModified, URL JavaDoc url)
69       throws IOException JavaDoc
70    {
71       super(context, parent, url, entryName);
72       this.lastModified = lastModified;
73    }
74
75    /**
76     * Add a child to an entry
77     * @param child
78     */

79    public void addChild(VirtualFileHandler child)
80    {
81       if( entryChildren == null )
82          entryChildren = new ArrayList JavaDoc<VirtualFileHandler>();
83       entryChildren.add(child);
84    }
85
86    @Override JavaDoc
87    public long getLastModified()
88    {
89       return lastModified;
90    }
91
92    @Override JavaDoc
93    public long getSize()
94    {
95       return 0;
96    }
97
98    public boolean isLeaf()
99    {
100       return false;
101    }
102
103    public boolean isHidden()
104    {
105       checkClosed();
106       return false;
107    }
108
109    @Override JavaDoc
110    public InputStream JavaDoc openStream() throws IOException JavaDoc
111    {
112       throw new IOException JavaDoc("Directories cannot be opened");
113    }
114
115    public List JavaDoc<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException JavaDoc
116    {
117       checkClosed();
118       List JavaDoc<VirtualFileHandler> children = entryChildren;
119       if( entryChildren == null )
120          children = Collections.emptyList();
121       return children;
122    }
123
124    public VirtualFileHandler findChild(String JavaDoc path) throws IOException JavaDoc
125    {
126       return super.structuredFindChild(path);
127    }
128
129    /**
130     * TODO: synchronization on lazy entryMap creation
131     */

132    public VirtualFileHandler createChildHandler(String JavaDoc name) throws IOException JavaDoc
133    {
134       if( entryChildren == null )
135          throw new FileNotFoundException JavaDoc(this+" has no children");
136       if( entryMap == null )
137       {
138          entryMap = new HashMap JavaDoc<String JavaDoc, VirtualFileHandler>();
139          for(VirtualFileHandler child : entryChildren)
140             entryMap.put(child.getName(), child);
141       }
142       VirtualFileHandler child = entryMap.get(name);
143       if( child == null )
144          throw new FileNotFoundException JavaDoc(this+" has no child: "+name);
145       return child;
146    }
147
148 }
149
Popular Tags