1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.jetspeed.services.rundata;
19
20
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
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
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
79 m_runDataStore = Collections.synchronizedMap(new HashMap());
80
81 }
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
105 JetspeedRunData r = (JetspeedRunData)super.getRunData(key, req, res, config);
106
107
108 m_runDataStore.put(Thread.currentThread(), r);
109
110
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 }
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
135 StateManagerService stateManager = (StateManagerService)TurbineServices
136 .getInstance().getService(StateManagerService.SERVICE_NAME);
137 if (stateManager != null)
138 {
139 stateManager.clearCurrentContext();
140 }
141
142
143 m_runDataStore.remove(Thread.currentThread());
144
145 if (logger.isDebugEnabled())
146 logger.debug("JetspeedRunDataService: releasing rundata for thread: "
147 + Thread.currentThread());
148
149
150 return super.putRunData(data);
151
152 }
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 }
168
169 }
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