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.HashMap ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.java.plugin.PathResolver; 31 import org.java.plugin.registry.Identity; 32 import org.java.plugin.registry.PluginDescriptor; 33 import org.java.plugin.registry.PluginElement; 34 import org.java.plugin.registry.PluginFragment; 35 import org.java.plugin.util.ExtendedProperties; 36 import org.java.plugin.util.IoUtil; 37 38 44 public class StandardPathResolver implements PathResolver { 45 protected Log log = LogFactory.getLog(getClass()); 46 47 private Map urlMap = 48 new HashMap (); 50 56 public void registerContext(final Identity idt, final URL url) { 57 if (!(idt instanceof PluginDescriptor) 58 && !(idt instanceof PluginFragment)) { 59 throw new IllegalArgumentException ( 60 "unsupported identity class " + idt.getClass().getName()); 62 } 63 URL oldUrl = (URL ) urlMap.put(idt.getId(), url); 64 if (oldUrl != null) { 65 log.warn("old context URL " + oldUrl + " has been replaced with new " + url + " for " + idt + " with key " + idt.getId()); } else { 70 if (log.isDebugEnabled()) { 71 log.debug("context URL " + url + " registered for " + idt + " with key " + idt.getId()); } 75 } 76 } 77 78 81 public void unregisterContext(final String id) { 82 URL url = (URL ) urlMap.remove(id); 83 if (url == null) { 84 log.warn("no context was registered with key " + id); } else { 86 if (log.isDebugEnabled()) { 87 log.debug("context URL " + url + " un-registered for key " + id); } 90 } 91 } 92 93 97 public URL resolvePath(final Identity identity, final String path) { 98 URL baseUrl; 99 if ((identity instanceof PluginDescriptor) 100 || (identity instanceof PluginFragment)) { 101 baseUrl = getRegisteredContext(identity.getId()); 102 } else if (identity instanceof PluginElement) { 103 PluginElement element = (PluginElement) identity; 104 if (element.getDeclaringPluginFragment() != null) { 105 baseUrl = getRegisteredContext( 106 element.getDeclaringPluginFragment().getId()); 107 } else { 108 baseUrl = getRegisteredContext( 109 element.getDeclaringPluginDescriptor().getId()); 110 } 111 } else { 112 throw new IllegalArgumentException ("unknown identity class " + identity.getClass().getName()); 114 } 115 return resolvePath(baseUrl, path); 116 } 117 118 121 public URL getRegisteredContext(final String id) { 122 URL result = (URL ) urlMap.get(id); 123 if (result == null) { 124 throw new IllegalArgumentException ("unknown plug-in or" + " plug-in fragment ID - " + id); } 127 return result; 128 } 129 130 133 public boolean isContextRegistered(final String id) { 134 return urlMap.containsKey(id); 135 } 136 137 143 protected URL resolvePath(final URL baseUrl, final String path) { 144 try { 145 165 if ("".equals(path) || "/".equals(path)) { return maybeJarUrl(baseUrl); 167 } 168 return maybeJarUrl(new URL (maybeJarUrl(baseUrl), path)); 169 } catch (MalformedURLException mue) { 170 log.error("can't create URL in context of " + baseUrl + " and path " + path, mue); throw new IllegalArgumentException ("path " + path + " in context of " + baseUrl + " cause creation of malformed URL"); } 176 } 177 178 protected URL maybeJarUrl(final URL url) throws MalformedURLException { 179 if ("jar".equalsIgnoreCase(url.getProtocol())) { return url; 181 } 182 File file = IoUtil.url2file(url); 183 if ((file == null) || !file.isFile()) { 184 return url; 185 } 186 String fileName = 187 file.getName().toLowerCase(Locale.getDefault()); 188 if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) { return new URL ("jar:" + IoUtil.file2url(file).toExternalForm() 192 + "!/"); } 194 return url; 195 } 196 197 201 public void configure(final ExtendedProperties config) throws Exception { 202 } 204 } 205 | Popular Tags |