@Override
  public Object processInvocation(InterceptorContext context) throws Exception {
    final Component component = context.getPrivateData(Component.class);

    // if a session bean is participating in a transaction, it
    // is an error for a client to invoke the remove method
    // on the session object's home or component interface.
    final ComponentView view = context.getPrivateData(ComponentView.class);
    if (view != null) {
      Ejb2xViewType viewType = view.getPrivateData(Ejb2xViewType.class);
      if (viewType != null) {
        // this means it is an EJB 2.x view
        // which is not allowed to remove while enrolled in a TX
        final StatefulTransactionMarker marker =
            context.getPrivateData(StatefulTransactionMarker.class);
        if (marker != null && !marker.isFirstInvocation()) {
          throw EjbLogger.ROOT_LOGGER.cannotRemoveWhileParticipatingInTransaction();
        }
      }
    }
    // just log a WARN and throw back the original exception
    if (component instanceof StatefulSessionComponent == false) {
      throw EjbLogger.ROOT_LOGGER.unexpectedComponent(component, StatefulSessionComponent.class);
    }
    final StatefulSessionComponent statefulComponent = (StatefulSessionComponent) component;
    Object invocationResult = null;
    try {
      // proceed
      invocationResult = context.proceed();
    } catch (Exception e) {
      // Exception caught during call to @Remove method. Handle it appropriately

      // If it's an application exception and if the @Remove method has set "retainIfException" to
      // true
      // then just throw back the exception and don't remove the session instance.
      if (this.isApplicationException(statefulComponent, e.getClass(), context.getMethod())
          && this.retainIfException) {
        throw e;
      }
      // otherwise, just remove it and throw back the original exception
      final StatefulSessionComponentInstance statefulComponentInstance =
          (StatefulSessionComponentInstance) context.getPrivateData(ComponentInstance.class);
      final SessionID sessionId = statefulComponentInstance.getId();
      statefulComponent.removeSession(sessionId);
      throw e;
    }
    final StatefulSessionComponentInstance statefulComponentInstance =
        (StatefulSessionComponentInstance) context.getPrivateData(ComponentInstance.class);
    final SessionID sessionId = statefulComponentInstance.getId();
    // just remove the session because of a call to @Remove method
    statefulComponent.removeSession(sessionId);
    // return the invocation result
    return invocationResult;
  }
 protected TransactionAttributeType getCurrentTransactionAttribute() {
   final InterceptorContext currentInvocationContext = CurrentInvocationContext.get();
   if (currentInvocationContext == null) {
     return null;
   }
   final Method invokedMethod = currentInvocationContext.getMethod();
   // if method is null, then it's a lifecycle invocation
   if (invokedMethod == null) {
     return null;
   }
   // get the tx attribute of the invoked method
   return this.getTransactionAttributeType(invokedMethod);
 }
 /**
  * Returns true if the {@link CurrentInvocationContext} represents a lifecycle callback
  * invocation. Else returns false.
  *
  * <p>This method internally relies on {@link CurrentInvocationContext#get()} to obtain the
  * current invocation context.
  *
  * <ul>
  *   <li>If the context is available then it looks for the method that was invoked. The absence of
  *       a method indicates a lifecycle callback.
  *   <li>If the context is <i>not</i> available, then this method returns false (i.e. it doesn't
  *       consider the current invocation as a lifecycle callback). This is for convenience, to
  *       allow the invocation of {@link javax.ejb.TimerService} methods in the absence of {@link
  *       CurrentInvocationContext}
  * </ul>
  *
  * <p>
  *
  * @return
  */
 protected boolean isLifecycleCallbackInvocation() {
   final InterceptorContext currentInvocationContext = CurrentInvocationContext.get();
   if (currentInvocationContext == null) {
     return false;
   }
   // If the method in current invocation context is null,
   // then it represents a lifecycle callback invocation
   Method invokedMethod = currentInvocationContext.getMethod();
   if (invokedMethod == null) {
     // it's a lifecycle callback
     return true;
   }
   // not a lifecycle callback
   return false;
 }
 @Override
 public Object processInvocation(InterceptorContext context) throws Exception {
   final Method invokedMethod = context.getMethod();
   final ComponentViewInstance componentViewInstance =
       context.getPrivateData(ComponentViewInstance.class);
   // For a lifecycle interception, the ComponentViewInstance (and the invoked business interface)
   // will be null.
   // On a normal method invocation, the invoked business interface will be obtained from the
   // ComponentViewInstance
   final Class<?> invokedBusinessInterface =
       componentViewInstance == null ? null : componentViewInstance.getViewClass();
   Object[] parameters = context.getParameters();
   SessionInvocationContext sessionInvocationContext =
       new CustomSessionInvocationContext(
           lifecycleCallback, context, invokedBusinessInterface, invokedMethod, parameters);
   context.putPrivateData(InvocationContext.class, sessionInvocationContext);
   CurrentInvocationContext.push(sessionInvocationContext);
   try {
     return context.proceed();
   } finally {
     CurrentInvocationContext.pop();
     context.putPrivateData(InvocationContext.class, null);
   }
 }
Exemple #5
0
  protected TransactionAttributeType getCurrentTransactionAttribute() {

    final InterceptorContext invocation = CurrentInvocationContext.get();
    final MethodIntf methodIntf = MethodIntfHelper.of(invocation);
    return getTransactionAttributeType(methodIntf, invocation.getMethod());
  }