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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617// package18packageorg.apache.jetspeed.services.rundata;
1920// Java classes21import java.util.Collections;
22import java.util.HashMap;
23import java.util.Map;
24import javax.servlet.ServletConfig;
25import javax.servlet.http.HttpServletRequest;
26import javax.servlet.http.HttpServletResponse;
2728// Turbine classes29import org.apache.turbine.services.rundata.TurbineRunDataService;
30import org.apache.turbine.services.InitializationException;
31import org.apache.turbine.util.RunData;
32import org.apache.turbine.util.TurbineException;
33import org.apache.turbine.services.TurbineServices;
3435// Jetspeed classes36import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
37import org.apache.jetspeed.services.logging.JetspeedLogger;
38import org.apache.jetspeed.services.rundata.JetspeedRunData;
39import org.apache.jetspeed.services.statemanager.StateManagerService;
4041/***42* The JetspeedRunDataService extends TurbineRunDataService,43* adding the ability to get the current runData object for the thread44* at any time. This is accomplished by storing the active runData objects45* in a map, keyed by thread.46* Also done here, because this is so nicely bracketed around each http request47* by Turbine, is the association of the http session for this request / thread48* with the state manager.49*50* @author <a href="mailto:ggolden@umich.edu">Glenn R. Golden</a>51* @version $Revision: 1.5 $52*/53publicclassJetspeedRunDataService54extends TurbineRunDataService
55 {
56/***57 * Static initialization of the logger for this class58 */59privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedRunDataService.class.getName());
6061/*** The collection of active JetspeedRunData objects, keyed by Thread. */62private Map m_runDataStore = null;
6364/********************************************************************************65 * Service implementation66 *******************************************************************************/6768/***69 * Initializes the service70 *71 * @throws InitializationException if initialization fails.72 */73publicvoid init()
74 throws InitializationException
75 {
76super.init();
7778// allocate a thread-safe map79 m_runDataStore = Collections.synchronizedMap(new HashMap());
8081 } // init8283/********************************************************************************84 * TurbineRunDataService implementation85 *******************************************************************************/8687/***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 */98public RunData getRunData(String key,
99 HttpServletRequest req,
100 HttpServletResponse res,
101 ServletConfig config)
102 throws TurbineException, IllegalArgumentException
103 {
104// let the super do the work105JetspeedRunData r = (JetspeedRunData)super.getRunData(key, req, res, config);
106107// store the rundata associated with this thread108 m_runDataStore.put(Thread.currentThread(), r);
109110// associate this http session with this thread in the state manager111StateManagerService stateManager = (StateManagerService)TurbineServices
112 .getInstance().getService(StateManagerService.SERVICE_NAME);
113if (stateManager != null)
114 {
115 stateManager.setCurrentContext(req.getSession(true));
116 }
117118if (logger.isDebugEnabled())
119 logger.debug("JetspeedRunDataService: storing rundata " + r
120 + " for thread: " + Thread.currentThread());
121122return r;
123124 } // getRunData125126/***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 */132publicboolean putRunData(RunData data)
133 {
134// un-associate this http session with this thread in the state manager135StateManagerService stateManager = (StateManagerService)TurbineServices
136 .getInstance().getService(StateManagerService.SERVICE_NAME);
137if (stateManager != null)
138 {
139 stateManager.clearCurrentContext();
140 }
141142// remove this thread's rundata143 m_runDataStore.remove(Thread.currentThread());
144145if (logger.isDebugEnabled())
146 logger.debug("JetspeedRunDataService: releasing rundata for thread: "147 + Thread.currentThread());
148149// let super do the work150returnsuper.putRunData(data);
151152 } // putRunData153154/***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 */158publicJetspeedRunData getCurrentRunData()
159 {
160if (logger.isDebugEnabled())
161 logger.debug("JetspeedRunDataService: accessing rundata "162 + m_runDataStore.get(Thread.currentThread())
163 + " for thread: " + Thread.currentThread());
164165return (JetspeedRunData) m_runDataStore.get(Thread.currentThread());
166167 } // getCurrentRunData168169 } // JetspeedRunDataService170171/***********************************************************************************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