View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.orchestra.lib.jsf;
20  
21  import java.util.Map;
22  
23  import javax.faces.FacesException;
24  import javax.faces.context.ExternalContext;
25  import javax.faces.context.FacesContext;
26  
27  import org.apache.myfaces.orchestra.CoreConfig;
28  import org.apache.myfaces.orchestra.conversation.ConversationContext;
29  import org.apache.myfaces.orchestra.conversation.ConversationManager;
30  
31  /**
32   * RequestHandler that ensures that only one thread is processing
33   * each ConversationContext at a time.
34   * 
35   * @since 1.1
36   */
37  class ContextLockRequestHandler implements RequestHandler
38  {
39      ConversationContext context;
40  
41      public void init(FacesContext facesContext) throws FacesException
42      {
43          if (getSerializeRequests(facesContext))
44          {
45              // Note that ConversationManager.getInstance requires the FrameworkAdapter
46              // to be initialised...
47              //
48              // ?? Why is false passed here ??
49              ConversationManager manager = ConversationManager.getInstance(false);
50              if (manager != null)
51              {
52                  context = manager.getCurrentConversationContext();
53                  if (context != null)
54                  {
55                      try
56                      {
57                          context.lockInterruptablyForCurrentThread();
58                      }
59                      catch(InterruptedException e)
60                      {
61                          throw new FacesException(e);
62                      }
63                  }
64              }
65          }
66      }
67  
68      public void deinit() throws FacesException
69      {
70          if (context != null)
71          {
72              context.unlockForCurrentThread();
73          }
74      }
75      
76      private boolean getSerializeRequests(FacesContext facesContext)
77      {
78          ExternalContext ec = facesContext.getExternalContext();
79  
80          // Check for deprecated setting via the OrchestraServletFilter.
81          Map reqScope = ec.getRequestMap();
82          Boolean serializeRequests = (Boolean) reqScope.get(CoreConfig.SERIALIZE_REQUESTS);
83          if (serializeRequests != null)
84          {
85              return serializeRequests.booleanValue();
86          }
87  
88          // Check for the normal global init param; true unless "false" is specified 
89          String value = ec.getInitParameter(CoreConfig.SERIALIZE_REQUESTS);
90          return !"false".equals(value); // NON-NLS
91      }
92  }
93