2
* Copyright (c) 2005-2010 Laf-Plugin Kirill Grouchnikov and contributors. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Flamingo Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package org.pushingpixels.lafplugin;
32
import java.io.InputStream;
33
import java.io.InputStreamReader;
37
import javax.swing.UIManager;
40
* Plugin manager for look-and-feels.
42
* @author Kirill Grouchnikov
43
* @author Erik Vickroy
44
* @author Robert Beeger
45
* @author Frederic Lavigne
46
* @author Pattrick Gotthardt
48
public class PluginManager {
49
private String mainTag;
51
private String pluginTag;
53
private String xmlName;
61
* The name of XML file that contains plugin configuration.
63
* The main tag in the XML configuration file.
65
* The tag that corresponds to a single plugin kind. Specifies
66
* the plugin kind that will be located in
67
* {@link #getAvailablePlugins(boolean)}.
69
public PluginManager(String xmlName, String mainTag, String pluginTag) {
70
this.xmlName = xmlName;
71
this.mainTag = mainTag;
72
this.pluginTag = pluginTag;
76
// protected String getPluginClass(URL pluginUrl) {
77
// InputStream is = null;
79
// DocumentBuilder builder = DocumentBuilderFactory.newInstance()
80
// .newDocumentBuilder();
81
// is = pluginUrl.openStream();
82
// Document doc = builder.parse(is);
83
// Node root = doc.getFirstChild();
84
// if (!this.mainTag.equals(root.getNodeName()))
86
// NodeList children = root.getChildNodes();
87
// for (int i = 0; i < children.getLength(); i++) {
88
// Node child = children.item(i);
89
// if (!this.pluginTag.equals(child.getNodeName()))
91
// if (child.getChildNodes().getLength() != 1)
93
// Node text = child.getFirstChild();
94
// if (text.getNodeType() != Node.TEXT_NODE)
96
// return text.getNodeValue();
99
// } catch (Exception exc) {
105
// } catch (Exception e) {
111
protected String getPluginClass(URL pluginUrl) {
112
InputStream is = null;
113
InputStreamReader isr = null;
115
XMLElement xml = new XMLElement();
116
is = pluginUrl.openStream();
117
isr = new InputStreamReader(is);
118
xml.parseFromReader(isr);
119
if (!this.mainTag.equals(xml.getName()))
121
Enumeration children = xml.enumerateChildren();
122
while (children.hasMoreElements()) {
123
XMLElement child = (XMLElement) children.nextElement();
124
if (!this.pluginTag.equals(child.getName()))
126
if (child.countChildren() != 0)
128
return child.getContent();
131
} catch (Exception exc) {
137
} catch (Exception e) {
143
} catch (Exception e) {
149
protected Object getPlugin(URL pluginUrl) throws Exception {
150
String pluginClassName = this.getPluginClass(pluginUrl);
151
if (pluginClassName == null)
153
ClassLoader classLoader = (ClassLoader) UIManager.get("ClassLoader");
154
if (classLoader == null)
155
classLoader = Thread.currentThread().getContextClassLoader();
156
Class pluginClass = Class.forName(pluginClassName, true, classLoader);
157
if (pluginClass == null)
159
Object pluginInstance = pluginClass.newInstance();
160
if (pluginInstance == null)
162
return pluginInstance;
166
* Returns a collection of all available plugins.
168
* @return Collection of all available plugins. The classpath is scanned
170
* @see #getAvailablePlugins(boolean)
172
public Set getAvailablePlugins() {
173
return this.getAvailablePlugins(false);
177
* Returns a collection of all available plugins. The parameter specifies
178
* whether the classpath should be rescanned or whether to return the
179
* already found plugins (after first-time scan).
182
* If <code>true</code>, the classpath is scanned for available
183
* plugins every time <code>this</code> function is called. If
184
* <code>false</code>, the classpath scan is performed only once.
185
* The consecutive calls return the cached result.
186
* @return Collection of all available plugins.
188
public Set getAvailablePlugins(boolean toReload) {
189
if (!toReload && (this.plugins != null))
192
this.plugins = new HashSet();
194
// the following is fix by Dag Joar and Christian Schlichtherle
195
// for application running with -Xbootclasspath VM flag. In this case,
196
// the using MyClass.class.getClassLoader() would return null,
197
// but the context class loader will function properly
198
// that classes will be properly loaded regardless of whether the lib is
199
// added to the system class path, the extension class path and
200
// regardless of the class loader architecture set up by some
202
ClassLoader cl = (ClassLoader) UIManager.get("ClassLoader");
204
cl = Thread.currentThread().getContextClassLoader();
206
Enumeration urls = cl.getResources(this.xmlName);
207
while (urls.hasMoreElements()) {
208
URL pluginUrl = (URL) urls.nextElement();
209
Object pluginInstance = this.getPlugin(pluginUrl);
210
if (pluginInstance != null)
211
this.plugins.add(pluginInstance);
214
} catch (Exception exc) {