예제 #1
0
  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;
  }
예제 #2
0
  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);
    }
  }
예제 #3
0
  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);
    }
  }
  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 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;
  }
예제 #7
0
  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");
  }
예제 #8
0
  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);
  }
예제 #9
0
  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;
  }