Search in sources :

Example 66 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class MdbPoolContainer method afterDelivery.

public void afterDelivery(final Object instance) throws SystemException {
    // get the mdb call context
    final ThreadContext callContext = ThreadContext.getThreadContext();
    final MdbCallContext mdbCallContext = callContext.get(MdbCallContext.class);
    // invoke the tx after method
    try {
        afterInvoke(mdbCallContext.txPolicy, callContext);
    } catch (final ApplicationException e) {
        callContext.setDiscardInstance(true);
        throw new SystemException("Should never get an Application exception", e);
    } finally {
        if (instance != null) {
            if (callContext.isDiscardInstance()) {
                this.instanceManager.discardInstance(callContext, instance);
            } else {
                try {
                    this.instanceManager.poolInstance(callContext, instance);
                } catch (OpenEJBException e) {
                    throw new SystemException("Should never get an OpenEJBException exception", e);
                }
            }
        }
        ThreadContext.exit(mdbCallContext.oldCallContext);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) ApplicationException(org.apache.openejb.ApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) SystemException(org.apache.openejb.SystemException) ThreadContext(org.apache.openejb.core.ThreadContext)

Example 67 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class MdbPoolContainer method createActivationSpec.

private ActivationSpec createActivationSpec(final BeanContext beanContext) throws OpenEJBException {
    try {
        // initialize the object recipe
        final ObjectRecipe objectRecipe = new ObjectRecipe(activationSpecClass);
        objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
        objectRecipe.disallow(Option.FIELD_INJECTION);
        final Map<String, String> activationProperties = beanContext.getActivationProperties();
        for (final Map.Entry<String, String> entry : activationProperties.entrySet()) {
            objectRecipe.setMethodProperty(entry.getKey(), entry.getValue());
        }
        objectRecipe.setMethodProperty("beanClass", beanContext.getBeanClass());
        // create the activationSpec
        final ActivationSpec activationSpec = (ActivationSpec) objectRecipe.create(activationSpecClass.getClassLoader());
        // verify all properties except "destination" and "destinationType" were consumed
        final Set<String> unusedProperties = new TreeSet<String>(objectRecipe.getUnsetProperties().keySet());
        unusedProperties.remove("destination");
        unusedProperties.remove("destinationType");
        unusedProperties.remove("destinationLookup");
        unusedProperties.remove("connectionFactoryLookup");
        unusedProperties.remove("beanClass");
        unusedProperties.remove("MdbActiveOnStartup");
        unusedProperties.remove("MdbJMXControl");
        unusedProperties.remove("DeliveryActive");
        if (!unusedProperties.isEmpty()) {
            final String text = "No setter found for the activation spec properties: " + unusedProperties;
            if (failOnUnknownActivationSpec) {
                throw new IllegalArgumentException(text);
            } else {
                logger.warning(text);
            }
        }
        // validate the activation spec
        try {
            activationSpec.validate();
        } catch (final UnsupportedOperationException uoe) {
            logger.info("ActivationSpec does not support validate. Implementation of validate is optional");
        }
        // also try validating using Bean Validation if there is a Validator available in the context.
        try {
            final Validator validator = (Validator) beanContext.getJndiContext().lookup("comp/Validator");
            final Set generalSet = validator.validate(activationSpec);
            if (!generalSet.isEmpty()) {
                throw new ConstraintViolationException("Constraint violation for ActivationSpec " + activationSpecClass.getName(), generalSet);
            }
        } catch (final NamingException e) {
            logger.debug("No Validator bound to JNDI context");
        }
        // set the resource adapter into the activation spec
        activationSpec.setResourceAdapter(resourceAdapter);
        return activationSpec;
    } catch (final Exception e) {
        throw new OpenEJBException("Unable to create activation spec", e);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) TreeSet(java.util.TreeSet) Set(java.util.Set) ResourceException(javax.resource.ResourceException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) NamingException(javax.naming.NamingException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConstraintViolationException(javax.validation.ConstraintViolationException) SystemException(org.apache.openejb.SystemException) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) TreeSet(java.util.TreeSet) ActivationSpec(javax.resource.spi.ActivationSpec) ConstraintViolationException(javax.validation.ConstraintViolationException) NamingException(javax.naming.NamingException) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Validator(javax.validation.Validator)

Example 68 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class StatefulContainer method createEJBObject.

protected ProxyInfo createEJBObject(final BeanContext beanContext, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
    // generate a new primary key
    final Object primaryKey = newPrimaryKey();
    final ThreadContext createContext = new ThreadContext(beanContext, primaryKey);
    final ThreadContext oldCallContext = ThreadContext.enter(createContext);
    Object runAs = null;
    try {
        if (oldCallContext != null) {
            final BeanContext oldBc = oldCallContext.getBeanContext();
            if (oldBc.getRunAsUser() != null || oldBc.getRunAs() != null) {
                runAs = AbstractSecurityService.class.cast(securityService).overrideWithRunAsContext(createContext, beanContext, oldBc);
            }
        }
        // Security check
        checkAuthorization(callMethod, interfaceType);
        // Create the extended entity managers for this instance
        final 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 (final EntityManagerAlreadyRegisteredException e) {
                throw new EJBException(e);
            }
        }
        createContext.setCurrentOperation(Operation.CREATE);
        createContext.setCurrentAllowedStates(null);
        // Start transaction
        final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(createContext.getBeanContext().getTransactionType(callMethod, interfaceType), createContext);
        Instance instance = null;
        try {
            try {
                final InstanceContext context = beanContext.newInstance();
                // Wrap-up everthing into a object
                instance = new Instance(beanContext, primaryKey, containerID, context.getBean(), context.getCreationalContext(), context.getInterceptors(), entityManagers, lockFactory.newLock(primaryKey.toString()));
            } catch (final Throwable throwable) {
                final ThreadContext callContext = ThreadContext.getThreadContext();
                EjbTransactionUtil.handleSystemException(callContext.getTransactionPolicy(), throwable, callContext);
                // should never be reached
                throw new IllegalStateException(throwable);
            }
            // add to cache
            if (isPassivable(beanContext)) {
                // no need to cache it it will never expires
                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
                final Method createOrInit = beanContext.getMatchingBeanMethod(callMethod);
                createContext.set(Method.class, createOrInit);
                // Initialize interceptor stack
                final 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 (final Throwable e) {
            handleException(createContext, txPolicy, e);
        } finally {
            // un register EntityManager
            unregisterEntityManagers(instance, createContext);
            afterInvoke(createContext, txPolicy, instance);
        }
        return new ProxyInfo(beanContext, primaryKey);
    } finally {
        if (runAs != null) {
            try {
                securityService.associate(runAs);
            } catch (final LoginException e) {
            // no-op
            }
        }
        ThreadContext.exit(oldCallContext);
    }
}
Also used : EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) SystemInstance(org.apache.openejb.loader.SystemInstance) ThreadContext(org.apache.openejb.core.ThreadContext) JtaTransactionPolicy(org.apache.openejb.core.transaction.JtaTransactionPolicy) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) Method(java.lang.reflect.Method) BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) InstanceContext(org.apache.openejb.core.InstanceContext) EntityManagerFactory(javax.persistence.EntityManagerFactory) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) LoginException(javax.security.auth.login.LoginException) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

