1 package org.codehaus.xfire; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 import java.util.HashMap; 6 7 8 /*** 9 * <p> 10 * The XFireFactory class allows you to embed XFire within your 11 * apps easily. 12 * </p> 13 * <p> 14 * This class assumes one XFire instance per JVM. To create many 15 * XFire instances you must use your own configuration and instantiation 16 * mechanism. 17 * </p> 18 * <p> 19 * If you want to provide your own Factory you must: 20 * <ul> 21 * <li>Register your factory via <code>registerFactory</code></li> 22 * <li>Implment <code>public static XFireFactory createInstance()</code> 23 * </ul> 24 * </p> 25 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> 26 */ 27 public class XFireFactory 28 { 29 private static XFireFactory standalone; 30 private static Class defaultFacClass; 31 private static HashMap factories = new HashMap(); 32 private static HashMap factoryClasses = new HashMap(); 33 34 private XFire xfire; 35 36 protected XFireFactory() 37 throws Exception 38 { 39 xfire = new DefaultXFire(); 40 } 41 42 protected XFireFactory(XFire xfire) 43 throws Exception 44 { 45 xfire = new DefaultXFire(); 46 } 47 48 protected static XFireFactory createInstance() 49 throws Exception 50 { 51 return new XFireFactory(); 52 } 53 54 public static XFireFactory newInstance() 55 throws Exception 56 { 57 if (standalone == null) 58 { 59 synchronized (XFireFactory.class) 60 { 61 if ( defaultFacClass != null ) 62 { 63 standalone = loadFactory(defaultFacClass); 64 } 65 else 66 { 67 standalone = new XFireFactory(); 68 } 69 } 70 } 71 return standalone; 72 } 73 74 private static XFireFactory loadFactory(Class clazz) 75 { 76 try 77 { 78 Method m = clazz.getMethod("createInstance", new Class[0]); 79 80 return (XFireFactory) m.invoke(null, new Object[0]); 81 } 82 catch (SecurityException e) 83 { 84 throw new RuntimeException("Couldn't load " + clazz.getName(), e); 85 } 86 catch (NoSuchMethodException e) 87 { 88 throw new RuntimeException("Factory doesn't implement newInstance(): " + clazz.getName(), e); 89 } 90 catch (IllegalArgumentException e) 91 { 92 throw new RuntimeException("Factory doesn't implement newInstance(): " + clazz.getName(), e); 93 } 94 catch (IllegalAccessException e) 95 { 96 throw new RuntimeException("Couldn't load " + clazz.getName(), e); 97 } 98 catch (InvocationTargetException e) 99 { 100 throw new RuntimeException("Couldn't load factory " + clazz.getName(), e); 101 } 102 103 } 104 105 public static XFireFactory newInstance(String selector) 106 throws Exception 107 { 108 XFireFactory fac = (XFireFactory) factories.get(selector); 109 if ( fac == null ) 110 { 111 synchronized (XFireFactory.class) 112 { 113 Class clazz = (Class) factoryClasses.get(selector); 114 if ( clazz == null ) 115 return null; 116 117 fac = loadFactory(clazz); 118 } 119 } 120 return fac; 121 } 122 123 /*** 124 * Register an XFireFactory class. 125 * @param factoryClass 126 * @param def Whether or not this should be the default factory. 127 */ 128 public static void registerFactory( Class factoryClass, boolean def ) 129 { 130 if ( def ) 131 defaultFacClass = factoryClass; 132 133 factoryClasses.put(factoryClass.getName(), factoryClass); 134 } 135 136 public XFire getXFire() throws Exception 137 { 138 return xfire; 139 } 140 }