Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.context » [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.context;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.util.Iterator;
   18   import java.util.Map;
   19   import java.util.Set;
   20   import java.util.Map.Entry;
   21   import java.util.concurrent.ConcurrentHashMap;
   22   
   23   import javax.enterprise.context.ApplicationScoped;
   24   import javax.enterprise.context.ContextNotActiveException;
   25   import javax.enterprise.context.ConversationScoped;
   26   import javax.enterprise.context.Dependent;
   27   import javax.enterprise.context.RequestScoped;
   28   import javax.enterprise.context.SessionScoped;
   29   import javax.enterprise.context.spi.Context;
   30   import javax.enterprise.context.spi.Contextual;
   31   import javax.enterprise.context.spi.CreationalContext;
   32   import javax.enterprise.inject.spi.Bean;
   33   
   34   import org.apache.webbeans.context.type.ContextTypes;
   35   
   36   /**
   37    * Abstract implementation of the {@link WebBeansContext} interfaces.
   38    * 
   39    * @see Context
   40    * @see RequestContext
   41    * @see DependentContext
   42    * @see SessionContext
   43    * @see ApplicationContext
   44    * @see ConversationContext
   45    */
   46   public abstract class AbstractContext implements WebBeansContext
   47   {
   48       /**Context status, active or not*/
   49       protected boolean active;
   50   
   51       /**Context type*/
   52       protected ContextTypes type;
   53   
   54       /**Context contextual instances*/
   55       protected Map<Contextual<?>, Object> componentInstanceMap = null;
   56   
   57       /**Contextual Scope Type*/
   58       protected Class<? extends Annotation> scopeType;
   59       
   60       /**Contextual to CreationalContext Map*/
   61       protected Map<Contextual<?>, CreationalContext<?>> creationalContextMap = new ConcurrentHashMap<Contextual<?>, CreationalContext<?>>();
   62   
   63       /**
   64        * Creates a new context instance
   65        */
   66       protected AbstractContext()
   67       {
   68   
   69       }
   70   
   71       /**
   72        * Creates a new context with given scope type.
   73        * 
   74        * @param scopeType context scope type
   75        */
   76       protected AbstractContext(Class<? extends Annotation> scopeType)
   77       {
   78           this.scopeType = scopeType;
   79           setComponentInstanceMap();
   80   
   81       }
   82   
   83       /**
   84        * Creates a new context with given context type.
   85        * 
   86        * @param type context type
   87        */
   88       protected AbstractContext(ContextTypes type)
   89       {
   90           this.type = type;
   91           configureScopeType(type);
   92           setComponentInstanceMap();
   93       }
   94   
   95       /**
   96        * Configures scope type from context type.
   97        * 
   98        * @param type context type
   99        */
  100       private void configureScopeType(ContextTypes type)
  101       {
  102           if (type.equals(ContextTypes.APPLICATION))
  103           {
  104               this.scopeType = ApplicationScoped.class;
  105           }
  106           else if (type.equals(ContextTypes.SESSION))
  107           {
  108               this.scopeType = SessionScoped.class;
  109           }
  110           else if (type.equals(ContextTypes.REQUEST))
  111           {
  112               this.scopeType = RequestScoped.class;
  113           }
  114           else if (type.equals(ContextTypes.DEPENDENT))
  115           {
  116               this.scopeType = Dependent.class;
  117           }
  118           else if (type.equals(ContextTypes.CONVERSATION))
  119           {
  120               this.scopeType = ConversationScoped.class;
  121           }
  122           else
  123           {
  124               throw new IllegalArgumentException("Not known scope type : " + type.toString());
  125           }
  126   
  127       }
  128   
  129       /**
  130        * {@inheritDoc}
  131        */
  132       @SuppressWarnings("unchecked")
  133       public <T> T get(Contextual<T> component)
  134       {
  135           checkActive();
  136           
  137           return (T) componentInstanceMap.get(component);
  138       }
  139   
  140       /**
  141        * {@inheritDoc}
  142        */
  143       public <T> T get(Contextual<T> component, CreationalContext<T> creationalContext)
  144       {
  145           checkActive();
  146           
  147           return getInstance(component, creationalContext);
  148       }
  149   
  150       /**
  151        * {@inheritDoc} 
  152        */
  153       @SuppressWarnings("unchecked")
  154       protected <T> T getInstance(Contextual<T> component, CreationalContext<T> creationalContext)
  155       {
  156           T instance = (T)componentInstanceMap.get(component);
  157   
  158           
  159           if (instance != null)
  160           {
  161               return instance;
  162           }
  163   
  164           else
  165           {
  166               if(creationalContext == null)
  167               {
  168                   return null;
  169               }
  170               
  171               else
  172               {
  173                   instance = component.create(creationalContext);
  174   
  175                   if (instance != null)
  176                   {
  177                       this.componentInstanceMap.put(component, instance);
  178                       this.creationalContextMap.put(component, creationalContext);
  179                   }
  180                   
  181               }            
  182           }
  183   
  184           return  instance;
  185       }
  186   
  187       /**
  188        * Destroy the given web beans component instance.
  189        * 
  190        * @param <T>
  191        * @param component web beans component
  192        * @param instance component instance
  193        */
  194       private <T> void destroyInstance(Contextual<T> component, T instance,CreationalContext<T> creationalContext)
  195       {
  196           //Destroy component
  197           component.destroy(instance,creationalContext);
  198       }
  199       
  200       /**
  201        * {@inheritDoc}
  202        */
  203       @SuppressWarnings("unchecked")
  204       public void destroy()
  205       {
  206           Set<Entry<Contextual<?>, Object>> entrySet = componentInstanceMap.entrySet();
  207           Iterator<Entry<Contextual<?>, Object>> it = entrySet.iterator();
  208   
  209           Contextual<?> component = null;
  210           while (it.hasNext())
  211           {
  212               component = it.next().getKey();
  213               
  214               Object instance = componentInstanceMap.get(component);
  215               //Get creational context
  216               CreationalContext<Object> cc = (CreationalContext<Object>)this.creationalContextMap.get(component);
  217   
  218               //Destroy instance
  219               destroyInstance((Bean<Object>) component, instance, cc);
  220   
  221           }
  222           
  223           //Clear cache
  224           componentInstanceMap.clear();
  225       }
  226   
  227       /**
  228        * Gets context active flag.
  229        * 
  230        * @return active flag
  231        */
  232       public boolean isActive()
  233       {
  234           return active;
  235       }
  236   
  237       /**
  238        * Set component active flag.
  239        * 
  240        * @param active active flag
  241        */
  242       public void setActive(boolean active)
  243       {
  244           this.active = active;
  245       }
  246   
  247       /**
  248        * Type of the context.
  249        * 
  250        * @return type of the context
  251        * @see ContextTypes
  252        */
  253       public ContextTypes getType()
  254       {
  255           return type;
  256       }
  257   
  258       /**
  259        * {@inheritDoc}
  260        */
  261       public Map<Contextual<?>, Object> getComponentInstanceMap()
  262       {
  263           return componentInstanceMap;
  264       }
  265   
  266       /**
  267        * {@inheritDoc}
  268        */
  269       public Class<? extends Annotation> getScope()
  270       {
  271   
  272           return this.scopeType;
  273       }
  274   
  275       /**
  276        * {@inheritDoc}
  277        */
  278       protected abstract void setComponentInstanceMap();
  279       
  280       /**
  281        * Check that context is active or throws exception.
  282        */
  283       protected void checkActive()
  284       {
  285           if (!active)
  286           {
  287               throw new ContextNotActiveException("WebBeans context with scope annotation @" + getScope().getName() + " is not active with respect to the current thread");
  288           }        
  289       }
  290   
  291   }

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