KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > boot > DefaultPluginsCollector


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2006 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.boot;
20
21 import java.io.File JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import javax.xml.parsers.SAXParserFactory JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.java.plugin.PluginManager.PluginLocation;
35 import org.java.plugin.standard.StandardPluginLocation;
36 import org.java.plugin.util.ExtendedProperties;
37 import org.xml.sax.Attributes JavaDoc;
38 import org.xml.sax.helpers.DefaultHandler JavaDoc;
39
40 /**
41  * Default implementation of plug-ins collector interface. Supported
42  * configuration parameters are:
43  * <dl>
44  * <dt>org.java.plugin.boot.pluginsRepositories</dt>
45  * <dd>Comma separated list of local plug-in repositories, given folders will
46  * be scanned for plug-ins. Default value is <code>./plugins</code>.</dd>
47  * <dt>org.java.plugin.boot.pluginsLocationsDescriptors</dt>
48  * <dd>Comma separated list of URLs for XML syntax files that describe
49  * available plug-in locations (see file syntax bellow). No default value
50  * provided.</dd>
51  * </dl>
52  * <p>
53  * Given repositories are scanned recursively collecting all folders that
54  * contain <code>plugin.xml</code> or <code>plugin-fragment.xml</code> and
55  * <code>*.zip</code> and <code>*.jar</code> files.
56  * </p>
57  * <p>
58  * Plug-ins locations descriptor is a simple XML syntax file that stores
59  * locations of all available plug-in manifests and contexts (in terms of
60  * {@link org.java.plugin.PluginManager.PluginLocation}). Here is an example:
61  * <pre>&lt;plugins&gt;
62  * &lt;plugin
63  * manifest="http://localhost/myPlugins/plugin1/plugin.xml"
64  * context="http://localhost/myPlugins/plugin1/"/&gt;
65  * &lt;plugin
66  * manifest="http://localhost/myPlugins/plugin2/plugin.xml"
67  * context="http://localhost/myPlugins/plugin2/"/&gt;
68  * &lt;plugin
69  * manifest="http://www.plugins.com/repository/plugin1/plugin.xml"
70  * context="http://www.plugins.com/repository/plugin1/"/&gt;
71  * &lt;plugin
72  * manifest="http://www.plugins.com/repository/plugin1/plugin.xml"
73  * context="http://www.plugins.com/repository/plugin1/"/&gt;
74  * &lt;/plugins&gt;</pre>
75  * Using such simple descriptor you may, for example, publish plug-ins on a site
76  * to make them available for clients without needing to download plug-ins
77  * manually.
78  * </p>
79  * @version $Id: DefaultPluginsCollector.java,v 1.6 2006/08/19 17:40:13 ddimon Exp $
80  */

81 public class DefaultPluginsCollector implements PluginsCollector {
82     protected static final String JavaDoc PARAM_PLUGINS_REPOSITORIES =
83         "org.java.plugin.boot.pluginsRepositories"; //$NON-NLS-1$
84
protected static final String JavaDoc PARAM_PLUGINS_LOCATIONS_DESCRIPTORS =
85         "org.java.plugin.boot.pluginsLocationsDescriptors"; //$NON-NLS-1$
86

87     protected Log log = LogFactory.getLog(this.getClass());
88     private List JavaDoc repositories;
89     private List JavaDoc descriptors;
90
91     /**
92      * @see org.java.plugin.boot.PluginsCollector#configure(
93      * org.java.plugin.util.ExtendedProperties)
94      */

95     public void configure(ExtendedProperties config) throws Exception JavaDoc {
96         repositories = new LinkedList JavaDoc();
97         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(
98                 config.getProperty(PARAM_PLUGINS_REPOSITORIES,
99                         '.' + File.separator + "plugins"), //$NON-NLS-1$
100
",", false); st.hasMoreTokens();) { //$NON-NLS-1$
101
String JavaDoc token = st.nextToken().trim();
102             if (token.length() == 0) {
103                 continue;
104             }
105             repositories.add(new File JavaDoc(token).getCanonicalFile());
106         }
107         log.debug("found " + repositories.size() //$NON-NLS-1$
108
+ " local plug-ins repositories"); //$NON-NLS-1$
109
descriptors = new LinkedList JavaDoc();
110         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(
111                 config.getProperty(PARAM_PLUGINS_LOCATIONS_DESCRIPTORS, ""), //$NON-NLS-1$
112
",", false); st.hasMoreTokens();) { //$NON-NLS-1$
113
String JavaDoc token = st.nextToken().trim();
114             if (token.length() == 0) {
115                 continue;
116             }
117             descriptors.add(new URL JavaDoc(token));
118         }
119         log.debug("found " + descriptors.size() //$NON-NLS-1$
120
+ " plug-ins locations descriptors"); //$NON-NLS-1$
121
}
122
123     /**
124      * @see org.java.plugin.boot.PluginsCollector#collectPluginLocations()
125      */

