Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.inject.instance » [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.inject.instance;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.lang.reflect.Type;
   18   import java.util.HashSet;
   19   import java.util.Iterator;
   20   import java.util.Set;
   21   
   22   import javax.enterprise.inject.Instance;
   23   import javax.enterprise.inject.TypeLiteral;
   24   import javax.enterprise.inject.spi.Bean;
   25   
   26   import org.apache.webbeans.container.InjectionResolver;
   27   import org.apache.webbeans.container.BeanManagerImpl;
   28   import org.apache.webbeans.container.ResolutionUtil;
   29   import org.apache.webbeans.util.AnnotationUtil;
   30   import org.apache.webbeans.util.ClassUtil;
   31   
   32   /**
   33    * Implements the {@link Instance} interface.
   34    * 
   35    * @param <T> specific instance type
   36    */
   37   class InstanceImpl<T> implements Instance<T>
   38   {
   39       /** Injected class type */
   40       private Type injectionClazz;
   41   
   42       /** Qualifier annotations appeared on the injection point */
   43       private Set<Annotation> qualifierAnnotations = new HashSet<Annotation>();
   44   
   45       /**
   46        * Creates new instance.
   47        * 
   48        * @param injectionClazz injection class type
   49        * @param actualTypeArguments actual type arguments
   50        * @param annotations qualifier annotations
   51        */
   52       InstanceImpl(Type injectionClazz, Annotation... annotations)
   53       {
   54           this.injectionClazz = injectionClazz;
   55   
   56           for (Annotation ann : annotations)
   57           {
   58               qualifierAnnotations.add(ann);
   59           }
   60       }
   61   
   62       /**
   63        * Returns the bean instance with given qualifier annotations.
   64        * 
   65        * @param annotations qualifier annotations
   66        * @return bean instance
   67        */
   68       @SuppressWarnings("unchecked")
   69       public T get()
   70       {
   71           T instance = null;
   72   
   73           Annotation[] anns = new Annotation[this.qualifierAnnotations.size()];
   74           anns = this.qualifierAnnotations.toArray(anns);
   75           
   76           Set<Bean<?>> beans = resolveBeans();
   77   
   78           ResolutionUtil.checkResolvedBeans(beans, ClassUtil.getClazz(this.injectionClazz),anns);
   79   
   80           Bean<?> bean = beans.iterator().next();
   81           instance = (T)BeanManagerImpl.getManager().getInstance(bean);
   82   
   83           return instance;
   84       }
   85   
   86       /**
   87        * Returns set of resolved beans.
   88        * 
   89        * @return set of resolved beans
   90        */
   91       private Set<Bean<?>> resolveBeans()
   92       {
   93           Annotation[] anns = new Annotation[this.qualifierAnnotations.size()];
   94           anns = this.qualifierAnnotations.toArray(anns);
   95   
   96           InjectionResolver resolver = InjectionResolver.getInstance();
   97           Set<Bean<?>> beans = resolver.implResolveByType(this.injectionClazz, anns);
   98           
   99           return beans;
  100       }
  101       
  102       /**
  103        * {@inheritDoc}
  104        */
  105       @Override
  106       public boolean isAmbiguous()
  107       {
  108           Set<Bean<?>> beans = resolveBeans();
  109           
  110           return beans.size() > 1 ? true : false;
  111       }
  112   
  113       /**
  114        * {@inheritDoc}
  115        */
  116       @Override
  117       public boolean isUnsatisfied()
  118       {
  119           Set<Bean<?>> beans = resolveBeans();
  120           
  121           return beans.size() == 0 ? true : false;
  122       }
  123   
  124       /**
  125        * {@inheritDoc}
  126        */
  127       @Override
  128       public Instance<T> select(Annotation... qualifiers)
  129       {
  130           Annotation[] newQualifiersArray = getAdditionalQualifiers(qualifiers);
  131           InstanceImpl<T> newInstance = new InstanceImpl<T>(this.injectionClazz, newQualifiersArray);
  132   
  133           return newInstance;
  134       }
  135   
  136       /**
  137        * Returns total qualifiers array
  138        * 
  139        * @param qualifiers additional qualifiers
  140        * @return total qualifiers array
  141        */
  142       private Annotation[] getAdditionalQualifiers(Annotation[] qualifiers)
  143       {
  144           AnnotationUtil.checkQualifierConditions(qualifiers);
  145           Set<Annotation> newQualifiers = new HashSet<Annotation>(this.qualifierAnnotations);
  146   
  147           if (qualifiers != null && qualifiers.length > 0)
  148           {
  149               for (Annotation annot : qualifiers)
  150               {
  151                   if (newQualifiers.contains(annot))
  152                   {
  153                       throw new IllegalArgumentException("Duplicate Qualifier Exception, " + this.toString());
  154                   }
  155   
  156                   newQualifiers.add(annot);
  157               }
  158           }
  159   
  160           Annotation[] newQualifiersArray = new Annotation[newQualifiers.size()];
  161           newQualifiersArray = newQualifiers.toArray(newQualifiersArray);
  162           
  163           return newQualifiersArray;
  164       }
  165       
  166       /**
  167        * {@inheritDoc}
  168        */
  169       @Override
  170       public <U extends T> Instance<U> select(Class<U> subtype, Annotation... qualifiers)
  171       {
  172           AnnotationUtil.checkQualifierConditions(qualifiers);
  173           
  174           Type sub = subtype;
  175           
  176           if(sub == null)
  177           {
  178               sub = this.injectionClazz;
  179           }
  180           
  181           Annotation[] newQualifiers = getAdditionalQualifiers(qualifiers);
  182           
  183           InstanceImpl<U> newInstance = new InstanceImpl<U>(sub, newQualifiers);
  184                       
  185           return newInstance;
  186       }
  187   
  188       /**
  189        * {@inheritDoc}
  190        */
  191       @Override
  192       public <U extends T> Instance<U> select(TypeLiteral<U> subtype, Annotation... qualifiers)
  193       {        
  194           return select(subtype.getRawType(), qualifiers);
  195       }
  196   
  197       /**
  198        * {@inheritDoc}
  199        */
  200       @Override
  201       @SuppressWarnings("unchecked")
  202       public Iterator<T> iterator()
  203       {
  204           Set<Bean<?>> beans = resolveBeans();
  205           Set<T> instances = new HashSet<T>();
  206           for(Bean<?> bean : beans)
  207           {
  208               T instance = (T)BeanManagerImpl.getManager().getInstance(bean);
  209               instances.add(instance);
  210           }
  211           
  212           return instances.iterator();
  213       }
  214   
  215       public String toString()
  216       {
  217           StringBuilder builder = new StringBuilder();
  218           builder.append("Instance<");
  219           builder.append(ClassUtil.getClazz(this.injectionClazz).getName());
  220           builder.append(">");
  221   
  222           builder.append(",with qualifier annotations {");
  223           int i = 0;
  224           for (Annotation qualifier : this.qualifierAnnotations)
  225           {
  226               if (i != 0)
  227               {
  228                   builder.append(",");
  229               }
  230   
  231               builder.append(qualifier.toString());
  232           }
  233   
  234           builder.append("}");
  235   
  236           return builder.toString();
  237       }
  238   }

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