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.Map;
   18   import java.util.concurrent.ConcurrentHashMap;
   19   
   20   import javax.enterprise.context.ApplicationScoped;
   21   import javax.enterprise.context.ContextNotActiveException;
   22   import javax.enterprise.context.ConversationScoped;
   23   import javax.enterprise.context.Dependent;
   24   import javax.enterprise.context.RequestScoped;
   25   import javax.enterprise.context.SessionScoped;
   26   import javax.enterprise.context.spi.Context;
   27   import javax.servlet.ServletContext;
   28   import javax.servlet.ServletRequestEvent;
   29   import javax.servlet.http.HttpServletRequest;
   30   import javax.servlet.http.HttpSession;
   31   
   32   import org.apache.webbeans.context.type.ContextTypes;
   33   import org.apache.webbeans.conversation.ConversationManager;
   34   
   35   /**
   36    * Gets current {@link WebBeansContext} instances on the current thread context.
   37    * 
   38    * @see WebBeansContext
   39    * @see AbstractContext
   40    */
   41   public final class ContextFactory
   42   {
   43       private static ThreadLocal<RequestContext> requestContext = null;
   44   
   45       private static ThreadLocal<SessionContext> sessionContext = null;
   46   
   47       private static ThreadLocal<ApplicationContext> applicationContext = null;
   48   
   49       private static ThreadLocal<ConversationContext> conversationContext = null;
   50   
   51       private static ThreadLocal<DependentContext> dependentContext = null;
   52   
   53       private static Map<ServletContext, ApplicationContext> currentApplicationContexts = new ConcurrentHashMap<ServletContext, ApplicationContext>();
   54   
   55       private static SessionContextManager sessionCtxManager = SessionContextManager.getInstance();
   56   
   57       private static ConversationManager conversationManager = ConversationManager.getInstance();
   58   
   59       static
   60       {
   61           requestContext = new ThreadLocal<RequestContext>();
   62           sessionContext = new ThreadLocal<SessionContext>();
   63           applicationContext = new ThreadLocal<ApplicationContext>();
   64           conversationContext = new ThreadLocal<ConversationContext>();
   65           dependentContext = new ThreadLocal<DependentContext>();
   66       }
   67   
   68       private ContextFactory()
   69       {
   70           throw new UnsupportedOperationException();
   71       }
   72   
   73       /**
   74        * Initialize requext context with the given request object.
   75        * 
   76        * @param request http servlet request object
   77        */
   78       public static void initRequestContext(ServletRequestEvent event)
   79       {
   80           requestContext.set(new RequestContext());// set thread local
   81           requestContext.get().setActive(true);
   82           
   83           if(event != null)
   84           {
   85               HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
   86               
   87               if (request != null)
   88               {
   89                   //Re-initialize thread local for session
   90                   HttpSession session = request.getSession(false);
   91                   
   92                   if(session != null)
   93                   {
   94                       initSessionContext(session);    
   95                   }
   96                               
   97                   //Re-initialize thread local for application
   98                   initApplicationContext(event.getServletContext());
   99               }            
  100           }
  101       }
  102   
  103       public static Context getCustomContext(Context context)
  104       {
  105           return new CustomContextImpl(context);
  106       }
  107       
  108       /**
  109        * Destroys the request context and all of its components.
  110        * 
  111        * @param request http servlet request object
  112        */
  113       public static void destroyRequestContext(HttpServletRequest request)
  114       {
  115           if (requestContext != null)
  116           {
  117               RequestContext context = getRequestContext();
  118   
  119               if (context != null)
  120               {
  121                   context.destroy();
  122               }
  123   
  124               requestContext.remove();
  125               
  126           }
  127       }
  128   
  129       /**
  130        * Creates the session context at the session start.
  131        * 
  132        * @param session http session object
  133        */
  134       public static void initSessionContext(HttpSession session)
  135       {
  136           String sessionId = session.getId();
  137           SessionContext currentSessionContext = sessionCtxManager.getSessionContextWithSessionId(sessionId);
  138   
  139           if (currentSessionContext == null)
  140           {
  141               currentSessionContext = new SessionContext();
  142               sessionCtxManager.addNewSessionContext(sessionId, currentSessionContext);
  143           }
  144   
  145           currentSessionContext.setActive(true);
  146   
  147           sessionContext.set(currentSessionContext);
  148       }
  149   
  150       /**
  151        * Destroys the session context and all of its components at the end of the
  152        * session.
  153        * 
  154        * @param session http session object
  155        */
  156       public static void destroySessionContext(HttpSession session)
  157       {
  158           if (sessionContext != null)
  159           {
  160               SessionContext context = getSessionContext();
  161   
  162               if (context != null)
  163               {
  164                   context.destroy();
  165               }
  166   
  167               sessionContext.remove();
  168   
  169           }
  170   
  171           sessionCtxManager.destroySessionContextWithSessionId(session.getId());
  172   
  173       }
  174   
  175       /**
  176        * Creates the application context at the application startup
  177        * 
  178        * @param servletContext servlet context object
  179        */
  180       public static void initApplicationContext(ServletContext servletContext)
  181       {
  182           
  183           if(servletContext != null && currentApplicationContexts.containsKey(servletContext))
  184           {
  185               applicationContext.set(currentApplicationContexts.get(servletContext));
  186           }
  187           
  188           else
  189           {
  190               ApplicationContext currentApplicationContext = new ApplicationContext();         
  191               currentApplicationContext.setActive(true);
  192               
  193               if(servletContext != null)
  194               {
  195                   currentApplicationContexts.put(servletContext, currentApplicationContext);
  196                   
  197               }
  198               
  199               applicationContext.set(currentApplicationContext);
  200      
  201           }
  202       }
  203   
  204       /**
  205        * Destroys the application context and all of its components at the end of
  206        * the application.
  207        * 
  208        * @param servletContext servlet context object
  209        */
  210       public static void destroyApplicationContext(ServletContext servletContext)
  211       {
  212           if (applicationContext != null)
  213           {
  214               ApplicationContext context = getApplicationContext();
  215   
  216               if (context != null)
  217               {
  218                   context.destroy();
  219               }
  220   
  221               applicationContext.remove();
  222   
  223           }
  224           
  225           if(servletContext != null)
  226           {
  227               currentApplicationContexts.remove(servletContext);   
  228           }
  229           
  230           sessionCtxManager.destroyAllSessions();
  231           conversationManager.destroyAllConversations();
  232       }
  233   
  234       public static void initConversationContext(ConversationContext context)
  235       {
  236           if (context == null)
  237           {
  238               if(conversationContext.get() == null)
  239               {
  240                   ConversationContext newContext = new ConversationContext();
  241                   newContext.setActive(true);
  242                   
  243                   conversationContext.set(newContext);                
  244               }
  245               else
  246               {
  247                   conversationContext.get().setActive(true);
  248               }
  249               
  250           }
  251           else
  252           {
  253               context.setActive(true);
  254               conversationContext.set(context);
  255           }
  256       }
  257   
  258       public static void destroyConversationContext()
  259       {
  260           if (conversationContext != null)
  261           {
  262               ConversationContext context = getConversationContext();
  263   
  264               if (context != null)
  265               {
  266                   context.destroy();
  267               }
  268   
  269               conversationContext.remove();
  270           }
  271       }
  272   
  273       /**
  274        * Gets the current context with given type.
  275        * 
  276        * @return the current context
  277        * @throws ContextNotActiveException if context is not active
  278        * @throws IllegalArgumentException if the type is not a standard context
  279        */
  280       public static WebBeansContext getStandartContext(ContextTypes type) throws ContextNotActiveException
  281       {
  282           WebBeansContext context = null;
  283   
  284           switch (type.getName())
  285           {
  286               case 0:
  287                   context = getRequestContext();
  288                   break;
  289       
  290               case 1:
  291                   context = getSessionContext();
  292                   break;
  293       
  294               case 2:
  295                   context = getApplicationContext();
  296                   break;
  297       
  298               case 3:
  299                   context = getConversationContext();
  300                   break;
  301                   
  302               case 4:
  303                   context = getDependentContext();
  304                   break;
  305               
  306               default:
  307                   throw new IllegalArgumentException("There is no such a standard context with name id=" + type.getName());
  308           }
  309   
  310           return context;
  311       }
  312   
  313       /**
  314        * Gets the standard context with given scope type.
  315        * 
  316        * @return the current context, or <code>null</code> if no standard context exists for the given scopeType
  317        */
  318       public static WebBeansContext getStandardContext(Class<? extends Annotation> scopeType)
  319       {
  320           WebBeansContext context = null;
  321   
  322           if (scopeType.equals(RequestScoped.class))
  323           {
  324               context = getRequestContext();
  325           }
  326           else if (scopeType.equals(SessionScoped.class))
  327           {
  328               context = getSessionContext();
  329           }
  330           else if (scopeType.equals(ApplicationScoped.class))
  331           {
  332               context = getApplicationContext();
  333           }
  334           else if (scopeType.equals(ConversationScoped.class))
  335           {
  336               context = getConversationContext();
  337   
  338           }
  339           else if (scopeType.equals(Dependent.class))
  340           {
  341               context = getDependentContext();
  342           }
  343           
  344           return context;
  345       }
  346   
  347       /*
  348        * Get current request ctx.
  349        */
  350       private static RequestContext getRequestContext()
  351       {
  352           return requestContext.get();
  353       }
  354   
  355       /*
  356        * Get current session ctx.
  357        */
  358       private static SessionContext getSessionContext()
  359       {
  360           return sessionContext.get();
  361       }
  362   
  363        private static ApplicationContext getApplicationContext()
  364       {
  365           return applicationContext.get();
  366       }
  367   
  368       /*
  369        * Get current conversation ctx.
  370        */
  371       private static ConversationContext getConversationContext()
  372       {
  373           return conversationContext.get();
  374       }
  375   
  376       public static DependentContext getDependentContext()
  377       {
  378           DependentContext dependentCtx = dependentContext.get();
  379   
  380           if (dependentCtx == null)
  381           {
  382               dependentCtx = new DependentContext();
  383               dependentContext.set(dependentCtx);
  384           }
  385   
  386           return dependentCtx;
  387       }     
  388   }

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