File: DefaultPluginsCollector.java

package info (click to toggle)
libjpf-java 1.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,280 kB
  • sloc: java: 13,449; xml: 337; makefile: 17
file content (239 lines) | stat: -rw-r--r-- 10,189 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2004-2007 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin.boot;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.java.plugin.PluginManager.PluginLocation;
import org.java.plugin.standard.StandardPluginLocation;
import org.java.plugin.util.ExtendedProperties;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Default implementation of plug-ins collector interface. Supported
 * configuration parameters are:
 * <dl>
 *   <dt>org.java.plugin.boot.pluginsRepositories</dt>
 *   <dd>Comma separated list of local plug-in repositories, given folders will
 *     be scanned for plug-ins. Default value is <code>./plugins</code>.</dd>
 *   <dt>org.java.plugin.boot.pluginsLocationsDescriptors</dt>
 *   <dd>Comma separated list of URLs for XML syntax files that describe
 *     available plug-in locations (see file syntax bellow). No default value
 *     provided.</dd>
 * </dl>
 * <p>
 * Given repositories are scanned recursively collecting all folders that
 * contain <code>plugin.xml</code> or <code>plugin-fragment.xml</code> and
 * <code>*.zip</code> and <code>*.jar</code> files.
 * </p>
 * <p>
 * Plug-ins locations descriptor is a simple XML syntax file that stores
 * locations of all available plug-in manifests and contexts (in terms of
 * {@link org.java.plugin.PluginManager.PluginLocation}). Here is an example:
 * <pre>&lt;plugins&gt;
 *   &lt;plugin
 *     manifest="http://localhost/myPlugins/plugin1/plugin.xml"
 *     context="http://localhost/myPlugins/plugin1/"/&gt;
 *   &lt;plugin
 *     manifest="http://localhost/myPlugins/plugin2/plugin.xml"
 *     context="http://localhost/myPlugins/plugin2/"/&gt;
 *   &lt;plugin
 *     manifest="http://www.plugins.com/repository/plugin1/plugin.xml"
 *     context="http://www.plugins.com/repository/plugin1/"/&gt;
 *   &lt;plugin
 *     manifest="http://www.plugins.com/repository/plugin1/plugin.xml"
 *     context="http://www.plugins.com/repository/plugin1/"/&gt;
 * &lt;/plugins&gt;</pre>
 * Using such simple descriptor you may, for example, publish plug-ins on a site
 * to make them available for clients without needing to download plug-ins
 * manually.
 * </p>
 * @version $Id$
 */
public class DefaultPluginsCollector implements PluginsCollector {
    protected static final String PARAM_PLUGINS_REPOSITORIES =
        "org.java.plugin.boot.pluginsRepositories"; //$NON-NLS-1$
    protected static final String PARAM_PLUGINS_LOCATIONS_DESCRIPTORS =
        "org.java.plugin.boot.pluginsLocationsDescriptors"; //$NON-NLS-1$

    protected Log log = LogFactory.getLog(this.getClass());
    private List<File> repositories;
    private List<URL> descriptors;

    /**
     * @see org.java.plugin.boot.PluginsCollector#configure(
     *      org.java.plugin.util.ExtendedProperties)
     */
    public void configure(ExtendedProperties config) throws Exception {
        repositories = new LinkedList<File>();
        for (StringTokenizer st = new StringTokenizer(
                config.getProperty(PARAM_PLUGINS_REPOSITORIES,
                        '.' + File.separator + "plugins"), //$NON-NLS-1$
                        ",", false); st.hasMoreTokens();) { //$NON-NLS-1$
            String token = st.nextToken().trim();
            if (token.length() == 0) {
                continue;
            }
            repositories.add(new File(token).getCanonicalFile());
        }
        log.debug("found " + repositories.size() //$NON-NLS-1$
                + " local plug-ins repositories"); //$NON-NLS-1$
        descriptors = new LinkedList<URL>();
        for (StringTokenizer st = new StringTokenizer(
                config.getProperty(PARAM_PLUGINS_LOCATIONS_DESCRIPTORS, ""), //$NON-NLS-1$
                ",", false); st.hasMoreTokens();) { //$NON-NLS-1$
            String token = st.nextToken().trim();
            if (token.length() == 0) {
                continue;
            }
            descriptors.add(new URL(token));
        }
        log.debug("found " + descriptors.size() //$NON-NLS-1$
                + " plug-ins locations descriptors"); //$NON-NLS-1$
    }

