1 package org.apache.turbine.services.factory; 2 3 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.commons.configuration.Configuration; 28 29 import org.apache.turbine.services.InitializationException; 30 import org.apache.turbine.services.TurbineBaseService; 31 import org.apache.turbine.util.TurbineException; 32 import org.apache.turbine.util.pool.ObjectInputStreamForContext; 33 34 42 public class TurbineFactoryService 43 extends TurbineBaseService 44 implements FactoryService 45 { 46 49 public static final String CLASS_LOADERS = "class.loaders"; 50 51 54 public static final String OBJECT_FACTORY = "factory."; 55 56 59 private static HashMap primitiveClasses; 60 61 { 62 primitiveClasses = new HashMap (8); 63 primitiveClasses.put(Boolean.TYPE.toString(), Boolean.TYPE); 64 primitiveClasses.put(Character.TYPE.toString(), Character.TYPE); 65 primitiveClasses.put(Byte.TYPE.toString(), Byte.TYPE); 66 primitiveClasses.put(Short.TYPE.toString(), Short.TYPE); 67 primitiveClasses.put(Integer.TYPE.toString(), Integer.TYPE); 68 primitiveClasses.put(Long.TYPE.toString(), Long.TYPE); 69 primitiveClasses.put(Float.TYPE.toString(), Float.TYPE); 70 primitiveClasses.put(Double.TYPE.toString(), Double.TYPE); 71 } 72 73 76 private ArrayList classLoaders = new ArrayList (); 77 78 81 private HashMap objectFactories = new HashMap (); 82 83 89 protected static Class getPrimitiveClass(String type) 90 { 91 return (Class ) primitiveClasses.get(type); 92 } 93 94 97 public TurbineFactoryService() 98 { 99 } 100 101 107 public void init() throws InitializationException 108 { 109 Configuration conf = getConfiguration(); 110 if (conf != null) 111 { 112 List loaders = conf.getList(CLASS_LOADERS); 113 if (loaders != null) 114 { 115 for (int i = 0; i < loaders.size(); i++) 116 { 117 try 118 { 119 classLoaders.add( 120 loadClass((String ) loaders.get(i)).newInstance()); 121 } 122 catch (Exception x) 123 { 124 throw new InitializationException( 125 "No such class loader '" + 126 (String ) loaders.get(i) + 127 "' for TurbineFactoryService", x); 128 } 129 } 130 } 131 132 String key,factory; 133 for (Iterator i = conf.getKeys(OBJECT_FACTORY); i.hasNext();) 134 { 135 key = (String ) i.next(); 136 factory = conf.getString(key); 137 138 142 objectFactories.put( 143 key.substring(OBJECT_FACTORY.length()), factory); 144 } 145 } 146 setInit(true); 147 } 148 149 156 public Object getInstance(String className) 157 throws TurbineException 158 { 159 if (className == null) 160 { 161 throw new TurbineException( 162 new NullPointerException ("String className")); 163 } 164 165 Factory factory = getFactory(className); 166 if (factory == null) 167 { 168 Class clazz; 169 try 170 { 171 clazz = loadClass(className); 172 } 173 catch (ClassNotFoundException x) 174 { 175 throw new TurbineException( 176 "Instantiation failed for class " + className, x); 177 } 178 return getInstance(clazz); 179 } 180 else 181 { 182 return factory.getInstance(); 183 } 184 } 185 186 197 public Object getInstance(String className, 198 ClassLoader loader) 199 throws TurbineException 200 { 201 if (className == null) 202 { 203 throw new TurbineException( 204 new NullPointerException ("String className")); 205 } 206 207 Factory factory = getFactory(className); 208 if (factory == null) 209 { 210 if (loader != null) 211 { 212 Class clazz; 213 try 214 { 215 clazz = loadClass(className, loader); 216 } 217 catch (ClassNotFoundException x) 218 { 219 throw new TurbineException( 220 "Instantiation failed for class " + className, x); 221 } 222 return getInstance(clazz); 223 } 224 else 225 { 226 return getInstance(className); 227 } 228 } 229 else 230 { 231 return factory.getInstance(loader); 232 } 233 } 234 235 246 public Object getInstance(String className, 247 Object [] params, 248 String [] signature) 249 throws TurbineException 250 { 251 if (className == null) 252 { 253 throw new TurbineException( 254 new NullPointerException ("String className")); 255 } 256 257 Factory factory = getFactory(className); 258 if (factory == null) 259 { 260 Class clazz; 261 try 262 { 263 clazz = loadClass(className); 264 } 265 catch (ClassNotFoundException x) 266 { 267 throw new TurbineException( 268 "Instantiation failed for class " + className, x); 269 } 270 return getInstance(clazz, params, signature); 271 } 272 else 273 { 274 return factory.getInstance(params, signature); 275 } 276 } 277 278 293 public Object getInstance(String className, 294 ClassLoader loader, 295 Object [] params, 296 String [] signature) 297 throws TurbineException 298 { 299 if (className == null) 300 { 301 throw new TurbineException( 302 new NullPointerException ("String className")); 303 } 304 305 Factory factory = getFactory(className); 306 if (factory == null) 307 { 308 if (loader != null) 309 { 310 Class clazz; 311 try 312 { 313 clazz = loadClass(className, loader); 314 } 315 catch (ClassNotFoundException x) 316 { 317 throw new TurbineException( 318 "Instantiation failed for class " + className, x); 319 } 320 return getInstance(clazz, params, signature); 321 } 322 else 323 { 324 return getInstance(className, params, signature); 325 } 326 } 327 else 328 { 329 return factory.getInstance(loader, params, signature); 330 } 331 } 332 333 340 public boolean isLoaderSupported(String className) 341 throws TurbineException 342 { 343 Factory factory = getFactory(className); 344 return factory != null ? 345 factory.isLoaderSupported() : true; 346 } 347 348 355 protected Object getInstance(Class clazz) 356 throws TurbineException 357 { 358 try 359 { 360 return clazz.newInstance(); 361 } 362 catch (Exception x) 363 { 364 throw new TurbineException( 365 "Instantiation failed for " + clazz.getName(), x); 366 } 367 } 368 369 380 protected Object getInstance(Class clazz, 381 Object params[], 382 String signature[]) 383 throws TurbineException 384 { 385 386 try 387 { 388 Class [] sign = getSignature(clazz, params, signature); 389 return clazz.getConstructor(sign).newInstance(params); 390 } 391 catch (Exception x) 392 { 393 throw new TurbineException( 394 "Instantiation failed for " + clazz.getName(), x); 395 } 396 } 397 398 409 public Class [] getSignature(Class clazz, 410 Object params[], 411 String signature[]) 412 throws ClassNotFoundException 413 { 414 if (signature != null) 415 { 416 417 ClassLoader tempLoader; 418 ClassLoader loader = clazz.getClassLoader(); 419 Class [] sign = new Class [signature.length]; 420 for (int i = 0; i < signature.length; i++) 421 { 422 423 sign[i] = getPrimitiveClass(signature[i]); 424 if (sign[i] == null) 425 { 426 427 if (loader != null) 428 { 429 430 sign[i] = loader.loadClass(signature[i]); 431 tempLoader = sign[i].getClassLoader(); 432 if ((params[i] != null) && 433 (tempLoader != null) && 434 !tempLoader.equals(params[i].getClass().getClassLoader())) 435 { 436 440 params[i] = switchObjectContext(params[i], loader); 441 } 442 } 443 else 444 { 445 446 sign[i] = loadClass(signature[i]); 447 } 448 } 449 } 450 return sign; 451 } 452 else 453 { 454 return null; 455 } 456 } 457 458 464 protected Object switchObjectContext(Object object, 465 ClassLoader loader) 466 { 467 ByteArrayOutputStream bout = 468 new ByteArrayOutputStream (); 469 try 470 { 471 ObjectOutputStream out = 472 new ObjectOutputStream (bout); 473 out.writeObject(object); 474 out.flush(); 475 } 476 catch (Exception x) 477 { 478 return object; 479 } 480 481 try 482 { 483 ByteArrayInputStream bin = 484 new ByteArrayInputStream (bout.toByteArray()); 485 ObjectInputStreamForContext in = 486 new ObjectInputStreamForContext(bin, loader); 487 488 return in.readObject(); 489 } 490 catch (Exception x) 491 { 492 return object; 493 } 494 } 495 496 503 protected Class loadClass(String className) 504 throws ClassNotFoundException 505 { 506 ClassLoader loader = this.getClass().getClassLoader(); 507 try 508 { 509 return loader != null ? 510 loader.loadClass(className) : Class.forName(className); 511 } 512 catch (ClassNotFoundException x) 513 { 514 515 for (Iterator i = classLoaders.iterator(); i.hasNext();) 516 { 517 try 518 { 519 return ((ClassLoader ) i.next()).loadClass(className); 520 } 521 catch (ClassNotFoundException xx) 522 { 523 } 524 } 525 526 527 throw x; 528 } 529 } 530 531 539 protected Class loadClass(String className, 540 ClassLoader loader) 541 throws ClassNotFoundException 542 { 543 return loader != null ? 544 loader.loadClass(className) : loadClass(className); 545 } 546 547 554 protected Factory getFactory(String className) 555 throws TurbineException 556 { 557 HashMap factories = objectFactories; 558 Object factory = factories.get(className); 559 if (factory != null) 560 { 561 if (factory instanceof String ) 562 { 563 564 try 565 { 566 factory = (Factory) getInstance((String ) factory); 567 ((Factory) factory).init(className); 568 } 569 catch (TurbineException x) 570 { 571 throw x; 572 } 573 catch (ClassCastException x) 574 { 575 throw new TurbineException( 576 "Incorrect factory " + (String ) factory + 577 " for class " + className, x); 578 } 579 factories = (HashMap ) factories.clone(); 580 factories.put(className, factory); 581 objectFactories = factories; 582 } 583 return (Factory) factory; 584 } 585 else 586 { 587 return null; 588 } 589 } 590 } 591 | Popular Tags |