1 package org.apache.turbine.services.rundata;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import javax.servlet.ServletConfig;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.commons.configuration.Configuration;
32
33 import org.apache.turbine.services.InitializationException;
34 import org.apache.turbine.services.TurbineBaseService;
35 import org.apache.turbine.services.pool.PoolService;
36 import org.apache.turbine.services.pool.TurbinePool;
37 import org.apache.turbine.util.RunData;
38 import org.apache.turbine.util.ServerData;
39 import org.apache.turbine.util.TurbineException;
40 import org.apache.turbine.util.parser.CookieParser;
41 import org.apache.turbine.util.parser.DefaultCookieParser;
42 import org.apache.turbine.util.parser.DefaultParameterParser;
43 import org.apache.turbine.util.parser.ParameterParser;
44
45 /***
46 * The RunData Service provides the implementations for RunData and
47 * related interfaces required by request processing. It supports
48 * different configurations of implementations, which can be selected
49 * by specifying a configuration key. It may use pooling, in which case
50 * the implementations should implement the Recyclable interface.
51 *
52 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
53 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
54 * @version $Id: TurbineRunDataService.java 645797 2008-04-08 08:51:24Z tv $
55 */
56 public class TurbineRunDataService
57 extends TurbineBaseService
58 implements RunDataService
59 {
60 /*** @deprecated Use RunDataService.RUN_DATA_KEY */
61 public static final String RUN_DATA =
62 RunDataService.RUN_DATA_KEY;
63
64 /*** @deprecated Use RunDataService.PARAMETER_PARSER_KEY */
65 public static final String PARAMETER_PARSER =
66 RunDataService.PARAMETER_PARSER_KEY;
67
68 /*** @deprecated Use RunDataService.COOKIE_PARSER_KEY */
69 public static final String COOKIE_PARSER =
70 RunDataService.COOKIE_PARSER_KEY;
71
72 /*** The default implementation of the RunData object*/
73 private static final String DEFAULT_RUN_DATA =
74 DefaultTurbineRunData.class.getName();
75
76 /*** The default implementation of the Parameter Parser object */
77 private static final String DEFAULT_PARAMETER_PARSER =
78 DefaultParameterParser.class.getName();
79
80 /*** The default implementation of the Cookie parser object */
81 private static final String DEFAULT_COOKIE_PARSER =
82 DefaultCookieParser.class.getName();
83
84 /*** The map of configurations. */
85 private Map configurations = new HashMap();
86
87 /*** Private reference to the pool service for object recycling */
88 private PoolService pool = null;
89
90 /***
91 * Constructs a RunData Service.
92 */
93 public TurbineRunDataService()
94 {
95 }
96
97 /***
98 * Initializes the service by setting the pool capacity.
99 *
100 * @throws InitializationException if initialization fails.
101 */
102 public void init()
103 throws InitializationException
104 {
105
106 String[] def = new String[]
107 {
108 DEFAULT_RUN_DATA,
109 DEFAULT_PARAMETER_PARSER,
110 DEFAULT_COOKIE_PARSER
111 };
112 configurations.put(DEFAULT_CONFIG, def.clone());
113
114
115 Configuration conf = getConfiguration();
116 if (conf != null)
117 {
118 String key,value;
119 String[] config;
120 String[] plist = new String[]
121 {
122 RUN_DATA_KEY,
123 PARAMETER_PARSER_KEY,
124 COOKIE_PARSER_KEY
125 };
126 for (Iterator i = conf.getKeys(); i.hasNext();)
127 {
128 key = (String) i.next();
129 value = conf.getString(key);
130 for (int j = 0; j < plist.length; j++)
131 {
132 if (key.endsWith(plist[j]) &&
133 (key.length() > (plist[j].length() + 1)))
134 {
135 key = key.substring(0, key.length() - plist[j].length() - 1);
136 config = (String[]) configurations.get(key);
137 if (config == null)
138 {
139 config = (String[]) def.clone();
140 configurations.put(key, config);
141 }
142 config[j] = value;
143 break;
144 }
145 }
146 }
147 }
148 pool = TurbinePool.getService();
149
150 if (pool == null)
151 {
152 throw new InitializationException("RunData Service requires"
153 + " configured Pool Service!");
154 }
155
156 setInit(true);
157 }
158
159 /***
160 * Gets a default RunData object.
161 *
162 * @param req a servlet request.
163 * @param res a servlet response.
164 * @param config a servlet config.
165 * @return a new or recycled RunData object.
166 * @throws TurbineException if the operation fails.
167 */
168 public RunData getRunData(HttpServletRequest req,
169 HttpServletResponse res,
170 ServletConfig config)
171 throws TurbineException
172 {
173 return getRunData(DEFAULT_CONFIG, req, res, config);
174 }
175
176 /***
177 * Gets a RunData instance from a specific configuration.
178 *
179 * @param key a configuration key.
180 * @param req a servlet request.
181 * @param res a servlet response.
182 * @param config a servlet config.
183 * @return a new or recycled RunData object.
184 * @throws TurbineException if the operation fails.
185 * @throws IllegalArgumentException if any of the parameters are null.
186 */
187 public RunData getRunData(String key,
188 HttpServletRequest req,
189 HttpServletResponse res,
190 ServletConfig config)
191 throws TurbineException,
192 IllegalArgumentException
193 {
194
195
196
197
198
199
200 if ((req == null)
201 || (res == null)
202 || (config == null))
203 {
204 throw new IllegalArgumentException("HttpServletRequest, "
205 + "HttpServletResponse or ServletConfig was null.");
206 }
207
208
209 String[] cfg = (String[]) configurations.get(key);
210 if (cfg == null)
211 {
212 throw new TurbineException("RunTime configuration '" + key + "' is undefined");
213 }
214
215 TurbineRunData data;
216 try
217 {
218 data = (TurbineRunData) pool.getInstance(cfg[0]);
219
220 ParameterParser pp = (ParameterParser) pool.getInstance(cfg[1]);
221 data.setParameterParser(pp);
222
223 CookieParser cp = (CookieParser) pool.getInstance(cfg[2]);
224 data.setCookieParser(cp);
225
226 Locale locale = req.getLocale();
227
228 if (locale == null)
229 {
230
231 locale = data.getLocale();
232 }
233
234
235 data.setLocale(locale);
236 }
237 catch (ClassCastException x)
238 {
239 throw new TurbineException("RunData configuration '" + key + "' is illegal", x);
240 }
241
242
243 data.setRequest(req);
244 data.setResponse(res);
245
246
247 data.setServletConfig(config);
248
249
250 data.setServerData(new ServerData(req));
251
252 return data;
253 }
254
255 /***
256 * Puts the used RunData object back to the factory for recycling.
257 *
258 * @param data the used RunData object.
259 * @return true, if pooling is supported and the object was accepted.
260 */
261 public boolean putRunData(RunData data)
262 {
263 if (data instanceof TurbineRunData)
264 {
265 pool.putInstance(((TurbineRunData) data).getParameterParser());
266 pool.putInstance(((TurbineRunData) data).getCookieParser());
267
268 return pool.putInstance(data);
269 }
270 else
271 {
272 return false;
273 }
274 }
275 }