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.annotation.Annotation;
   17   import java.lang.reflect.Member;
   18   import java.lang.reflect.ParameterizedType;
   19   import java.lang.reflect.Type;
   20   import java.util.ArrayList;
   21   import java.util.HashSet;
   22   import java.util.List;
   23   import java.util.Set;
   24   
   25   import javax.enterprise.context.spi.CreationalContext;
   26   import javax.enterprise.inject.CreationException;
   27   import javax.enterprise.inject.spi.Bean;
   28   import javax.enterprise.inject.spi.InjectionPoint;
   29   
   30   import org.apache.webbeans.config.inheritance.BeanInheritedMetaData;
   31   import org.apache.webbeans.config.inheritance.IBeanInheritedMetaData;
   32   import org.apache.webbeans.container.BeanManagerImpl;
   33   import org.apache.webbeans.context.creational.CreationalContextFactory;
   34   import org.apache.webbeans.context.creational.CreationalContextImpl;
   35   import org.apache.webbeans.deployment.DeploymentTypeManager;
   36   import org.apache.webbeans.inject.AlternativesManager;
   37   import org.apache.webbeans.intercept.InterceptorData;
   38   import org.apache.webbeans.logger.WebBeansLogger;
   39   import org.apache.webbeans.util.ClassUtil;
   40   
   41   /**
   42    * Abstract implementation of the {@link BaseBean} contract. 
   43    * 
   44    * @version $Rev: 811507 $ $Date: 2009-09-04 21:18:41 +0300 (Fri, 04 Sep 2009) $
   45    * 
   46    * @see BaseBean
   47    * @see Bean
   48    */
   49   public abstract class AbstractBean<T> extends BaseBean<T>
   50   {
   51       /**Logger instance*/
   52       private final WebBeansLogger logger = WebBeansLogger.getLogger(getClass());
   53       
   54       /** Name of the bean */
   55       protected String name;
   56   
   57       /** Deployment type of the bean */
   58       protected Annotation type;
   59   
   60       /** Scope type of the bean */
   61       protected Annotation implScopeType;
   62   
   63       /** Qualifiers of the bean */
   64       protected Set<Annotation> implQualifiers = new HashSet<Annotation>();
   65   
   66       /** Api types of the bean */
   67       protected Set<Type> apiTypes = new HashSet<Type>();
   68   
   69       /** Web Beans type */
   70       protected WebBeansType webBeansType;
   71   
   72       /** Return type of the bean */
   73       protected Class<T> returnType;
   74   
   75       /** Stereotypes of the bean */
   76       protected Set<Annotation> stereoTypes = new HashSet<Annotation>();
   77       
   78       /**This bean is specialized or not*/
   79       protected boolean specializedBean;
   80   
   81       /**
   82        * Holds the all of the interceptor related data, contains around-invoke,
   83        * post-construct and pre-destroy
   84        */
   85       protected List<InterceptorData> interceptorStack = new ArrayList<InterceptorData>();
   86   
   87       /** Holds decorator stack */
   88       protected List<Object> decoratorStack = new ArrayList<Object>();
   89   
   90       /** The bean is serializable or not */
   91       protected boolean serializable;
   92   
   93       /** The bean allows nullable object */
   94       protected boolean nullable = true;
   95       
   96       /**Beans injection points*/
   97       protected Set<InjectionPoint> injectionPoints = new HashSet<InjectionPoint>();
   98       
   99       /**Bean inherited meta data*/
  100       protected IBeanInheritedMetaData inheritedMetaData;
  101       
  102       /**Tracks dependent injection point owner, can be null*/
  103       protected InjectionPoint dependentOwnerInjectionPoint;
  104       
  105       /**Creational context*/
  106       protected CreationalContext<T> creationalContext = null;
  107   
  108       /**
  109        * Constructor definiton. Each subclass redefines its own constructor with
  110        * calling this.
  111        * 
  112        * @param name name of the bean
  113        * @param webBeansType web beans type
  114        */
  115       protected AbstractBean(WebBeansType webBeansType, Class<T> returnType)
  116       {
  117           super(BeanManagerImpl.getManager());
  118           this.webBeansType = webBeansType;
  119           this.returnType = returnType;
  120       }
  121       
  122       /**
  123        * Creates a new instance.
  124        * 
  125        * @param webBeanType beans type
  126        */
  127       protected AbstractBean(WebBeansType webBeanType)
  128       {
  129           super(BeanManagerImpl.getManager());
  130           this.webBeansType = webBeanType;
  131           
  132       }
  133       
  134       /**
  135        * {@inheritDoc}
  136        */
  137       public IBeanInheritedMetaData getInheritedMetaData()
  138       {
  139           return this.inheritedMetaData;
  140       }
  141       
  142       /**
  143        * Sets inherited meta data.
  144        */
  145       protected void setInheritedMetaData()
  146       {
  147           this.inheritedMetaData = new BeanInheritedMetaData<T>(this);
  148       }
  149   
  150       /**
  151        * {@inheritDoc}
  152        */
  153       public T create(CreationalContext<T> creationalContext)
  154       {
  155           T instance = null;
  156           try
  157           {
  158               this.creationalContext = creationalContext;
  159               instance = createInstance(this.creationalContext);
  160   
  161           }
  162           catch (Exception re)
  163           {
  164               Throwable throwable = ClassUtil.getRootException(re);
  165               
  166               if(throwable instanceof RuntimeException)
  167               {
  168                   RuntimeException rt = (RuntimeException)throwable;
  169                   
  170                   throw rt;
  171               }
  172               else
  173               {
  174                   throw new CreationException(throwable);
  175               }
  176               
  177           }
  178   
  179           return instance;
  180       }
  181   
  182       /**
  183        * Creates the instance of the bean that has a specific implementation
  184        * type. Each subclass must define its own create mechanism.
  185        * 
  186        * @return instance of the bean
  187        */
  188       protected abstract T createInstance(CreationalContext<T> creationalContext);
  189   
  190       /*
  191        * (non-Javadoc)
  192        * @see javax.webbeans.bean.Component#destroy(java.lang.Object)
  193        */
  194       public void destroy(T instance, CreationalContext<T> creationalContext)
  195       {
  196           try
  197           {
  198               //Destory dependent instances
  199               this.creationalContext.release();
  200               
  201               //Destroy instance, call @PreDestroy
  202               destroyInstance(instance);
  203                           
  204               //Clear Decorator and Interceptor Stack
  205               this.decoratorStack.clear();
  206               this.interceptorStack.clear();
  207               
  208               //Reset it
  209               this.dependentOwnerInjectionPoint = null;  
  210               
  211           }catch(Exception e)
  212           {
  213               logger.fatal("Exception is thrown while destroying bean instance : " + toString());
  214               e.printStackTrace();
  215           }
  216       }
  217   
  218       /**
  219        * Destroy the instance of the bean. Each subclass must define its own
  220        * destroy mechanism.
  221        * 
  222        * @param instance instance of the bean that is being destroyed
  223        */
  224       protected void destroyInstance(T instance)
  225       {
  226           
  227       }
  228   
  229       /**
  230        * Get bean type.
  231        * 
  232        * @return bean type
  233        */
  234       public Annotation getType()
  235       {
  236           return type;
  237       }
  238       
  239       
  240       /**
  241        * Get return types of the bean.
  242        */
  243       public Class<?> getBeanClass()
  244       {
  245           if(IBeanHasParent.class.isAssignableFrom(getClass()))
  246           {
  247               @SuppressWarnings("unchecked")
  248               IBeanHasParent<T> comp = (IBeanHasParent<T>)this;
  249               
  250               return comp.getParent().getBeanClass();
  251           }
  252           
  253           return getReturnType();
  254       }
  255   
  256       /**
  257        * Set bean type.
  258        * 
  259        * @param type bean type
  260        */
  261       public void setType(Annotation type)
  262       {
  263           this.type = type;
  264       }
  265   
  266       /**
  267        * Get scope type.
  268        * 
  269        * @return scope type
  270        */
  271       public Annotation getImplScopeType()
  272       {
  273           return implScopeType;
  274       }
  275   
  276       /**
  277        * Set scope type.
  278        * 
  279        * @param scopeType scope type
  280        */
  281       public void setImplScopeType(Annotation scopeType)
  282       {
  283           this.implScopeType = scopeType;
  284       }
  285   
  286       /**
  287        * Name of the bean.
  288        * 
  289        * @return name of the bean
  290        */
  291       public String getName()
  292       {
  293           return name;
  294       }
  295   
  296       /**
  297        * Get web bean type of the bean.
  298        * 
  299        * @return web beans type
  300        */
  301       public WebBeansType getWebBeansType()
  302       {
  303           return webBeansType;
  304       }
  305   
  306       /**
  307        * Add new stereotype.
  308        *
  309        * @param stereoType new stereotype annotation
  310        */
  311       public void addStereoType(Annotation stereoType)
  312       {
  313           this.stereoTypes.add(stereoType);
  314       }
  315   
  316       /**
  317        * Add new api type.
  318        *
  319        * @param apiType new api type
  320        */
  321       public void addApiType(Class<?> apiType)
  322       {
  323           this.apiTypes.add(apiType);
  324       }
  325   
  326       /**
  327        * Get qualifiers.
  328        *
  329        * @return qualifiers
  330        */
  331       public Set<Annotation> getImplQualifiers()
  332       {
  333           return implQualifiers;
  334       }
  335   
  336       /**
  337        * Gets the stereotypes.
  338        *
  339        * @return stereotypes of the bean
  340        */
  341       public Set<Annotation> getOwbStereotypes()
  342       {
  343           return this.stereoTypes;
  344       }
  345   
  346       /**
  347        * Add new qualifier.
  348        *
  349        * @param qualifier new qualifier
  350        */
  351       public void addQualifier(Annotation qualifier)
  352       {
  353           this.implQualifiers.add(qualifier);
  354       }
  355   
  356       /**
  357        * Set name.
  358        * 
  359        * @param name new name
  360        */
  361       public void setName(String name)
  362       {
  363           if (this.name == null)
  364               this.name = name;
  365           else
  366               throw new UnsupportedOperationException("Component name is not null, is " + this.name);
  367       }
  368   
  369       /**
  370        * Gets predecence of the bean.
  371        * 
  372        * @return precedence
  373        */
  374       public int getPrecedence()
  375       {
  376           return DeploymentTypeManager.getInstance().getPrecedence(getDeploymentType());
  377       }
  378   
  379       /*
  380        * (non-Javadoc)
  381        * @see javax.webbeans.manager.Bean#getQualifiers()
  382        */
  383       @Override
  384       public Set<Annotation> getQualifiers()
  385       {
  386           return this.implQualifiers;
  387       }
  388   
  389       /*
  390        * (non-Javadoc)
  391        * @see javax.webbeans.manager.Bean#getDeploymentType()
  392        */
  393       @Override
  394       public Class<? extends Annotation> getDeploymentType()
  395       {
  396           return this.type.annotationType();
  397       }
  398   
  399       /*
  400        * (non-Javadoc)
  401        * @see javax.webbeans.manager.Bean#getScope()
  402        */
  403       @Override
  404       public Class<? extends Annotation> getScope()
  405       {
  406           return this.implScopeType.annotationType();
  407       }
  408   
  409       
  410       public Set<Type> getTypes()
  411       {        
  412           return this.apiTypes;
  413       }
  414   
  415       /**
  416        * Gets type of the producer method.
  417        * 
  418        * @return type of the producer method
  419        */
  420       public Class<T> getReturnType()
  421       {
  422           return returnType;
  423       }
  424   
  425       /**
  426        * Gets the dependent bean instance.
  427        * 
  428        * @param dependentComponent dependent web beans bean
  429        * @return the dependent bean instance
  430        */
  431       public Object getDependent(Bean<?> dependentComponent, InjectionPoint injectionPoint)
  432       {
  433           Object object = null;
  434           
  435           //Setting injection point owner
  436           AbstractBean<?> dependent = (AbstractBean<?>)dependentComponent;
  437           dependent.setDependentOwnerInjectionPoint(injectionPoint);        
  438           
  439           @SuppressWarnings("unchecked")
  440           CreationalContext<?> dependentCreational = CreationalContextFactory.getInstance().getCreationalContext(dependentComponent);
  441           
  442           //Get dependent instance
  443           object = BeanManagerImpl.getManager().getReference(dependentComponent, injectionPoint.getType(), dependentCreational);
  444           
  445           CreationalContextImpl<T> cc = (CreationalContextImpl<T>)this.creationalContext;
  446           
  447           if(cc == null)
  448           {
  449               System.out.println(this);
  450           }
  451   
  452           //Put this into the dependent map
  453           cc.addDependent(dependentComponent, object);
  454   
  455           return object;
  456       }
  457       
  458       /**
  459        * {@inheritDoc}
  460        */
  461       public List<InterceptorData> getInterceptorStack()
  462       {
  463           return this.interceptorStack;
  464       }
  465   
  466       /**
  467        * {@inheritDoc}
  468        */
  469       public List<Object> getDecoratorStack()
  470       {
  471           return this.decoratorStack;
  472       }
  473       
  474       /**
  475        * {@inheritDoc}
  476        */
  477       @Override
  478       public void setNullable(boolean nullable)
  479       {
  480           this.nullable = nullable;
  481       }
  482   
  483       /**
  484        * {@inheritDoc}
  485        */
  486       @Override
  487       public void setSerializable(boolean serializable)
  488       {
  489           this.serializable = serializable;
  490   
  491       }
  492   
  493       /**
  494        * {@inheritDoc}
  495        */
  496       @Override
  497       public boolean isNullable()
  498       {
  499   
  500           return this.nullable;
  501       }
  502   
  503       /**
  504        * {@inheritDoc}
  505        */
  506       @Override
  507       public boolean isSerializable()
  508       {
  509           return this.serializable;
  510       }
  511   
  512       /**
  513        * {@inheritDoc}
  514        */    
  515       public void addInjectionPoint(InjectionPoint injectionPoint)
  516       {
  517           this.injectionPoints.add(injectionPoint);
  518       }
  519       
  520       /**
  521        * {@inheritDoc}
  522        */    
  523       public Set<InjectionPoint> getInjectionPoints()
  524       {
  525           return this.injectionPoints;
  526       }
  527       
  528       /**
  529        * @return the dependentOwnerInjectionPoint
  530        */
  531       public InjectionPoint getDependentOwnerInjectionPoint()
  532       {
  533           return dependentOwnerInjectionPoint;
  534       }
  535   
  536       /**
  537        * @param dependentOwnerInjectionPoint the dependentOwnerInjectionPoint to set
  538        */
  539       public void setDependentOwnerInjectionPoint(InjectionPoint dependentOwnerInjectionPoint)
  540       {
  541           this.dependentOwnerInjectionPoint = dependentOwnerInjectionPoint;
  542       }
  543       
  544       /**
  545        * {@inheritDoc}
  546        */    
  547       public void setSpecializedBean(boolean specialized)
  548       {
  549           this.specializedBean = specialized;
  550       }
  551       
  552       
  553       /**
  554        * {@inheritDoc}
  555        */    
  556       public boolean isSpecializedBean()
  557       {
  558           return this.specializedBean;
  559       }
  560       
  561       /**
  562        * {@inheritDoc}
  563        */    
  564       public List<InjectionPoint> getInjectionPoint(Member member)
  565       {
  566           List<InjectionPoint> points = new ArrayList<InjectionPoint>();
  567           
  568           for(InjectionPoint ip : injectionPoints)
  569           {
  570               if(ip.getMember().equals(member))
  571               {
  572                   points.add(ip);
  573               }
  574           }
  575           
  576           return points;
  577       }
  578       
  579       /**
  580        * {@inheritDoc}
  581        */    
  582       public Set<Class<? extends Annotation>> getStereotypes()
  583       {
  584           Set<Class<? extends Annotation>> set = new HashSet<Class<? extends Annotation>>();
  585           
  586           for(Annotation ann : this.stereoTypes)
  587           {
  588               set.add(ann.annotationType());
  589           }
  590           
  591           return set;
  592       }
  593       
  594       /**
  595        * {@inheritDoc}
  596        */    
  597       public CreationalContext<T> getCreationalContext()
  598       {
  599           return this.creationalContext;
  600       }
  601   
  602       /**
  603        * {@inheritDoc}
  604        */
  605       public  void setCreationalContext(CreationalContext<T> creationalContext)
  606       {
  607           this.creationalContext = creationalContext;
  608       }
  609       
  610       /**
  611        * {@inheritDoc}
  612        */    
  613       public boolean isAlternative()
  614       {
  615           return AlternativesManager.getInstance().isBeanHasAlternative(this);
  616       }
  617       
  618       /**
  619        * {@inheritDoc}
  620        */    
  621       public String toString()
  622       {
  623           StringBuilder builder = new StringBuilder();
  624           builder.append("{\n");        
  625           builder.append("\tName : "+ getName() +", WebBeans Type: "+ getWebBeansType() + "\n");
  626           builder.append("\tAPI Types:\n");
  627           builder.append("\t[\n");
  628           
  629           for(Type clazz : getTypes())
  630           {
  631               if(clazz instanceof Class)
  632               {
  633                   builder.append("\t\t\t"+((Class<?>)clazz).getName()+ "\n");    
  634               }
  635               else
  636               {
  637                   Class<?> rawType = (Class<?>)((ParameterizedType)clazz).getRawType();
  638                   builder.append("\t\t\t"+rawType.getName()+ "\n");
  639               }
  640                           
  641           }
  642           
  643           builder.append("\t]\n");
  644           builder.append("\t,\n");
  645           builder.append("\tQualifiers:\n");
  646           builder.append("\t[\n");
  647           
  648           for(Annotation ann : getQualifiers())
  649           {
  650               builder.append("\t\t\t"+ann.annotationType().getName()+"\n");
  651           }
  652           
  653           builder.append("\t]\n");
  654           builder.append("}\n");
  655           
  656           return builder.toString();
  657       }
  658       
  659       protected WebBeansLogger getLogger()
  660       {
  661           return this.logger;
  662       }
  663   }

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