KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > standard > StandardPathResolver


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2007 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.standard;
20
21 import java.io.File JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
39  * Standard simple implementation of path resolver. For resolving it uses
40  * plug-in element registration (see {@link #registerContext(Identity, URL)})
41  * procedure.
42  * @version $Id: StandardPathResolver.java,v 1.16 2007/01/05 13:22:09 ddimon Exp $
43  */

44 public class StandardPathResolver implements PathResolver {
45     protected Log log = LogFactory.getLog(getClass());
46     
47     private Map JavaDoc urlMap =
48         new HashMap JavaDoc(); // <pluginId or fragmentId, home URL>
49

50     /**
51      * This implementation accepts {@link PluginDescriptor} or
52      * {@link PluginFragment} as valid plug-in elements.
53      * @see org.java.plugin.PathResolver#registerContext(
54      * org.java.plugin.registry.Identity, java.net.URL)
55      */

56     public void registerContext(final Identity idt, final URL JavaDoc url) {
57         if (!(idt instanceof PluginDescriptor)
58                 && !(idt instanceof PluginFragment)) {
59             throw new IllegalArgumentException JavaDoc(
60                     "unsupported identity class " //$NON-NLS-1$
61
+ idt.getClass().getName());
62         }
63         URL JavaDoc oldUrl = (URL JavaDoc) urlMap.put(idt.getId(), url);
64         if (oldUrl != null) {
65             log.warn("old context URL " + oldUrl //$NON-NLS-1$
66
+ " has been replaced with new " + url //$NON-NLS-1$
67
+ " for " + idt //$NON-NLS-1$
68
+ " with key " + idt.getId()); //$NON-NLS-1$
69
} else {
70             if (log.isDebugEnabled()) {
71                 log.debug("context URL " + url //$NON-NLS-1$
72
+ " registered for " + idt //$NON-NLS-1$
73
+ " with key " + idt.getId()); //$NON-NLS-1$
74
}
75         }
76     }
77
78     /**
79      * @see org.java.plugin.PathResolver#unregisterContext(java.lang.String)
80      */

81     public void unregisterContext(final String JavaDoc id) {
82         URL JavaDoc url = (URL JavaDoc) urlMap.remove(id);
83         if (url == null) {
84             log.warn("no context was registered with key " + id); //$NON-NLS-1$
85
} else {
86             if (log.isDebugEnabled()) {
87                 log.debug("context URL " + url //$NON-NLS-1$
88
+ " un-registered for key " + id); //$NON-NLS-1$
89
}
90         }
91     }
92
93     /**
94      * @see org.java.plugin.PathResolver#resolvePath(
95      * org.java.plugin.registry.Identity, java.lang.String)
96      */

97     public URL JavaDoc resolvePath(final Identity identity, final String JavaDoc path) {
98         URL JavaDoc 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 JavaDoc("unknown identity class " //$NON-NLS-1$
113
+ identity.getClass().getName());
114         }
115         return resolvePath(baseUrl, path);
116     }
117     
118     /**
119      * @see org.java.plugin.PathResolver#getRegisteredContext(java.lang.String)
120      */

121     public URL JavaDoc getRegisteredContext(final String JavaDoc id) {
122         URL JavaDoc result = (URL JavaDoc) urlMap.get(id);
123         if (result == null) {
124             throw new IllegalArgumentException JavaDoc("unknown plug-in or" //$NON-NLS-1$
125
+ " plug-in fragment ID - " + id); //$NON-NLS-1$
126
}
127         return result;
128     }
129     
130     /**
131      * @see org.java.plugin.PathResolver#isContextRegistered(java.lang.String)
132      */

133     public boolean isContextRegistered(final String JavaDoc id) {
134         return urlMap.containsKey(id);
135     }
136
137     /**
138      * Resolves given path against given base URL.
139      * @param baseUrl base URL to resolve given path
140      * @param path path to be resolved
141      * @return resolved URL
142      */

143     protected URL JavaDoc resolvePath(final URL JavaDoc baseUrl, final String JavaDoc path) {
144         try {
145             //return new URL(homeUrl, path);
146
// Patch from Sebastian Kopsan
147
/*
148             File file = IoUtil.url2file(baseUrl);
149             if ((file != null) && file.isFile()) {
150                 String fileName =
151                     file.getName().toLowerCase(Locale.getDefault());
152                 if (fileName.endsWith(".jar") //$NON-NLS-1$
153                         || fileName.endsWith(".zip")) { //$NON-NLS-1$
154                     if ("".equals(path) || "/".equals(path)) { //$NON-NLS-1$ //$NON-NLS-2$
155                         return new URL("jar:" //$NON-NLS-1$
156                                 + IoUtil.file2url(file).toExternalForm()
157                                 + "!/"); //$NON-NLS-1$
158                     }
159                     return new URL("jar:" //$NON-NLS-1$
160                             + IoUtil.file2url(file).toExternalForm()
161                             + "!/" + path); //$NON-NLS-1$
162                 }
163             }
164             */

165             if ("".equals(path) || "/".equals(path)) { //$NON-NLS-1$ //$NON-NLS-2$
166
return maybeJarUrl(baseUrl);
167             }
168             return maybeJarUrl(new URL JavaDoc(maybeJarUrl(baseUrl), path));
169         } catch (MalformedURLException JavaDoc mue) {
170             log.error("can't create URL in context of " + baseUrl //$NON-NLS-1$
171
+ " and path " + path, mue); //$NON-NLS-1$
172
throw new IllegalArgumentException JavaDoc("path " + path //$NON-NLS-1$
173
+ " in context of " + baseUrl //$NON-NLS-1$
174
+ " cause creation of malformed URL"); //$NON-NLS-1$
175
}
176     }
177     
178     protected URL JavaDoc maybeJarUrl(final URL JavaDoc url) throws MalformedURLException JavaDoc {
179         if ("jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
180
return url;
181         }
182         File JavaDoc file = IoUtil.url2file(url);
183         if ((file == null) || !file.isFile()) {
184             return url;
185         }
186         String JavaDoc fileName =
187             file.getName().toLowerCase(Locale.getDefault());
188         if (fileName.endsWith(".jar") //$NON-NLS-1$
189
|| fileName.endsWith(".zip")) { //$NON-NLS-1$
190
return new URL JavaDoc("jar:" //$NON-NLS-1$
191
+ IoUtil.file2url(file).toExternalForm()
192                     + "!/"); //$NON-NLS-1$
193
}
194         return url;
195     }
196
197     /**
198      * No configuration parameters expected in this implementation.
199      * @see org.java.plugin.PathResolver#configure(ExtendedProperties)
200      */

201     public void configure(final ExtendedProperties config) throws Exception JavaDoc {
202         // no-op
203
}
204 }
205
Popular Tags