1 21 22 package org.opensubsystems.core.util; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.Properties ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 import org.opensubsystems.core.error.OSSDynamicClassException; 32 import org.opensubsystems.core.error.OSSException; 33 34 44 public class ClassFactory 45 { 46 48 51 private static final String IMPL_LOCK = "IMPL_LOCK"; 52 53 55 58 private static Logger s_logger = Log.getInstance(ClassFactory.class); 59 60 63 private static ClassFactory s_defaultInstance; 64 65 67 70 public ClassFactory( 71 ) 72 { 73 } 74 75 82 public static ClassFactory getInstance( 83 ) throws OSSException 84 { 85 if (s_defaultInstance == null) 86 { 87 synchronized (IMPL_LOCK) 90 { 91 setInstance(new ClassFactory()); 92 } 93 } 94 95 return s_defaultInstance; 96 } 97 98 105 public static void setInstance( 106 ClassFactory defaultInstance 107 ) 108 { 109 if (GlobalConstants.ERROR_CHECKING) 110 { 111 assert defaultInstance != null : "Default instance cannot be null"; 112 } 113 114 synchronized (IMPL_LOCK) 115 { 116 s_defaultInstance = defaultInstance; 117 } 118 } 119 120 122 135 public Object createInstance( 136 Class clsClassIdentifier 137 ) throws OSSException 138 { 139 return createInstance(clsClassIdentifier.getName()); 140 } 141 142 157 public Object createInstance( 158 Class clsClassIdentifier, 159 Class clsDefault 160 ) throws OSSException 161 { 162 return createInstance(clsClassIdentifier.getName(), clsDefault); 163 } 164 165 180 public Object createInstance( 181 String strClassIdentifier, 182 Class clsDefault 183 ) throws OSSException 184 { 185 Object objInstance; 186 187 try 188 { 189 objInstance = createInstance(strClassIdentifier); 190 } 191 catch (OSSDynamicClassException dceExc) 192 { 193 objInstance = ClassUtils.createNewInstance(clsDefault); 194 verifyInstance(objInstance); 195 } 196 197 return objInstance; 198 } 199 200 213 public Object createInstance( 214 String strClassIdentifier 215 ) throws OSSException 216 { 217 String strModifier; 218 Object objInstance = null; 219 List lstClassNames = new ArrayList (); 220 221 strModifier = getModifier(); 222 createConfiguredClassNames(strClassIdentifier, strModifier, lstClassNames); 223 if (!lstClassNames.isEmpty()) 224 { 225 objInstance = instantiateClass(lstClassNames, strClassIdentifier); 229 if (objInstance != null) 230 { 231 s_logger.finest("Class instantiation for " + strClassIdentifier + 232 " based on configured setting instantiated " + 233 objInstance.getClass().getName()); 234 } 235 } 236 else 237 { 238 createDefaultClassNames(strClassIdentifier, strModifier, lstClassNames); 242 if (!lstClassNames.isEmpty()) 243 { 244 objInstance = instantiateClass(lstClassNames, strClassIdentifier); 245 if (objInstance != null) 246 { 247 s_logger.finest("Class instantiation for " + strClassIdentifier + 248 " based on default setting instantiated " + 249 objInstance.getClass().getName()); 250 } 251 } 252 else 253 { 254 throw new OSSDynamicClassException("No class names available for " 255 + strClassIdentifier); 256 } 257 } 258 259 if (objInstance == null) 260 { 261 throw new OSSDynamicClassException("Failed to create instance for " 262 + strClassIdentifier); 263 } 264 265 return objInstance; 266 } 267 268 270 280 protected Object instantiateClass( 281 List lstClassNames, 282 String strClassIdentifier 283 ) throws OSSException 284 { 285 Object objInstance = null; 286 String strClassName; 287 Iterator classNames; 288 289 for (classNames = lstClassNames.iterator(); 290 (classNames.hasNext()) && (objInstance == null);) 291 { 292 strClassName = (String )classNames.next(); 293 try 294 { 295 objInstance = ClassUtils.createNewInstance(strClassName); 296 Log.s_logger.finer("Speculative class instantiation for " 298 + strClassIdentifier + " succeeded for class " 299 + strClassName); 300 } 301 catch (OSSDynamicClassException dceExc) 302 { 303 s_logger.log(Level.FINEST, 306 "Speculative class instantiation for " 307 + strClassIdentifier + " failed for class " 308 + strClassName 309 + " with error " + dceExc.getCause()); 311 } 313 } 314 verifyInstance(objInstance); 315 316 return objInstance; 317 } 318 319 326 protected void verifyInstance( 327 Object objInstance 328 ) throws OSSException 329 { 330 } 332 333 345 protected void createDefaultClassNames( 346 String strClassIdentifier, 347 String strModifier, 348 List lstClassNames 349 ) throws OSSException 350 { 351 lstClassNames.add(strClassIdentifier); 353 } 354 355 369 protected void createConfiguredClassNames( 370 String strClassIdentifier, 371 String strModifier, 372 List lstClassNames 373 ) throws OSSException 374 { 375 Properties prpSettings; 382 String strClassName = null; 383 StringBuffer sbProperty = new StringBuffer (strClassIdentifier); 384 385 prpSettings = Config.getInstance().getPropertiesSafely(); 386 387 if (strModifier != null) 389 { 390 sbProperty.append("."); 391 sbProperty.append(strModifier.toLowerCase()); 392 strClassName = prpSettings.getProperty(sbProperty.toString(), null); 393 if (strClassName != null) 394 { 395 lstClassNames.add(strClassName); 396 } 397 } 398 strClassName = prpSettings.getProperty(strClassIdentifier, null); 399 if (strClassName != null) 400 { 401 lstClassNames.add(strClassName); 402 } 403 } 404 405 412 protected String getModifier( 413 ) throws OSSException 414 { 415 return null; 416 } 417 } 418 | Popular Tags |