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 java.util.Set;
 
 import  org.jboss.invocation.Interceptor;
 import  org.jboss.invocation.InterceptorContext;
 import  org.jboss.invocation.InterceptorFactory;
 import  org.jboss.invocation.InterceptorFactoryContext;
 
 import static org.jboss.as.ejb3.EjbMessages.MESSAGES;

Interceptor that hooks up finder methods for BMP entity beans

This is a view level interceptor that should be attached to finder methods on the home interface.

Author(s):
Stuart Douglas
 
 public class EntityBeanHomeFinderInterceptorFactory implements InterceptorFactory {
 
     protected enum ReturnType {
         COLLECTION,
         ENUMERATION,
         SINGLE
     }
 
     private final Method finderMethod;
     private final ReturnType returnType;
     private final InjectedValue<ComponentViewviewToCreate = new InjectedValue<ComponentView>();
 
     public EntityBeanHomeFinderInterceptorFactory(final Method finderMethod) {
         this. = finderMethod;
         if (Collection.class.isAssignableFrom(finderMethod.getReturnType())) {
              = .;
         } else if (Enumeration.class.isAssignableFrom(finderMethod.getReturnType())) {
              = .;
         } else {
              = .;
         }
     }
 
     @Override
     public Interceptor create(final InterceptorFactoryContext context) {
 
         final EntityBeanComponent component = (EntityBeanComponentcontext.getContextData().get(Component.class);
 
         return new Interceptor() {
             @Override
             public Object processInvocation(final InterceptorContext contextthrows 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.);
                     result = invokeFind(contextinstance);
                     return prepareResults(contextresultcomponent);
                 } finally {
                     component.releaseEntityBeanInstance(instance);
                     context.putPrivateData(InvocationType.classinvocationType);
                 }
             }
 
        };
    }
    protected Object prepareResults(final InterceptorContext contextfinal Object resultfinal EntityBeanComponent componentthrows Exception {
        switch () {
            case : {
                Collection keys = (Collectionresult;
                final Set<Objectresults = new HashSet<Object>();
                if (keys != null) {
                    for (Object key : keys) {
                        results.add(getLocalObject(key));
                    }
                }
                return results;
            }
            case : {
                Enumeration keys = (Enumerationresult;
                final Set<Objectresults = new HashSet<Object>();
                if (keys != null) {
                    while (keys.hasMoreElements()) {
                        Object key = keys.nextElement();
                        results.add(getLocalObject(key));
                    }
                }
                final Iterator<Objectiterator = results.iterator();
                return new Enumeration<Object>() {
                    @Override
                    public boolean hasMoreElements() {
                        return iterator.hasNext();
                    }
                    @Override
                    public Object nextElement() {
                        return iterator.next();
                    }
                };
            }
            default: {
                if (result == null) {
                    throw .couldNotFindEntity(, Arrays.toString(context.getParameters()));
                }
                return getLocalObject(result);
            }
        }
    }
    protected Object invokeFind(final InterceptorContext contextfinal EntityBeanComponentInstance instancethrows Exception {
        Method oldMethod = context.getMethod();
        try {
            context.putPrivateData(ComponentInstance.classinstance);
            context.setMethod();
            context.setTarget(instance.getInstance());
            return instance.getInterceptor().processInvocation(context);
        } finally {
            context.setMethod(oldMethod);
            context.setTarget(null);
            context.putPrivateData(ComponentInstance.classnull);
        }
    }
    protected Object getLocalObject(final Object resultthrows Exception {
        final HashMap<ObjectObjectcreate = new HashMap<ObjectObject>();
        create.put(.result);
        return .getValue().createInstance(create).getInstance();
    }
        return ;
    }
    public ReturnType getReturnType() {
        return ;
    }
New to GrepCode? Check out our FAQ X