Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.jsf » [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.jsf;
   15   
   16   import javax.enterprise.context.ConversationScoped;
   17   import javax.faces.component.UIViewRoot;
   18   import javax.faces.event.PhaseEvent;
   19   import javax.faces.event.PhaseId;
   20   import javax.faces.event.PhaseListener;
   21   
   22   import org.apache.webbeans.container.BeanManagerImpl;
   23   import org.apache.webbeans.context.ContextFactory;
   24   import org.apache.webbeans.context.ConversationContext;
   25   import org.apache.webbeans.conversation.ConversationImpl;
   26   import org.apache.webbeans.conversation.ConversationManager;
   27   import org.apache.webbeans.logger.WebBeansLogger;
   28   import org.apache.webbeans.util.JSFUtil;
   29   
   30   public class WebBeansPhaseListener implements PhaseListener
   31   {
   32       private static final long serialVersionUID = -8131516076829979596L;
   33   
   34       private static WebBeansLogger logger = WebBeansLogger.getLogger(WebBeansPhaseListener.class);
   35   
   36       private static ConversationManager conversationManager = ConversationManager.getInstance();
   37       
   38       public static final String CONVERSATION_ATTR_ID = "javax_webbeans_ConversationId";
   39   
   40       private ConversationImpl conversation = null;
   41   
   42       public void afterPhase(PhaseEvent phaseEvent)
   43       {
   44           if (phaseEvent.getPhaseId().equals(PhaseId.RESTORE_VIEW))
   45           {
   46               if (!JSFUtil.isPostBack())
   47               {
   48                   String cid = JSFUtil.getExternalContext().getRequestParameterMap().get("cid");
   49   
   50                   // non-faces get request
   51                   if (cid == null || cid.equals(""))
   52                   {
   53                       logger.info("Create new transitional conversation for non-faces request with view id : " + JSFUtil.getViewId());
   54                       
   55                       conversation = (ConversationImpl) conversationManager.createNewConversation();
   56   
   57                       ContextFactory.initConversationContext(null);
   58   
   59                   }
   60                   else
   61                   {
   62                       logger.info("Propogation of the conversation with id : " + cid + " for view : " + JSFUtil.getViewId());
   63                       conversation = (ConversationImpl) conversationManager.getConversation(cid);
   64   
   65                       // can not restore conversation, create new transitional
   66                       if (conversation == null)
   67                       {
   68                           logger.info("Propogated conversation can not be restored for view id : " + JSFUtil.getViewId() + ". Creates new transitional conversation");
   69                           conversation = (ConversationImpl) conversationManager.createNewConversation();
   70   
   71                           ContextFactory.initConversationContext(null);
   72                       }
   73                       else
   74                       {
   75                           ContextFactory.initConversationContext(conversationManager.getConversationContext(conversation));
   76                       }
   77                   }
   78               }
   79               else
   80               {
   81                   logger.info("Postback JSF Request for view id : " + JSFUtil.getViewId());
   82   
   83                   UIViewRoot viewRoot = JSFUtil.getViewRoot();
   84                   
   85                   Object attr = viewRoot.getAttributes().get(CONVERSATION_ATTR_ID);
   86   
   87                   String conversationId = null;
   88                   
   89                   if(attr != null)
   90                   {
   91                       conversationId = attr.toString();
   92                   }
   93                   
   94                   if (conversationId != null)
   95                   {
   96                       // look long running conversation if exist
   97                       conversation = (ConversationImpl) conversationManager.getConversation(conversationId);
   98                       
   99                       ContextFactory.initConversationContext(conversationManager.getConversationContext(conversation));
  100   
  101                   }
  102   
  103                   // no long running conversation, create one transitional
  104                   else
  105                   {
  106                       logger.info("Create new transient conversation for JSF postback view id : " + JSFUtil.getViewId());
  107                       
  108                       conversation = (ConversationImpl) conversationManager.createNewConversation();
  109   
  110                       ContextFactory.initConversationContext(null);
  111                   }
  112               }
  113           }
  114   
  115           else if (phaseEvent.getPhaseId().equals(PhaseId.RENDER_RESPONSE))
  116           {
  117               ConversationContext context = (ConversationContext) BeanManagerImpl.getManager().getContext(ConversationScoped.class);
  118   
  119               // if long running, saves it
  120               if (conversation.isLongRunning())
  121               {
  122                   logger.info("Conversation with id : " + conversation.getId() + " is marked as long running conversation");
  123                   
  124                   context.setActive(false);
  125                   
  126               }
  127   
  128               // else destroy conversation context
  129               else
  130               {
  131                   logger.info("Destroying the conversation context for view id : " + JSFUtil.getViewId());
  132                   
  133                   ContextFactory.destroyConversationContext();                                    
  134               }
  135   
  136           }
  137   
  138       }
  139   
  140       public void beforePhase(PhaseEvent phaseEvent)
  141       {
  142           if (phaseEvent.getPhaseId().equals(PhaseId.RESTORE_VIEW))
  143           {            
  144               ContextFactory.initConversationContext(null);            
  145           }
  146           
  147           else if(phaseEvent.getPhaseId().equals(PhaseId.APPLY_REQUEST_VALUES))
  148           {
  149   
  150               if (JSFUtil.isPostBack())
  151               {
  152                   logger.info("Activating the conversation context for view id : " + JSFUtil.getViewId());                
  153   
  154                   conversation.updateTimeOut();
  155               }            
  156           }
  157   
  158           else if (phaseEvent.getPhaseId().equals(PhaseId.RENDER_RESPONSE))
  159           {
  160               ConversationContext context = (ConversationContext) BeanManagerImpl.getManager().getContext(ConversationScoped.class);
  161   
  162               if (!JSFUtil.isPostBack())
  163               {
  164                   logger.info("Activating the conversation context for view id : " + JSFUtil.getViewId());
  165                   context.setActive(true);
  166   
  167                   conversation.updateTimeOut();
  168               }
  169   
  170               if (conversation.isLongRunning())
  171               {
  172                   UIViewRoot viewRoot = JSFUtil.getViewRoot();
  173                   
  174                   viewRoot.getAttributes().put(CONVERSATION_ATTR_ID, conversation.getId());
  175                   
  176                   JSFUtil.getExternalContext().getRequestMap().put("cid",conversation.getId());
  177               }
  178               else
  179               {
  180                   UIViewRoot viewRoot = JSFUtil.getViewRoot();
  181                   
  182                   viewRoot.getAttributes().remove(CONVERSATION_ATTR_ID);                
  183               }
  184           }
  185   
  186       }
  187   
  188       public PhaseId getPhaseId()
  189       {
  190           return PhaseId.ANY_PHASE;
  191       }
  192   
  193   }

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