Java 类javax.ejb.MessageDrivenBean 实例源码

项目:tomee    文件:MdbInstanceFactory.java   
/**
 * Frees an instance no longer needed by the resource adapter.  This method makes all the necessary lifecycle
 * callbacks and decrements the instance count.  This method should not be used to disposed of beans that have
 * thrown a system exception.  Instead the discardInstance method should be called.
 *
 * @param instance             the bean instance to free
 * @param ignoredInstanceCount
 */
public void freeInstance(final Instance instance, final boolean ignoredInstanceCount) {
    if (instance == null) {
        throw new NullPointerException("bean is null");
    }

    // decrement the instance count
    if (!ignoredInstanceCount) {
        synchronized (this) {
            instanceCount--;
        }
    }

    final ThreadContext callContext = ThreadContext.getThreadContext();

    final Operation originalOperation = callContext == null ? null : callContext.getCurrentOperation();
    final BaseContext.State[] originalAllowedStates = callContext == null ? null : callContext.getCurrentAllowedStates();

    try {
        // call post destroy method
        if (callContext != null) {
            callContext.setCurrentOperation(Operation.PRE_DESTROY);
        }
        final Method remove = instance.bean instanceof MessageDrivenBean ? MessageDrivenBean.class.getMethod("ejbRemove") : null;
        final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
        final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
        interceptorStack.invoke();
        if (instance.creationalContext != null) {
            instance.creationalContext.release();
        }
    } catch (final Throwable re) {
        MdbInstanceFactory.logger.error("The bean instance " + instance.bean + " threw a system exception:" + re, re);
    } finally {

        if (callContext != null) {
            callContext.setCurrentOperation(originalOperation);
            callContext.setCurrentAllowedStates(originalAllowedStates);
        }
    }
}
项目:tomee    文件:MdbInstanceFactory.java   
private Object constructBean() throws UnavailableException {
    final BeanContext beanContext = this.beanContext;

    final ThreadContext callContext = new ThreadContext(beanContext, null, Operation.INJECTION);
    final ThreadContext oldContext = ThreadContext.enter(callContext);

    try {
        final InstanceContext context = beanContext.newInstance();

        if (context.getBean() instanceof MessageDrivenBean) {
            callContext.setCurrentOperation(Operation.CREATE);
            final Method create = beanContext.getCreateMethod();
            final InterceptorStack ejbCreate = new InterceptorStack(context.getBean(), create, Operation.CREATE, new ArrayList(), new HashMap());
            ejbCreate.invoke();
        }

        return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext());
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        final String message = "The bean instance threw a system exception:" + e;
        MdbInstanceFactory.logger.error(message, e);
        throw new UnavailableException(message, e);
    } finally {
        ThreadContext.exit(oldContext);
    }
}