KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > extensions > ExtensionTypeManager


1 package com.sslexplorer.extensions;
2
3 import java.util.HashMap JavaDoc;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 import com.sslexplorer.extensions.types.AgentType;
9 import com.sslexplorer.extensions.types.DefaultAgentType;
10 import com.sslexplorer.extensions.types.LanguagePackType;
11 import com.sslexplorer.extensions.types.PluginType;
12
13
14 /**
15  * Managers a registry of <i>Extension Types</i> name and the class
16  * names that must be instantiated when these extension types are
17  * encountered in an extension descriptor.
18  *
19  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
20  */

21 public class ExtensionTypeManager {
22
23     private static ExtensionTypeManager instance;
24     private static Log log = LogFactory.getLog(ExtensionTypeManager.class);
25     
26     private HashMap JavaDoc<String JavaDoc, Class JavaDoc> extensionTypes = new HashMap JavaDoc<String JavaDoc, Class JavaDoc>();
27     
28     /**
29      * Constructor.
30      */

31     public ExtensionTypeManager() {
32         extensionTypes.put(PluginType.TYPE, PluginType.class);
33         extensionTypes.put(AgentType.TYPE, AgentType.class);
34         extensionTypes.put(DefaultAgentType.TYPE, DefaultAgentType.class);
35         extensionTypes.put(LanguagePackType.TYPE, LanguagePackType.class);
36     }
37     
38     /**
39      * Get an instance of the extension type manager, lazily creating it.
40      *
41      * @return extension type manager
42      */

43     public static ExtensionTypeManager getInstance() {
44         return instance==null ? instance = new ExtensionTypeManager() : instance;
45     }
46     
47     /**
48      * Register a new extension type.
49      *
50      * @param type extension type name
51      * @param cls extension type class
52      */

53     public void registerExtensionType(String JavaDoc type, Class JavaDoc cls) {
54         extensionTypes.put(type, cls);
55     }
56
57     /**
58      * Unregister a registered extension type.
59      *
60      * @param type extension type name
61      */

62     public void unregisterExtensionType(String JavaDoc type) {
63         extensionTypes.remove(type);
64     }
65     
66     /**
67      * Create a new extension type instance given the type name. The
68      * bundle is required for the exception that may get thrown.
69
70      * @param type type name
71      * @param bundle bundle containing the type
72      * @return extension type instance
73      * @throws ExtensionException if the extension type cannot be located ({@link ExtensionException#UNKNOWN_EXTENSION_TYPE}.
74      */

75     public ExtensionType getExtensionType(String JavaDoc type, ExtensionBundle bundle) throws ExtensionException {
76         try {
77             if(extensionTypes.containsKey(type)) {
78                 return (ExtensionType) extensionTypes.get(type).newInstance();
79             }
80         } catch (Throwable JavaDoc t) {
81             log.error("Failed to create extension type " + type, t);
82         }
83         throw new ExtensionException(ExtensionException.UNKNOWN_EXTENSION_TYPE, type, bundle.getId());
84         
85     }
86 }
87
Popular Tags