Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.container.activity » [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
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    * 
    9    *       http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.webbeans.container.activity;
   18   
   19   
   20   import java.util.ArrayList;
   21   import java.util.List;
   22   import java.util.Map;
   23   import java.util.Set;
   24   import java.util.concurrent.ConcurrentHashMap;
   25   
   26   import javax.enterprise.context.spi.Context;
   27   
   28   import org.apache.webbeans.config.WebBeansFinder;
   29   import org.apache.webbeans.container.BeanManagerImpl;
   30   import org.apache.webbeans.exception.WebBeansException;
   31   
   32   /**
   33    * Class is responsible for managing the activities.
   34    * 
   35    * <p>
   36    * There is always one root activity.
   37    * </p>
   38    * 
   39    * @version $Rev: 791061 $ $Date: 2009-07-04 09:20:35 +0300 (Sat, 04 Jul 2009) $
   40    */
   41   public class ActivityManager
   42   {
   43       /**Root activity*/
   44       private BeanManagerImpl rootActivity = null;
   45       
   46       /**Setted current activities*/
   47       private Map<Context, BeanManagerImpl> currentActivityMap = new ConcurrentHashMap<Context, BeanManagerImpl>();
   48       
   49       /**
   50        * Used by the system. Do not
   51        * instantiate this from outside.
   52        */
   53       public ActivityManager()
   54       {
   55           
   56       }
   57       
   58       /**
   59        * Gets the activity manager.
   60        * 
   61        * @return the singleton acitivity manager
   62        */
   63       public static ActivityManager getInstance()
   64       {
   65           ActivityManager currentActivityManager = (ActivityManager)WebBeansFinder.getSingletonInstance(ActivityManager.class.getName());
   66           
   67           return currentActivityManager;
   68       }
   69       
   70       
   71       /**
   72        * Sets the root activity
   73        * 
   74        * @param rootActivity root activity
   75        */
   76       public synchronized void  setRootActivity(BeanManagerImpl rootActivity)
   77       {
   78           this.rootActivity = rootActivity;
   79       }
   80       
   81       /**
   82        * Gets root activity
   83        * 
   84        * @return the root activity
   85        */
   86       public BeanManagerImpl getRootActivity()
   87       {
   88           return this.rootActivity;
   89       }
   90   
   91       
   92       /**
   93        * Add new current activity for the context.
   94        * 
   95        * @param context
   96        * @param currentManager
   97        */
   98       public void addCurrentActivity(Context context, BeanManagerImpl currentManager)
   99       {
  100           this.currentActivityMap.put(context, currentManager); 
  101       }
  102       
  103       /**
  104        * Looks for the registered current activities.
  105        * <ul>
  106        *  <li>If there are more than one activity, throws exception.</li>
  107        *  <li>If no registered current activity, return the root activity.
  108        * </ul>
  109        * 
  110        * 
  111        * @return the current activity
  112        * @throws WebBeansException if more than one current activity exist
  113        */
  114       public BeanManagerImpl getCurrentActivity()
  115       {
  116           BeanManagerImpl currentActivity = null;
  117           
  118           Set<Context> contexts = this.currentActivityMap.keySet();
  119           List<BeanManagerImpl> managers = new ArrayList<BeanManagerImpl>(); 
  120           for(Context context : contexts)
  121           {
  122               if(context.isActive())
  123               {
  124                   managers.add(this.currentActivityMap.get(context));
  125               }
  126           }
  127           
  128           if(managers.size() > 1)
  129           {
  130               throw new WebBeansException("There are more than one current activity");
  131           }
  132           else
  133           {
  134               if(!managers.isEmpty())
  135               {
  136                   currentActivity = managers.get(0);   
  137               }            
  138           }
  139   
  140           
  141           if(currentActivity == null)
  142           {
  143               return getRootActivity();
  144           }
  145           
  146           return currentActivity;
  147       }
  148   }

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