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;
 
 
 
 import  org.jboss.metadata.ejb.spec.PersistenceType;
 
 import static org.jboss.as.ejb3.EjbMessages.MESSAGES;

View configurator for the home interface of an entity bean

Author(s):
Stuart Douglas
 
 public class EntityBeanHomeViewConfigurator implements ViewConfigurator {
 
 
     @Override
     public void configure(final DeploymentPhaseContext contextfinal ComponentConfiguration componentConfigurationfinal ViewDescription descriptionfinal ViewConfiguration configurationthrows DeploymentUnitProcessingException {
         final DeploymentUnit deploymentUnit = context.getDeploymentUnit();
         final DeploymentReflectionIndex deploymentReflectionIndex = deploymentUnit.getAttachment(.);
 
         configuration.addClientPostConstructInterceptor(org.jboss.invocation.Interceptors.getTerminalInterceptorFactory(), ..);
         configuration.addClientPreDestroyInterceptor(org.jboss.invocation.Interceptors.getTerminalInterceptorFactory(), ..);
 
         final boolean localHome = EjbHomeViewDescription.class.cast(description).getMethodIntf() == .;
 
         final EntityBeanComponentDescription componentDescription = (EntityBeanComponentDescriptioncomponentConfiguration.getComponentDescription();
         final EJBViewDescription createdView = localHome ? componentDescription.getEjbLocalView() : componentDescription.getEjbRemoteView();
 
         final DeploymentClassIndex classIndex = deploymentUnit.getAttachment(.);
 
         for (final Method method : configuration.getProxyFactory().getCachedMethods()) {
 
 
             if (method.getName().equals("equals") && method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == Object.class) {
             } else if (method.getName().equals("hashCode") && method.getParameterTypes().length == 0) {
             } else if (method.getName().equals("toString") && method.getParameterTypes().length == 0) {
                 //TODO: toString
             } else if (method.getName().startsWith("create")) {
                 //we have a create method.
                 //lets resolve the corresponding ejbCreate method
                 final Method ejbCreate = resolveEjbMethod("create""ejbCreate"componentDescription.getPrimaryKeyType(), componentConfiguration.getComponentClass(), deploymentReflectionIndexmethodcomponentConfiguration.getComponentName());
                final Method ejbPostCreate = resolveEjbMethod("create""ejbPostCreate"void.class.getName(), componentConfiguration.getComponentClass(), deploymentReflectionIndexmethodcomponentConfiguration.getComponentName());
                final EntityBeanHomeCreateInterceptorFactory factory = new EntityBeanHomeCreateInterceptorFactory(ejbCreateejbPostCreate);
                //add a dependency on the view to create
                configuration.getDependencies().add(new DependencyConfigurator<ViewService>() {
                    @Override
                    public void configureDependency(final ServiceBuilder<?> serviceBuilderfinal ViewService servicethrows DeploymentUnitProcessingException {
                        serviceBuilder.addDependency(createdView.getServiceName(), ComponentView.classfactory.getViewToCreate());
                    }
                });
                //add the interceptor
                configuration.addViewInterceptor(methodfactory..);
            } else if (method.getName().startsWith("find")) {
                final Method ejbFind = resolveEjbFinderMethod(componentConfiguration.getComponentClass(), deploymentReflectionIndexmethodcomponentConfiguration.getComponentName(), componentDescription.getPersistenceType());
                if(!Modifier.isPublic(ejbFind.getModifiers())) {
                    throw ..ejbMethodMustBePublic("ejbFind"ejbFind);
                }
                final EntityBeanHomeFinderInterceptorFactory interceptorFactory = createHomeFindInterceptorFactory(ejbFindlocalHome);
                configuration.getDependencies().add(new DependencyConfigurator<ViewService>() {
                    @Override
                    public void configureDependency(final ServiceBuilder<?> serviceBuilderfinal ViewService servicethrows DeploymentUnitProcessingException {
                        serviceBuilder.addDependency(createdView.getServiceName(), ComponentView.classinterceptorFactory.getViewToCreate());
                    }
                });
                configuration.addViewInterceptor(methodinterceptorFactory..);
            } else if (method.getName().equals("remove") && method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == Object.class) {
                configuration.addViewInterceptor(method, EntityBeanHomeRemoveInterceptorFactory.instance(!localHome), ..);
            } else if (method.getName().equals("remove") && method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == Handle.class) {
            } else if (method.getName().equals("getEJBMetaData") && method.getParameterTypes().length == 0) {
                final Class<?> ejbObjectClass;
                final Class<?> pkClass;
                try {
                    ejbObjectClass = classIndex.classIndex(createdView.getViewClassName()).getModuleClass();
                    pkClass = classIndex.classIndex(componentDescription.getPrimaryKeyType()).getModuleClass();
                } catch (ClassNotFoundException e) {
                    throw ..failedToLoadViewClassForComponent(ecomponentDescription.getComponentName());
                }
                final EjbMetadataInterceptorFactory factory = new EjbMetadataInterceptorFactory(ejbObjectClassconfiguration.getViewClass(), pkClassfalsefalse);
                //add a dependency on the view to create
                componentConfiguration.getStartDependencies().add(new DependencyConfigurator<ComponentStartService>() {
                    @Override
                    public void configureDependency(final ServiceBuilder<?> serviceBuilderfinal ComponentStartService servicethrows DeploymentUnitProcessingException {
                        serviceBuilder.addDependency(configuration.getViewServiceName(), ComponentView.classfactory.getHomeView());
                    }
                });
                //add the interceptor
                configuration.addViewInterceptor(methodfactory..);
            } else if (method.getName().equals("getHomeHandle") && method.getParameterTypes().length == 0) {
                //handled elsewhere
            } else {
                //we have a home business method
                Method home = resolveEjbHomeBusinessMethod(componentConfiguration.getComponentClass(), deploymentReflectionIndexmethodcomponentConfiguration.getComponentName());
                if(!Modifier.isPublic(home.getModifiers())) {
                    throw ..ejbMethodMustBePublic("ejbHome"home);
                }
                configuration.addViewInterceptor(methodnew EntityBeanHomeMethodInterceptorFactory(home), ..);
            }
        }
    }
    protected EntityBeanHomeFinderInterceptorFactory createHomeFindInterceptorFactory(final Method ejbFindfinal boolean localHome) {
        return new EntityBeanHomeFinderInterceptorFactory(ejbFind);
    }
    private Method resolveEjbMethod(final String userNamefinal String ejbMethodNamefinal String returnTypefinal Class<?> componentClassfinal DeploymentReflectionIndex indexfinal Method methodfinal String ejbNamethrows DeploymentUnitProcessingException {
        final String name = method.getName().replaceFirst(userNameejbMethodName);
        Class<?> clazz = componentClass;
        while (clazz != Object.class) {
            final ClassReflectionIndex<?> classIndex = index.getClassIndex(clazz);
            Method ret = classIndex.getMethod(returnTypenamenamesOf(method.getParameterTypes()));
            if (ret != null) {
                return ret;
            }
            clazz = clazz.getSuperclass();
        }
        throw .failToResolveMethodForHomeInterface(ejbMethodNamemethodejbName);
    }
    protected Method resolveEjbFinderMethod(final Class<?> componentClassfinal DeploymentReflectionIndex indexfinal Method methodfinal String ejbNamefinal PersistenceType persistenceTypethrows DeploymentUnitProcessingException {
        final String name = method.getName().replaceFirst("find""ejbFind");
        Class<?> clazz = componentClass;
        while (clazz != Object.class) {
            final ClassReflectionIndex<?> classIndex = index.getClassIndex(clazz);
            Collection<Methodmethods = classIndex.getMethods(namemethod.getParameterTypes());
            if (!methods.isEmpty()) {
                return methods.iterator().next();
            }
            clazz = clazz.getSuperclass();
        }
        if (PersistenceType.Bean == persistenceType) {
            throw .failToResolveMethodForHomeInterface("ejbFind"methodejbName);
        }
        return method;
    }
    protected Method resolveEjbHomeBusinessMethod(final Class<?> componentClassfinal DeploymentReflectionIndex indexfinal Method methodfinal String ejbNamethrows DeploymentUnitProcessingException {
        final String name = "ejbHome" + Character.toUpperCase(method.getName().charAt(0)) + method.getName().substring(1);
        Class<?> clazz = componentClass;
        while (clazz != Object.class) {
            final ClassReflectionIndex<?> classIndex = index.getClassIndex(clazz);
            Collection<Methodmethods = classIndex.getMethods(namemethod.getParameterTypes());
            if (!methods.isEmpty()) {
                return methods.iterator().next();
            }
            clazz = clazz.getSuperclass();
        }
        throw .failToResolveMethodForHomeInterface("ejbHome"methodejbName);
    }
    private static String[] namesOf(final Class<?>[] types) {
        final String[] strings = new String[types.length];
        for (int i = 0, typesLength = types.lengthi < typesLengthi++) {
            strings[i] = types[i].getName();
        }
        return strings;
    }
New to GrepCode? Check out our FAQ X