1 16 package com.jdon.bussinessproxy.target; 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 21 import javax.ejb.EJBHome ; 22 import javax.ejb.EJBLocalHome ; 23 import javax.ejb.EJBLocalObject ; 24 import javax.ejb.EJBObject ; 25 import javax.naming.InitialContext ; 26 27 import com.jdon.bussinessproxy.TargetMetaDef; 28 import com.jdon.bussinessproxy.meta.DistributedTargetMetaDef; 29 import com.jdon.bussinessproxy.meta.EJBTargetMetaDef; 30 import com.jdon.servicelocator.ServiceLocatorException; 31 import com.jdon.servicelocator.web.ServiceLocator; 32 import com.jdon.util.Debug; 33 34 38 public class EJBObjectFactory { 39 private final static String module = EJBObjectFactory.class.getName(); 40 41 private ServiceLocator sl; 42 43 44 47 public EJBObjectFactory(ServiceLocator sl) { 48 super(); 49 this.sl = sl; 50 } 51 52 public Object create(TargetMetaDef targetMetaDef) throws Exception { 53 Object obj = null; 54 Debug.logVerbose("[JdonFramework] enter createObject in EJBTargetService " + targetMetaDef.getClassName(), module); 55 try { 56 if (targetMetaDef instanceof EJBTargetMetaDef){ 57 return createEJB2(targetMetaDef); 58 }else if (targetMetaDef instanceof DistributedTargetMetaDef){ 59 DistributedTargetMetaDef dtargetMetaDef = (DistributedTargetMetaDef)targetMetaDef; 60 Debug.logVerbose("[JdonFramework] this is EJB3 JNDIName=" + dtargetMetaDef.getJndiName(), module); 61 InitialContext ctx = new InitialContext (); 62 obj = ctx.lookup(dtargetMetaDef.getJndiName()); 63 } 64 } catch (Exception ex) { 65 Debug.logError("[JdonFramework]create ejb error: " + ex, module); 66 throw new Exception (ex); 67 } 68 Debug.logVerbose("[JdonFramework] enter createObject in EJBTargetService " + obj.getClass().getName(), module); 69 return obj; 70 } 71 72 private Object createEJB2(TargetMetaDef targetMetaDef) throws Exception { 73 EJBTargetMetaDef eJBMetaDef = (EJBTargetMetaDef) targetMetaDef; 74 if (eJBMetaDef.isLocal()) { 75 return createEJBLocal(eJBMetaDef); 76 } else { 77 return createEJBRemote(eJBMetaDef); 78 } 79 } 80 81 private Object createEJBLocal(EJBTargetMetaDef eJBMetaDef) throws Exception { 82 Debug.logVerbose("[JdonFramework] this is EJB2 local " 83 + eJBMetaDef.getClassName(), module); 84 Object obj = null; 85 try { 86 Class [] params = null; 87 Object [] paramos = null; 88 EJBLocalHome home = (EJBLocalHome ) sl.getLocalHome(eJBMetaDef 89 .getJndiName()); 90 Method createMethod = home.getClass().getMethod("create", params); 91 obj = (EJBLocalObject ) createMethod.invoke(home, paramos); 92 } catch (SecurityException e) { 93 e.printStackTrace(); 94 throw new Exception (e); 95 } catch (IllegalArgumentException e) { 96 e.printStackTrace(); 97 throw new Exception (e); 98 } catch (ServiceLocatorException e) { 99 Debug.logError("[JdonFramework]locator error: " + e, module); 100 throw new Exception ("JNID error:" + e); 101 } catch (NoSuchMethodException e) { 102 e.printStackTrace(); 103 throw new Exception (e); 104 } catch (IllegalAccessException e) { 105 e.printStackTrace(); 106 throw new Exception (e); 107 } catch (InvocationTargetException e) { 108 e.printStackTrace(); 109 throw new Exception (e); 110 } 111 return obj; 112 } 113 114 private Object createEJBRemote(EJBTargetMetaDef eJBMetaDef) 115 throws Exception { 116 Debug.logVerbose("[JdonFramework] this is EJB2 remote " 117 + eJBMetaDef.getClassName(), module); 118 Object obj = null; 119 Class [] params = null; 120 Object [] paramos = null; 121 try { 122 EJBHome home = sl.getRemoteHome(eJBMetaDef.getJndiName(), 123 eJBMetaDef.getHomeClass()); 124 Method createMethod = home.getClass().getMethod("create", params); 125 obj = (EJBObject ) createMethod.invoke(home, paramos); 126 } catch (SecurityException e) { 127 e.printStackTrace(); 128 throw new Exception (e); 129 } catch (IllegalArgumentException e) { 130 e.printStackTrace(); 131 throw new Exception (e); 132 } catch (ServiceLocatorException e) { 133 Debug.logError("[JdonFramework]locator error: " + e, module); 134 throw new Exception ("JNID error:" + e); 135 } catch (NoSuchMethodException e) { 136 e.printStackTrace(); 137 throw new Exception (e); 138 } catch (IllegalAccessException e) { 139 e.printStackTrace(); 140 throw new Exception (e); 141 } catch (InvocationTargetException e) { 142 e.printStackTrace(); 143 throw new Exception (e); 144 } 145 return obj; 146 } 147 148 } 149 | Popular Tags |