View Javadoc

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