1 17 18 package org.apache.geronimo.deployment.util; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.JarURLConnection ; 24 import java.net.URL ; 25 import java.net.URLConnection ; 26 import java.util.jar.JarFile ; 27 import java.util.zip.ZipException ; 28 29 42 public class URLType { 43 public static final String MANIFEST_LOCATION = "META-INF/MANIFEST.MF"; 44 45 public static final URLType RESOURCE = new URLType("RESOURCE"); 46 public static final URLType COLLECTION = new URLType("COLLECTION"); 47 public static final URLType PACKED_ARCHIVE = new URLType("PACKED_ARCHIVE"); 48 public static final URLType UNPACKED_ARCHIVE = new URLType("UNPACKED_ARCHIVE"); 49 50 public static URLType getType(File file) throws IOException { 51 if (file.isDirectory()) { 52 if (new File (file, MANIFEST_LOCATION).exists()) { 55 return UNPACKED_ARCHIVE; 56 } else { 57 return COLLECTION; 58 } 59 } else { 60 try { 62 JarFile jar = null; 63 try { 64 jar = new JarFile (file); 65 jar.getManifest(); 66 } finally { 67 if (jar != null) { 68 jar.close(); 69 } 70 } 71 return PACKED_ARCHIVE; 72 } catch (ZipException e) { 73 return RESOURCE; 74 } 75 } 76 } 77 78 85 public static URLType getType(URL url) throws IOException { 86 if (url.toString().endsWith("/")) { 87 URL metaInfURL = new URL (url, MANIFEST_LOCATION); 88 URLConnection urlConnection = metaInfURL.openConnection(); 89 urlConnection.connect(); 90 try { 91 InputStream is = urlConnection.getInputStream(); 92 is.close(); 93 return UNPACKED_ARCHIVE; 94 } catch (IOException e) { 95 return COLLECTION; 96 } 97 } else { 98 URL jarURL = new URL ("jar:" + url.toString() + "!/"); 99 JarURLConnection jarConnection = (JarURLConnection ) jarURL.openConnection(); 100 try { 101 jarConnection.getManifest(); 102 return PACKED_ARCHIVE; 103 } catch (ZipException e) { 104 return RESOURCE; 105 } 106 } 107 } 108 109 private final String desc; 110 111 private URLType(final String desc) { 112 this.desc = desc; 113 } 114 115 public boolean equals(Object obj) { 116 return this == obj; 117 } 118 119 public String toString() { 120 return desc; 121 } 122 } 123 | Popular Tags |