KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > plugins > context > file > LinkHandler


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.file;
23
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.jboss.virtual.VFSUtils;
34 import org.jboss.virtual.plugins.context.AbstractURLHandler;
35 import org.jboss.virtual.plugins.context.DelegatingHandler;
36 import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
37 import org.jboss.virtual.plugins.vfs.helpers.PathTokenizer;
38 import org.jboss.virtual.spi.LinkInfo;
39 import org.jboss.virtual.spi.VFSContext;
40 import org.jboss.virtual.spi.VFSContextFactory;
41 import org.jboss.virtual.spi.VFSContextFactoryLocator;
42 import org.jboss.virtual.spi.VirtualFileHandler;
43
44 /**
45  * A handler for link directories.
46  *
47  * @author Scott.Stark@jboss.org
48  * @version $Revision: 1.1 $
49  */

50 public class LinkHandler extends AbstractURLHandler
51    implements StructuredVirtualFileHandler
52 {
53    private static final long serialVersionUID = 1;
54    /** The link information */
55    private List JavaDoc<LinkInfo> links;
56    private HashMap JavaDoc<String JavaDoc, VirtualFileHandler> linkTargets =
57       new HashMap JavaDoc<String JavaDoc, VirtualFileHandler>(3);
58
59    class ParentOfLink extends AbstractURLHandler
60       implements StructuredVirtualFileHandler
61    {
62       private static final long serialVersionUID = 1;
63       private HashMap JavaDoc<String JavaDoc, VirtualFileHandler> children =
64          new HashMap JavaDoc<String JavaDoc, VirtualFileHandler>(1);
65
66       public ParentOfLink(VFSContext context, VirtualFileHandler parent, URL JavaDoc url, String JavaDoc name)
67       {
68          super(context, parent, url, name);
69       }
70       void addChild(VirtualFileHandler child, String JavaDoc name)
71       {
72          children.put(name, child);
73       }
74       public VirtualFileHandler findChild(String JavaDoc path) throws IOException JavaDoc
75       {
76          return structuredFindChild(path);
77       }
78
79       public VirtualFileHandler createChildHandler(String JavaDoc name) throws IOException JavaDoc
80       {
81          return children.get(name);
82       }
83
84       public List JavaDoc<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException JavaDoc
85       {
86          // TODO Auto-generated method stub
87
return null;
88       }
89
90       public boolean isLeaf() throws IOException JavaDoc
91       {
92          return false;
93       }
94    }
95
96    /**
97     * Create a new LinkHandler.
98     *
99     * @param context the context
100     * @param parent the parent
101     * @param uri the uri
102     * @param name the name
103     * @param links the links
104     * @throws IOException for an error accessing the file system
105     * @throws IllegalArgumentException for a null context, url
106     * @throws URISyntaxException if the uri cannot be parsed
107     */

108    public LinkHandler(FileSystemContext context, VirtualFileHandler parent, URI JavaDoc uri, String JavaDoc name,
109          List JavaDoc<LinkInfo> links)
110       throws IOException JavaDoc, URISyntaxException JavaDoc
111    {
112       // TODO: This URL is not consistent with the getName, but does point to the raw link file
113
super(context, parent, uri.toURL(), name);
114       this.links = links;
115       // Create handlers for the links and add
116
for(LinkInfo link : links)
117       {
118          String JavaDoc linkName = link.getName();
119          if( linkName == null )
120             linkName = VFSUtils.getName(link.getLinkTarget());
121          if( linkName != null )
122          {
123             String JavaDoc[] paths = PathTokenizer.getTokens(linkName);
124             int n = 0;
125             VirtualFileHandler linkParent = this;
126             String JavaDoc atom;
127             // Look for an existing parent
128
for(; n < paths.length-1; n ++)
129             {
130                atom = paths[n];
131                try
132                {
133                   linkParent = linkParent.findChild(atom);
134                }
135                catch(IOException JavaDoc e)
136                {
137                   break;
138                }
139             }
140             // Create any missing parents
141
for(; n < paths.length-1; n ++)
142             {
143                atom = paths[n];
144                URL JavaDoc polURL = new URL JavaDoc(linkParent.toURI().toURL(), atom);
145                ParentOfLink pol = new ParentOfLink(this.getVFSContext(), linkParent, polURL, atom);
146                if( linkParent == this )
147                {
148                   linkTargets.put(atom, pol);
149                }
150                else
151                {
152                   ParentOfLink prevPOL = (ParentOfLink) linkParent;
153                   prevPOL.addChild(pol, atom);
154                }
155                linkParent = pol;
156             }
157                
158             // Create the link handler
159
atom = paths[n];
160             VirtualFileHandler linkHandler = createLinkHandler(linkParent, atom, link.getLinkTarget());
161             if( linkParent == this )
162             {
163                linkTargets.put(atom, linkHandler);
164             }
165             else
166             {
167                ParentOfLink prevPOL = (ParentOfLink) linkParent;
168                prevPOL.addChild(linkHandler, atom);
169             }
170          }
171       }
172    }
173
174    public boolean isLeaf()
175    {
176       return false;
177    }
178
179    public List JavaDoc<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException JavaDoc
180    {
181       return new ArrayList JavaDoc<VirtualFileHandler>(linkTargets.values());
182    }
183
184    public VirtualFileHandler findChild(String JavaDoc path) throws IOException JavaDoc
185    {
186       return structuredFindChild(path);
187    }
188    public VirtualFileHandler createChildHandler(String JavaDoc name) throws IOException JavaDoc
189    {
190       VirtualFileHandler handler = linkTargets.get(name);
191       if( handler == null )
192       {
193          throw new FileNotFoundException JavaDoc("Failed to find link for: "+name+", parent: "+this);
194       }
195       return handler;
196    }
197
198    @Override JavaDoc
199    protected void doClose()
200    {
201       super.doClose();
202       links.clear();
203    }
204    
205    protected VirtualFileHandler createLinkHandler(VirtualFileHandler parent, String JavaDoc name, URI JavaDoc linkURI)
206       throws IOException JavaDoc
207    {
208       VFSContextFactory factory = VFSContextFactoryLocator.getFactory(linkURI);
209       VFSContext context = factory.getVFS(linkURI);
210       VirtualFileHandler rootHandler = context.getRoot();
211       // Wrap the handler in a delegate so we can change the parent and name
212
// TODO: if the factory caches contexts the root handler may not point to the link
213
return new DelegatingHandler(this.getVFSContext(), parent, name, rootHandler);
214    }
215 }
216
Popular Tags