Example 69 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class StatefulContainer method undeploy.

@Override
public synchronized void undeploy(final BeanContext beanContext) throws OpenEJBException {
    final Data data = (Data) beanContext.getContainerData();
    final MBeanServer server = LocalMBeanServer.get();
    for (final ObjectName objectName : data.jmxNames) {
        try {
            server.unregisterMBean(objectName);
        } catch (final Exception e) {
            logger.error("Unable to unregister MBean " + objectName);
        }
    }
    deploymentsById.remove(beanContext.getDeploymentID());
    beanContext.setContainer(null);
    beanContext.setContainerData(null);
    if (isPassivable(beanContext)) {
        cache.removeAll(new BeanContextFilter(beanContext.getId()));
    }
}
Also used : InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) OpenEJBException(org.apache.openejb.OpenEJBException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) SystemException(org.apache.openejb.SystemException) NoSuchObjectException(java.rmi.NoSuchObjectException) EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) ObjectName(javax.management.ObjectName)

Example 70 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class StatefulContainer method obtainInstance.

@SuppressWarnings("LockAcquiredButNotSafelyReleased")
private Instance obtainInstance(final Object primaryKey, final ThreadContext callContext, final Method callMethod, final boolean checkOutIfNecessary) throws OpenEJBException {
    if (primaryKey == null) {
        throw new SystemException(new NullPointerException("Cannot obtain an instance of the stateful session bean with a null session id"));
    }
    final Transaction currentTransaction = getTransaction(callContext);
    // Find the instance
    Instance instance;
    synchronized (this) {
        instance = checkedOutInstances.get(primaryKey);
        if (instance == null) {
            // no need to check for extended persistence contexts it shouldn't happen
            try {
                instance = cache.checkOut(primaryKey, checkOutIfNecessary);
            } catch (final OpenEJBException e) {
                throw e;
            } catch (final Exception e) {
                throw new SystemException("Unexpected load exception", e);
            }
            // Did we find the instance?
            if (instance == null) {
                throw new InvalidateReferenceException(new NoSuchObjectException("Not Found"));
            }
            // remember instance until it is returned to the cache
            checkedOutInstances.put(primaryKey, instance);
        }
    }
    final Duration accessTimeout = getAccessTimeout(instance.beanContext, callMethod);
    final LockFactory.StatefulLock currLock = instance.getLock();
    final boolean lockAcquired;
    if (accessTimeout == null || accessTimeout.getTime() < 0) {
        // wait indefinitely for a lock
        currLock.lock();
        lockAcquired = true;
    } else if (accessTimeout.getTime() == 0) {
        // concurrent calls are not allowed, lock only once
        lockAcquired = currLock.tryLock();
    } else {
        // try to get a lock within the specified period.
        try {
            lockAcquired = currLock.tryLock(accessTimeout.getTime(), accessTimeout.getUnit());
        } catch (final InterruptedException e) {
            throw new ApplicationException("Unable to get lock.", e);
        }
    }
    // Did we acquire the lock to the current execution?
    if (!lockAcquired) {
        throw new ApplicationException(new ConcurrentAccessTimeoutException("Unable to get lock."));
    }
    if (instance.getTransaction() != null) {
        if (!instance.getTransaction().equals(currentTransaction) && !instance.getLock().tryLock()) {
            throw new ApplicationException(new RemoteException("Instance is in a transaction and cannot be invoked outside that transaction.  See EJB 3.0 Section 4.4.4"));
        }
    } else {
        instance.setTransaction(currentTransaction);
    }
    // Mark the instance in use so we can detect reentrant calls
    instance.setInUse(true);
    return instance;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) SystemInstance(org.apache.openejb.loader.SystemInstance) Duration(org.apache.openejb.util.Duration) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) OpenEJBException(org.apache.openejb.OpenEJBException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) SystemException(org.apache.openejb.SystemException) NoSuchObjectException(java.rmi.NoSuchObjectException) EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) Transaction(javax.transaction.Transaction) SuspendedTransaction(org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction) EjbUserTransaction(org.apache.openejb.core.transaction.EjbUserTransaction) NoSuchObjectException(java.rmi.NoSuchObjectException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) RemoteException(java.rmi.RemoteException)

Aggregations

OpenEJBException (org.apache.openejb.OpenEJBException)187 IOException (java.io.IOException)55 NamingException (javax.naming.NamingException)34 URL (java.net.URL)31 MalformedURLException (java.net.MalformedURLException)30 ApplicationException (org.apache.openejb.ApplicationException)29 BeanContext (org.apache.openejb.BeanContext)29 File (java.io.File)26 ArrayList (java.util.ArrayList)26 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)23 SystemException (org.apache.openejb.SystemException)22 Method (java.lang.reflect.Method)20 HashMap (java.util.HashMap)18 ThreadContext (org.apache.openejb.core.ThreadContext)17 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)15 RemoteException (java.rmi.RemoteException)14 EJBException (javax.ejb.EJBException)14 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)14 HashSet (java.util.HashSet)13 Properties (java.util.Properties)13