    /**
     * @see org.java.plugin.boot.PluginsCollector#collectPluginLocations()
     */
    public Collection<PluginLocation> collectPluginLocations() {
        List<PluginLocation> result = new LinkedList<PluginLocation>();
        for (File file : repositories) {
            if (file.isDirectory()) {
                processFolder(file, result);
            } else if (file.isFile()) {
                processFile(file, result);
            } else {
                log.warn("unknown repository location " + file); //$NON-NLS-1$
            }
        }
        for (URL url : descriptors) {
            processDescriptor(url, result);
        }
        return result;
    }

    protected void processFolder(final File folder, final List<PluginLocation> result) {
        log.debug("processing folder - " + folder); //$NON-NLS-1$
        try {
            PluginLocation pluginLocation =
                StandardPluginLocation.create(folder);
            if (pluginLocation != null) {
                result.add(pluginLocation);
                return;
            }
        } catch (MalformedURLException mue) {
            log.warn("failed collecting plug-in folder " + folder //$NON-NLS-1$
                    + ", ignoring it", mue); //$NON-NLS-1$
            return;
        }
        File[] files = folder.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                processFolder(file, result);
            } else if (file.isFile()) {
                processFile(file, result);
            }
        }
    }
    
    protected void processFile(final File file, final List<PluginLocation> result) {
        log.debug("processing file - " + file); //$NON-NLS-1$
        try {
            PluginLocation pluginLocation = StandardPluginLocation.create(file);
            if (pluginLocation != null) {
                result.add(pluginLocation);
            }
        } catch (MalformedURLException mue) {
            log.warn("failed collecting plug-in file " + file //$NON-NLS-1$
                    + ", ignoring it", mue); //$NON-NLS-1$
        }
    }
    
    private void processDescriptor(final URL url, final List<PluginLocation> result) {
        log.debug("processing plug-ins locations descriptor, URL=" + url); //$NON-NLS-1$
        try {
            SAXParserFactory.newInstance().newSAXParser().parse(
                    url.toExternalForm(),
                    new LocationsDescriptorHandler(result));
        } catch (Exception e) {
            log.warn("failed processing plug-ins locations descriptor, URL=" //$NON-NLS-1$
                    + url, e);
        }
    }
    
    private final class LocationsDescriptorHandler extends DefaultHandler {
        private final List<PluginLocation> resultData;

        LocationsDescriptorHandler(final List<PluginLocation> result) {
            resultData = result;
        }
        
        /**
         * @see org.xml.sax.helpers.DefaultHandler#startElement(
         *      java.lang.String, java.lang.String, java.lang.String,
         *      org.xml.sax.Attributes)
         */
        @Override
        public void startElement(final String uri, final String localName,
                final String qName, final Attributes attributes) {
            if (!"plugin".equals(qName)) { //$NON-NLS-1$
                return;
            }
            String manifest = attributes.getValue("manifest"); //$NON-NLS-1$
            if (manifest == null) {
                log.warn("manifest attribute missing"); //$NON-NLS-1$
                return;
            }
            URL manifestUrl;
            try {
                manifestUrl = new URL(manifest);
            } catch (MalformedURLException mue) {
                log.warn("invalid manifest URL - " + manifest, mue); //$NON-NLS-1$
                return;
            }
            String context = attributes.getValue("context"); //$NON-NLS-1$
            if (context == null) {
                log.warn("context attribute missing"); //$NON-NLS-1$
                return;
            }
            URL contextUrl;
            try {
                contextUrl = new URL(context);
            } catch (MalformedURLException mue) {
                log.warn("invalid context URL - " + context, mue); //$NON-NLS-1$
                return;
            }
            resultData.add(new StandardPluginLocation(contextUrl, manifestUrl));
            log.debug("got plug-in location from descriptor, manifestUrl=" //$NON-NLS-1$
                    + manifestUrl + ", contextURL=" + contextUrl); //$NON-NLS-1$
        }
    }
}