126     public Collection JavaDoc collectPluginLocations() {
127         List JavaDoc result = new LinkedList JavaDoc();
128         for (Iterator JavaDoc it = repositories.iterator(); it.hasNext();) {
129             File JavaDoc file = (File JavaDoc) it.next();
130             if (file.isDirectory()) {
131                 processFolder(file, result);
132             } else if (file.isFile()) {
133                 processFile(file, result);
134             } else {
135                 log.warn("unknown repository location " + file); //$NON-NLS-1$
136
}
137         }
138         for (Iterator JavaDoc it = descriptors.iterator(); it.hasNext();) {
139             processDescriptor((URL JavaDoc) it.next(), result);
140         }
141         return result;
142     }
143
144     protected void processFolder(final File JavaDoc folder, final List JavaDoc result) {
145         log.debug("processing folder - " + folder); //$NON-NLS-1$
146
try {
147             PluginLocation pluginLocation =
148                 StandardPluginLocation.create(folder);
149             if (pluginLocation != null) {
150                 result.add(pluginLocation);
151                 return;
152             }
153         } catch (MalformedURLException JavaDoc mue) {
154             log.warn("failed collecting plug-in folder " + folder //$NON-NLS-1$
155
+ ", ignoring it", mue); //$NON-NLS-1$
156
return;
157         }
158         File JavaDoc[] files = folder.listFiles();
159         for (int i = 0; i < files.length; i++) {
160             File JavaDoc file = files[i];
161             if (file.isDirectory()) {
162                 processFolder(file, result);
163             } else if (file.isFile()) {
164                 processFile(file, result);
165             }
166         }
167     }
168     
169     protected void processFile(final File JavaDoc file, final List JavaDoc result) {
170         log.debug("processing file - " + file); //$NON-NLS-1$
171
try {
172             PluginLocation pluginLocation = StandardPluginLocation.create(file);
173             if (pluginLocation != null) {
174                 result.add(pluginLocation);
175             }
176         } catch (MalformedURLException JavaDoc mue) {
177             log.warn("failed collecting plug-in file " + file //$NON-NLS-1$
178
+ ", ignoring it", mue); //$NON-NLS-1$
179
}
180     }
181     
182     private void processDescriptor(final URL JavaDoc url, final List JavaDoc result) {
183         log.debug("processing plug-ins locations descriptor, URL=" + url); //$NON-NLS-1$
184
try {
185             SAXParserFactory.newInstance().newSAXParser().parse(
186                     url.toExternalForm(),
187                     new LocationsDescriptorHandler(result));
188         } catch (Exception JavaDoc e) {
189             log.warn("failed processing plug-ins locations descriptor, URL=" //$NON-NLS-1$
190
+ url, e);
191         }
192     }
193     
194     private final class LocationsDescriptorHandler extends DefaultHandler JavaDoc {
195         private final List JavaDoc resultData;
196
197         LocationsDescriptorHandler(final List JavaDoc result) {
198             resultData = result;
199         }
200         
201         /**
202          * @see org.xml.sax.helpers.DefaultHandler#startElement(
203          * java.lang.String, java.lang.String, java.lang.String,
204          * org.xml.sax.Attributes)
205          */

206         public void startElement(final String JavaDoc uri, final String JavaDoc localName,
207                 final String JavaDoc qName, final Attributes JavaDoc attributes) {
208             if (!"plugin".equals(qName)) { //$NON-NLS-1$
209
return;
210             }
211             String JavaDoc manifest = attributes.getValue("manifest"); //$NON-NLS-1$
212
if (manifest == null) {
213                 log.warn("manifest attribute missing"); //$NON-NLS-1$
214
return;
215             }
216             URL JavaDoc manifestUrl;
217             try {
218                 manifestUrl = new URL JavaDoc(manifest);
219             } catch (MalformedURLException JavaDoc mue) {
220                 log.warn("invalid manifest URL - " + manifest, mue); //$NON-NLS-1$
221
return;
222             }
223             String JavaDoc context = attributes.getValue("context"); //$NON-NLS-1$
224
if (context == null) {
225                 log.warn("context attribute missing"); //$NON-NLS-1$
226
return;
227             }
228             URL JavaDoc contextUrl;
229             try {
230                 contextUrl = new URL JavaDoc(context);
231             } catch (MalformedURLException JavaDoc mue) {
232                 log.warn("invalid context URL - " + context, mue); //$NON-NLS-1$
233
return;
234             }
235             resultData.add(new StandardPluginLocation(contextUrl, manifestUrl));
236             log.debug("got plug-in location from descriptor, manifestUrl=" //$NON-NLS-1$
237
+ manifestUrl + ", contextURL=" + contextUrl); //$NON-NLS-1$
238
}
239     }
240 }
241
Popular Tags