1 19 20 package za.org.coefficient.util.common; 21 22 import org.apache.commons.beanutils.MethodUtils; 23 24 import za.org.coefficient.exception.ConfigurationException; 25 import za.org.coefficient.interfaces.Invoker; 26 import za.org.coefficient.core.Constants; 27 28 32 public class InvokerFactory { 33 34 public static boolean RUN_AS_EJB = true; 35 36 private static String INVOKER_LOCAL_EJB_IMPLEMENTATION = 37 "za.org.coefficient.invokers.ejb.EjbInvoker"; 38 private static String INVOKER_LOCAL_WEBAPP_IMPLEMENTATION = 39 "za.org.coefficient.invokers.webapp.WebAppInvoker"; 40 private static String INVOKER_REMOTE_EJB_IMPLEMENTATION = 41 "za.org.coefficient.invokers.ejb.interfaces.EjbInvokerFacadeHome"; 42 private static String INVOKER_REMOTE_EJB_JNDI_NAME = 43 "za/org/coefficient/util/EjbInvokerFacade"; 44 private static final String CREATE = "create"; 45 46 private static Invoker local_mi = null; 47 private static Object remote_mi = null; 48 private static Object lock = new Object (); 49 50 static { 51 init(); 52 } 53 54 public static Invoker getInvoker() throws Exception { 55 Class cls = null; 56 try { 57 if(RUN_AS_EJB) { 58 cls = Class.forName(INVOKER_LOCAL_EJB_IMPLEMENTATION); 59 } else { 60 cls = Class.forName(INVOKER_LOCAL_WEBAPP_IMPLEMENTATION); 61 } 62 } catch(Exception e) { 63 throw new Exception ("The implementation provided to create a Module Invoker is not on the classpath"); 64 } 65 66 synchronized(lock) { 68 69 if(local_mi == null || !(local_mi.getClass().equals(cls))) { 72 try { 73 Object obj = cls.newInstance(); 74 if(obj instanceof Invoker) { 75 local_mi = (Invoker)obj; 76 return (Invoker)obj; 77 } else { 78 throw new Exception ("The implementation provided to create a Module Invoker, " + cls.getName() + " is not of type Invoker"); 79 } 80 } catch(Exception e) { 81 throw new Exception ("Unable to instantiate: "+ cls.getName()); 82 } 83 } else { 84 return local_mi; 85 } 86 } 87 } 88 89 98 public static Invoker getRemoteInvoker() throws Exception { 99 if(RUN_AS_EJB) { 100 if(remote_mi == null) { 101 javax.naming.InitialContext initialContext = 102 new javax.naming.InitialContext (); 103 try { 104 java.lang.Object objRef = 105 initialContext.lookup(INVOKER_REMOTE_EJB_JNDI_NAME); 106 remote_mi = javax.rmi.PortableRemoteObject.narrow(objRef, Class.forName(INVOKER_REMOTE_EJB_IMPLEMENTATION)); 107 } finally { 108 initialContext.close(); 109 } 110 } 111 Object modInvoker = MethodUtils.invokeMethod(remote_mi, CREATE, null); 112 if(modInvoker != null && modInvoker instanceof Invoker) { 113 return (Invoker)modInvoker; 114 } else { 115 throw new ConfigurationException("Unable to create a remote module invoker"); 116 } 117 } else { 118 return getInvoker(); 119 } 120 } 121 122 private static void init() { 123 try { 124 Class.forName(INVOKER_LOCAL_EJB_IMPLEMENTATION); 125 } catch (ClassNotFoundException cnfe) { 126 RUN_AS_EJB = false; 127 } 128 } 129 130 } 131 | Popular Tags |