1 22 package org.jboss.virtual.plugins.context.file; 23 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.net.URI ; 27 import java.net.URISyntaxException ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.List ; 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 50 public class LinkHandler extends AbstractURLHandler 51 implements StructuredVirtualFileHandler 52 { 53 private static final long serialVersionUID = 1; 54 55 private List <LinkInfo> links; 56 private HashMap <String , VirtualFileHandler> linkTargets = 57 new HashMap <String , VirtualFileHandler>(3); 58 59 class ParentOfLink extends AbstractURLHandler 60 implements StructuredVirtualFileHandler 61 { 62 private static final long serialVersionUID = 1; 63 private HashMap <String , VirtualFileHandler> children = 64 new HashMap <String , VirtualFileHandler>(1); 65 66 public ParentOfLink(VFSContext context, VirtualFileHandler parent, URL url, String name) 67 { 68 super(context, parent, url, name); 69 } 70 void addChild(VirtualFileHandler child, String name) 71 { 72 children.put(name, child); 73 } 74 public VirtualFileHandler findChild(String path) throws IOException 75 { 76 return structuredFindChild(path); 77 } 78 79 public VirtualFileHandler createChildHandler(String name) throws IOException 80 { 81 return children.get(name); 82 } 83 84 public List <VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException 85 { 86 return null; 88 } 89 90 public boolean isLeaf() throws IOException 91 { 92 return false; 93 } 94 } 95 96 108 public LinkHandler(FileSystemContext context, VirtualFileHandler parent, URI uri, String name, 109 List <LinkInfo> links) 110 throws IOException , URISyntaxException 111 { 112 super(context, parent, uri.toURL(), name); 114 this.links = links; 115 for(LinkInfo link : links) 117 { 118 String linkName = link.getName(); 119 if( linkName == null ) 120 linkName = VFSUtils.getName(link.getLinkTarget()); 121 if( linkName != null ) 122 { 123 String [] paths = PathTokenizer.getTokens(linkName); 124 int n = 0; 125 VirtualFileHandler linkParent = this; 126 String atom; 127 for(; n < paths.length-1; n ++) 129 { 130 atom = paths[n]; 131 try 132 { 133 linkParent = linkParent.findChild(atom); 134 } 135 catch(IOException e) 136 { 137 break; 138 } 139 } 140 for(; n < paths.length-1; n ++) 142 { 143 atom = paths[n]; 144 URL polURL = new URL (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 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 <VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException 180 { 181 return new ArrayList <VirtualFileHandler>(linkTargets.values()); 182 } 183 184 public VirtualFileHandler findChild(String path) throws IOException 185 { 186 return structuredFindChild(path); 187 } 188 public VirtualFileHandler createChildHandler(String name) throws IOException 189 { 190 VirtualFileHandler handler = linkTargets.get(name); 191 if( handler == null ) 192 { 193 throw new FileNotFoundException ("Failed to find link for: "+name+", parent: "+this); 194 } 195 return handler; 196 } 197 198 @Override 199 protected void doClose() 200 { 201 super.doClose(); 202 links.clear(); 203 } 204 205 protected VirtualFileHandler createLinkHandler(VirtualFileHandler parent, String name, URI linkURI) 206 throws IOException 207 { 208 VFSContextFactory factory = VFSContextFactoryLocator.getFactory(linkURI); 209 VFSContext context = factory.getVFS(linkURI); 210 VirtualFileHandler rootHandler = context.getRoot(); 211 return new DelegatingHandler(this.getVFSContext(), parent, name, rootHandler); 214 } 215 } 216 | Popular Tags |