1 22 package org.jboss.virtual; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.UnsupportedEncodingException ; 27 import java.net.URI ; 28 import java.net.URISyntaxException ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.HashMap ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 import java.util.StringTokenizer ; 37 import java.util.jar.Attributes ; 38 import java.util.jar.JarFile ; 39 import java.util.jar.Manifest ; 40 41 import org.jboss.logging.Logger; 42 import org.jboss.util.StringPropertyReplacer; 43 import org.jboss.virtual.spi.LinkInfo; 44 45 51 public class VFSUtils 52 { 53 54 private static final Logger log = Logger.getLogger(VFSUtils.class); 55 56 public static final String VFS_LINK_PREFIX = ".vfslink"; 57 58 public static final String VFS_LINK_NAME = "vfs.link.name"; 59 public static final String VFS_LINK_TARGET = "vfs.link.target"; 60 61 68 public static String getPathsString(Collection <VirtualFile> paths) 69 { 70 StringBuilder buffer = new StringBuilder (); 71 boolean first = true; 72 for (VirtualFile path : paths) 73 { 74 if (path == null) 75 throw new IllegalArgumentException ("Null path in " + paths); 76 if (first == false) 77 buffer.append(':'); 78 else 79 first = false; 80 buffer.append(path.getPathName()); 81 } 82 83 if (first == true) 84 buffer.append("<empty>"); 85 86 return buffer.toString(); 87 } 88 89 99 public static void addManifestLocations(VirtualFile file, List <VirtualFile> paths) throws IOException 100 { 101 if (file == null) 102 throw new IllegalArgumentException ("Null file"); 103 if (paths == null) 104 throw new IllegalArgumentException ("Null paths"); 105 106 Manifest manifest = getManifest(file); 107 if (manifest == null) 108 return; 109 110 Attributes mainAttributes = manifest.getMainAttributes(); 111 String classPath = mainAttributes.getValue(Attributes.Name.CLASS_PATH); 112 113 if (classPath == null) 114 { 115 if (log.isTraceEnabled()) 116 log.trace("Manifest has no Class-Path for " + file.getPathName()); 117 return; 118 } 119 120 VirtualFile parent = file.getParent(); 121 if (parent == null) 122 throw new IllegalStateException (file + " has no parent."); 123 124 URL parentURL = null; 125 URL vfsRootURL = null; 126 int rootPathLength = 0; 127 try 128 { 129 parentURL = parent.toURL(); 130 vfsRootURL = file.getVFS().getRoot().toURL(); 131 rootPathLength = vfsRootURL.getPath().length(); 132 } 133 catch(URISyntaxException e) 134 { 135 IOException ioe = new IOException ("Failed to get parent URL"); 136 ioe.initCause(e); 137 } 138 139 StringTokenizer tokenizer = new StringTokenizer (classPath); 140 while (tokenizer.hasMoreTokens()) 141 { 142 String path = tokenizer.nextToken(); 143 144 try 145 { 146 URL libURL = new URL (parentURL, path); 147 String libPath = libURL.getPath(); 148 if( rootPathLength > libPath.length() ) 150 throw new IOException ("Invalid rootPath: "+vfsRootURL+", libPath: "+libPath); 151 String vfsLibPath = libPath.substring(rootPathLength); 152 VirtualFile vf = file.getVFS().findChild(vfsLibPath); 153 paths.add(vf); 154 } 155 catch (IOException e) 156 { 157 log.debug("Manifest Class-Path entry " + path + " ignored for " + file.getPathName() + " reason=" + e); 158 } 159 } 160 } 161 162 172 public static Manifest getManifest(VirtualFile archive) throws IOException 173 { 174 if (archive == null) 175 throw new IllegalArgumentException ("Null archive"); 176 177 VirtualFile manifest; 178 try 179 { 180 manifest = archive.findChild(JarFile.MANIFEST_NAME); 181 } 182 catch (IOException ignored) 183 { 184 log.debug("Can't find manifest for " + archive.getPathName()); 185 return null; 186 } 187 188 InputStream stream = manifest.openStream(); 189 try 190 { 191 return new Manifest (stream); 192 } 193 finally 194 { 195 try 196 { 197 stream.close(); 198 } 199 catch (IOException ignored) 200 { 201 } 202 } 203 } 204 205 214 public static Manifest getManifest(VFS archive) throws IOException 215 { 216 VirtualFile root = archive.getRoot(); 217 return getManifest(root); 218 } 219 220 227 public static String fixName(String name) 228 { 229 if (name == null) 230 throw new IllegalArgumentException ("Null name"); 231 232 int length = name.length(); 233 if (length <= 1) 234 return name; 235 if (name.charAt(length-1) == '/') 236 return name.substring(0, length-1); 237 return name; 238 } 239 240 245 public static String getName(URI uri) 246 { 247 String name = uri.getPath(); 248 if( name != null ) 249 { 250 int lastSlash = name.lastIndexOf('/'); 252 if( lastSlash > 0 ) 253 name = name.substring(lastSlash+1); 254 } 255 return name; 256 } 257 258 264 public static Map <String , String > parseURLQuery(String query) 265 { 266 HashMap <String , String > pairsMap = new HashMap <String , String >(); 267 if( query != null ) 268 { 269 StringTokenizer tokenizer = new StringTokenizer (query, "=&"); 270 while( tokenizer.hasMoreTokens() ) 271 { 272 String name = tokenizer.nextToken(); 273 String value = tokenizer.nextToken(); 274 pairsMap.put(name, value); 275 } 276 } 277 return pairsMap; 278 } 279 280 285 public static boolean isLink(String name) 286 { 287 return name.indexOf(VFS_LINK_PREFIX) >= 0; 288 } 289 290 301 public static List <LinkInfo> readLinkInfo(InputStream is, String name, Properties props) 302 throws IOException , URISyntaxException 303 { 304 ArrayList <LinkInfo> info = new ArrayList <LinkInfo>(); 305 if( name.endsWith(".properties") ) 306 parseLinkProperties(is, info, props); 307 else 308 throw new UnsupportedEncodingException ("Unknown link format: "+name); 309 return info; 310 } 311 312 321 public static void parseLinkProperties(InputStream is, List <LinkInfo> info, Properties props) 322 throws IOException , URISyntaxException 323 { 324 props.load(is); 325 for(int n = 0; ; n ++) 327 { 328 String nameKey = VFS_LINK_NAME + "." + n; 329 String name = props.getProperty(nameKey); 330 String uriKey = VFS_LINK_TARGET + "." + n; 331 String uri = props.getProperty(uriKey); 332 if (uri == null) 334 { 335 break; 336 } 337 uri = StringPropertyReplacer.replaceProperties(uri); 339 LinkInfo link = new LinkInfo(name, new URI (uri)); 340 info.add(link); 341 } 342 } 343 344 350 public static URI toURI(URL url) 351 throws URISyntaxException 352 { 353 String urispec = url.toExternalForm(); 354 urispec = urispec.replaceAll(" ", "%20"); 356 URI uri = new URI (urispec); 357 return uri; 358 } 359 } 360 | Popular Tags |