1 6 7 package org.jfox.ejb; 8 9 import java.lang.reflect.Method ; 10 import java.lang.reflect.Proxy ; 11 import java.rmi.RemoteException ; 12 import java.util.HashMap ; 13 import java.util.List ; 14 import java.util.Map ; 15 import javax.ejb.EJBException ; 16 import javax.ejb.EJBHome ; 17 import javax.ejb.EJBMetaData ; 18 import javax.ejb.EnterpriseBean ; 19 import javax.ejb.HomeHandle ; 20 import javax.ejb.TimedObject ; 21 import javax.ejb.TimerService ; 22 import javax.naming.LinkRef ; 23 import javax.naming.NamingException ; 24 25 import org.jfox.ejb.connector.EJBConnectorInvoker; 26 import org.jfox.ejb.connector.EJBInvocation; 27 import org.jfox.ejb.handler.HandlerChain; 28 import org.jfox.ejb.meta.EJBDescriptor; 29 import org.jfox.ejb.meta.EJBReferenceDescriptor; 30 import org.jfox.ejb.meta.EnvEntryDescriptor; 31 import org.jfox.ejb.meta.ResourceEnvRefDescriptor; 32 import org.jfox.ejb.meta.ResourceRefDescriptor; 33 import org.jfox.ejb.timer.EJBTimerService; 34 import org.jfox.ejb.timer.TimedObjectMethod; 35 import org.jfox.ioc.common.AbstractComponent; 36 import org.jfox.ioc.util.Methods; 37 import org.jfox.jndi.InitialContextHelper; 38 import org.jfox.jndi.enc.EnterpriseContext; 39 40 45 46 public abstract class BucketSupport extends AbstractComponent implements Bucket { 47 protected BucketMeta bucketMeta = null; 49 51 protected String homeClassName = null; 53 57 protected Map beanMethods = new HashMap (); 58 protected Map homeMethods = new HashMap (); 59 60 protected static Map ejbObjectMethods = new HashMap (5); 61 static { 62 ejbObjectMethods.put(String.valueOf(Methods.getMethodHash(EJBObjectMethod.GetEJBHome)),EJBObjectMethod.GetEJBHome); 63 ejbObjectMethods.put(String.valueOf(Methods.getMethodHash(EJBObjectMethod.GetHandle)), EJBObjectMethod.GetHandle); 64 ejbObjectMethods.put(String.valueOf(Methods.getMethodHash(EJBObjectMethod.GetPrimaryKey)), EJBObjectMethod.GetPrimaryKey); 65 ejbObjectMethods.put(String.valueOf(Methods.getMethodHash(EJBObjectMethod.IsIdentical)), EJBObjectMethod.IsIdentical); 66 ejbObjectMethods.put(String.valueOf(Methods.getMethodHash(EJBObjectMethod.Remove)), EJBObjectMethod.Remove); 67 } 68 69 protected EJBObjectIDGenerator idgen = null; 70 71 protected HandlerChain handlerChain = null; 73 74 protected EnterpriseContext enc = new EnterpriseContext(); 76 77 protected TimerService timerService; 78 79 public BucketSupport() { 80 81 } 82 83 public void setBucketMeta(BucketMeta meta) { 84 this.bucketMeta = meta; 85 } 86 87 public void setHandlerChain(HandlerChain handlerChain) { 88 this.handlerChain = handlerChain; 89 } 90 91 public ClassLoader getEJBClassLoader() { 92 return bucketMeta.getBeanClass().getClassLoader(); 93 } 94 95 public EJBMetaData getEJBMetaData() throws RemoteException { 96 return bucketMeta.getEJBMetaData(); 97 } 98 99 public EJBHome getEJBHome() throws RemoteException { 100 return bucketMeta.getEJBMetaData().getEJBHome(); 101 } 102 103 public HomeHandle getHomeHandle() throws RemoteException { 104 return new HomeHandleImpl(bucketMeta.getEJBDescriptor().getJndiName()); 105 } 106 107 public EnterpriseContext getEnterpriseContext() { 108 return enc; 109 } 110 111 public void ejbCreate(EnterpriseBean bean, String createMethod, Object [] args) throws RemoteException { 113 char c = createMethod.charAt(0); 114 String ejbCreateMethodName = "ejb" + Character.toUpperCase(c) + createMethod.substring(1); 115 116 logger.debug(bean + " " + ejbCreateMethodName); 117 Class [] signatures = null; 118 if(args != null && args.length != 0) { 119 signatures = new Class [args.length]; 120 for(int i = 0; i < args.length; i++) { 121 signatures[i] = args[i].getClass(); 122 } 123 } 124 125 try { 126 bean.getClass().getMethod(ejbCreateMethodName, signatures).invoke(bean, args); 127 } 128 catch(Exception e) { 129 throw new RemoteException (e.getMessage(), e); 130 } 131 } 132 133 138 private EJBHome createEJBHome() { 139 EJBHome home = (EJBHome ) Proxy.newProxyInstance(bucketMeta.getEJBMetaData().getHomeInterfaceClass().getClassLoader(), 140 new Class []{bucketMeta.getEJBMetaData().getHomeInterfaceClass()}, 141 new EJBConnectorInvoker(this.getHomeObjectId(),null)); 142 logger.debug("createEJBHome " + homeClassName + " Successful"); 143 return home; 144 } 145 146 152 protected Map introspectorHomeMethods(Class classObj) { 153 Map homeMethods = new HashMap (); 154 Method [] methods = classObj.getMethods(); 155 for(int i = 0; i < methods.length; i++) { 156 long hashCode = Methods.getMethodHash(methods[i]); 157 homeMethods.put(String.valueOf(hashCode), methods[i]); 158 logger.debug("Cache Home method: " + hashCode + " => " + classObj.getName() + "." + methods[i].getName()); 159 } 160 return homeMethods; 161 } 162 163 170 protected Map introspectorBeanMethods(Class beanClass, Class remoteInterface) { 171 Map methods = new HashMap (); 172 Method [] remoteMethods = remoteInterface.getDeclaredMethods(); 173 for(int i = 0; i < remoteMethods.length; i++) { 174 Method _method = remoteMethods[i]; 175 try { 176 Method method = beanClass.getMethod(_method.getName(), _method.getParameterTypes()); 177 long hashCode = Methods.getMethodHash(method); 178 methods.put(String.valueOf(hashCode), method); 179 logger.debug("Cache Bean method: " + hashCode + " => " + beanClass.getName() + "." + method.getName()); 180 } 181 catch(Exception e) { 182 throw new EJBException (e); 183 } 184 } 185 186 if(TimedObject .class.isAssignableFrom(beanClass)) { 188 long hashCode = Methods.getMethodHash(TimedObjectMethod.TimeOut); 189 methods.put(String.valueOf(hashCode), TimedObjectMethod.TimeOut); 190 logger.debug("Cache business method: " + hashCode + " => " + TimedObjectMethod.TimeOut.getName()); 191 } 192 193 return methods; 194 } 195 196 197 public Method getHomeMethod(EJBInvocation invocation) throws NoSuchMethodException { 198 if(!homeMethods.containsKey(invocation.getMethodHash())) { 199 throw new NoSuchMethodException ("no such EJBHome method with method hash is " + invocation.getMethodHash()); 200 } 201 return (Method ) homeMethods.get(invocation.getMethodHash()); 202 } 203 204 public Method getBeanMethod(EJBInvocation invocation) throws NoSuchMethodException { 205 String hash = invocation.getMethodHash(); 206 if(beanMethods.containsKey(hash)) { 207 return (Method )beanMethods.get(hash); 208 } 209 if(ejbObjectMethods.containsKey(invocation.getMethodHash())){ 210 return (Method )ejbObjectMethods.get(hash); 211 } 212 throw new NoSuchMethodException ("no such EnterpriseBean method with method hash is " + invocation.getMethodHash()); 213 } 214 215 public EJBDescriptor getEJBDescriptor() { 216 return bucketMeta.getEJBDescriptor(); 217 } 218 219 public TimerService getTimerService() { 220 return timerService; 221 } 222 223 public EJBObjectId nextObjectId() { 224 return idgen.nextBeanObjectId(); 225 } 226 227 public EJBObjectId getHomeObjectId() { 228 return idgen.getHomeObjectId(); 229 } 230 231 public Class getBeanClass() { 232 return bucketMeta.getBeanClass(); 233 } 234 235 242 public Object invokeHome(EJBInvocation invocation) throws Exception { 243 logger.debug("invokeHome " + invocation.getMethod().getName()); 244 return handlerChain.invokeHome(this, invocation); 245 } 247 248 255 public Object invokeBean(EJBInvocation invocation) throws Exception { 256 logger.debug("invokeBean " + invocation.getMethod().getName()); 257 return handlerChain.invokeBean(this, invocation); 258 259 } 260 261 protected void doInit() throws Exception { 262 idgen = EJBObjectIDGenerator.newInstance(bucketMeta.getEJBDescriptor().getEjbName(), 264 bucketMeta.getEJBMetaData().getHomeInterfaceClass().getName(), 265 bucketMeta.getEJBMetaData().getRemoteInterfaceClass().getName()); 266 267 this.homeClassName = bucketMeta.getEJBMetaData().getHomeInterfaceClass().getName(); 268 homeMethods = introspectorHomeMethods(bucketMeta.getEJBMetaData().getHomeInterfaceClass()); 269 beanMethods = introspectorBeanMethods(bucketMeta.getBeanClass(), bucketMeta.getEJBMetaData().getRemoteInterfaceClass()); 270 272 timerService = new EJBTimerService(bucketMeta.getEJBDescriptor().getEjbName()); 273 274 ((EJBMetaDataImpl) bucketMeta.getEJBMetaData()).setEJBHome(createEJBHome()); 276 277 EJBDescriptor ejbDescriptor = bucketMeta.getEJBDescriptor(); 278 InitialContextHelper.getInitialContext().bind(ejbDescriptor.getJndiName(), bucketMeta.getEJBMetaData().getEJBHome()); 279 280 bindReference(); 282 283 } 284 285 protected void doDestroy() throws Exception { 286 InitialContextHelper.getInitialContext().unbind(bucketMeta.getEJBDescriptor().getJndiName()); 287 } 288 289 protected void bindReference() { 290 EJBDescriptor ejbDescriptor = bucketMeta.getEJBDescriptor(); 291 try { 292 List entries = ejbDescriptor.getEnvEntries(); 293 for(int i = 0; i < entries.size(); i++) { 294 EnvEntryDescriptor env = (EnvEntryDescriptor) entries.get(i); 295 String name = env.getName(); 296 Object value = env.getValueObject(); 297 enc.bind(name, value); 298 logger.debug("bind env-entry, name: " + name); 299 } 300 301 List ejbRefs = ejbDescriptor.getEjbReferences(); 302 for(int i = 0; i < ejbRefs.size(); i++) { 303 EJBReferenceDescriptor ejbRef = (EJBReferenceDescriptor) ejbRefs.get(i); 304 LinkRef linkRef = new LinkRef (ejbRef.getRefName()); 306 enc.bind(ejbRef.getRefName(), linkRef); 307 logger.debug("bind ejb-ref, name: " + ejbRef.getRefName()); 308 } 309 310 List resRefs = ejbDescriptor.getResourceReferences(); 311 for(int i = 0; i < resRefs.size(); i++) { 312 ResourceRefDescriptor resRef = (ResourceRefDescriptor) resRefs.get(i); 313 LinkRef linkRef = new LinkRef (resRef.getRefName()); 315 enc.bind(resRef.getRefName(), linkRef); 316 logger.debug("bind resource-ref, name: " + resRef.getRefName()); 317 } 318 319 List resourceEnvRefs = ejbDescriptor.getResourceEnvReferences(); 320 for(int i = 0; i < resourceEnvRefs.size(); i++) { 321 ResourceEnvRefDescriptor resourceEnvRef = (ResourceEnvRefDescriptor) resourceEnvRefs.get(i); 322 LinkRef linkRef = new LinkRef (resourceEnvRef.getRefName()); 324 enc.bind(resourceEnvRef.getRefName(), linkRef); 325 logger.debug("bind resource-env-ref, name: " + resourceEnvRef.getRefName()); 326 } 327 328 } 329 catch(NamingException e) { 330 logger.warn("bind EnterpriseContext error", e); 331 } 332 } 333 334 335 } 336 337 | Popular Tags |