1 package org.apache.turbine.util;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.IOException;
58
59 import javax.servlet.ServletConfig;
60 import javax.servlet.http.HttpSession;
61 import javax.servlet.http.HttpServletRequest;
62 import javax.servlet.http.HttpServletResponse;
63
64 import org.apache.turbine.services.TurbineServices;
65 import org.apache.turbine.services.pool.PoolService;
66 import org.apache.turbine.services.rundata.RunDataService;
67 import org.apache.turbine.services.rundata.TurbineRunData;
68 import org.apache.turbine.services.rundata.DefaultTurbineRunData;
69
70 import org.apache.turbine.util.parser.DefaultCookieParser;
71 import org.apache.turbine.util.parser.DefaultParameterParser;
72
73 /***
74 * Creates instances of RunData for use within Turbine or 3rd party
75 * applications.
76 *
77 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
78 * @author <a href="mailto:burton@relativity.yi.org">Kevin A. Burton</a>
79 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
80 * @version $Id$
81 */
82 public class RunDataFactory
83 {
84 /***
85 * A flag for the RunData Service.
86 */
87 private static boolean tryRunDataService = true;
88
89 /***
90 * Open way to get RunData information across Turbine..
91 *
92 * @param req An HttpServletRequest.
93 * @param res An HttpServletResponse.
94 * @param config A ServletConfig.
95 * @throws TurbineException.
96 */
97 public static RunData getRunData( HttpServletRequest req,
98 HttpServletResponse res,
99 ServletConfig config )
100 throws TurbineException,
101 IllegalArgumentException
102 {
103 /*
104 * NOTE: getRunData( HttpServletRequest req,
105 * HttpServletResponse res ) has been deprecated 3-3-2000.
106 * Wait a couple months (before Turbine 1.0) and remove this
107 * method. Also don't allow null for req, res, or config as
108 * these are now required by Turbine. Uncomment the below as
109 * this should include the necessary functionality when we are
110 * ready.
111 */
112 if ( req == null ||
113 res == null ||
114 config == null )
115 {
116 throw new IllegalArgumentException(
117 "RunDataFactory fatal error: HttpServletRequest, " +
118 "HttpServletResponse or ServletConfig were null." );
119 }
120
121 // Create a new RunData object. This object caches all the
122 // information that is needed for the execution lifetime of a
123 // single request. A new RunData object is created for each
124 // and every request and is passed to each and every module.
125 // Since each thread has its own RunData object, it is not
126 // necessary to perform syncronization for the data within
127 // this object.
128 // Try to retrieve the RunData implementation from the RunData Service.
129 if (tryRunDataService)
130 {
131 try
132 {
133 RunDataService service = (RunDataService)
134 TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
135 return service.getRunData(req,res,config);
136 }
137 catch (Exception x)
138 {
139 tryRunDataService = false;
140 }
141 }
142
143 // Failed, create a default implementation using the Pool Service.
144 PoolService pool = (PoolService)
145 TurbineServices.getInstance().getService(PoolService.SERVICE_NAME);
146 TurbineRunData data = (TurbineRunData)
147 pool.getInstance(DefaultTurbineRunData.class);
148
149 // Cache some information that will be used elsewhere.
150 data.setRequest(req);
151 data.setResponse(res);
152
153 // Let the implementation to create messages on demand.
154 // data.setMessages(new FormMessages());
155
156 // data.context = this.getServletContext();
157
158 // Don't set this because if we want to output via
159 // res.getOutputStream() then we will get an
160 // IllegalStateException (already called getWriter()). The
161 // solution is to only do this if data.getOut() is called and
162 // data.out is null. -jss
163
164 // data.setOut(data.getResponse().getWriter());
165
166 // Allow Turbine to work with both 2.2 (and 2.1) and 2.0
167 // Servlet API.
168 String contextPath = null;
169 Class jsdkClass = HttpServletRequest.class;
170 try
171 {
172 java.lang.reflect.Method meth =
173 jsdkClass.getDeclaredMethod("getContextPath", null);
174 contextPath = (String) meth.invoke(req, null);
175 }
176 catch (Exception ex)
177 {
178 // Ignore a NoSuchMethodException because it means we are
179 // using Servlet API 2.0. Make sure scriptName is not
180 // null.
181 contextPath = "";
182 }
183
184 String scriptName = contextPath + data.getRequest().getServletPath();
185
186 // Sets the default cookie parser.
187 data.setCookieParser(new DefaultCookieParser());
188
189 // Contains all of the GET/POST parameters.
190 data.setParameterParser(new DefaultParameterParser());
191
192 // Get the HttpSession object.
193 data.setSession( data.getRequest().getSession(true) );
194
195 // Set the servlet configuration in RunData for use in loading
196 // other servlets.
197 data.setServletConfig(config);
198
199 // Now set the ServerData.
200 data.setServerData( new ServerData( data.getRequest().getServerName(),
201 data.getRequest().getServerPort(),
202 data.getRequest().getScheme(),
203 scriptName,
204 contextPath ) );
205 return (RunData) data;
206 }
207
208 /***
209 * Returns the used RunData object back to the factory for recycling.
210 *
211 * @param data the used RunData object.
212 */
213 public static void putRunData( RunData data )
214 {
215 // Try to return the RunData implementation to the RunData Service.
216 if (tryRunDataService)
217 {
218 try
219 {
220 RunDataService service = (RunDataService)
221 TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
222 service.putRunData(data);
223 return;
224 }
225 catch (Exception x)
226 {
227 }
228 }
229
230 // Failed, use the Pool Service instead.
231 PoolService pool = (PoolService)
232 TurbineServices.getInstance().getService(PoolService.SERVICE_NAME);
233 pool.putInstance(data);
234 }
235 }
This page was automatically generated by Maven