Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.ejb.util » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    * 
    9    *       http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.webbeans.ejb.util;
   18   
   19   import java.lang.annotation.Annotation;
   20   import java.lang.reflect.Method;
   21   import java.util.HashMap;
   22   import java.util.List;
   23   import java.util.Map;
   24   import java.util.Set;
   25   
   26   import javax.enterprise.inject.spi.AnnotatedField;
   27   import javax.enterprise.inject.spi.AnnotatedMethod;
   28   import javax.enterprise.inject.spi.AnnotatedType;
   29   import javax.enterprise.inject.spi.Bean;
   30   import javax.enterprise.inject.spi.Producer;
   31   
   32   import org.apache.openejb.DeploymentInfo;
   33   import org.apache.webbeans.component.ProducerFieldBean;
   34   import org.apache.webbeans.component.ProducerMethodBean;
   35   import org.apache.webbeans.component.creation.BeanCreator.MetaDataProvider;
   36   import org.apache.webbeans.container.BeanManagerImpl;
   37   import org.apache.webbeans.ejb.component.EjbBean;
   38   import org.apache.webbeans.ejb.component.creation.EjbBeanCreatorImpl;
   39   import org.apache.webbeans.exception.WebBeansConfigurationException;
   40   import org.apache.webbeans.exception.WebBeansException;
   41   import org.apache.webbeans.portable.AnnotatedElementFactory;
   42   import org.apache.webbeans.portable.events.ProcessAnnotatedTypeImpl;
   43   import org.apache.webbeans.portable.events.ProcessInjectionTargetImpl;
   44   import org.apache.webbeans.portable.events.ProcessProducerImpl;
   45   import org.apache.webbeans.portable.events.ProcessSessionBeanImpl;
   46   import org.apache.webbeans.portable.events.generics.GProcessSessionBean;
   47   import org.apache.webbeans.util.ClassUtil;
   48   import org.apache.webbeans.util.WebBeansUtil;
   49   
   50   @SuppressWarnings("unchecked")
   51   public final class EjbUtility
   52   {
   53       private EjbUtility()
   54       {
   55           
   56       }
   57           
   58       public static <T> void fireEvents(Class<T> clazz, EjbBean<T> ejbBean)
   59       {
   60           AnnotatedType<T> annotatedType = AnnotatedElementFactory.newAnnotatedType(clazz);
   61           
   62           //Fires ProcessAnnotatedType
   63           ProcessAnnotatedTypeImpl<T> processAnnotatedEvent = WebBeansUtil.fireProcessAnnotatedTypeEvent(annotatedType);             
   64           EjbBeanCreatorImpl<T> ejbBeanCreator = new EjbBeanCreatorImpl<T>(ejbBean);
   65           ejbBeanCreator.checkCreateConditions();
   66           
   67           if(processAnnotatedEvent.isVeto())
   68           {
   69               return;
   70           }
   71           
   72           if(processAnnotatedEvent.isSet())
   73           {
   74               ejbBeanCreator.setMetaDataProvider(MetaDataProvider.THIRDPARTY);
   75           }
   76           
   77           //Define meta-data
   78           ejbBeanCreator.defineSerializable();
   79           ejbBeanCreator.defineStereoTypes();
   80           Class<? extends Annotation> deploymentType = ejbBeanCreator.defineDeploymentType("There are more than one @DeploymentType annotation in Session Bean implementation class : " + ejbBean.getReturnType().getName());
   81           ejbBeanCreator.defineApiType();
   82           ejbBeanCreator.defineScopeType("Session Bean implementation class : " + clazz.getName() + " stereotypes must declare same @ScopeType annotations");
   83           ejbBeanCreator.defineQualifier();
   84           ejbBeanCreator.defineName(WebBeansUtil.getSimpleWebBeanDefaultName(clazz.getSimpleName()));            
   85           Set<ProducerMethodBean<?>> producerMethodBeans = ejbBeanCreator.defineProducerMethods();        
   86           checkProducerMethods(producerMethodBeans, ejbBean);
   87           Set<ProducerFieldBean<?>> producerFieldBeans = ejbBeanCreator.defineProducerFields();           
   88           ejbBeanCreator.defineDisposalMethods();
   89           ejbBeanCreator.defineInjectedFields();
   90           ejbBeanCreator.defineInjectedMethods();
   91           ejbBeanCreator.defineObserverMethods();        
   92           
   93           //Fires ProcessInjectionTarget
   94           ProcessInjectionTargetImpl<T> processInjectionTargetEvent = WebBeansUtil.fireProcessInjectionTargetEvent(ejbBean);            
   95           if(processInjectionTargetEvent.isSet())
   96           {
   97               ejbBeanCreator.setInjectedTarget(processInjectionTargetEvent.getInjectionTarget());
   98           }
   99           
  100           Map<ProducerMethodBean<?>,AnnotatedMethod<?>> annotatedMethods = new HashMap<ProducerMethodBean<?>, AnnotatedMethod<?>>(); 
  101           for(ProducerMethodBean<?> producerMethod : producerMethodBeans)
  102           {
  103               AnnotatedMethod<?> method = AnnotatedElementFactory.newAnnotatedMethod(producerMethod.getCreatorMethod(), producerMethod.getParent().getReturnType());
  104               ProcessProducerImpl<?, ?> producerEvent = WebBeansUtil.fireProcessProducerEventForMethod(producerMethod,method);
  105   
  106               annotatedMethods.put(producerMethod, method);
  107               
  108               if(producerEvent.isProducerSet())
  109               {
  110                   producerMethod.setProducer((Producer) ejbBeanCreator.getProducer());
  111               }
  112               
  113               producerEvent.setProducerSet(false);
  114           }
  115           
  116           Map<ProducerFieldBean<?>,AnnotatedField<?>> annotatedFields = new HashMap<ProducerFieldBean<?>, AnnotatedField<?>>();
  117           for(ProducerFieldBean<?> producerField : producerFieldBeans)
  118           {
  119               AnnotatedField<?> field = AnnotatedElementFactory.newAnnotatedField(producerField.getCreatorField(), producerField.getParent().getReturnType());
  120               ProcessProducerImpl<?, ?> producerEvent = WebBeansUtil.fireProcessProducerEventForField(producerField, field);
  121               
  122               annotatedFields.put(producerField, field);
  123               
  124               if(producerEvent.isProducerSet())
  125               {
  126                   producerField.setProducer((Producer) ejbBeanCreator.getProducer());
  127               }
  128               
  129               producerEvent.setProducerSet(false);
  130           }
  131   
  132           //Fires ProcessManagedBean
  133           ProcessSessionBeanImpl<T> processBeanEvent = new GProcessSessionBean((Bean<Object>)ejbBean,annotatedType,ejbBean.getEjbName(),ejbBean.getEjbType());            
  134           BeanManagerImpl.getManager().fireEvent(processBeanEvent, new Annotation[0]);
  135           
  136           
  137           //Fires ProcessProducerMethod
  138           WebBeansUtil.fireProcessProducerMethodBeanEvent(annotatedMethods);
  139           
  140           //Fires ProcessProducerField
  141           WebBeansUtil.fireProcessProducerFieldBeanEvent(annotatedFields);
  142                   
  143           //Set InjectionTarget that is used by the container to inject dependencies!
  144           if(ejbBeanCreator.isInjectionTargetSet())
  145           {
  146               ejbBean.setInjectionTarget(ejbBeanCreator.getInjectedTarget());   
  147           }
  148           
  149           // Check if the deployment type is enabled.
  150           if (WebBeansUtil.isDeploymentTypeEnabled(deploymentType))
  151           {                                
  152               BeanManagerImpl.getManager().addBean(ejbBean);
  153               BeanManagerImpl.getManager().getBeans().addAll(producerMethodBeans);
  154               BeanManagerImpl.getManager().getBeans().addAll(producerFieldBeans);
  155           }        
  156       }
  157       
  158       private static void checkProducerMethods(Set<ProducerMethodBean<?>> producerMethodBeans, EjbBean<?> bean)
  159       {
  160           for(ProducerMethodBean<?> producerMethodBean : producerMethodBeans)
  161           {
  162               Method producerMethod = producerMethodBean.getCreatorMethod();
  163               if(!ClassUtil.isStatic(producerMethod.getModifiers()))
  164               {
  165                   if(!isBusinessMethod(producerMethod, bean))
  166                   {
  167                       throw new WebBeansConfigurationException("Producer Method Bean must be business method of session bean : " + bean);
  168                   }
  169               }
  170           }        
  171       }
  172       
  173       public static boolean isBusinessMethod(Method method, EjbBean<?> bean)
  174       {
  175           DeploymentInfo info = bean.getDeploymentInfo();
  176           List<Class> businessLocals = info.getBusinessLocalInterfaces();
  177           for(Class clz : businessLocals)
  178           {
  179               try
  180               {
  181                   clz.getMethod(method.getName(), method.getParameterTypes());
  182                   
  183                   return true;
  184               }
  185               catch (SecurityException e)
  186               {
  187                   throw new WebBeansException("Security exception",e);
  188               }
  189               catch (NoSuchMethodException e)
  190               {
  191                   continue;
  192               }
  193           }
  194           
  195           return false;
  196       }
  197   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.ejb.util » [javadoc | source]