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.util.Set;
   18   
   19   import javax.enterprise.context.spi.CreationalContext;
   20   import javax.enterprise.inject.spi.Bean;
   21   import javax.enterprise.inject.spi.InjectionPoint;
   22   import javax.enterprise.inject.spi.Producer;
   23   
   24   import org.apache.webbeans.util.WebBeansUtil;
   25   
   26   /**
   27    * Abstract class for producer components.
   28    * 
   29    * @version $Rev: 811507 $ $Date: 2009-09-04 21:18:41 +0300 (Fri, 04 Sep 2009) $
   30    * @param <T> bean type info
   31    */
   32   public abstract class AbstractProducerBean<T> extends AbstractBean<T> implements IBeanHasParent<T>
   33   {
   34       /** Owner of the producer field component */
   35       protected AbstractBean<?> ownerComponent;
   36   
   37       /** Producer instance responsible for produce and dispose */
   38       protected Producer<T> producer;
   39   
   40       /**
   41        * Create a new instance.
   42        * 
   43        * @param type webbeans typr
   44        * @param returnType bean type info
   45        * @param ownerComponent owner bean
   46        */
   47       protected AbstractProducerBean(WebBeansType type, Class<T> returnType, AbstractBean<?> ownerComponent)
   48       {
   49           super(type, returnType);
   50           this.ownerComponent = ownerComponent;
   51       }
   52   
   53       /**
   54        * {@inheritDoc}
   55        */
   56       public AbstractBean<?> getParent()
   57       {
   58           return this.ownerComponent;
   59       }
   60   
   61       /**
   62        * {@inheritDoc}
   63        */
   64       public void dispose(T instance)
   65       {
   66           // Do nothing
   67       }
   68   
   69       /**
   70        * Sets producer instance.
   71        * 
   72        * @param producer producer instance
   73        */
   74       public void setProducer(Producer<T> producer)
   75       {
   76           this.producer = producer;
   77       }
   78   
   79       /**
   80        * Gets producer instance.
   81        * 
   82        * @return producer instance
   83        */
   84       public Producer<T> getProducer()
   85       {
   86           return this.producer;
   87       }
   88   
   89       /**
   90        * Returns true if producer is set.
   91        * 
   92        * @return true if producer instance is set
   93        */
   94       protected boolean isProducerSet()
   95       {
   96           return producer != null ? true : false;
   97       }
   98   
   99       /**
  100        * {@inheritDoc}
  101        */
  102       @Override
  103       protected T createInstance(CreationalContext<T> creationalContext)
  104       {
  105           return null;
  106       }
  107   
  108       /**
  109        * Destroys bean.
  110        * 
  111        * @param <K> bean type info
  112        * @param bean bean info
  113        * @param instance bean instance
  114        */
  115       @SuppressWarnings("unchecked")
  116       protected <K> void destroyBean(Bean<?> bean, Object instance)
  117       {
  118           Bean<K> destroy = (Bean<K>) bean;
  119           K inst = (K) instance;
  120   
  121           CreationalContext<K> cc = (CreationalContext<K>) this.creationalContext;
  122           destroy.destroy(inst, cc);
  123       }
  124   
  125       /**
  126        * Returns producer bean's owner bean instance.
  127        * 
  128        * @return owner bean instance
  129        */
  130       @SuppressWarnings("unchecked")
  131       protected Object getParentInstance()
  132       {
  133           // return getManager().getInstance(this.ownerComponent);
  134   
  135           Object parentInstance = null;
  136   
  137           // Added for most specialized bean
  138           Annotation[] anns = new Annotation[this.ownerComponent.getQualifiers().size()];
  139           anns = this.ownerComponent.getQualifiers().toArray(anns);
  140   
  141           Bean<?> specialize = WebBeansUtil.getMostSpecializedBean(getManager(), (AbstractBean<T>) this.ownerComponent);
  142   
  143           if (specialize != null)
  144           {
  145               parentInstance = getManager().getReference(specialize, null, null);
  146           }
  147           else
  148           {
  149               parentInstance = getManager().getReference(this.ownerComponent, null, null);
  150           }
  151   
  152           return parentInstance;
  153   
  154       }
  155   
  156       /**
  157        * {@inheritDoc}
  158        */
  159       @Override
  160       protected void destroyInstance(T instance)
  161       {
  162   
  163       }
  164   
  165       /**
  166        * {@inheritDoc}
  167        */
  168       @Override
  169       public Set<InjectionPoint> getInjectionPoints()
  170       {
  171           if (isProducerSet())
  172           {
  173               return getProducer().getInjectionPoints();
  174           }
  175   
  176           return super.getInjectionPoints();
  177       }
  178   
  179   }

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