1 22 package org.jboss.virtual.plugins.context.jar; 23 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.jar.JarEntry ; 34 import java.util.jar.JarFile ; 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 48 public class JarEntryHandler extends AbstractURLHandler 49 implements StructuredVirtualFileHandler 50 { 51 52 private static final long serialVersionUID = 1L; 53 54 55 private transient final JarFile jar; 56 57 58 private transient final JarEntry entry; 59 private transient List <VirtualFileHandler> entryChildren; 60 private transient Map <String , VirtualFileHandler> entryMap; 61 62 73 public JarEntryHandler(VFSContext context, VirtualFileHandler parent, JarFile jar, 74 JarEntry entry, String entryName, URL url) 75 throws IOException 76 { 77 super(context, parent, url, entryName); 78 if (jar == null) 79 throw new IllegalArgumentException ("Null jar"); 80 81 this.jar = jar; 82 this.entry = entry; 83 } 84 85 89 public void addChild(VirtualFileHandler child) 90 { 91 if( entryChildren == null ) 92 entryChildren = new ArrayList <VirtualFileHandler>(); 93 entryChildren.add(child); 94 } 95 96 101 protected JarEntry getEntry() 102 { 103 checkClosed(); 104 return entry; 105 } 106 107 @Override 108 public long getLastModified() 109 { 110 return getEntry().getTime(); 111 } 112 113 @Override 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 131 public InputStream openStream() throws IOException 132 { 133 return jar.getInputStream(getEntry()); 134 } 135 136 public List <VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException 137 { 138 checkClosed(); 139 List <VirtualFileHandler> children = entryChildren; 140 if( entryChildren == null ) 141 children = Collections.emptyList(); 142 return children; 143 } 144 145 public VirtualFileHandler findChild(String path) throws IOException 146 { 147 return super.structuredFindChild(path); 148 } 149 150 153 public VirtualFileHandler createChildHandler(String name) throws IOException 154 { 155 if( entryChildren == null ) 156 throw new FileNotFoundException (this+" has no children"); 157 if( entryMap == null ) 158 { 159 entryMap = new HashMap <String , 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 (this+" has no child: "+name); 166 return child; 167 } 168 169 } 170 | Popular Tags |