001: package org.apache.turbine.services.rundata;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.servlet.ServletConfig;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.configuration.Configuration;
031:
032: import org.apache.turbine.services.InitializationException;
033: import org.apache.turbine.services.TurbineBaseService;
034: import org.apache.turbine.services.pool.PoolService;
035: import org.apache.turbine.services.pool.TurbinePool;
036: import org.apache.turbine.util.RunData;
037: import org.apache.turbine.util.ServerData;
038: import org.apache.turbine.util.TurbineException;
039: import org.apache.turbine.util.parser.CookieParser;
040: import org.apache.turbine.util.parser.DefaultCookieParser;
041: import org.apache.turbine.util.parser.DefaultParameterParser;
042: import org.apache.turbine.util.parser.ParameterParser;
043:
044: /**
045: * The RunData Service provides the implementations for RunData and
046: * related interfaces required by request processing. It supports
047: * different configurations of implementations, which can be selected
048: * by specifying a configuration key. It may use pooling, in which case
049: * the implementations should implement the Recyclable interface.
050: *
051: * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
052: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
053: * @version $Id: TurbineRunDataService.java 534527 2007-05-02 16:10:59Z tv $
054: */
055: public class TurbineRunDataService extends TurbineBaseService implements
056: RunDataService {
057: /** @deprecated Use RunDataService.RUN_DATA_KEY */
058: public static final String RUN_DATA = RunDataService.RUN_DATA_KEY;
059:
060: /** @deprecated Use RunDataService.PARAMETER_PARSER_KEY */
061: public static final String PARAMETER_PARSER = RunDataService.PARAMETER_PARSER_KEY;
062:
063: /** @deprecated Use RunDataService.COOKIE_PARSER_KEY */
064: public static final String COOKIE_PARSER = RunDataService.COOKIE_PARSER_KEY;
065:
066: /** The default implementation of the RunData object*/
067: private static final String DEFAULT_RUN_DATA = DefaultTurbineRunData.class
068: .getName();
069:
070: /** The default implementation of the Parameter Parser object */
071: private static final String DEFAULT_PARAMETER_PARSER = DefaultParameterParser.class
072: .getName();
073:
074: /** The default implementation of the Cookie parser object */
075: private static final String DEFAULT_COOKIE_PARSER = DefaultCookieParser.class
076: .getName();
077:
078: /** The map of configurations. */
079: private Map configurations = new HashMap();
080:
081: /** Private reference to the pool service for object recycling */
082: private PoolService pool = null;
083:
084: /**
085: * Constructs a RunData Service.
086: */
087: public TurbineRunDataService() {
088: }
089:
090: /**
091: * Initializes the service by setting the pool capacity.
092: *
093: * @throws InitializationException if initialization fails.
094: */
095: public void init() throws InitializationException {
096: // Create a default configuration.
097: String[] def = new String[] { DEFAULT_RUN_DATA,
098: DEFAULT_PARAMETER_PARSER, DEFAULT_COOKIE_PARSER };
099: configurations.put(DEFAULT_CONFIG, def.clone());
100:
101: // Check other configurations.
102: Configuration conf = getConfiguration();
103: if (conf != null) {
104: String key, value;
105: String[] config;
106: String[] plist = new String[] { RUN_DATA_KEY,
107: PARAMETER_PARSER_KEY, COOKIE_PARSER_KEY };
108: for (Iterator i = conf.getKeys(); i.hasNext();) {
109: key = (String) i.next();
110: value = conf.getString(key);
111: for (int j = 0; j < plist.length; j++) {
112: if (key.endsWith(plist[j])
113: && (key.length() > (plist[j].length() + 1))) {
114: key = key.substring(0, key.length()
115: - plist[j].length() - 1);
116: config = (String[]) configurations.get(key);
117: if (config == null) {
118: config = (String[]) def.clone();
119: configurations.put(key, config);
120: }
121: config[j] = value;
122: break;
123: }
124: }
125: }
126: }
127: pool = TurbinePool.getService();
128:
129: if (pool == null) {
130: throw new InitializationException(
131: "RunData Service requires"
132: + " configured Pool Service!");
133: }
134:
135: setInit(true);
136: }
137:
138: /**
139: * Gets a default RunData object.
140: *
141: * @param req a servlet request.
142: * @param res a servlet response.
143: * @param config a servlet config.
144: * @return a new or recycled RunData object.
145: * @throws TurbineException if the operation fails.
146: */
147: public RunData getRunData(HttpServletRequest req,
148: HttpServletResponse res, ServletConfig config)
149: throws TurbineException {
150: return getRunData(DEFAULT_CONFIG, req, res, config);
151: }
152:
153: /**
154: * Gets a RunData instance from a specific configuration.
155: *
156: * @param key a configuration key.
157: * @param req a servlet request.
158: * @param res a servlet response.
159: * @param config a servlet config.
160: * @return a new or recycled RunData object.
161: * @throws TurbineException if the operation fails.
162: * @throws IllegalArgumentException if any of the parameters are null.
163: */
164: public RunData getRunData(String key, HttpServletRequest req,
165: HttpServletResponse res, ServletConfig config)
166: throws TurbineException, IllegalArgumentException {
167: // The RunData object caches all the information that is needed for
168: // the execution lifetime of a single request. A RunData object
169: // is created/recycled for each and every request and is passed
170: // to each and every module. Since each thread has its own RunData
171: // object, it is not necessary to perform syncronization for
172: // the data within this object.
173: if ((req == null) || (res == null) || (config == null)) {
174: throw new IllegalArgumentException("HttpServletRequest, "
175: + "HttpServletResponse or ServletConfig was null.");
176: }
177:
178: // Get the specified configuration.
179: String[] cfg = (String[]) configurations.get(key);
180: if (cfg == null) {
181: throw new TurbineException("RunTime configuration '" + key
182: + "' is undefined");
183: }
184:
185: TurbineRunData data;
186: try {
187: data = (TurbineRunData) pool.getInstance(cfg[0]);
188: data.setParameterParser((ParameterParser) pool
189: .getInstance(cfg[1]));
190: data.setCookieParser((CookieParser) pool
191: .getInstance(cfg[2]));
192: } catch (ClassCastException x) {
193: throw new TurbineException("RunData configuration '" + key
194: + "' is illegal", x);
195: }
196:
197: // Set the request and response.
198: data.setRequest(req);
199: data.setResponse(res);
200:
201: // Set the servlet configuration.
202: data.setServletConfig(config);
203:
204: // Set the ServerData.
205: data.setServerData(new ServerData(req));
206:
207: return data;
208: }
209:
210: /**
211: * Puts the used RunData object back to the factory for recycling.
212: *
213: * @param data the used RunData object.
214: * @return true, if pooling is supported and the object was accepted.
215: */
216: public boolean putRunData(RunData data) {
217: if (data instanceof TurbineRunData) {
218: pool.putInstance(((TurbineRunData) data)
219: .getParameterParser());
220: pool.putInstance(((TurbineRunData) data).getCookieParser());
221:
222: return pool.putInstance(data);
223: } else {
224: return false;
225: }
226: }
227: }
|