Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.component » [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 this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.component;
   15   
   16   import java.lang.reflect.Field;
   17   import java.lang.reflect.Method;
   18   import java.util.HashSet;
   19   import java.util.Set;
   20   
   21   import javax.decorator.Decorates;
   22   import javax.enterprise.context.spi.CreationalContext;
   23   import javax.enterprise.inject.spi.InjectionPoint;
   24   import javax.enterprise.inject.spi.InjectionTarget;
   25   
   26   import org.apache.webbeans.config.DefinitionUtil;
   27   import org.apache.webbeans.context.creational.CreationalContextImpl;
   28   import org.apache.webbeans.exception.WebBeansException;
   29   import org.apache.webbeans.inject.InjectableField;
   30   import org.apache.webbeans.inject.InjectableMethods;
   31   import org.apache.webbeans.intercept.InterceptorType;
   32   import org.apache.webbeans.intercept.InvocationContextImpl;
   33   import org.apache.webbeans.logger.WebBeansLogger;
   34   import org.apache.webbeans.plugins.OpenWebBeansResourcePlugin;
   35   import org.apache.webbeans.plugins.PluginLoader;
   36   import org.apache.webbeans.util.WebBeansUtil;
   37   
   38   /**
   39    * Abstract class for owning observer methods.
   40    * 
   41    * @version $Rev: 809213 $ $Date: 2009-08-30 01:30:54 +0300 (Sun, 30 Aug 2009) $
   42    * @param <T> bean class
   43    */
   44   public abstract class AbstractInjectionTargetBean<T> extends AbstractBean<T> implements InjectionTargetBean<T>
   45   {
   46       /** Logger instance */
   47       private final WebBeansLogger logger = WebBeansLogger.getLogger(getClass());
   48   
   49       /** Bean observable method */
   50       private Set<Method> observableMethods = new HashSet<Method>();
   51   
   52       /** Injected fields of the component */
   53       private Set<Field> injectedFields = new HashSet<Field>();
   54   
   55       /** Injected methods of the component */
   56       private Set<Method> injectedMethods = new HashSet<Method>();
   57   
   58       /** @deprecated */
   59       /* Removed from specification */
   60       protected boolean fromRealizes;
   61   
   62       /**
   63        * InjectionTargt instance. If this is not null, it is used for creating
   64        * instance.
   65        * 
   66        * @see InjectionTarget
   67        */
   68       protected InjectionTarget<T> injectionTarget;
   69   
   70       /**
   71        * Creates a new observer owner component.
   72        * 
   73        * @param webBeansType webbean type
   74        * @param returnType bean class type
   75        */
   76       protected AbstractInjectionTargetBean(WebBeansType webBeansType, Class<T> returnType)
   77       {
   78           super(webBeansType, returnType);
   79       }
   80   
   81       /**
   82        * {@inheritDoc}
   83        */
   84       protected T createInstance(CreationalContext<T> creationalContext)
   85       {
   86           T instance = null;
   87   
   88           //If injection target is set by Exntesion Observer, use it
   89           if (isInjectionTargetSet())
   90           {
   91               //Create instance
   92               instance = getInjectionTarget().produce(creationalContext);
   93               
   94               //Injection Operation
   95               getInjectionTarget().inject(instance, creationalContext);
   96               
   97               //Call @PostConstrcut
   98               postConstruct(instance);
   99           }
  100           //Default operations
  101           else
  102           {
  103               //Default creation phases
  104               instance = createDefaultInstance(creationalContext);
  105           }
  106   
  107           return instance;
  108       }
  109   
  110       /**
  111        * Returns bean instance.
  112        * 
  113        * @param creationalContext creational context
  114        * @return bean instance
  115        */
  116       protected T createDefaultInstance(CreationalContext<T> creationalContext)
  117       {
  118           beforeConstructor();
  119   
  120           T instance = createComponentInstance(creationalContext);
  121   
  122           afterConstructor(instance, creationalContext);
  123   
  124           return instance;
  125       }
  126   
  127       /**
  128        * {@inheritDoc}
  129        */
  130       protected void destroyInstance(T instance)
  131       {
  132           destroyComponentInstance(instance);
  133       }
  134   
  135       /**
  136        * Sub-classes must override this method to create bean instance.
  137        * 
  138        * @param creationalContext creational context
  139        * @return bean instance
  140        */
  141       abstract protected T createComponentInstance(CreationalContext<T> creationalContext);
  142   
  143       /**
  144        * Sub-classes must override this method to destroy bean instance.
  145        * 
  146        * @param instance object instance.
  147        */
  148       protected void destroyComponentInstance(T instance)
  149       {
  150           preDestroy(instance);
  151   
  152           // Remove it from creational context, if any
  153           CreationalContextImpl<T> cc = (CreationalContextImpl<T>) this.creationalContext;
  154           cc.remove();
  155       }
  156   
  157       /**
  158        * Called before constructor.
  159        */
  160       protected void beforeConstructor()
  161       {
  162   
  163       }
  164   
  165       /**
  166        * Called after bean instance is created.
  167        * 
  168        * @param instance bean instance
  169        * @param creationalContext cretional context object
  170        */
  171       protected void afterConstructor(T instance, CreationalContext<T> creationalContext)
  172       {
  173           // Inject fields
  174           injectFields(instance, creationalContext);
  175   
  176           // Inject methods
  177           injectMethods(instance, creationalContext);
  178   
  179           // Interceptor and decorator stack
  180           if (getWebBeansType().equals(WebBeansType.MANAGED))
  181           {
  182               DefinitionUtil.defineSimpleWebBeanInterceptorStack(this);
  183               DefinitionUtil.defineWebBeanDecoratorStack(this, instance);
  184           }
  185   
  186           postConstruct(instance);
  187       }
  188   
  189       /**
  190        * {@inheritDoc}
  191        */
  192       public void postConstruct(T instance)
  193       {
  194           if (isInjectionTargetSet())
  195           {
  196               getInjectionTarget().postConstruct(instance);
  197           }
  198           else
  199           {
  200               postConstructDefault(instance);
  201           }
  202       }
  203   
  204       /**
  205        * Default post construct.
  206        * 
  207        * @param instance bean instance
  208        */
  209       protected void postConstructDefault(T instance)
  210       {
  211           if(getWebBeansType().equals(WebBeansType.MANAGED))
  212           {
  213               //Inject Resources
  214               OpenWebBeansResourcePlugin resourcePlugin = PluginLoader.getInstance().getResourcePlugin();
  215               if(resourcePlugin != null)
  216               {
  217                   resourcePlugin.injectResources(instance);
  218               }
  219               
  220               // Call Post Construct
  221               if (WebBeansUtil.isContainsInterceptorMethod(getInterceptorStack(), InterceptorType.POST_CONSTRUCT))
  222               {
  223                   InvocationContextImpl impl = new InvocationContextImpl(null, instance, null, null, WebBeansUtil.getInterceptorMethods(getInterceptorStack(), InterceptorType.POST_CONSTRUCT), InterceptorType.POST_CONSTRUCT);
  224                   try
  225                   {
  226                       impl.proceed();
  227                   }
  228   
  229                   catch (Exception e)
  230                   {
  231                       logger.error("Error is occured while executing @PostConstruct", e);
  232                       throw new WebBeansException(e);
  233                   }
  234               }            
  235           }        
  236       }
  237   
  238       /**
  239        * {@inheritDoc}
  240        */
  241       public void preDestroy(T instance)
  242       {
  243           if (isInjectionTargetSet())
  244           {
  245               getInjectionTarget().preDestroy(instance);
  246           }
  247           else
  248           {
  249               preDestroyDefault(instance);
  250           }
  251       }
  252   
  253       /**
  254        * Default predestroy.
  255        * 
  256        * @param instance bean instance
  257        */
  258       protected void preDestroyDefault(T instance)
  259       {
  260           if(getWebBeansType().equals(WebBeansType.MANAGED))
  261           {
  262               if (WebBeansUtil.isContainsInterceptorMethod(getInterceptorStack(), InterceptorType.PRE_DESTROY))
  263               {
  264                   InvocationContextImpl impl = new InvocationContextImpl(null, instance, null, null, WebBeansUtil.getInterceptorMethods(getInterceptorStack(), InterceptorType.PRE_DESTROY), InterceptorType.PRE_DESTROY);
  265                   try
  266                   {
  267                       impl.proceed();
  268                   }
  269                   catch (Exception e)
  270                   {
  271                       getLogger().error("Error is occ?red while executing @PreDestroy method", e);
  272                       throw new WebBeansException(e);
  273                   }
  274               }            
  275           }
  276       }
  277   
  278       /**
  279        * Injects fields of the bean after constructing.
  280        * 
  281        * @param instance bean instance
  282        * @param creationalContext creational context
  283        */
  284       public void injectFields(T instance, CreationalContext<T> creationalContext)
  285       {
  286           Set<Field> fields = getInjectedFields();
  287           for (Field field : fields)
  288           {
  289               if (field.getAnnotation(Decorates.class) == null)
  290               {
  291                   InjectableField f = new InjectableField(field, instance, this, creationalContext);
  292                   f.doInjection();
  293               }
  294           }
  295       }
  296   
  297       /**
  298        * Injects all {@link Initializer} methods of the bean instance.
  299        * 
  300        * @param instance bean instance
  301        * @param creationalContext creational context instance
  302        */
  303       @SuppressWarnings("unchecked")
  304       public void injectMethods(T instance, CreationalContext<T> creationalContext)
  305       {
  306           Set<Method> methods = getInjectedMethods();
  307   
  308           for (Method method : methods)
  309           {
  310               InjectableMethods m = new InjectableMethods(method, instance, this, creationalContext);
  311               m.doInjection();
  312           }
  313       }
  314   
  315       /**
  316        * {@inheritDoc}
  317        */
  318       public void injectResources(T instance, CreationalContext<T> creationalContext)
  319       {
  320           // TODO Java EE injections
  321       }
  322   
  323       /**
  324        * {@inheritDoc}
  325        */
  326       public void addObservableMethod(Method observerMethod)
  327       {
  328           this.observableMethods.add(observerMethod);
  329       }
  330   
  331       /**
  332        * {@inheritDoc}
  333        */
  334       public Set<Method> getObservableMethods()
  335       {
  336           return this.observableMethods;
  337       }
  338   
  339       /**
  340        * @return the fromRealizes
  341        * @deprecated
  342        */
  343       public boolean isFromRealizes()
  344       {
  345           return fromRealizes;
  346       }
  347   
  348       /**
  349        * @param fromRealizes the fromRealizes to set
  350        * @deprecated
  351        */
  352       public void setFromRealizes(boolean fromRealizes)
  353       {
  354           this.fromRealizes = fromRealizes;
  355       }
  356   
  357       /**
  358        * Gets injected fields.
  359        * 
  360        * @return injected fields
  361        */
  362       public Set<Field> getInjectedFields()
  363       {
  364           return this.injectedFields;
  365       }
  366   
  367       /**
  368        * Add new injected field.
  369        * 
  370        * @param field new injected field
  371        */
  372       public void addInjectedField(Field field)
  373       {
  374           this.injectedFields.add(field);
  375       }
  376   
  377       /**
  378        * Gets injected methods.
  379        * 
  380        * @return injected methods
  381        */
  382       public Set<Method> getInjectedMethods()
  383       {
  384           return this.injectedMethods;
  385       }
  386   
  387       /**
  388        * Add new injected method.
  389        * 
  390        * @param field new injected method
  391        */
  392       public void addInjectedMethod(Method method)
  393       {
  394           this.injectedMethods.add(method);
  395       }
  396   
  397       /**
  398        * Sets injection target instance.
  399        * 
  400        * @param injectionTarget injection target instance
  401        */
  402       public void setInjectionTarget(InjectionTarget<T> injectionTarget)
  403       {
  404           this.injectionTarget = injectionTarget;
  405       }
  406   
  407       /**
  408        * Returns injection target.
  409        * 
  410        * @return injection target
  411        */
  412       public InjectionTarget<T> getInjectionTarget()
  413       {
  414           return this.injectionTarget;
  415       }
  416   
  417       /**
  418        * Returns true if injection target instance set, false otherwise.
  419        * 
  420        * @return true if injection target instance set, false otherwise
  421        */
  422       protected boolean isInjectionTargetSet()
  423       {
  424           return this.injectionTarget != null ? true : false;
  425       }
  426   
  427       /**
  428        * Returns bean logger instance.
  429        * 
  430        * @return logger
  431        */
  432       protected WebBeansLogger getLogger()
  433       {
  434           return this.logger;
  435       }
  436   
  437       /**
  438        * {@inheritDoc}
  439        */
  440       @Override
  441       public Set<InjectionPoint> getInjectionPoints()
  442       {
  443           if (isInjectionTargetSet())
  444           {
  445               return getInjectionTarget().getInjectionPoints();
  446           }
  447   
  448           return super.getInjectionPoints();
  449       }
  450   
  451   }

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