View Javadoc

1   /*
2    * Copyright 2000-2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  // package
18  package org.apache.jetspeed.services.rundata;
19  
20  // Java classes
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  import javax.servlet.ServletConfig;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  // Turbine classes
29  import org.apache.turbine.services.rundata.TurbineRunDataService;
30  import org.apache.turbine.services.InitializationException;
31  import org.apache.turbine.util.RunData;
32  import org.apache.turbine.util.TurbineException;
33  import org.apache.turbine.services.TurbineServices;
34  
35  // Jetspeed classes
36  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
37  import org.apache.jetspeed.services.logging.JetspeedLogger;
38  import org.apache.jetspeed.services.rundata.JetspeedRunData;
39  import org.apache.jetspeed.services.statemanager.StateManagerService;
40  
41  /***
42  * The JetspeedRunDataService extends TurbineRunDataService,
43  * adding the ability to get the current runData object for the thread
44  * at any time.  This is accomplished by storing the active runData objects
45  * in a map, keyed by thread.
46  * Also done here, because this is so nicely bracketed around each http request
47  * by Turbine, is the association of the http session for this request / thread
48  * with the state manager.
49  *
50  * @author <a href="mailto:ggolden@umich.edu">Glenn R. Golden</a>
51  * @version $Revision: 1.5 $
52  */
53  public class JetspeedRunDataService
54      extends TurbineRunDataService 
55  {
56      /***
57       * Static initialization of the logger for this class
58       */    
59      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedRunDataService.class.getName());
60      
61      /*** The collection of active JetspeedRunData objects, keyed by Thread. */
62      private Map m_runDataStore = null;
63  
64      /********************************************************************************
65      * Service implementation
66      *******************************************************************************/
67  
68      /***
69      * Initializes the service
70      *
71      * @throws InitializationException if initialization fails.
72      */
73      public void init()
74          throws InitializationException
75      {
76          super.init();
77  
78          // allocate a thread-safe map
79          m_runDataStore = Collections.synchronizedMap(new HashMap());
80  
81      }   // init
82  
83      /********************************************************************************
84      * TurbineRunDataService implementation
85      *******************************************************************************/
86  
87      /***
88      * Gets a RunData instance from a specific configuration.
89      *
90      * @param key a configuration key.
91      * @param req a servlet request.
92      * @param res a servlet response.
93      * @param config a servlet config.
94      * @return a new or recycled RunData object.
95      * @throws TurbineException if the operation fails.
96      * @throws IllegalArgumentException if any of the parameters are null.
97      */
98      public RunData getRunData(String key,
99                                  HttpServletRequest req,
100                                 HttpServletResponse res,
101                                 ServletConfig config)
102         throws TurbineException, IllegalArgumentException
103     {
104         // let the super do the work
105         JetspeedRunData r = (JetspeedRunData)super.getRunData(key, req, res, config);
106 
107         // store the rundata associated with this thread
108         m_runDataStore.put(Thread.currentThread(), r);
109 
110         // associate this http session with this thread in the state manager
111         StateManagerService stateManager = (StateManagerService)TurbineServices
112                 .getInstance().getService(StateManagerService.SERVICE_NAME);
113         if (stateManager != null)
114         {
115             stateManager.setCurrentContext(req.getSession(true));
116         }
117 
118         if (logger.isDebugEnabled())
119             logger.debug("JetspeedRunDataService: storing rundata " + r
120                         + " for thread: " + Thread.currentThread());
121 
122         return r;
123 
124     }   // getRunData
125 
126     /***
127     * Puts the used RunData object back to the factory for recycling.
128     *
129     * @param data the used RunData object.
130     * @return true, if pooling is supported and the object was accepted.
131     */
132     public boolean putRunData(RunData data)
133     {
134         // un-associate this http session with this thread in the state manager
135         StateManagerService stateManager = (StateManagerService)TurbineServices
136                 .getInstance().getService(StateManagerService.SERVICE_NAME);
137         if (stateManager != null)
138         {
139             stateManager.clearCurrentContext();
140         }
141 
142         // remove this thread's rundata
143         m_runDataStore.remove(Thread.currentThread());
144 
145         if (logger.isDebugEnabled())
146             logger.debug("JetspeedRunDataService: releasing rundata for thread: "
147                 + Thread.currentThread());
148 
149         // let super do the work
150         return super.putRunData(data);
151 
152     }   // putRunData
153 
154     /***
155     * Access the current rundata object - the one associated with the current thread.
156     * @return The current JetspeedRunData object associatd with the current thread.
157     */
158     public JetspeedRunData getCurrentRunData()
159     {
160         if (logger.isDebugEnabled())
161             logger.debug("JetspeedRunDataService: accessing rundata "
162                         + m_runDataStore.get(Thread.currentThread())
163                         + " for thread: " + Thread.currentThread());
164 
165         return (JetspeedRunData) m_runDataStore.get(Thread.currentThread());
166 
167     }   // getCurrentRunData
168 
169 }   // JetspeedRunDataService
170 
171 /***********************************************************************************
172 *
173 * $Header: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/rundata/JetspeedRunDataService.java,v 1.5 2004/02/23 03:36:10 jford Exp $
174 *
175 **********************************************************************************/
176