Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2010, Red Hat Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.as.ejb3.component.entity.interceptors;
 
 
 
 import  org.jboss.invocation.Interceptor;
 import  org.jboss.invocation.InterceptorContext;
 import  org.jboss.invocation.InterceptorFactory;
 import  org.jboss.invocation.InterceptorFactoryContext;
 import  org.jboss.invocation.Interceptors;
 
 import static org.jboss.as.ejb3.EjbMessages.MESSAGES;

Interceptor factory for entity beans that class the corresponding ejbCreate method.

This is a post construct interceptor for the Ejb(Local)Object view

Author(s):
Stuart Douglas
 
 public class EntityBeanEjbCreateMethodInterceptorFactory implements InterceptorFactory {
 
 
     }
 
     @Override
     public Interceptor create(InterceptorFactoryContext context) {
         final Object existing = context.getContextData().get(.);
 
         final AtomicReference<ObjectprimaryKeyReference = new AtomicReference<Object>();
         context.getContextData().put(.primaryKeyReference);
 
         final Method ejbCreate = (Methodcontext.getContextData().get(.);
         final Method ejbPostCreate = (Methodcontext.getContextData().get(.);
         final Object[] params = (Object[]) context.getContextData().get(.);
 
         return new Interceptor() {
             @Override
             public Object processInvocation(final InterceptorContext contextthrows Exception {
 
                 if (existing != null) {
                     primaryKeyReference.set(existing);
                     return existing;
                 }
                 final Component component = context.getPrivateData(Component.class);
                 if (!(component instanceof EntityBeanComponent)) {
                     throw .unexpectedComponent(componentEntityBeanComponent.class);
                 }
                 final EntityBeanComponent entityBeanComponent = (EntityBeanComponentcomponent;
                 //grab an unasociated entity bean from the pool
                 final EntityBeanComponentInstance instance = entityBeanComponent.acquireUnAssociatedInstance();
 
                 //call the ejbCreate method
                 final Object primaryKey = invokeEjbCreate(contextejbCreateinstanceparams);
                 instance.associate(primaryKey);
                 primaryKeyReference.set(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;
                 entityBeanComponent.getCache().create(instance);
 
                 try {
 
                    invokeEjbPostCreate(contextejbPostCreateinstanceparams);
                    //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) {
                        transactionSynchronizationRegistry.registerInterposedSynchronization(new Synchronization() {
                            @Override
                            public void beforeCompletion() {
                            }
                            @Override
                            public void afterCompletion(final int status) {
                                entityBeanComponent.getCache().release(instancestatus == .);
                            }
                        });
                        synchronizationRegistered = true;
                    }
                    return context.proceed();
                } catch (Exception e) {
                    entityBeanComponent.getCache().release(instancefalse);
                    exception = true;
                    throw e;
                } finally {
                    if (!synchronizationRegistered && !exception) {
                        entityBeanComponent.getCache().release(instancetrue);
                    }
                }
            }
        };
    }
    protected void invokeEjbPostCreate(final InterceptorContext contextfinal Method ejbPostCreatefinal EntityBeanComponentInstance instancefinal Object[] paramsthrows Exception {
        try {
            ejbPostCreate.invoke(instance.getInstance(), params);
        } catch (InvocationTargetException e) {
            throw Interceptors.rethrow(e.getCause());
        }
    }
    protected Object invokeEjbCreate(final InterceptorContext contextfinal Method ejbCreatefinal EntityBeanComponentInstance instancefinal Object[] paramsthrows Exception {
        final InvocationType invocationType = context.getPrivateData(InvocationType.class);
        try {
            context.putPrivateData(InvocationType.class.);
            return ejbCreate.invoke(instance.getInstance(), params);
        } catch (InvocationTargetException e) {
            throw Interceptors.rethrow(e.getCause());
        } finally {
            context.putPrivateData(InvocationType.classinvocationType);
        }
    }
New to GrepCode? Check out our FAQ X