Save This Page
Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.event » [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.event;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.lang.reflect.Type;
   18   import java.util.ArrayList;
   19   import java.util.List;
   20   
   21   import javax.enterprise.event.Event;
   22   import javax.enterprise.inject.TypeLiteral;
   23   
   24   import org.apache.webbeans.container.BeanManagerImpl;
   25   import org.apache.webbeans.util.AnnotationUtil;
   26   import org.apache.webbeans.util.ClassUtil;
   27   
   28   /**
   29    * Event implementation.
   30    * 
   31    * @version $Rev: 811507 $ $Date: 2009-09-04 21:18:41 +0300 (Fri, 04 Sep 2009) $
   32    *
   33    * @param <T> event type
   34    * @see Event
   35    */
   36   public class EventImpl<T> implements Event<T>
   37   {
   38       /**Event binding types*/
   39       private Annotation[] injectedBindings;
   40   
   41       /**Event types*/
   42       private Type eventType;
   43   
   44       /**Bean manager*/
   45       private BeanManagerImpl manager = null;
   46   
   47       /**
   48        * Creates a new event.
   49        * 
   50        * @param injectedBindings event bindings
   51        * @param eventType event type
   52        */
   53       public EventImpl(Annotation[] injectedBindings, Type eventType)
   54       {
   55           this.injectedBindings = injectedBindings;
   56           this.eventType = eventType;
   57           this.manager = BeanManagerImpl.getManager();
   58       }
   59   
   60       /**
   61        * Fires event with given event object.
   62        */
   63       public void fire(T event)
   64       {
   65           this.manager.fireEvent(event, this.injectedBindings);
   66       }
   67   
   68       /**
   69        * Returns total binding annotations.
   70        * 
   71        * @param annotations new annotations
   72        * @return total binding annotations
   73        */
   74       private Annotation[] getEventBindings(Annotation... annotations)
   75       {
   76           AnnotationUtil.checkQualifierConditions(annotations);
   77           
   78           List<Annotation> eventBindings = new ArrayList<Annotation>();
   79           Annotation[] anns = null;
   80   
   81           for (Annotation binding : annotations)
   82           {
   83               eventBindings.add(binding);
   84           }
   85   
   86           anns = new Annotation[eventBindings.size()];
   87           anns = eventBindings.toArray(anns);
   88   
   89           return anns;
   90   
   91       }
   92   
   93       /**
   94        * {@inheritDoc}
   95        */
   96       @Override
   97       public Event<T> select(Annotation... bindings)
   98       {
   99           Event<T> sub = new EventImpl<T>(getEventBindings(bindings),this.eventType);
  100           
  101           return sub;
  102       }
  103       
  104       /**
  105        * {@inheritDoc}
  106        */
  107       @Override
  108       public <U extends T> Event<U> select(Class<U> subtype, Annotation... bindings)
  109       {
  110           if(ClassUtil.isDefinitionConstainsTypeVariables(subtype))
  111           {
  112               throw new IllegalArgumentException("Class : " + subtype + " cannot contain type variable");
  113           }
  114           
  115           Type sub = subtype;
  116           
  117           if(sub == null)
  118           {
  119               sub = this.eventType;
  120           }
  121           
  122           Event<U> subEvent = new EventImpl<U>(getEventBindings(bindings),sub);
  123           
  124           return subEvent;
  125       }
  126       
  127       /**
  128        * {@inheritDoc}
  129        */
  130       @Override
  131       public <U extends T> Event<U> select(TypeLiteral<U> subtype, Annotation... bindings)
  132       {
  133           return select(subtype.getRawType(), bindings);
  134       }
  135   }

Save This Page
Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.event » [javadoc | source]