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.Method;
   18   import java.lang.reflect.Modifier;
   19   import java.lang.reflect.Type;
   20   import java.util.ArrayList;
   21   import java.util.List;
   22   
   23   import javax.enterprise.context.Dependent;
   24   import javax.enterprise.context.spi.Context;
   25   import javax.enterprise.event.Observer;
   26   import javax.enterprise.event.Observes;
   27   
   28   import org.apache.webbeans.annotation.CurrentLiteral;
   29   import org.apache.webbeans.component.AbstractBean;
   30   import org.apache.webbeans.component.InjectionTargetBean;
   31   import org.apache.webbeans.container.InjectionResolver;
   32   import org.apache.webbeans.container.BeanManagerImpl;
   33   import org.apache.webbeans.container.activity.ActivityManager;
   34   import org.apache.webbeans.exception.WebBeansException;
   35   import org.apache.webbeans.logger.WebBeansLogger;
   36   import org.apache.webbeans.util.AnnotationUtil;
   37   import org.apache.webbeans.util.WebBeansUtil;
   38   
   39   /**
   40    * Defines observers that are declared in observer methods.
   41    * <p>
   42    * Example:
   43    * <pre>
   44    *  public class X {
   45    *      
   46    *      public void afterLoggedIn(@Observes @Current LoggedInEvent event)
   47    *      {
   48    *          .....
   49    *      }
   50    *  }
   51    * </pre>
   52    * Above class X instance observes for the event with type <code>LoggedInEvent</code>
   53    * and event qualifier is <code>Current</code>. Whenever event is fired, its {@link Observer#notify()}
   54    * method is called.
   55    * </p>
   56    * 
   57    * @version $Rev: 811507 $ $Date: 2009-09-04 21:18:41 +0300 (Fri, 04 Sep 2009) $
   58    *
   59    * @param <T> event type
   60    */
   61   public class BeanObserverImpl<T> implements Observer<T>
   62   {
   63       /**Logger instance*/
   64       private static final WebBeansLogger logger = WebBeansLogger.getLogger(BeanObserverImpl.class);
   65   
   66       /**Observer owner bean that defines observer method*/
   67       private final InjectionTargetBean<?> bean;
   68   
   69       /**Event observer method*/
   70       private final Method observerMethod;
   71   
   72       /**Using existing bean instance or not*/
   73       private final boolean ifExist;
   74   
   75       /**Observer transaction type*/
   76       private final TransactionalObserverType type;
   77   
   78       /**
   79        * Creates a new bean observer instance.
   80        * 
   81        * @param bean owner
   82        * @param observerMethod method
   83        * @param ifExist if exist parameter
   84        * @param type transaction type
   85        */
   86       public BeanObserverImpl(InjectionTargetBean<?> bean, Method observerMethod, boolean ifExist, TransactionalObserverType type)
   87       {
   88           this.bean = bean;
   89           this.observerMethod = observerMethod;
   90           this.ifExist = ifExist;
   91           this.type = type;
   92       }
   93   
   94       /**
   95        * {@inheritDoc}
   96        */
   97       @SuppressWarnings("unchecked")
   98       public void notify(T event)
   99       {
  100           AbstractBean<Object> baseComponent = (AbstractBean<Object>) bean;
  101           AbstractBean<Object> specializedComponent = null;
  102           Object object = null;
  103   
  104           try
  105           {
  106               BeanManagerImpl manager = ActivityManager.getInstance().getCurrentActivity();
  107               specializedComponent = (AbstractBean<Object>)WebBeansUtil.getMostSpecializedBean(manager, baseComponent);        
  108               Context context = manager.getContext(specializedComponent.getScope());
  109               
  110               if(this.ifExist)
  111               {
  112                   object = context.get(specializedComponent);
  113               }
  114               else
  115               {
  116                   object = manager.getInstance(specializedComponent);    
  117               }
  118               
  119   
  120               if (this.ifExist && object == null)
  121               {
  122                   return;
  123               }
  124   
  125               if (object != null)
  126               {
  127                   Object[] args = null;
  128                   
  129                   List<Object> argsObjects = getMethodArguments(event);
  130                   args = new Object[argsObjects.size()];
  131                   args = argsObjects.toArray(args);
  132   
  133                   if (!this.observerMethod.isAccessible())
  134                   {
  135                       this.observerMethod.setAccessible(true);
  136                   }
  137   
  138                   //Static or not
  139                   if (Modifier.isStatic(this.observerMethod.getModifiers()))
  140                   {
  141                       object = null;
  142                   }
  143   
  144                   //Invoke Method
  145                   this.observerMethod.invoke(object, args);
  146               }
  147           }
  148           catch (Exception e)
  149           {
  150               if (!getType().equals(TransactionalObserverType.NONE))
  151               {
  152                   logger.error("Error is occured while notifying observer in class : " + observerMethod.getDeclaringClass().getName() + " in method : " + observerMethod.getName(), e);
  153               }
  154               else
  155               {
  156                   throw new WebBeansException(e.getCause());
  157               }
  158           }
  159           finally
  160           {
  161               if (baseComponent.getScope().equals(Dependent.class))
  162               {
  163                   baseComponent.destroy(object,baseComponent.getCreationalContext());
  164               }
  165           }
  166   
  167       }
  168   
  169       /**
  170        * Returns list of observer method parameters.
  171        * 
  172        * @param event event instance
  173        * @return list of observer method parameters
  174        */
  175       protected List<Object> getMethodArguments(Object event)
  176       {
  177           Type[] types = this.observerMethod.getGenericParameterTypes();
  178   
  179           Annotation[][] annots = this.observerMethod.getParameterAnnotations();
  180   
  181           List<Object> list = new ArrayList<Object>();
  182   
  183           BeanManagerImpl manager = ActivityManager.getInstance().getCurrentActivity();
  184   
  185           if (types.length > 0)
  186           {
  187               int i = 0;
  188               for (Type type : types)
  189               {
  190                   Annotation[] annot = annots[i];
  191   
  192                   boolean observesAnnotation = false;
  193   
  194                   if (annot.length == 0)
  195                   {
  196                       annot = new Annotation[1];
  197                       annot[0] = new CurrentLiteral();
  198                   }
  199                   else
  200                   {
  201                       for (Annotation observersAnnot : annot)
  202                       {
  203                           if (observersAnnot.annotationType().equals(Observes.class))
  204                           {
  205                               list.add(event);
  206                               observesAnnotation = true;
  207                               break;
  208                           }
  209                       }
  210                   }
  211   
  212                   if (!observesAnnotation)
  213                   {
  214                       //Get parameter annotations
  215                       Annotation[] bindingTypes = AnnotationUtil.getQualifierAnnotations(annot);
  216   
  217                       if (bindingTypes.length > 0)
  218                       {
  219                           list.add(manager.getInstance(InjectionResolver.getInstance().implResolveByType(type, bindingTypes).iterator().next()));
  220                       }
  221                       else
  222                       {
  223                           list.add(null);
  224                       }
  225                   }
  226                   
  227                   i++;
  228               }
  229           }
  230   
  231           return list;
  232       }
  233   
  234       /**
  235        * Returrns observer owner bean.
  236        * 
  237        * @return the bean
  238        */
  239       public InjectionTargetBean<?> getBean()
  240       {
  241           return bean;
  242       }
  243   
  244       /**
  245        * Returns observer's transactional type.
  246        * 
  247        * @return transactional type
  248        */
  249       public TransactionalObserverType getType()
  250       {
  251           return type;
  252       }
  253   
  254   }

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