Package org.jboss.as.ejb3.component.entity

Examples of org.jboss.as.ejb3.component.entity.EntityBeanComponent


    }

    @Override
    public Interceptor create(final InterceptorFactoryContext context) {

        final EntityBeanComponent component = (EntityBeanComponent) context.getContextData().get(Component.class);

        return new Interceptor() {
            @Override
            public Object processInvocation(final InterceptorContext context) throws Exception {

                //grab a bean from the pool to invoke the finder method on
                final EntityBeanComponentInstance instance = component.getPool().get();
                final Object result;
                try {
                    result = invokeFind(context, instance);
                    return prepareResults(context, result, component);

                } finally {
                    component.getPool().release(instance);
                }
            }

        };
    }
View Full Code Here


    private final Object threadLock = new Object();
    private final OwnableReentrantLock lock = new OwnableReentrantLock();

    @Override
    public Object processInvocation(InterceptorContext context) throws Exception {
        final EntityBeanComponent component = getComponent(context, EntityBeanComponent.class);
        final EntityBeanComponentInstance instance = (EntityBeanComponentInstance) context.getPrivateData(ComponentInstance.class);

        //we do not synchronize for instances that are not associated with an identity
        if (instance.getPrimaryKey() == null) {
            return context.proceed();
        }

        final TransactionSynchronizationRegistry transactionSynchronizationRegistry = component.getTransactionSynchronizationRegistry();
        if (ROOT_LOGGER.isTraceEnabled()) {
            ROOT_LOGGER.trace("Trying to acquire lock: " + lock + " for entity bean " + instance + " during invocation: " + context);
        }
        // we obtain a lock in this synchronization interceptor because the lock needs to be tied to the synchronization
        // so that it can released on the tx synchronization callbacks
        final Object lockOwner = getLockOwner(transactionSynchronizationRegistry);
        lock.pushOwner(lockOwner);
        try {
            lock.lock();
            synchronized (lock) {
                if (ROOT_LOGGER.isTraceEnabled()) {
                    ROOT_LOGGER.trace("Acquired lock: " + lock + " for entity bean instance: " + instance + " during invocation: " + context);
                }

                Object currentTransactionKey = null;
                try {
                    // get the key to current transaction associated with this thread
                    currentTransactionKey = transactionSynchronizationRegistry.getTransactionKey();
                    if (!instance.isSynchronizeRegistered()) {
                        component.getCache().reference(instance);
                        // if this entity instance is already associated with a different transaction, then it's an error

                        // if the thread is currently associated with a tx, then register a tx synchronization
                        if (currentTransactionKey != null) {
                            // register a tx synchronization for this entity instance
View Full Code Here

    public Interceptor create(final  InterceptorFactoryContext factoryContext) {

        return new AbstractEJBInterceptor() {
            @Override
            public Object processInvocation(final InterceptorContext context) throws Exception {
                final EntityBeanComponent component = getComponent(context, EntityBeanComponent.class);

                final Object primaryKey = context.getPrivateData(EntityBeanComponent.PRIMARY_KEY_CONTEXT_KEY);
                if (primaryKey == null) {
                    throw MESSAGES.primaryKeyIsNull();
                }

                final EntityBeanComponentInstance instance = component.getCache().get(primaryKey);

                if(instance.isRemoved()) {
                    throw MESSAGES.instaceWasRemoved(component.getComponentName(),primaryKey);
                }

                try {
                    context.putPrivateData(ComponentInstance.class, instance);
                    return context.proceed();
                } catch (Exception ex) {
                    // Detect app exception
                    if (component.getApplicationException(ex.getClass(), context.getMethod()) != null) {
                        // it's an application exception, just throw it back.
                        throw ex;
                    }
                    if (ex instanceof ConcurrentAccessTimeoutException || ex instanceof ConcurrentAccessException) {
                        throw ex;
                    }
                    if (ex instanceof RuntimeException || ex instanceof RemoteException) {
                        if (ROOT_LOGGER.isTraceEnabled())
                            ROOT_LOGGER.trace("Discarding bean " + primaryKey + " because of exception", ex);
                        component.getCache().discard(instance);
                    }
                    throw ex;
                } catch (final Error e) {
                    if (ROOT_LOGGER.isTraceEnabled())
                        ROOT_LOGGER.trace("Discarding bean " + primaryKey + " because of error", e);
                    component.getCache().discard(instance);
                    throw e;
                } catch (final Throwable t) {
                    if (ROOT_LOGGER.isTraceEnabled())
                        ROOT_LOGGER.trace("Discarding bean " + primaryKey + " because of Throwable", t);
                    component.getCache().discard(instance);
                    throw new RuntimeException(t);
                } finally {
                    // the StatefulSessionSynchronizationInterceptor will take care of releasing
                    context.putPrivateData(ComponentInstance.class, null);
                }
View Full Code Here

    }

    public Interceptor create(final InterceptorFactoryContext context) {
        return new AbstractEJBInterceptor() {
            public Object processInvocation(final InterceptorContext context) throws Exception {
                final EntityBeanComponent component = getComponent(context, EntityBeanComponent.class);
                final EntityBeanComponentInstance instance = component.getCache().get(context.getParameters()[0]);
                final Method oldMethod = context.getMethod();
                final Object[] oldParams = context.getParameters();
                try {
                    context.putPrivateData(ComponentInstance.class, instance);
                    context.setMethod(ejbRemove);
View Full Code Here

            // Initialize beanPOA and create metadata depending on the kind of bean
            if (component instanceof EntityBeanComponent) {

                // This is an entity bean (lifespan: persistent)
                beanServantRegistry = poaRegistry.getValue().getRegistryWithPersistentPOAPerServant();
                final EntityBeanComponent entityBeanComponent = (EntityBeanComponent) component;
                final Class pkClass = entityBeanComponent.getPrimaryKeyClass();
                ejbMetaData = new EJBMetaDataImp(entityBeanComponent.getRemoteClass(), entityBeanComponent.getHomeClass(), pkClass, false, false, ejbHome);
            } else {
                // This is a session bean (lifespan: transient)
                beanServantRegistry = poaRegistry.getValue().getRegistryWithTransientPOAPerServant();
                if (component instanceof StatelessSessionComponent) {
                    // Stateless session bean
View Full Code Here

    }

    @Override
    public Interceptor create(final InterceptorFactoryContext context) {

        final EntityBeanComponent component = (EntityBeanComponent) context.getContextData().get(Component.class);

        return new Interceptor() {
            @Override
            public Object processInvocation(final InterceptorContext context) throws Exception {

                //grab a bean from the pool to invoke the finder method on
                final EntityBeanComponentInstance instance = component.acquireUnAssociatedInstance();
                final Object result;
                final InvocationType invocationType = context.getPrivateData(InvocationType.class);
                try {
                    context.putPrivateData(InvocationType.class, InvocationType.FINDER_METHOD);
                    result = invokeFind(context, instance);
                    return prepareResults(context, result, component);
                } finally {
                    component.releaseEntityBeanInstance(instance);
                    context.putPrivateData(InvocationType.class, invocationType);
                }
            }

        };
View Full Code Here

* @author Stuart Douglas
*/
public class EntityBeanSynchronizationInterceptor extends AbstractEJBInterceptor {
    @Override
    public Object processInvocation(InterceptorContext context) throws Exception {
        final EntityBeanComponent component = getComponent(context, EntityBeanComponent.class);
        final EntityBeanComponentInstance instance = (EntityBeanComponentInstance) context.getPrivateData(ComponentInstance.class);
        final OwnableReentrantLock lock = instance.getLock();
        //we do not synchronize for instances that are not associated with an identity
        if (instance.getPrimaryKey() == null) {
            return context.proceed();
        }

        final TransactionSynchronizationRegistry transactionSynchronizationRegistry = component.getTransactionSynchronizationRegistry();
        if (ROOT_LOGGER.isTraceEnabled()) {
            ROOT_LOGGER.trace("Trying to acquire lock: " + lock + " for entity bean " + instance + " during invocation: " + context);
        }
        // we obtain a lock in this synchronization interceptor because the lock needs to be tied to the synchronization
        // so that it can released on the tx synchronization callbacks
        final Object lockOwner = getLockOwner(transactionSynchronizationRegistry);
        lock.pushOwner(lockOwner);
        try {
            lock.lock();
            boolean syncRegistered = false;
            synchronized (lock) {

                //if the previous transaction was rolled back we re-load the entity bean state in the current TX
                if(instance.isReloadRequired()) {
                    instance.reload();
                }

                if (ROOT_LOGGER.isTraceEnabled()) {
                    ROOT_LOGGER.trace("Acquired lock: " + lock + " for entity bean instance: " + instance + " during invocation: " + context);
                }

                if (context.getPrivateData(InternalInvocationMarker.class) == null) {
                    if (instance.isRemoved() || instance.isDiscarded()) {
                        final Object primaryKey = context.getPrivateData(EntityBeanComponent.PRIMARY_KEY_CONTEXT_KEY);
                        component.getCache().release(instance, true);
                        lock.unlock();
                        throw EjbLogger.ROOT_LOGGER.instanceWasRemoved(component.getComponentName(), primaryKey);
                    }
                }

                Object currentTransactionKey = null;
                try {
                    // get the key to current transaction associated with this thread
                    currentTransactionKey = transactionSynchronizationRegistry.getTransactionKey();
                    if (!instance.isSynchronizeRegistered()) {
                        // if this entity instance is already associated with a different transaction, then it's an error

                        // if the thread is currently associated with a tx, then register a tx synchronization
                        if (currentTransactionKey != null) {
                            // register a tx synchronization for this entity instance
                            final Synchronization entitySynchronization = new EntityBeanSynchronization(instance, lockOwner);
                            transactionSynchronizationRegistry.registerInterposedSynchronization(entitySynchronization);
                            syncRegistered = true;
                            if (ROOT_LOGGER.isTraceEnabled()) {
                                ROOT_LOGGER.trace("Registered tx synchronization: " + entitySynchronization + " for tx: " + currentTransactionKey +
                                        " associated with stateful component instance: " + instance);
                            }
                        }
                        instance.setSynchronizationRegistered(true);
                    }
                    // proceed with the invocation
                    return context.proceed();

                } finally {
                    // if the current call did *not* register a tx SessionSynchronization, then we have to explicitly mark the
                    // entity instance as "no longer in use". If it registered a tx EntityBeanSynchronization, then releasing the lock is
                    // taken care off by a tx synchronization callbacks.
                    //if is important to note that we increase the lock count on every invocation
                    //which means we need to release it on every invocation except for the one where we actually registered the TX synchronization
                    if (currentTransactionKey == null) {
                        instance.store();
                        releaseInstance(instance, true);
                    } else if (!syncRegistered) {
                        component.getCache().release(instance, true);
                        lock.unlock();
                    }
                }
            }
        } finally {
View Full Code Here

    public static final InterceptorFactory FACTORY = new ImmediateInterceptorFactory(new EntityBeanAssociatingInterceptor());

    @Override
    public Object processInvocation(final InterceptorContext context) throws Exception {
        final EntityBeanComponent component = getComponent(context, EntityBeanComponent.class);

        final Object primaryKey = context.getPrivateData(EntityBeanComponent.PRIMARY_KEY_CONTEXT_KEY);
        if (primaryKey == null) {
            throw EjbLogger.ROOT_LOGGER.primaryKeyIsNull();
        }
        final EntityBeanComponentInstance instance;
        try {
            instance = component.getCache().get(primaryKey);

            if (instance.isRemoved()) {
                component.getCache().release(instance, true);
                throw EjbLogger.ROOT_LOGGER.instanceWasRemoved(component.getComponentName(), primaryKey);
            }
        } catch (javax.ejb.NoSuchEntityException e) {
            //if this is a timer service invocation we throw a special exception
            //that tells the invoker that this timer no longer exists, and can be removed

            if (context.getPrivateData(InvocationType.class) == InvocationType.TIMER) {
                throw new BeanRemovedException(e);
            }
            throw e;
        }

        try {
            context.putPrivateData(ComponentInstance.class, instance);
            return context.proceed();
        } catch (Exception ex) {
            // Detect app exception
            if (component.getApplicationException(ex.getClass(), context.getMethod()) != null) {
                // it's an application exception, just throw it back.
                throw ex;
            }
            if (ex instanceof ConcurrentAccessTimeoutException || ex instanceof ConcurrentAccessException) {
                throw ex;
View Full Code Here

            // Initialize beanPOA and create metadata depending on the kind of bean
            if (component instanceof EntityBeanComponent) {

                // This is an entity bean (lifespan: persistent)
                beanServantRegistry = poaRegistry.getValue().getRegistryWithPersistentPOAPerServant();
                final EntityBeanComponent entityBeanComponent = (EntityBeanComponent) component;
                final Class pkClass = entityBeanComponent.getPrimaryKeyClass();
                ejbMetaData = new EJBMetaDataImplIIOP(entityBeanComponent.getRemoteClass(), entityBeanComponent.getHomeClass(), pkClass, false, false, homeHandle);
            } else {
                // This is a session bean (lifespan: transient)
                beanServantRegistry = poaRegistry.getValue().getRegistryWithTransientPOAPerServant();
                if (component instanceof StatelessSessionComponent) {
                    // Stateless session bean
View Full Code Here

        }
        final Component component = context.getPrivateData(Component.class);
        if (!(component instanceof EntityBeanComponent)) {
            throw EjbLogger.ROOT_LOGGER.unexpectedComponent(component, EntityBeanComponent.class);
        }
        final EntityBeanComponent entityBeanComponent = (EntityBeanComponent) component;
        //grab an unasociated entity bean from the pool
        final EntityBeanComponentInstance instance = entityBeanComponent.acquireUnAssociatedInstance();

        //call the ejbCreate method
        final Object primaryKey = invokeEjbCreate(context, ejbCreate, instance, params);
        instance.associate(primaryKey);
        clientInstance.setViewInstanceData(EntityBeanComponent.PRIMARY_KEY_CONTEXT_KEY, primaryKey);

        //now add the instance to the cache, so it is usable
        //note that we do not release it back to the pool
        //the cache will do that when it is expired or removed
        boolean synchronizationRegistered = false;
        boolean exception = false;
        final boolean instanceCachedBefore = entityBeanComponent.getCache().contains(primaryKey);
        entityBeanComponent.getCache().create(instance);

        try {

            invokeEjbPostCreate(context, ejbPostCreate, instance, params);

            //if a transaction is active we register a sync
            //and if the transaction is rolled back we release the instance back into the pool

            final TransactionSynchronizationRegistry transactionSynchronizationRegistry = entityBeanComponent.getTransactionSynchronizationRegistry();
            if (transactionSynchronizationRegistry.getTransactionKey() != null) {
                if (!instanceCachedBefore) {
                    transactionSynchronizationRegistry.registerInterposedSynchronization(new Synchronization() {
                        @Override
                        public void beforeCompletion() {

                        }

                        @Override
                        public void afterCompletion(final int status) {
                            entityBeanComponent.getCache().release(instance, status == Status.STATUS_COMMITTED);
                        }
                    });
                }
                synchronizationRegistered = true;
            }
            return context.proceed();
        } catch (Exception e) {
            entityBeanComponent.getCache().release(instance, false);
            exception = true;
            throw e;
        } finally {
            if (!synchronizationRegistered && !exception) {
                entityBeanComponent.getCache().release(instance, true);
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.jboss.as.ejb3.component.entity.EntityBeanComponent

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.