KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > HandlerFactory


1 package org.sapia.magnet.domain;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.io.InputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 // Import of Apache's log4j
13
// ------------------------
14
import org.apache.log4j.Logger;
15
16
17 /**
18  *
19  *
20  * @author Jean-Cedric Desrochers
21  *
22  * <dl>
23  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
24  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
25  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
26  * </dl>
27  */

28 public class HandlerFactory {
29
30   /////////////////////////////////////////////////////////////////////////////////////////
31
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
32
/////////////////////////////////////////////////////////////////////////////////////////
33

34   /** Defines the logger instance for this class. */
35   private static final Logger _theLogger = Logger.getLogger(HandlerFactory.class);
36
37   /** Defines the singleton instance of this class. */
38   private static final HandlerFactory _theInstance = new HandlerFactory();
39
40   /** Defines the resource name that contains the launch handler factory configuration. */
41   private static final String JavaDoc RESOURCE_LAUNCH_HANDLER_FACTORY = "resources/launchHandlerFactory.properties";
42
43   /** Defines the resource name that contains the protocol handler factory configuration. */
44   private static final String JavaDoc RESOURCE_PROTOCOL_HANDLER_FACTORY = "resources/protocolHandlerFactory.properties";
45
46   /** Defines the resource name that contains the script handler factory configuration. */
47   private static final String JavaDoc RESOURCE_SCRIPT_HANDLER_FACTORY = "resources/scriptHandlerFactory.properties";
48
49   /////////////////////////////////////////////////////////////////////////////////////////
50
/////////////////////////////////// STATIC METHODS ////////////////////////////////////
51
/////////////////////////////////////////////////////////////////////////////////////////
52

53   /**
54    * Returns the singleton handler factory instance.
55    *
56    * @return The singleton handler factory instance.
57    */

58   public static HandlerFactory getInstance() {
59     return _theInstance;
60   }
61
62   /////////////////////////////////////////////////////////////////////////////////////////
63
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
64
/////////////////////////////////////////////////////////////////////////////////////////
65

66   /** Defines the map that contains the classes for launch handlers. */
67   private Map JavaDoc _theLaunchHandlerClasses;
68
69   /** Defines the map that contains the classes for protocol handlers. */
70   private Map JavaDoc _theProtocolHandlerClasses;
71
72   /** Defines the map that contains the classes for script handlers. */
73   private Map JavaDoc _theScriptHandlerClasses;
74
75   /////////////////////////////////////////////////////////////////////////////////////////
76
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
77
/////////////////////////////////////////////////////////////////////////////////////////
78

79   /**
80    * Creates a new HandlerFactory instance. Constructor is hidden on purpose.
81    */

82   protected HandlerFactory() {
83     _theLaunchHandlerClasses = new HashMap JavaDoc();
84     _theProtocolHandlerClasses = new HashMap JavaDoc();
85     _theScriptHandlerClasses = new HashMap JavaDoc();
86     initialize();
87   }
88
89   /////////////////////////////////////////////////////////////////////////////////////////
90
/////////////////////////////////// UTILITY METHODS ///////////////////////////////////
91
/////////////////////////////////////////////////////////////////////////////////////////
92

93   /**
94    * Initializes this handler factory instance.
95    */

96   private void initialize() {
97     InputStream JavaDoc anInput = null;
98     Properties JavaDoc someProperties;
99
100     // Loading the launch handler factory configuration properties
101
try {
102       anInput = getClass().getResourceAsStream(RESOURCE_LAUNCH_HANDLER_FACTORY);
103       someProperties = new Properties JavaDoc();
104       someProperties.load(anInput);
105       for (Enumeration JavaDoc enumeration = someProperties.propertyNames(); enumeration.hasMoreElements(); ) {
106         String JavaDoc aName = (String JavaDoc) enumeration.nextElement();
107         String JavaDoc aValue = someProperties.getProperty(aName);
108         _theLaunchHandlerClasses.put(aName, aValue);
109       }
110     } catch (IOException JavaDoc ioe) {
111       _theLogger.warn("Error getting the input stream of the laucnh handler factory configuration", ioe);
112     } finally {
113       if (anInput != null) {
114         try {
115           anInput.close();
116         } catch (IOException JavaDoc ioe) {
117           _theLogger.warn("Error closing the input stream of the laucnh handler factory configuration", ioe);
118         }
119       }
120     }
121
122     // Loading the protocol handler factory configuration properties
123
try {
124       anInput = getClass().getResourceAsStream(RESOURCE_PROTOCOL_HANDLER_FACTORY);
125       someProperties = new Properties JavaDoc();
126       someProperties.load(anInput);
127       for (Enumeration JavaDoc enumeration = someProperties.propertyNames(); enumeration.hasMoreElements(); ) {
128         String JavaDoc aName = (String JavaDoc) enumeration.nextElement();
129         String JavaDoc aValue = someProperties.getProperty(aName);
130         _theProtocolHandlerClasses.put(aName, aValue);
131       }
132     } catch (IOException JavaDoc ioe) {
133       _theLogger.warn("Error getting the input stream of the protocol handler factory configuration", ioe);
134     } finally {
135       if (anInput != null) {
136         try {
137           anInput.close();
138         } catch (IOException JavaDoc ioe) {
139           _theLogger.warn("Error closing the input stream of the protocol handler factory configuration", ioe);
140         }
141       }
142     }
143
144     // Loading the script handler factory configuration properties
145
try {
146       anInput = getClass().getResourceAsStream(RESOURCE_SCRIPT_HANDLER_FACTORY);
147       someProperties = new Properties JavaDoc();
148       someProperties.load(anInput);
149       for (Enumeration JavaDoc enumeration = someProperties.propertyNames(); enumeration.hasMoreElements(); ) {
150         String JavaDoc aName = (String JavaDoc) enumeration.nextElement();
151         String JavaDoc aValue = someProperties.getProperty(aName);
152         _theScriptHandlerClasses.put(aName, aValue);
153       }
154     } catch (IOException JavaDoc ioe) {
155       _theLogger.warn("Error getting the input stream of the script handler factory configuration", ioe);
156     } finally {
157       if (anInput != null) {
158         try {
159           anInput.close();
160         } catch (IOException JavaDoc ioe) {
161           _theLogger.warn("Error closing the input stream of the script handler factory configuration", ioe);
162         }
163       }
164     }
165   }
166
167   /////////////////////////////////////////////////////////////////////////////////////////
168
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
169
/////////////////////////////////////////////////////////////////////////////////////////
170

171   /**
172    * Adds the new association between the launcher type and the class name
173    * passed in.
174    *
175    * @param aType The laucnher type.
176    * @param aClassName The name of the class that handles the laucnher.
177    */

178   public void addLaunchHandler(String JavaDoc aType, String JavaDoc aClassName) {
179     _theLaunchHandlerClasses.put(aType, aClassName);
180   }
181
182   /**
183    * Adds the new association between the protocol name and the class name
184    * passed in.
185    *
186    * @param aProtocol A protocol name.
187    * @param aClassName The name of the class that handles the protocol.
188    */

189   public void addProtocolHandler(String JavaDoc aProtocol, String JavaDoc aClassName) {
190     _theProtocolHandlerClasses.put(aProtocol, aClassName);
191   }
192
193   /**
194    * Adds the new association between the script type and the class name
195    * passed in.
196    *
197    * @param aType The script type.
198    * @param aClassName The name of the class that handles the script.
199    */

200   public void addScriptHandler(String JavaDoc aType, String JavaDoc aClassName) {
201     _theScriptHandlerClasses.put(aType, aClassName);
202   }
203
204   /**
205    * Creates protocol handler for the protocol name passed in.
206    *
207    * @param aProtocol The protocol name.
208    * @return The created protocol handler.
209    * @exception ObjectCreationException If an error occurs while creating the protocol handler.
210    */

211   public ProtocolHandlerIF createProtocolHandler(String JavaDoc aProtocol) throws ObjectCreationException {
212     String JavaDoc aClassName = (String JavaDoc) _theProtocolHandlerClasses.get(aProtocol);
213     if (aClassName != null) {
214       try {
215         Class JavaDoc aClass = Class.forName(aClassName);
216         Object JavaDoc aHandler = aClass.newInstance();
217         return (ProtocolHandlerIF) aHandler;
218       } catch (ClassNotFoundException JavaDoc cnfe) {
219         String JavaDoc aMessage = "Unable to create a handler for the protocol " + aProtocol +
220                           " - the associated class is not found " + aClassName;
221         throw new ObjectCreationException(aMessage, cnfe);
222       } catch (IllegalAccessException JavaDoc iae) {
223         String JavaDoc aMessage = "Unable to create a handler for the protocol " + aProtocol +
224                           " - could not access the associated class " + aClassName;
225         throw new ObjectCreationException(aMessage, iae);
226       } catch (InstantiationException JavaDoc ie) {
227         String JavaDoc aMessage = "Unable to create a handler for the protocol " + aProtocol +
228                           " - could not call the default constructor of " + aClassName;
229         throw new ObjectCreationException(aMessage, ie);
230       } catch (ClassCastException JavaDoc cce) {
231         String JavaDoc aMessage = "Unable to create a handler for the protocol " + aProtocol +
232                           " - the associated class " + aClassName + " is not a ProtocolHandlerIF";
233         throw new ObjectCreationException(aMessage, cce);
234       }
235     } else {
236       throw new ObjectCreationException("The protocol of the path to render is invalid: " + aProtocol);
237     }
238   }
239
240   /**
241    * Creates a new launcher handler for the type passed in.
242    *
243    * @param aLauncherType The type for which to create a handler.
244    * @return The launcher handler created.
245    * @exception ObjectCreationException If an error occurs while creating the launch handler.
246    */

247   public LaunchHandlerIF createLaunchHandler(String JavaDoc aLauncherType) throws ObjectCreationException {
248     String JavaDoc aClassName = (String JavaDoc) _theLaunchHandlerClasses.get(aLauncherType);
249     if (aClassName != null) {
250       try {
251         Class JavaDoc aClass = Class.forName(aClassName);
252         Object JavaDoc aHandler = aClass.newInstance();
253         return (LaunchHandlerIF) aHandler;
254       } catch (ClassNotFoundException JavaDoc cnfe) {
255         String JavaDoc aMessage = "Unable to create a handler for the launcher type " + aLauncherType +
256                           " - the associated class is not found " + aClassName;
257         throw new ObjectCreationException(aMessage, cnfe);
258       } catch (IllegalAccessException JavaDoc iae) {
259         String JavaDoc aMessage = "Unable to create a handler for the launcher type " + aLauncherType +
260                           " - could not access the associated class " + aClassName;
261         throw new ObjectCreationException(aMessage, iae);
262       } catch (InstantiationException JavaDoc ie) {
263         String JavaDoc aMessage = "Unable to create a handler for the launcher type " + aLauncherType +
264                           " - could not call the default constructor of " + aClassName;
265         throw new ObjectCreationException(aMessage, ie);
266       } catch (ClassCastException JavaDoc cce) {
267         String JavaDoc aMessage = "Unable to create a handler for the launcher type " + aLauncherType +
268                           " - the associated class " + aClassName + " is not a LaunchHandlerIF";
269         throw new ObjectCreationException(aMessage, cce);
270       }
271     } else {
272       throw new ObjectCreationException("The launcher type is invalid: " + aLauncherType);
273     }
274   }
275
276   /**
277    * Creates a new script handler for the script type passed in.
278    *
279    * @param aScriptType The type of script for which to create a handler.
280    * @return The created script handler.
281    * @exception ObjectCreationException If an error occurs while creating the script handler.
282    */

283   public ScriptHandlerIF createScriptHandler(String JavaDoc aScriptType) throws ObjectCreationException {
284     String JavaDoc aClassName = (String JavaDoc) _theScriptHandlerClasses.get(aScriptType);
285     if (aClassName != null) {
286       try {
287         Class JavaDoc aClass = Class.forName(aClassName);
288         Object JavaDoc aHandler = aClass.newInstance();
289         return (ScriptHandlerIF) aHandler;
290       } catch (ClassNotFoundException JavaDoc cnfe) {
291         String JavaDoc aMessage = "Unable to create a handler for the script type " + aScriptType +
292                           " - the associated class is not found " + aClassName;
293         throw new ObjectCreationException(aMessage, cnfe);
294       } catch (IllegalAccessException JavaDoc iae) {
295         String JavaDoc aMessage = "Unable to create a handler for the script type " + aScriptType +
296                           " - could not access the associated class " + aClassName;
297         throw new ObjectCreationException(aMessage, iae);
298       } catch (InstantiationException JavaDoc ie) {
299         String JavaDoc aMessage = "Unable to create a handler for the script type " + aScriptType +
300                           " - could not call the default constructor of " + aClassName;
301         throw new ObjectCreationException(aMessage, ie);
302       } catch (ClassCastException JavaDoc cce) {
303         String JavaDoc aMessage = "Unable to create a handler for the script type " + aScriptType +
304                           " - the associated class " + aClassName + " is not a LaunchHandlerIF";
305         throw new ObjectCreationException(aMessage, cce);
306       }
307     } else {
308       throw new ObjectCreationException("The script type is invalid: " + aScriptType);
309     }
310   }
311 }
312
Popular Tags