001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.plugin;
019:
020: import java.io.InputStream;
021: import java.util.Enumeration;
022: import java.util.Hashtable;
023: import java.util.Vector;
024:
025: import org.columba.api.plugin.ExtensionMetadata;
026: import org.columba.api.plugin.IExtension;
027: import org.columba.api.plugin.IExtensionHandler;
028: import org.columba.api.plugin.PluginMetadata;
029:
030: /**
031: * Extension handler is a registry for extensions and resembles a hook to extend
032: * Columba's functionality.
033: *
034: * @author fdietz
035: *
036: */
037: public class ExtensionHandler implements IExtensionHandler {
038:
039: private static final java.util.logging.Logger LOG = java.util.logging.Logger
040: .getLogger("org.columba.core.plugin");
041:
042: private String id;
043: private String parent;
044:
045: protected Hashtable map = new Hashtable();
046:
047: /**
048: * @param id unique extension handler id
049: * @param parent unique id of this extension handlers parent, can be <code>null</code>
050: */
051: public ExtensionHandler(String id, String parent) {
052: if (id == null)
053: throw new IllegalArgumentException("id == null");
054:
055: this .id = id;
056: }
057:
058: /**
059: * @see org.columba.api.plugin.IExtensionHandler#addExtension(java.lang.String,
060: * org.columba.api.plugin.IExtension)
061: */
062: public void addExtension(String id, IExtension extension) {
063: if (id == null)
064: throw new IllegalArgumentException("id == null");
065: if (extension == null)
066: throw new IllegalArgumentException("extension == null");
067:
068: if (map.containsKey(id)) {
069: LOG.severe("duplicate id=" + id);
070: return;
071: }
072:
073: map.put(id, extension);
074:
075: }
076:
077: /**
078: * @see org.columba.api.plugin.IExtensionHandler#getExtension(java.lang.String)
079: */
080: public IExtension getExtension(String id) {
081: if (id == null)
082: throw new IllegalArgumentException("id == null");
083:
084: if (map.containsKey(id))
085: return (IExtension) map.get(id);
086:
087: return null;
088: }
089:
090: /**
091: * @see org.columba.api.plugin.IExtensionHandler#getId()
092: */
093: public String getId() {
094: return id;
095: }
096:
097: /**
098: * @see org.columba.api.plugin.IExtensionHandler#exists(java.lang.String)
099: */
100: public boolean exists(String id) {
101: return map.containsKey(id);
102: }
103:
104: /**
105: * @see org.columba.core.plugin.IExtensionHandler#loadExtensionsFromFile(java.lang.String)
106: */
107:
108: /**
109: * @param id
110: */
111: public void handlePluginError(String id) {
112:
113: // get plugin id
114: IExtension extension = getExtension(id);
115: ExtensionMetadata metadata = extension.getMetadata();
116:
117: LOG.severe("Failed to load extension= " + metadata.getId());
118: LOG.severe("Classname= " + metadata.getClassname());
119:
120: // JOptionPane.showMessageDialog(null, new MultiLineLabel(MessageFormat
121: // .format(GlobalResourceLoader.getString(RESOURCE_PATH,
122: // "pluginmanager", "errLoad.msg"), new String[] { id })),
123: // GlobalResourceLoader.getString(RESOURCE_PATH, "pluginmanager",
124: // "errLoad.title"), JOptionPane.ERROR_MESSAGE);
125:
126: // disable plugin
127:
128: }
129:
130: /**
131: * @return Returns the map.
132: */
133: /**
134: * @return
135: */
136: public Hashtable getMap() {
137: return map;
138: }
139:
140: /**
141: * @see org.columba.api.plugin.IExtensionHandler#getPluginIdList()
142: */
143: public String[] getPluginIdList() {
144: Vector result = new Vector();
145: Enumeration _enum = map.elements();
146: while (_enum.hasMoreElements()) {
147: IExtension extension = (IExtension) _enum.nextElement();
148: String id = extension.getMetadata().getId();
149:
150: result.add(id);
151: }
152:
153: return (String[]) result.toArray(new String[0]);
154: }
155:
156: /**
157: * @see org.columba.api.plugin.IExtensionHandler#getExtensionEnumeration()
158: */
159: public Enumeration getExtensionEnumeration() {
160: return map.elements();
161: }
162:
163: /**
164: * @see org.columba.api.plugin.IExtensionHandler#getExternalExtensionsEnumeration()
165: */
166: public Enumeration getExternalExtensionsEnumeration() {
167: Enumeration e = getExtensionEnumeration();
168:
169: Vector v = new Vector();
170: while (e.hasMoreElements()) {
171: IExtension extension = (IExtension) e.nextElement();
172: if (extension.isInternal() == false)
173: v.add(extension);
174: }
175:
176: return v.elements();
177: }
178:
179: /**
180: * @see org.columba.api.plugin.IExtensionHandler#loadExtensionsFromStream(InputStream)
181: */
182: public void loadExtensionsFromStream(InputStream is) {
183: Enumeration e = new ExtensionXMLParser()
184: .loadExtensionsFromStream(is, null, true);
185: while (e.hasMoreElements()) {
186: IExtension extension = (IExtension) e.nextElement();
187: addExtension(extension.getMetadata().getId(), extension);
188: }
189: }
190:
191: public void loadExternalExtensionsFromStream(
192: PluginMetadata pluginMetadata, InputStream is) {
193: Enumeration e = new ExtensionXMLParser()
194: .loadExtensionsFromStream(is, pluginMetadata, false);
195: while (e.hasMoreElements()) {
196: IExtension extension = (IExtension) e.nextElement();
197: addExtension(extension.getMetadata().getId(), extension);
198: }
199: }
200: }
|