~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to laf-plugin/src/main/java/org/pushingpixels/lafplugin/PluginManager.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Laf-Plugin Kirill Grouchnikov and contributors. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without 
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 *  o Redistributions of source code must retain the above copyright notice, 
 
8
 *    this list of conditions and the following disclaimer. 
 
9
 *     
 
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. 
 
13
 *     
 
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. 
 
17
 *     
 
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. 
 
29
 */
 
30
package org.pushingpixels.lafplugin;
 
31
 
 
32
import java.io.InputStream;
 
33
import java.io.InputStreamReader;
 
34
import java.net.URL;
 
35
import java.util.*;
 
36
 
 
37
import javax.swing.UIManager;
 
38
 
 
39
/**
 
40
 * Plugin manager for look-and-feels.
 
41
 * 
 
42
 * @author Kirill Grouchnikov
 
43
 * @author Erik Vickroy
 
44
 * @author Robert Beeger
 
45
 * @author Frederic Lavigne
 
46
 * @author Pattrick Gotthardt
 
47
 */
 
48
public class PluginManager {
 
49
        private String mainTag;
 
50
 
 
51
        private String pluginTag;
 
52
 
 
53
        private String xmlName;
 
54
 
 
55
        private Set plugins;
 
56
 
 
57
        /**
 
58
         * Simple constructor.
 
59
         * 
 
60
         * @param xmlName
 
61
         *            The name of XML file that contains plugin configuration.
 
62
         * @param mainTag
 
63
         *            The main tag in the XML configuration file.
 
64
         * @param pluginTag
 
65
         *            The tag that corresponds to a single plugin kind. Specifies
 
66
         *            the plugin kind that will be located in
 
67
         *            {@link #getAvailablePlugins(boolean)}.
 
68
         */
 
69
        public PluginManager(String xmlName, String mainTag, String pluginTag) {
 
70
                this.xmlName = xmlName;
 
71
                this.mainTag = mainTag;
 
72
                this.pluginTag = pluginTag;
 
73
                this.plugins = null;
 
74
        }
 
75
 
 
76
        // protected String getPluginClass(URL pluginUrl) {
 
77
        // InputStream is = null;
 
78
        // try {
 
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()))
 
85
        // return null;
 
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()))
 
90
        // continue;
 
91
        // if (child.getChildNodes().getLength() != 1)
 
92
        // return null;
 
93
        // Node text = child.getFirstChild();
 
94
        // if (text.getNodeType() != Node.TEXT_NODE)
 
95
        // return null;
 
96
        // return text.getNodeValue();
 
97
        // }
 
98
        // return null;
 
99
        // } catch (Exception exc) {
 
100
        // return null;
 
101
        // } finally {
 
102
        // if (is != null) {
 
103
        // try {
 
104
        // is.close();
 
105
        // } catch (Exception e) {
 
106
        // }
 
107
        // }
 
108
        // }
 
109
        // }
 
110
        //
 
111
        protected String getPluginClass(URL pluginUrl) {
 
112
                InputStream is = null;
 
113
                InputStreamReader isr = null;
 
114
                try {
 
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()))
 
120
                                return null;
 
121
                        Enumeration children = xml.enumerateChildren();
 
122
                        while (children.hasMoreElements()) {
 
123
                                XMLElement child = (XMLElement) children.nextElement();
 
124
                                if (!this.pluginTag.equals(child.getName()))
 
125
                                        continue;
 
126
                                if (child.countChildren() != 0)
 
127
                                        return null;
 
128
                                return child.getContent();
 
129
                        }
 
130
                        return null;
 
131
                } catch (Exception exc) {
 
132
                        return null;
 
133
                } finally {
 
134
                        if (isr != null) {
 
135
                                try {
 
136
                                        isr.close();
 
137
                                } catch (Exception e) {
 
138
                                }
 
139
                        }
 
140
                        if (is != null) {
 
141
                                try {
 
142
                                        is.close();
 
143
                                } catch (Exception e) {
 
144
                                }
 
145
                        }
 
146
                }
 
147
        }
 
148
 
 
149
        protected Object getPlugin(URL pluginUrl) throws Exception {
 
150
                String pluginClassName = this.getPluginClass(pluginUrl);
 
151
                if (pluginClassName == null)
 
152
                        return 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)
 
158
                        return null;
 
159
                Object pluginInstance = pluginClass.newInstance();
 
160
                if (pluginInstance == null)
 
161
                        return null;
 
162
                return pluginInstance;
 
163
        }
 
164
 
 
165
        /**
 
166
         * Returns a collection of all available plugins.
 
167
         * 
 
168
         * @return Collection of all available plugins. The classpath is scanned
 
169
         *         only once.
 
170
         * @see #getAvailablePlugins(boolean)
 
171
         */
 
172
        public Set getAvailablePlugins() {
 
173
                return this.getAvailablePlugins(false);
 
174
        }
 
175
 
 
176
        /**
 
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).
 
180
         * 
 
181
         * @param toReload
 
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.
 
187
         */
 
188
        public Set getAvailablePlugins(boolean toReload) {
 
189
                if (!toReload && (this.plugins != null))
 
190
                        return this.plugins;
 
191
 
 
192
                this.plugins = new HashSet();
 
193
 
 
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
 
201
                // frameworks.
 
202
                ClassLoader cl = (ClassLoader) UIManager.get("ClassLoader");
 
203
                if (cl == null)
 
204
                        cl = Thread.currentThread().getContextClassLoader();
 
205
                try {
 
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);
 
212
 
 
213
                        }
 
214
                } catch (Exception exc) {
 
215
                        return null;
 
216
                }
 
217
 
 
218
                return plugins;
 
219
        }
 
220
}