Search in sources :

Example 16 with InterceptorStack

use of org.apache.openejb.core.interceptor.InterceptorStack in project tomee by apache.

the class SingletonInstanceManager method createInstance.

private Instance createInstance(final ThreadContext callContext, final BeanContext beanContext) throws ApplicationException {
    try {
        initializeDependencies(beanContext);
        final InstanceContext context = beanContext.newInstance();
        if (context.getBean() instanceof SessionBean) {
            final Operation originalOperation = callContext.getCurrentOperation();
            try {
                callContext.setCurrentOperation(Operation.CREATE);
                final Method create = beanContext.getCreateMethod();
                final InterceptorStack ejbCreate = new InterceptorStack(context.getBean(), create, Operation.CREATE, new ArrayList<InterceptorData>(), new HashMap());
                ejbCreate.invoke();
            } finally {
                callContext.setCurrentOperation(originalOperation);
            }
        }
        final ReadWriteLock lock;
        if (beanContext.isBeanManagedConcurrency()) {
            // Bean-Managed Concurrency
            lock = new BeanManagedLock();
        } else {
            // Container-Managed Concurrency
            lock = new ReentrantReadWriteLock();
        }
        return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext(), lock);
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        final String t = "The bean instance " + beanContext.getDeploymentID() + " threw a system exception:" + e;
        logger.error(t, e);
        throw new ApplicationException(new NoSuchEJBException("Singleton failed to initialize").initCause(e));
    }
}
Also used : NoSuchEJBException(javax.ejb.NoSuchEJBException) HashMap(java.util.HashMap) SystemInstance(org.apache.openejb.loader.SystemInstance) Operation(org.apache.openejb.core.Operation) Method(java.lang.reflect.Method) SessionBean(javax.ejb.SessionBean) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApplicationException(org.apache.openejb.ApplicationException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) InstanceContext(org.apache.openejb.core.InstanceContext) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData)

Example 17 with InterceptorStack

use of org.apache.openejb.core.interceptor.InterceptorStack in project tomee by apache.

the class StatelessContainer method invokeWebService.

private Object invokeWebService(final Object[] args, final BeanContext beanContext, final Method runMethod, final Instance instance) throws Exception {
    if (args.length < 2) {
        throw new IllegalArgumentException("WebService calls must follow format {messageContext, interceptor, [arg...]}.");
    }
    final Object messageContext = args[0];
    // This object will be used as an interceptor in the stack and will be responsible
    // for unmarshalling the soap message parts into an argument list that will be
    // used for the actual method invocation.
    //
    // We just need to make it an interceptor in the OpenEJB sense and tack it on the end
    // of our stack.
    final Object interceptor = args[1];
    final Class<?> interceptorClass = interceptor.getClass();
    //  Add the webservice interceptor to the list of interceptor instances
    final Map<String, Object> interceptors = new HashMap<String, Object>(instance.interceptors);
    interceptors.put(interceptor.getClass().getName(), interceptor);
    //  Create an InterceptorData for the webservice interceptor to the list of interceptorDatas for this method
    final List<InterceptorData> interceptorDatas = new ArrayList<InterceptorData>();
    final InterceptorData providerData = new InterceptorData(interceptorClass);
    providerData.getAroundInvoke().addAll(retrieveAroundInvokes(interceptorClass));
    interceptorDatas.add(0, providerData);
    interceptorDatas.addAll(beanContext.getMethodInterceptors(runMethod));
    final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, Operation.BUSINESS_WS, interceptorDatas, interceptors);
    final Object[] params = new Object[runMethod.getParameterTypes().length];
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    Object returnValue = null;
    if (messageContext instanceof javax.xml.rpc.handler.MessageContext) {
        threadContext.set(javax.xml.rpc.handler.MessageContext.class, (javax.xml.rpc.handler.MessageContext) messageContext);
        returnValue = interceptorStack.invoke((javax.xml.rpc.handler.MessageContext) messageContext, params);
    } else if (messageContext instanceof javax.xml.ws.handler.MessageContext) {
        AddressingSupport wsaSupport = NoAddressingSupport.INSTANCE;
        for (int i = 2; i < args.length; i++) {
            if (args[i] instanceof AddressingSupport) {
                wsaSupport = (AddressingSupport) args[i];
            }
        }
        threadContext.set(AddressingSupport.class, wsaSupport);
        threadContext.set(javax.xml.ws.handler.MessageContext.class, (javax.xml.ws.handler.MessageContext) messageContext);
        returnValue = interceptorStack.invoke((javax.xml.ws.handler.MessageContext) messageContext, params);
    }
    return returnValue;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ThreadContext(org.apache.openejb.core.ThreadContext) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) NoAddressingSupport(org.apache.openejb.core.webservices.NoAddressingSupport) AddressingSupport(org.apache.openejb.core.webservices.AddressingSupport)

Aggregations

InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)17 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)16 Method (java.lang.reflect.Method)13 BeanContext (org.apache.openejb.BeanContext)10 ThreadContext (org.apache.openejb.core.ThreadContext)10 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)10 SystemInstance (org.apache.openejb.loader.SystemInstance)8 BeanTransactionPolicy (org.apache.openejb.core.transaction.BeanTransactionPolicy)6 JtaTransactionPolicy (org.apache.openejb.core.transaction.JtaTransactionPolicy)6 HashMap (java.util.HashMap)5 SessionBean (javax.ejb.SessionBean)5 InstanceContext (org.apache.openejb.core.InstanceContext)5 ArrayList (java.util.ArrayList)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 EJBLocalObject (javax.ejb.EJBLocalObject)3 EJBObject (javax.ejb.EJBObject)3 EntityManagerFactory (javax.persistence.EntityManagerFactory)3 LoginException (javax.security.auth.login.LoginException)3 ApplicationException (org.apache.openejb.ApplicationException)3