| 1 package org.sapia.magnet.domain; 2 3 import java.io.InputStream ; 6 import java.io.IOException ; 7 import java.util.Enumeration ; 8 import java.util.HashMap ; 9 import java.util.Map ; 10 import java.util.Properties ; 11 12 import org.apache.log4j.Logger; 15 16 17 28 public class HandlerFactory { 29 30 34 35 private static final Logger _theLogger = Logger.getLogger(HandlerFactory.class); 36 37 38 private static final HandlerFactory _theInstance = new HandlerFactory(); 39 40 41 private static final String RESOURCE_LAUNCH_HANDLER_FACTORY = "resources/launchHandlerFactory.properties"; 42 43 44 private static final String RESOURCE_PROTOCOL_HANDLER_FACTORY = "resources/protocolHandlerFactory.properties"; 45 46 47 private static final String RESOURCE_SCRIPT_HANDLER_FACTORY = "resources/scriptHandlerFactory.properties"; 48 49 53 58 public static HandlerFactory getInstance() { 59 return _theInstance; 60 } 61 62 66 67 private Map _theLaunchHandlerClasses; 68 69 70 private Map _theProtocolHandlerClasses; 71 72 73 private Map _theScriptHandlerClasses; 74 75 79 82 protected HandlerFactory() { 83 _theLaunchHandlerClasses = new HashMap (); 84 _theProtocolHandlerClasses = new HashMap (); 85 _theScriptHandlerClasses = new HashMap (); 86 initialize(); 87 } 88 89 93 96 private void initialize() { 97 InputStream anInput = null; 98 Properties someProperties; 99 100 try { 102 anInput = getClass().getResourceAsStream(RESOURCE_LAUNCH_HANDLER_FACTORY); 103 someProperties = new Properties (); 104 someProperties.load(anInput); 105 for (Enumeration enumeration = someProperties.propertyNames(); enumeration.hasMoreElements(); ) { 106 String aName = (String ) enumeration.nextElement(); 107 String aValue = someProperties.getProperty(aName); 108 _theLaunchHandlerClasses.put(aName, aValue); 109 } 110 } catch (IOException 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 ioe) { 117 _theLogger.warn("Error closing the input stream of the laucnh handler factory configuration", ioe); 118 } 119 } 120 } 121 122 try { 124 anInput = getClass().getResourceAsStream(RESOURCE_PROTOCOL_HANDLER_FACTORY); 125 someProperties = new Properties (); 126 someProperties.load(anInput); 127 for (Enumeration enumeration = someProperties.propertyNames(); enumeration.hasMoreElements(); ) { 128 String aName = (String ) enumeration.nextElement(); 129 String aValue = someProperties.getProperty(aName); 130 _theProtocolHandlerClasses.put(aName, aValue); 131 } 132 } catch (IOException 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 ioe) { 139 _theLogger.warn("Error closing the input stream of the protocol handler factory configuration", ioe); 140 } 141 } 142 } 143 144 try { 146 anInput = getClass().getResourceAsStream(RESOURCE_SCRIPT_HANDLER_FACTORY); 147 someProperties = new Properties (); 148 someProperties.load(anInput); 149 for (Enumeration enumeration = someProperties.propertyNames(); enumeration.hasMoreElements(); ) { 150 String aName = (String ) enumeration.nextElement(); 151 String aValue = someProperties.getProperty(aName); 152 _theScriptHandlerClasses.put(aName, aValue); 153 } 154 } catch (IOException 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 ioe) { 161 _theLogger.warn("Error closing the input stream of the script handler factory configuration", ioe); 162 } 163 } 164 } 165 } 166 167 171 178 public void addLaunchHandler(String aType, String aClassName) { 179 _theLaunchHandlerClasses.put(aType, aClassName); 180 } 181 182 189 public void addProtocolHandler(String aProtocol, String aClassName) { 190 _theProtocolHandlerClasses.put(aProtocol, aClassName); 191 } 192 193 200 public void addScriptHandler(String aType, String aClassName) { 201 _theScriptHandlerClasses.put(aType, aClassName); 202 } 203 204 211 public ProtocolHandlerIF createProtocolHandler(String aProtocol) throws ObjectCreationException { 212 String aClassName = (String ) _theProtocolHandlerClasses.get(aProtocol); 213 if (aClassName != null) { 214 try { 215 Class aClass = Class.forName(aClassName); 216 Object aHandler = aClass.newInstance(); 217 return (ProtocolHandlerIF) aHandler; 218 } catch (ClassNotFoundException cnfe) { 219 String 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 iae) { 223 String 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 ie) { 227 String 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 cce) { 231 String 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 247 public LaunchHandlerIF createLaunchHandler(String aLauncherType) throws ObjectCreationException { 248 String aClassName = (String ) _theLaunchHandlerClasses.get(aLauncherType); 249 if (aClassName != null) { 250 try { 251 Class aClass = Class.forName(aClassName); 252 Object aHandler = aClass.newInstance(); 253 return (LaunchHandlerIF) aHandler; 254 } catch (ClassNotFoundException cnfe) { 255 String 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 iae) { 259 String 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 ie) { 263 String 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 cce) { 267 String 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 283 public ScriptHandlerIF createScriptHandler(String aScriptType) throws ObjectCreationException { 284 String aClassName = (String ) _theScriptHandlerClasses.get(aScriptType); 285 if (aClassName != null) { 286 try { 287 Class aClass = Class.forName(aClassName); 288 Object aHandler = aClass.newInstance(); 289 return (ScriptHandlerIF) aHandler; 290 } catch (ClassNotFoundException cnfe) { 291 String 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 iae) { 295 String 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 ie) { 299 String 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 cce) { 303 String 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 |