private void removeEJBObject( final Method callMethod, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException { final BeanContext beanContext = callContext.getBeanContext(); final TransactionPolicy txPolicy = createTransactionPolicy( beanContext.getTransactionType(callMethod, interfaceType), callContext); try { final EntityBean entityBean = (EntityBean) cmpEngine.loadBean(callContext, callContext.getPrimaryKey()); if (entityBean == null) { throw new NoSuchObjectException( callContext.getBeanContext().getDeploymentID() + " " + callContext.getPrimaryKey()); } ejbRemove(entityBean); cmpEngine.removeBean(callContext); } catch (final NoSuchObjectException e) { handleApplicationException(txPolicy, e, false); } catch (final Throwable e) { // handle reflection exception handleSystemException(txPolicy, e, callContext); } finally { afterInvoke(txPolicy, callContext); } }
public void afterLoad(Instance instance) throws SystemException, ApplicationException { BeanContext beanContext = instance.beanContext; ThreadContext threadContext = new ThreadContext(instance.beanContext, instance.primaryKey, Operation.ACTIVATE); ThreadContext oldContext = ThreadContext.enter(threadContext); try { Method remove = instance.bean instanceof SessionBean ? SessionBean.class.getMethod("ejbActivate") : null; List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors(); InterceptorStack interceptorStack = new InterceptorStack( instance.bean, remove, Operation.ACTIVATE, callbackInterceptors, instance.interceptors); interceptorStack.invoke(); } catch (Throwable callbackException) { discardInstance(threadContext); handleSystemException( threadContext.getTransactionPolicy(), callbackException, threadContext); } finally { ThreadContext.exit(oldContext); } }
private Object findByPrimaryKey( final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException { final BeanContext beanContext = callContext.getBeanContext(); final TransactionPolicy txPolicy = createTransactionPolicy( beanContext.getTransactionType(callMethod, interfaceType), callContext); try { final EntityBean bean = (EntityBean) cmpEngine.loadBean(callContext, args[0]); if (bean == null) { throw new ObjectNotFoundException(beanContext.getDeploymentID() + " : " + args[0]); } // rebuild the primary key final KeyGenerator kg = beanContext.getKeyGenerator(); final Object primaryKey = kg.getPrimaryKey(bean); // create a new ProxyInfo based on the deployment info and primary key return new ProxyInfo(beanContext, primaryKey); } catch (final FinderException fe) { handleApplicationException(txPolicy, fe, false); } catch (final Throwable e) { // handle reflection exception handleSystemException(txPolicy, e, callContext); } finally { afterInvoke(txPolicy, callContext); } throw new AssertionError("Should not get here"); }
private void handleException(ThreadContext callContext, TransactionPolicy txPolicy, Throwable e) throws ApplicationException { if (e instanceof ApplicationException) { throw (ApplicationException) e; } ExceptionType type = callContext.getBeanContext().getExceptionType(e); if (type == SYSTEM) { discardInstance(callContext); handleSystemException(txPolicy, e, callContext); } else { handleApplicationException(txPolicy, e, type == APPLICATION_ROLLBACK); } }
private Object businessMethod( final Method callMethod, final Method runMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException { final BeanContext beanContext = callContext.getBeanContext(); final TransactionPolicy txPolicy = createTransactionPolicy( beanContext.getTransactionType(callMethod, interfaceType), callContext); final EntityBean bean; Object returnValue = null; entrancyTracker.enter(beanContext, callContext.getPrimaryKey()); try { bean = (EntityBean) cmpEngine.loadBean(callContext, callContext.getPrimaryKey()); if (bean == null) { throw new NoSuchObjectException( beanContext.getDeploymentID() + " : " + callContext.getPrimaryKey()); } returnValue = runMethod.invoke(bean, args); // when there is not transaction, merge the data from the bean back into the cmp engine cmpEngine.storeBeanIfNoTx(callContext, bean); } catch (final NoSuchObjectException e) { handleApplicationException(txPolicy, e, false); } catch (Throwable e) { if (e instanceof InvocationTargetException) { e = ((InvocationTargetException) e).getTargetException(); } final ExceptionType type = callContext.getBeanContext().getExceptionType(e); if (type == ExceptionType.SYSTEM) { /* System Exception ****************************/ handleSystemException(txPolicy, e, callContext); } else { /* Application Exception ***********************/ handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK); } } finally { entrancyTracker.exit(beanContext, callContext.getPrimaryKey()); afterInvoke(txPolicy, callContext); } return returnValue; }
private void afterInvoke(ThreadContext callContext, TransactionPolicy txPolicy, Instance instance) throws OpenEJBException { try { unregisterEntityManagers(instance, callContext); if (instance != null && txPolicy instanceof BeanTransactionPolicy) { // suspend the currently running transaction if any SuspendedTransaction suspendedTransaction = null; try { BeanTransactionPolicy beanTxEnv = (BeanTransactionPolicy) txPolicy; suspendedTransaction = beanTxEnv.suspendUserTransaction(); } catch (SystemException e) { handleSystemException(txPolicy, e, callContext); } finally { instance.setBeanTransaction(suspendedTransaction); } } } finally { if (instance != null) { instance.setInUse(false); } EjbTransactionUtil.afterInvoke(txPolicy, callContext); } }
protected Object _invoke( Method callMethod, Method runMethod, Object[] args, Instance instance, ThreadContext callContext, InterfaceType type) throws OpenEJBException { BeanContext beanContext = callContext.getBeanContext(); TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext); Object returnValue = null; try { if (type == InterfaceType.SERVICE_ENDPOINT) { callContext.setCurrentOperation(Operation.BUSINESS_WS); returnValue = invokeWebService(args, beanContext, runMethod, instance, returnValue); } else { List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod); InterceptorStack interceptorStack = new InterceptorStack( instance.bean, runMethod, type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, instance.interceptors); returnValue = interceptorStack.invoke(args); } } catch (Throwable re) { // handle reflection exception ExceptionType exceptionType = beanContext.getExceptionType(re); if (exceptionType == ExceptionType.SYSTEM) { /* System Exception ****************************/ // The bean instance is not put into the pool via instanceManager.poolInstance // and therefore the instance will be garbage collected and destroyed. // In case of StrictPooling flag being set to true we also release the semaphore // in the discardInstance method of the instanceManager. callContext.setDiscardInstance(true); handleSystemException(txPolicy, re, callContext); } else { /* Application Exception ***********************/ handleApplicationException( txPolicy, re, exceptionType == ExceptionType.APPLICATION_ROLLBACK); } } finally { try { afterInvoke(txPolicy, callContext); } catch (SystemException e) { callContext.setDiscardInstance(true); throw e; } catch (ApplicationException e) { throw e; } catch (RuntimeException e) { callContext.setDiscardInstance(true); throw e; } } return returnValue; }
protected ProxyInfo createEJBObject( BeanContext beanContext, Method callMethod, Object[] args, InterfaceType interfaceType) throws OpenEJBException { // generate a new primary key Object primaryKey = newPrimaryKey(); ThreadContext createContext = new ThreadContext(beanContext, primaryKey); ThreadContext oldCallContext = ThreadContext.enter(createContext); try { // Security check checkAuthorization(callMethod, interfaceType); // Create the extended entity managers for this instance Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> entityManagers = createEntityManagers(beanContext); // Register the newly created entity managers if (entityManagers != null) { try { entityManagerRegistry.addEntityManagers( (String) beanContext.getDeploymentID(), primaryKey, entityManagers); } catch (EntityManagerAlreadyRegisteredException e) { throw new EJBException(e); } } createContext.setCurrentOperation(Operation.CREATE); createContext.setCurrentAllowedStates(null); // Start transaction TransactionPolicy txPolicy = createTransactionPolicy( createContext.getBeanContext().getTransactionType(callMethod, interfaceType), createContext); Instance instance = null; try { // Create new instance try { final InstanceContext context = beanContext.newInstance(); // Wrap-up everthing into a object instance = new Instance( beanContext, primaryKey, context.getBean(), context.getInterceptors(), context.getCreationalContext(), entityManagers); } catch (Throwable throwable) { ThreadContext callContext = ThreadContext.getThreadContext(); handleSystemException(callContext.getTransactionPolicy(), throwable, callContext); throw new IllegalStateException(throwable); // should never be reached } // add to cache cache.add(primaryKey, instance); // instance starts checked-out checkedOutInstances.put(primaryKey, instance); // Register for synchronization callbacks registerSessionSynchronization(instance, createContext); // Invoke create for legacy beans if (!callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessRemoteHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalBeanHome.class)) { // Setup for business invocation Method createOrInit = beanContext.getMatchingBeanMethod(callMethod); createContext.set(Method.class, createOrInit); // Initialize interceptor stack InterceptorStack interceptorStack = new InterceptorStack( instance.bean, createOrInit, Operation.CREATE, new ArrayList<InterceptorData>(), new HashMap<String, Object>()); // Invoke if (args == null) { interceptorStack.invoke(); } else { interceptorStack.invoke(args); } } } catch (Throwable e) { handleException(createContext, txPolicy, e); } finally { afterInvoke(createContext, txPolicy, instance); } return new ProxyInfo(beanContext, primaryKey); } finally { ThreadContext.exit(oldCallContext); } }
public void freeInstance(ThreadContext callContext) { BeanContext beanContext = callContext.getBeanContext(); Data data = (Data) beanContext.getContainerData(); Future<Instance> instanceFuture = data.singleton.get(); // Possible the instance was never created if (instanceFuture == null) return; Instance instance; try { instance = instanceFuture.get(); } catch (InterruptedException e) { Thread.interrupted(); logger.error( "Singleton shutdown failed because the thread was interrupted: " + beanContext.getDeploymentID(), e); return; } catch (ExecutionException e) { // Instance was never initialized return; } try { callContext.setCurrentOperation(Operation.PRE_DESTROY); callContext.setCurrentAllowedStates(null); Method remove = instance.bean instanceof SessionBean ? beanContext.getCreateMethod() : null; List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors(); InterceptorStack interceptorStack = new InterceptorStack( instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors); // Transaction Demarcation for Singleton PostConstruct method TransactionType transactionType; if (beanContext.getComponentType() == BeanType.SINGLETON) { Set<Method> callbacks = callbackInterceptors.get(callbackInterceptors.size() - 1).getPreDestroy(); if (callbacks.isEmpty()) { transactionType = TransactionType.RequiresNew; } else { transactionType = beanContext.getTransactionType(callbacks.iterator().next()); if (transactionType == TransactionType.Required) { transactionType = TransactionType.RequiresNew; } } } else { transactionType = beanContext.isBeanManagedTransaction() ? TransactionType.BeanManaged : TransactionType.NotSupported; } TransactionPolicy transactionPolicy = EjbTransactionUtil.createTransactionPolicy(transactionType, callContext); try { // Call the chain interceptorStack.invoke(); if (instance.creationalContext != null) { instance.creationalContext.release(); } } catch (Throwable e) { // RollBack Transaction EjbTransactionUtil.handleSystemException(transactionPolicy, e, callContext); } finally { EjbTransactionUtil.afterInvoke(transactionPolicy, callContext); } } catch (Throwable re) { logger.error("Singleton shutdown failed: " + beanContext.getDeploymentID(), re); } }
protected Object _invoke( Method callMethod, Method runMethod, Object[] args, Instance instance, ThreadContext callContext, InterfaceType callType) throws OpenEJBException { BeanContext beanContext = callContext.getBeanContext(); Duration accessTimeout = getAccessTimeout(beanContext, runMethod); boolean read = beanContext.getConcurrencyAttribute(runMethod) == javax.ejb.LockType.READ; final Lock lock = aquireLock(read, accessTimeout, instance, runMethod); Object returnValue; try { TransactionPolicy txPolicy = createTransactionPolicy( beanContext.getTransactionType(callMethod, callType), callContext); returnValue = null; try { if (callType == InterfaceType.SERVICE_ENDPOINT) { callContext.setCurrentOperation(Operation.BUSINESS_WS); returnValue = invokeWebService(args, beanContext, runMethod, instance); } else { List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod); InterceptorStack interceptorStack = new InterceptorStack( instance.bean, runMethod, callType == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, instance.interceptors); returnValue = interceptorStack.invoke(args); } } catch (Throwable e) { // handle reflection exception ExceptionType type = beanContext.getExceptionType(e); if (type == ExceptionType.SYSTEM) { /* System Exception ****************************/ // The bean instance is not put into the pool via instanceManager.poolInstance // and therefore the instance will be garbage collected and destroyed. // For this reason the discardInstance method of the StatelessInstanceManager // does nothing. handleSystemException(txPolicy, e, callContext); } else { /* Application Exception ***********************/ handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK); } } finally { afterInvoke(txPolicy, callContext); } } finally { lock.unlock(); } return returnValue; }
private Object findEJBObject( final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException { final BeanContext beanContext = callContext.getBeanContext(); final TransactionPolicy txPolicy = createTransactionPolicy( beanContext.getTransactionType(callMethod, interfaceType), callContext); try { final List<Object> results = cmpEngine.queryBeans(callContext, callMethod, args); final KeyGenerator kg = beanContext.getKeyGenerator(); // The following block of code is responsible for returning ProxyInfo object(s) for each // matching entity bean found by the query. If its a multi-value find operation a Vector // of ProxyInfo objects will be returned. If its a single-value find operation then a // single ProxyInfo object is returned. if (callMethod.getReturnType() == Collection.class || callMethod.getReturnType() == Enumeration.class) { final List<ProxyInfo> proxies = new ArrayList<ProxyInfo>(); for (final Object value : results) { final EntityBean bean = (EntityBean) value; if (value == null) { proxies.add(null); } else { // get the primary key final Object primaryKey = kg.getPrimaryKey(bean); // create a new ProxyInfo based on the deployment info and primary key and add it to the // vector proxies.add(new ProxyInfo(beanContext, primaryKey)); } } if (callMethod.getReturnType() == Enumeration.class) { return new Enumerator(proxies); } else { return proxies; } } else { if (results.size() != 1) { throw new ObjectNotFoundException( "A Enteprise bean with deployment_id = " + beanContext.getDeploymentID() + (args != null && args.length >= 1 ? " and primarykey = " + args[0] : "") + " Does not exist"); } // create a new ProxyInfo based on the deployment info and primary key final EntityBean bean = (EntityBean) results.get(0); if (bean == null) { return null; } else { final Object primaryKey = kg.getPrimaryKey(bean); return new ProxyInfo(beanContext, primaryKey); } } } catch (final FinderException fe) { handleApplicationException(txPolicy, fe, false); } catch (final Throwable e) { // handle reflection exception handleSystemException(txPolicy, e, callContext); } finally { afterInvoke(txPolicy, callContext); } throw new AssertionError("Should not get here"); }
private ProxyInfo createEJBObject( final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException { final BeanContext beanContext = callContext.getBeanContext(); final TransactionPolicy txPolicy = createTransactionPolicy( beanContext.getTransactionType(callMethod, interfaceType), callContext); final EntityBean bean; Object primaryKey = null; try { // Obtain a bean instance from the method ready pool bean = createNewInstance(callContext); // set the entity context setEntityContext(bean); // Obtain the proper ejbCreate() method final Method ejbCreateMethod = beanContext.getMatchingBeanMethod(callMethod); // Set current operation for allowed operations callContext.setCurrentOperation(Operation.CREATE); // Invoke the proper ejbCreate() method on the instance ejbCreateMethod.invoke(bean, args); // create the new bean primaryKey = cmpEngine.createBean(bean, callContext); // determine post create callback method final Method ejbPostCreateMethod = beanContext.getMatchingPostCreateMethod(ejbCreateMethod); // create a new context containing the pk for the post create call final ThreadContext postCreateContext = new ThreadContext(beanContext, primaryKey); postCreateContext.setCurrentOperation(Operation.POST_CREATE); final ThreadContext oldContext = ThreadContext.enter(postCreateContext); try { // Invoke the ejbPostCreate method on the bean instance ejbPostCreateMethod.invoke(bean, args); // According to section 9.1.5.1 of the EJB 1.1 specification, the "ejbPostCreate(...) // method executes in the same transaction context as the previous ejbCreate(...) method." // // The bean is first insterted using db.create( ) and then after ejbPostCreate( ) its // updated using db.update(). This protocol allows for visablity of the bean after ejbCreate // within the current trasnaction. } finally { ThreadContext.exit(oldContext); } // when there is not transaction, merge the data from the bean back into the cmp engine cmpEngine.storeBeanIfNoTx(callContext, bean); } catch (Throwable e) { if (e instanceof InvocationTargetException) { e = ((InvocationTargetException) e).getTargetException(); } final ExceptionType type = callContext.getBeanContext().getExceptionType(e); if (type == ExceptionType.SYSTEM) { /* System Exception ****************************/ handleSystemException(txPolicy, e, callContext); } else { /* Application Exception ***********************/ handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK); } } finally { afterInvoke(txPolicy, callContext); } return new ProxyInfo(beanContext, primaryKey); }
private Object homeMethod( final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException { final BeanContext beanContext = callContext.getBeanContext(); final TransactionPolicy txPolicy = createTransactionPolicy( beanContext.getTransactionType(callMethod, interfaceType), callContext); final EntityBean bean; Object returnValue = null; try { /* Obtain a bean instance from the method ready pool */ bean = createNewInstance(callContext); // set the entity context setEntityContext(bean); try { callContext.setCurrentOperation(Operation.HOME); final Method runMethod = beanContext.getMatchingBeanMethod(callMethod); try { returnValue = runMethod.invoke(bean, args); } catch (final IllegalArgumentException e) { System.out.println("********************************************************"); System.out.println("callMethod = " + callMethod); System.out.println("runMethod = " + runMethod); System.out.println("bean = " + bean.getClass().getName()); throw e; } } finally { unsetEntityContext(bean); } } catch (Throwable e) { if (e instanceof InvocationTargetException) { e = ((InvocationTargetException) e).getTargetException(); } final ExceptionType type = callContext.getBeanContext().getExceptionType(e); if (type == ExceptionType.SYSTEM) { /* System Exception ****************************/ handleSystemException(txPolicy, e, callContext); } else { /* Application Exception ***********************/ handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK); } } finally { afterInvoke(txPolicy, callContext); } return returnValue; }