KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > plugin > ExtensionHandler


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.plugin;
19
20 import java.io.InputStream JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.columba.api.plugin.ExtensionMetadata;
26 import org.columba.api.plugin.IExtension;
27 import org.columba.api.plugin.IExtensionHandler;
28 import org.columba.api.plugin.PluginMetadata;
29
30 /**
31  * Extension handler is a registry for extensions and resembles a hook to extend
32  * Columba's functionality.
33  *
34  * @author fdietz
35  *
36  */

37 public class ExtensionHandler implements IExtensionHandler {
38
39     private static final java.util.logging.Logger JavaDoc LOG = java.util.logging.Logger
40             .getLogger("org.columba.core.plugin");
41
42     private String JavaDoc id;
43     private String JavaDoc parent;
44     
45     protected Hashtable JavaDoc map = new Hashtable JavaDoc();
46
47     /**
48      * @param id unique extension handler id
49      * @param parent unique id of this extension handlers parent, can be <code>null</code>
50      */

51     public ExtensionHandler(String JavaDoc id, String JavaDoc parent) {
52         if ( id == null ) throw new IllegalArgumentException JavaDoc("id == null");
53         
54         this.id = id;
55     }
56
57     /**
58      * @see org.columba.api.plugin.IExtensionHandler#addExtension(java.lang.String,
59      * org.columba.api.plugin.IExtension)
60      */

61     public void addExtension(String JavaDoc id, IExtension extension) {
62         if (id == null)
63             throw new IllegalArgumentException JavaDoc("id == null");
64         if (extension == null)
65             throw new IllegalArgumentException JavaDoc("extension == null");
66
67         if (map.containsKey(id)) {
68             LOG.severe("duplicate id=" + id);
69             return;
70         }
71
72         map.put(id, extension);
73
74     }
75
76     /**
77      * @see org.columba.api.plugin.IExtensionHandler#getExtension(java.lang.String)
78      */

79     public IExtension getExtension(String JavaDoc id) {
80         if (id == null)
81             throw new IllegalArgumentException JavaDoc("id == null");
82
83         if (map.containsKey(id))
84             return (IExtension) map.get(id);
85
86         return null;
87     }
88
89     /**
90      * @see org.columba.api.plugin.IExtensionHandler#getId()
91      */

92     public String JavaDoc getId() {
93         return id;
94     }
95
96     /**
97      * @see org.columba.api.plugin.IExtensionHandler#exists(java.lang.String)
98      */

99     public boolean exists(String JavaDoc id) {
100         return map.containsKey(id);
101     }
102
103     /**
104      * @see org.columba.core.plugin.IExtensionHandler#loadExtensionsFromFile(java.lang.String)
105      */

106
107     /**
108      * @param id
109      */

110     public void handlePluginError(String JavaDoc id) {
111
112         // get plugin id
113
IExtension extension = getExtension(id);
114         ExtensionMetadata metadata = extension.getMetadata();
115
116         LOG.severe("Failed to load extension= " + metadata.getId());
117         LOG.severe("Classname= " + metadata.getClassname());
118
119         // JOptionPane.showMessageDialog(null, new MultiLineLabel(MessageFormat
120
// .format(GlobalResourceLoader.getString(RESOURCE_PATH,
121
// "pluginmanager", "errLoad.msg"), new String[] { id })),
122
// GlobalResourceLoader.getString(RESOURCE_PATH, "pluginmanager",
123
// "errLoad.title"), JOptionPane.ERROR_MESSAGE);
124

125         // disable plugin
126

127     }
128
129     /**
130      * @return Returns the map.
131      */

132     /**
133      * @return
134      */

135     public Hashtable JavaDoc getMap() {
136         return map;
137     }
138
139     /**
140      * @see org.columba.api.plugin.IExtensionHandler#getPluginIdList()
141      */

142     public String JavaDoc[] getPluginIdList() {
143         Vector JavaDoc result = new Vector JavaDoc();
144         Enumeration JavaDoc _enum = map.elements();
145         while (_enum.hasMoreElements()) {
146             IExtension extension = (IExtension) _enum.nextElement();
147             String JavaDoc id = extension.getMetadata().getId();
148
149             result.add(id);
150         }
151
152         return (String JavaDoc[]) result.toArray(new String JavaDoc[0]);
153     }
154
155     /**
156      * @see org.columba.api.plugin.IExtensionHandler#getExtensionEnumeration()
157      */

158     public Enumeration JavaDoc getExtensionEnumeration() {
159         return map.elements();
160     }
161
162     /**
163      * @see org.columba.api.plugin.IExtensionHandler#getExternalExtensionsEnumeration()
164      */

165     public Enumeration JavaDoc getExternalExtensionsEnumeration() {
166         Enumeration JavaDoc e = getExtensionEnumeration();
167
168         Vector JavaDoc v = new Vector JavaDoc();
169         while (e.hasMoreElements()) {
170             IExtension extension = (IExtension) e.nextElement();
171             if (extension.isInternal() == false)
172                 v.add(extension);
173         }
174
175         return v.elements();
176     }
177
178     /**
179      * @see org.columba.api.plugin.IExtensionHandler#loadExtensionsFromStream(InputStream)
180      */

181     public void loadExtensionsFromStream(InputStream JavaDoc is) {
182         Enumeration JavaDoc e = new ExtensionXMLParser().loadExtensionsFromStream(is,
183                 null, true);
184         while (e.hasMoreElements()) {
185             IExtension extension = (IExtension) e.nextElement();
186             addExtension(extension.getMetadata().getId(), extension);
187         }
188     }
189
190     public void loadExternalExtensionsFromStream(PluginMetadata pluginMetadata,
191             InputStream JavaDoc is) {
192         Enumeration JavaDoc e = new ExtensionXMLParser().loadExtensionsFromStream(is,
193                 pluginMetadata, false);
194         while (e.hasMoreElements()) {
195             IExtension extension = (IExtension) e.nextElement();
196             addExtension(extension.getMetadata().getId(), extension);
197         }
198     }
199 }
Popular Tags