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.boot;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.Collection;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.StringTokenizer;
027:
028: import javax.xml.parsers.SAXParserFactory;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.java.plugin.PluginManager.PluginLocation;
033: import org.java.plugin.standard.StandardPluginLocation;
034: import org.java.plugin.util.ExtendedProperties;
035: import org.xml.sax.Attributes;
036: import org.xml.sax.helpers.DefaultHandler;
037:
038: /**
039: * Default implementation of plug-ins collector interface. Supported
040: * configuration parameters are:
041: * <dl>
042: * <dt>org.java.plugin.boot.pluginsRepositories</dt>
043: * <dd>Comma separated list of local plug-in repositories, given folders will
044: * be scanned for plug-ins. Default value is <code>./plugins</code>.</dd>
045: * <dt>org.java.plugin.boot.pluginsLocationsDescriptors</dt>
046: * <dd>Comma separated list of URLs for XML syntax files that describe
047: * available plug-in locations (see file syntax bellow). No default value
048: * provided.</dd>
049: * </dl>
050: * <p>
051: * Given repositories are scanned recursively collecting all folders that
052: * contain <code>plugin.xml</code> or <code>plugin-fragment.xml</code> and
053: * <code>*.zip</code> and <code>*.jar</code> files.
054: * </p>
055: * <p>
056: * Plug-ins locations descriptor is a simple XML syntax file that stores
057: * locations of all available plug-in manifests and contexts (in terms of
058: * {@link org.java.plugin.PluginManager.PluginLocation}). Here is an example:
059: * <pre><plugins>
060: * <plugin
061: * manifest="http://localhost/myPlugins/plugin1/plugin.xml"
062: * context="http://localhost/myPlugins/plugin1/"/>
063: * <plugin
064: * manifest="http://localhost/myPlugins/plugin2/plugin.xml"
065: * context="http://localhost/myPlugins/plugin2/"/>
066: * <plugin
067: * manifest="http://www.plugins.com/repository/plugin1/plugin.xml"
068: * context="http://www.plugins.com/repository/plugin1/"/>
069: * <plugin
070: * manifest="http://www.plugins.com/repository/plugin1/plugin.xml"
071: * context="http://www.plugins.com/repository/plugin1/"/>
072: * </plugins></pre>
073: * Using such simple descriptor you may, for example, publish plug-ins on a site
074: * to make them available for clients without needing to download plug-ins
075: * manually.
076: * </p>
077: * @version $Id$
078: */
079: public class DefaultPluginsCollector implements PluginsCollector {
080: protected static final String PARAM_PLUGINS_REPOSITORIES = "org.java.plugin.boot.pluginsRepositories"; //$NON-NLS-1$
081: protected static final String PARAM_PLUGINS_LOCATIONS_DESCRIPTORS = "org.java.plugin.boot.pluginsLocationsDescriptors"; //$NON-NLS-1$
082:
083: protected Log log = LogFactory.getLog(this .getClass());
084: private List<File> repositories;
085: private List<URL> descriptors;
086:
087: /**
088: * @see org.java.plugin.boot.PluginsCollector#configure(
089: * org.java.plugin.util.ExtendedProperties)
090: */
091: public void configure(ExtendedProperties config) throws Exception {
092: repositories = new LinkedList<File>();
093: for (StringTokenizer st = new StringTokenizer(config
094: .getProperty(PARAM_PLUGINS_REPOSITORIES, '.'
095: + File.separator + "plugins"), //$NON-NLS-1$
096: ",", false); st.hasMoreTokens();) { //$NON-NLS-1$
097: String token = st.nextToken().trim();
098: if (token.length() == 0) {
099: continue;
100: }
101: repositories.add(new File(token).getCanonicalFile());
102: }
103: log.debug("found " + repositories.size() //$NON-NLS-1$
104: + " local plug-ins repositories"); //$NON-NLS-1$
105: descriptors = new LinkedList<URL>();
106: for (StringTokenizer st = new StringTokenizer(config
107: .getProperty(PARAM_PLUGINS_LOCATIONS_DESCRIPTORS, ""), //$NON-NLS-1$
108: ",", false); st.hasMoreTokens();) { //$NON-NLS-1$
109: String token = st.nextToken().trim();
110: if (token.length() == 0) {
111: continue;
112: }
113: descriptors.add(new URL(token));
114: }
115: log.debug("found " + descriptors.size() //$NON-NLS-1$
116: + " plug-ins locations descriptors"); //$NON-NLS-1$
117: }
118:
119: /**
120: * @see org.java.plugin.boot.PluginsCollector#collectPluginLocations()
121: */
122: public Collection<PluginLocation> collectPluginLocations() {
123: List<PluginLocation> result = new LinkedList<PluginLocation>();
124: for (File file : repositories) {
125: if (file.isDirectory()) {
126: processFolder(file, result);
127: } else if (file.isFile()) {
128: processFile(file, result);
129: } else {
130: log.warn("unknown repository location " + file); //$NON-NLS-1$
131: }
132: }
133: for (URL url : descriptors) {
134: processDescriptor(url, result);
135: }
136: return result;
137: }
138:
139: protected void processFolder(final File folder,
140: final List<PluginLocation> result) {
141: log.debug("processing folder - " + folder); //$NON-NLS-1$
142: try {
143: PluginLocation pluginLocation = StandardPluginLocation
144: .create(folder);
145: if (pluginLocation != null) {
146: result.add(pluginLocation);
147: return;
148: }
149: } catch (MalformedURLException mue) {
150: log.warn("failed collecting plug-in folder " + folder //$NON-NLS-1$
151: + ", ignoring it", mue); //$NON-NLS-1$
152: return;
153: }
154: File[] files = folder.listFiles();
155: for (int i = 0; i < files.length; i++) {
156: File file = files[i];
157: if (file.isDirectory()) {
158: processFolder(file, result);
159: } else if (file.isFile()) {
160: processFile(file, result);
161: }
162: }
163: }
164:
165: protected void processFile(final File file,
166: final List<PluginLocation> result) {
167: log.debug("processing file - " + file); //$NON-NLS-1$
168: try {
169: PluginLocation pluginLocation = StandardPluginLocation
170: .create(file);
171: if (pluginLocation != null) {
172: result.add(pluginLocation);
173: }
174: } catch (MalformedURLException mue) {
175: log.warn("failed collecting plug-in file " + file //$NON-NLS-1$
176: + ", ignoring it", mue); //$NON-NLS-1$
177: }
178: }
179:
180: private void processDescriptor(final URL url,
181: final List<PluginLocation> result) {
182: log
183: .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 e) {
189: log.warn(
190: "failed processing plug-ins locations descriptor, URL=" //$NON-NLS-1$
191: + url, e);
192: }
193: }
194:
195: private final class LocationsDescriptorHandler extends
196: DefaultHandler {
197: private final List<PluginLocation> resultData;
198:
199: LocationsDescriptorHandler(final List<PluginLocation> result) {
200: resultData = result;
201: }
202:
203: /**
204: * @see org.xml.sax.helpers.DefaultHandler#startElement(
205: * java.lang.String, java.lang.String, java.lang.String,
206: * org.xml.sax.Attributes)
207: */
208: @Override
209: public void startElement(final String uri,
210: final String localName, final String qName,
211: final Attributes attributes) {
212: if (!"plugin".equals(qName)) { //$NON-NLS-1$
213: return;
214: }
215: String manifest = attributes.getValue("manifest"); //$NON-NLS-1$
216: if (manifest == null) {
217: log.warn("manifest attribute missing"); //$NON-NLS-1$
218: return;
219: }
220: URL manifestUrl;
221: try {
222: manifestUrl = new URL(manifest);
223: } catch (MalformedURLException mue) {
224: log.warn("invalid manifest URL - " + manifest, mue); //$NON-NLS-1$
225: return;
226: }
227: String context = attributes.getValue("context"); //$NON-NLS-1$
228: if (context == null) {
229: log.warn("context attribute missing"); //$NON-NLS-1$
230: return;
231: }
232: URL contextUrl;
233: try {
234: contextUrl = new URL(context);
235: } catch (MalformedURLException mue) {
236: log.warn("invalid context URL - " + context, mue); //$NON-NLS-1$
237: return;
238: }
239: resultData.add(new StandardPluginLocation(contextUrl,
240: manifestUrl));
241: log
242: .debug("got plug-in location from descriptor, manifestUrl=" //$NON-NLS-1$
243: + manifestUrl
244: + ", contextURL=" + contextUrl); //$NON-NLS-1$
245: }
246: }
247: }
|