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.HashSet;
   19   import java.util.Set;
   20   
   21   import javax.enterprise.event.Observer;
   22   
   23   
   24   /**
   25    * Wrapper around the {@link Observer} instance.
   26    * 
   27    * @param <T> generic event type
   28    */
   29   public class ObserverWrapper<T>
   30   {
   31       /** Event binding types apeearing on the parameter */
   32       private Set<Annotation> eventQualifiers = new HashSet<Annotation>();
   33   
   34       /** Event object type */
   35       private Type eventType;
   36   
   37       /**Wrapped observer instance*/
   38       private Observer<T> observer;
   39   
   40       /**Using <code>@IfExist</code>*/
   41       private boolean ifExist;
   42   
   43       /**Transactional observer type*/
   44       private TransactionalObserverType transObserverType;
   45   
   46       /**
   47        * Constructs new observer instance.
   48        * 
   49        * @param component web beans component defines the observer method
   50        * @param observerMethod observer method
   51        * @param eventType event type
   52        */
   53       public ObserverWrapper(Observer<T> observer, Class<T> eventType, Annotation... annotations)
   54       {
   55           this(observer, false, TransactionalObserverType.NONE, eventType, annotations);
   56       }
   57   
   58       public ObserverWrapper(Observer<T> observer, boolean ifExist, TransactionalObserverType type, Type eventType, Annotation... annotations)
   59       {
   60           for (Annotation annotation : annotations)
   61           {
   62               this.eventQualifiers.add(annotation);
   63           }
   64   
   65           this.observer = observer;
   66           this.eventType = eventType;
   67           this.ifExist = ifExist;
   68           this.transObserverType = type;
   69       }
   70   
   71       public boolean isObserverOfQualifiers(Annotation... annotations)
   72       {
   73           boolean ok = true;
   74                   
   75           if(this.eventQualifiers.size() >= annotations.length)
   76           {
   77               for (Annotation annot : annotations)
   78               {
   79                   if (!this.eventQualifiers.contains(annot))
   80                   {
   81                       ok = false;
   82                       break;
   83                   }
   84               }            
   85           }
   86           else
   87           {
   88               Set<Annotation> eventAnnots = new HashSet<Annotation>();
   89               
   90               for(Annotation eventAnnot : annotations)
   91               {
   92                   eventAnnots.add(eventAnnot);
   93               }
   94               
   95               for (Annotation annot : this.eventQualifiers)
   96               {
   97                   if (!eventAnnots.contains(annot))
   98                   {
   99                       ok = false;
  100                       break;
  101                   }
  102               }            
  103               
  104           }
  105           
  106           return ok;
  107   
  108       }
  109   
  110       
  111       /**
  112        * Gets event binding types.
  113        */
  114       public Set<Annotation> getEventQualifiers()
  115       {
  116           return this.eventQualifiers;
  117       }
  118   
  119       /**
  120        * Gets event type.
  121        */
  122       public Type getEventType()
  123       {
  124           return this.eventType;
  125       }
  126   
  127       /**
  128        * @return the observer
  129        */
  130       public Observer<T> getObserver()
  131       {
  132           return observer;
  133       }
  134   
  135       /**
  136        * @return the ifExist
  137        */
  138       public boolean isIfExist()
  139       {
  140           return ifExist;
  141       }
  142   
  143       /**
  144        * @return the transObserverType
  145        */
  146       public TransactionalObserverType getTransObserverType()
  147       {
  148           return transObserverType;
  149       }
  150   
  151   }

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