1 19 package org.java.plugin.standard; 20 21 import java.io.File ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Locale ; 25 26 import org.java.plugin.PluginManager.PluginLocation; 27 import org.java.plugin.util.IoUtil; 28 29 38 public class StandardPluginLocation implements PluginLocation { 39 57 public static PluginLocation create(final File file) 58 throws MalformedURLException { 59 if (file.isDirectory()) { 60 URL manifestUrl = getManifestUrl(file); 61 return (manifestUrl == null) ? null 62 : new StandardPluginLocation( 63 IoUtil.file2url(file), manifestUrl); 64 } 65 String fileName = file.getName().toLowerCase(Locale.getDefault()); 66 if (!fileName.endsWith(".jar") && !fileName.endsWith(".zip")) { return null; 69 } 70 URL manifestUrl = getManifestUrl(file); 71 return (manifestUrl == null) ? null 72 : new StandardPluginLocation(new URL ("jar:" + IoUtil.file2url(file).toExternalForm() 74 + "!/"), manifestUrl); } 76 77 private static URL getManifestUrl(final File file) 78 throws MalformedURLException { 79 if (file.isDirectory()) { 80 File result = new File (file, "plugin.xml"); if (result.isFile()) { 82 return IoUtil.file2url(result); 83 } 84 result = new File (file, "plugin-fragment.xml"); if (result.isFile()) { 86 return IoUtil.file2url(result); 87 } 88 result = new File (file, "META-INF" + File.separator + "plugin.xml"); if (result.isFile()) { 91 return IoUtil.file2url(result); 92 } 93 result = new File (file, "META-INF" + File.separator + "plugin-fragment.xml"); if (result.isFile()) { 96 return IoUtil.file2url(result); 97 } 98 return null; 99 } 100 if (!file.isFile()) { 101 return null; 102 } 103 URL url = new URL ("jar:" + IoUtil.file2url(file).toExternalForm() 105 + "!/plugin.xml"); if (IoUtil.isResourceExists(url)) { 107 return url; 108 } 109 url = new URL ("jar:" + IoUtil.file2url(file).toExternalForm() 111 + "!/plugin-fragment.xml"); if (IoUtil.isResourceExists(url)) { 113 return url; 114 } 115 url = new URL ("jar:" + IoUtil.file2url(file).toExternalForm() 117 + "!/META-INF/plugin.xml"); if (IoUtil.isResourceExists(url)) { 119 return url; 120 } 121 url = new URL ("jar:" + IoUtil.file2url(file).toExternalForm() 123 + "!/META-INF/plugin-fragment.xml"); if (IoUtil.isResourceExists(url)) { 125 return url; 126 } 127 return null; 128 } 129 130 private final URL context; 131 private final URL manifest; 132 133 138 public StandardPluginLocation(final URL aContext, final URL aManifest) { 139 if (aContext == null) { 140 throw new NullPointerException ("context"); } 142 if (aManifest == null) { 143 throw new NullPointerException ("manifest"); } 145 context = aContext; 146 manifest = aManifest; 147 } 148 149 157 public StandardPluginLocation(final File file, final String manifestPath) 158 throws MalformedURLException { 159 if (file.isDirectory()) { 160 context = IoUtil.file2url(file); 161 } else { 162 context = new URL ("jar:" + IoUtil.file2url(file).toExternalForm() + "!/"); } 165 manifest = new URL (context, manifestPath.startsWith("/") ? manifestPath.substring(1) : manifestPath); 167 } 168 169 172 public URL getManifestLocation() { 173 return manifest; 174 } 175 176 179 public URL getContextLocation() { 180 return context; 181 } 182 183 186 public String toString() { 187 return context.toString(); 188 } 189 } 190 | Popular Tags |