001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.standard;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.HashMap;
024: import java.util.Locale;
025: import java.util.Map;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.java.plugin.PathResolver;
030: import org.java.plugin.registry.Identity;
031: import org.java.plugin.registry.PluginDescriptor;
032: import org.java.plugin.registry.PluginElement;
033: import org.java.plugin.registry.PluginFragment;
034: import org.java.plugin.util.ExtendedProperties;
035: import org.java.plugin.util.IoUtil;
036:
037: /**
038: * Standard simple implementation of path resolver. For resolving it uses
039: * plug-in element registration (see {@link #registerContext(Identity, URL)})
040: * procedure.
041: * @version $Id$
042: */
043: public class StandardPathResolver implements PathResolver {
044: protected Log log = LogFactory.getLog(getClass());
045: private Map<String, URL> urlMap = new HashMap<String, URL>();
046:
047: /**
048: * This implementation accepts {@link PluginDescriptor} or
049: * {@link PluginFragment} as valid plug-in elements.
050: * @see org.java.plugin.PathResolver#registerContext(
051: * org.java.plugin.registry.Identity, java.net.URL)
052: */
053: public void registerContext(final Identity idt, final URL url) {
054: if (!(idt instanceof PluginDescriptor)
055: && !(idt instanceof PluginFragment)) {
056: throw new IllegalArgumentException(
057: "unsupported identity class " //$NON-NLS-1$
058: + idt.getClass().getName());
059: }
060: final URL oldUrl = urlMap.put(idt.getId(), url);
061: if (oldUrl != null) {
062: log.warn("old context URL " + oldUrl //$NON-NLS-1$
063: + " has been replaced with new " + url //$NON-NLS-1$
064: + " for " + idt //$NON-NLS-1$
065: + " with key " + idt.getId()); //$NON-NLS-1$
066: } else {
067: if (log.isDebugEnabled()) {
068: log.debug("context URL " + url //$NON-NLS-1$
069: + " registered for " + idt //$NON-NLS-1$
070: + " with key " + idt.getId()); //$NON-NLS-1$
071: }
072: }
073: }
074:
075: /**
076: * @see org.java.plugin.PathResolver#unregisterContext(java.lang.String)
077: */
078: public void unregisterContext(final String id) {
079: final URL url = urlMap.remove(id);
080: if (url == null) {
081: log.warn("no context was registered with key " + id); //$NON-NLS-1$
082: } else {
083: if (log.isDebugEnabled()) {
084: log.debug("context URL " + url //$NON-NLS-1$
085: + " un-registered for key " + id); //$NON-NLS-1$
086: }
087: }
088: }
089:
090: /**
091: * @see org.java.plugin.PathResolver#resolvePath(
092: * org.java.plugin.registry.Identity, java.lang.String)
093: */
094: public URL resolvePath(final Identity identity, final String path) {
095: URL baseUrl;
096: if ((identity instanceof PluginDescriptor)
097: || (identity instanceof PluginFragment)) {
098: baseUrl = getRegisteredContext(identity.getId());
099: } else if (identity instanceof PluginElement) {
100: PluginElement<?> element = (PluginElement) identity;
101: if (element.getDeclaringPluginFragment() != null) {
102: baseUrl = getRegisteredContext(element
103: .getDeclaringPluginFragment().getId());
104: } else {
105: baseUrl = getRegisteredContext(element
106: .getDeclaringPluginDescriptor().getId());
107: }
108: } else {
109: throw new IllegalArgumentException(
110: "unknown identity class " //$NON-NLS-1$
111: + identity.getClass().getName());
112: }
113: return resolvePath(baseUrl, path);
114: }
115:
116: /**
117: * @see org.java.plugin.PathResolver#getRegisteredContext(java.lang.String)
118: */
119: public URL getRegisteredContext(final String id) {
120: final URL result = urlMap.get(id);
121: if (result == null) {
122: throw new IllegalArgumentException("unknown plug-in or" //$NON-NLS-1$
123: + " plug-in fragment ID - " + id); //$NON-NLS-1$
124: }
125: return result;
126: }
127:
128: /**
129: * @see org.java.plugin.PathResolver#isContextRegistered(java.lang.String)
130: */
131: public boolean isContextRegistered(final String id) {
132: return urlMap.containsKey(id);
133: }
134:
135: /**
136: * Resolves given path against given base URL.
137: * @param baseUrl base URL to resolve given path
138: * @param path path to be resolved
139: * @return resolved URL
140: */
141: protected URL resolvePath(final URL baseUrl, final String path) {
142: try {
143: if ("".equals(path) || "/".equals(path)) { //$NON-NLS-1$ //$NON-NLS-2$
144: return maybeJarUrl(baseUrl);
145: }
146: return maybeJarUrl(new URL(maybeJarUrl(baseUrl), path));
147: } catch (MalformedURLException mue) {
148: log.error("can't create URL in context of " + baseUrl //$NON-NLS-1$
149: + " and path " + path, mue); //$NON-NLS-1$
150: throw new IllegalArgumentException("path " + path //$NON-NLS-1$
151: + " in context of " + baseUrl //$NON-NLS-1$
152: + " cause creation of malformed URL"); //$NON-NLS-1$
153: }
154: }
155:
156: protected URL maybeJarUrl(final URL url)
157: throws MalformedURLException {
158: if ("jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
159: return url;
160: }
161: File file = IoUtil.url2file(url);
162: if ((file == null) || !file.isFile()) {
163: return url;
164: }
165: String fileName = file.getName().toLowerCase(
166: Locale.getDefault());
167: if (fileName.endsWith(".jar") //$NON-NLS-1$
168: || fileName.endsWith(".zip")) { //$NON-NLS-1$
169: return new URL("jar:" //$NON-NLS-1$
170: + IoUtil.file2url(file).toExternalForm() + "!/"); //$NON-NLS-1$
171: }
172: return url;
173: }
174:
175: /**
176: * No configuration parameters expected in this implementation.
177: * @see org.java.plugin.PathResolver#configure(ExtendedProperties)
178: */
179: public void configure(final ExtendedProperties config)
180: throws Exception {
181: // no-op
182: }
183: }
|