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.Modifier;
   18   import java.lang.reflect.ParameterizedType;
   19   import java.lang.reflect.Type;
   20   
   21   import javax.enterprise.context.Dependent;
   22   import javax.enterprise.context.spi.CreationalContext;
   23   
   24   import org.apache.webbeans.exception.WebBeansException;
   25   import org.apache.webbeans.util.WebBeansUtil;
   26   
   27   /**
   28    * Defines the producer field component implementation.
   29    * 
   30    * @param <T> Type of the field decleration
   31    */
   32   public class ProducerFieldBean<T> extends AbstractProducerBean<T> implements IBeanHasParent<T>
   33   {
   34       /** Producer field that defines the component */
   35       private Field producerField;
   36   
   37       /**
   38        * Defines the new producer field component.
   39        * 
   40        * @param returnType type of the field decleration
   41        */
   42       public ProducerFieldBean(AbstractBean<?> ownerComponent, Class<T> returnType)
   43       {
   44           super(WebBeansType.PRODUCERFIELD, returnType, ownerComponent);
   45       }
   46   
   47       /**
   48        * {@inheritDoc}
   49        */
   50       @Override
   51       protected T createInstance(CreationalContext<T> creationalContext)
   52       {
   53           T instance = null;
   54   
   55           if (isProducerSet())
   56           {
   57               instance = producer.produce(creationalContext);
   58           }
   59           else
   60           {
   61               instance = createDefaultInstance(creationalContext);
   62           }
   63   
   64           checkNullInstance(instance);
   65           checkScopeType();
   66   
   67           return instance;
   68   
   69       }
   70   
   71       /**
   72        * Default producer method creation.
   73        * 
   74        * @param creationalContext creational context
   75        * @return producer method instance
   76        */
   77       @SuppressWarnings("unchecked")
   78       protected T createDefaultInstance(CreationalContext<T> creationalContext)
   79       {
   80           T instance = null;
   81           Object parentInstance = null;
   82           try
   83           {
   84               if (!producerField.isAccessible())
   85               {
   86                   producerField.setAccessible(true);
   87               }
   88   
   89               if (Modifier.isStatic(producerField.getModifiers()))
   90               {
   91                   instance = (T) producerField.get(null);
   92               }
   93               else
   94               {
   95                   parentInstance = getParentInstance();
   96                   instance = (T) producerField.get(parentInstance);
   97               }
   98           }catch(Exception e)
   99           {
  100               throw new WebBeansException(e);
  101           }
  102           finally
  103           {
  104               if (this.ownerComponent.getScope().equals(Dependent.class))
  105               {
  106                   destroyBean(this.ownerComponent, parentInstance);
  107               }
  108           }
  109   
  110           return instance;
  111   
  112       }
  113   
  114       /**
  115        * Gets creator field.
  116        * 
  117        * @return creator field
  118        */
  119       public Field getCreatorField()
  120       {
  121           return this.producerField;
  122       }
  123   
  124       /**
  125        * Returns actual type arguments.
  126        * 
  127        * @return actual type arguments
  128        */
  129       public Type[] getActualTypeArguments()
  130       {
  131           Type type = producerField.getGenericType();
  132           if (type instanceof ParameterizedType)
  133           {
  134               ParameterizedType pType = (ParameterizedType) type;
  135               return pType.getActualTypeArguments();
  136           }
  137   
  138           else
  139           {
  140               return new Type[0];
  141           }
  142   
  143       }
  144   
  145       /**
  146        * Set producer field.
  147        * 
  148        * @param field producer field
  149        */
  150       public void setProducerField(Field field)
  151       {
  152           this.producerField = field;
  153       }
  154   
  155       /**
  156        * Check null instance.
  157        * 
  158        * @param instance bean instance
  159        */
  160       protected void checkNullInstance(Object instance)
  161       {
  162           String errorMessage = "WebBeans producer field : " + producerField.getName() + " return type in the component implementation class : " + this.ownerComponent.getReturnType().getName() + " scope must be @Dependent to create null instance";
  163           WebBeansUtil.checkNullInstance(instance, this.getScope(), errorMessage);
  164       }
  165   
  166       /**
  167        * Check scope type passivation controls.
  168        */
  169       protected void checkScopeType()
  170       {
  171           String errorMessage = "WebBeans producer method : " + producerField.getName() + " return type in the component implementation class : " + this.ownerComponent.getReturnType().getName() + " with passivating scope @" + this.getScope().getName() + " must be Serializable";
  172           WebBeansUtil.checkSerializableScopeType(this.getScope(), this.isSerializable(), errorMessage);
  173       }
  174   
  175       /**
  176        * {@inheritDoc}
  177        */
  178       public String toString()
  179       {
  180           return super.toString();
  181       }
  182   }

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