KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > RunDataFactory


1 package org.apache.turbine.util;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import javax.servlet.ServletConfig JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.apache.turbine.services.pool.TurbinePool;
27 import org.apache.turbine.services.rundata.DefaultTurbineRunData;
28 import org.apache.turbine.services.rundata.TurbineRunData;
29 import org.apache.turbine.services.rundata.TurbineRunDataFacade;
30 import org.apache.turbine.util.parser.DefaultCookieParser;
31 import org.apache.turbine.util.parser.DefaultParameterParser;
32
33 /**
34  * Creates instances of RunData for use within Turbine or 3rd party
35  * applications.
36  *
37  * @author <a HREF="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
38  * @author <a HREF="mailto:burton@relativity.yi.org">Kevin A. Burton</a>
39  * @author <a HREF="mailto:john.mcnally@clearink.com">John D. McNally</a>
40  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
41  * @version $Id: RunDataFactory.java,v 1.6.2.2 2004/05/20 03:16:38 seade Exp $
42  * @deprecated This factory tries to be the RunData Service if no RunData Service is
43  * configured. RunData Service is now mandatory for Turbine so use it directly without
44  * this factory.
45  */

46 public class RunDataFactory
47 {
48     /** Logging */
49     private static Log log = LogFactory.getLog(RunDataFactory.class);
50
51     /**
52      * A flag for the RunData Service.
53      */

54     private static boolean tryRunDataService = true;
55
56     /**
57      * Open way to get RunData information across Turbine..
58      *
59      * @param req An HttpServletRequest.
60      * @param res An HttpServletResponse.
61      * @param config A ServletConfig.
62      * @throws TurbineException.
63      */

64     public static RunData getRunData(HttpServletRequest JavaDoc req,
65                                      HttpServletResponse JavaDoc res,
66                                      ServletConfig JavaDoc config)
67             throws TurbineException,
68                    IllegalArgumentException JavaDoc
69     {
70         // NOTE: getRunData( HttpServletRequest req,
71
// HttpServletResponse res ) has been deprecated 3-3-2000.
72
// Wait a couple months (before Turbine 1.0) and remove this
73
// method. Also don't allow null for req, res, or config as
74
// these are now required by Turbine. Uncomment the below as
75
// this should include the necessary functionality when we are
76
// ready.
77
if (req == null ||
78                 res == null ||
79                 config == null)
80         {
81             throw new IllegalArgumentException JavaDoc(
82                     "RunDataFactory fatal error: HttpServletRequest, " +
83                     "HttpServletResponse or ServletConfig were null.");
84         }
85
86         // Create a new RunData object. This object caches all the
87
// information that is needed for the execution lifetime of a
88
// single request. A new RunData object is created for each
89
// and every request and is passed to each and every module.
90
// Since each thread has its own RunData object, it is not
91
// necessary to perform syncronization for the data within
92
// this object.
93
// Try to retrieve the RunData implementation from the RunData Service.
94
if (tryRunDataService)
95         {
96             try
97             {
98                 return TurbineRunDataFacade.getRunData(req, res, config);
99             }
100             catch (Exception JavaDoc x)
101             {
102                 log.info("No Run Data Service available, not trying again!");
103                 tryRunDataService = false;
104             }
105         }
106
107         // Failed, create a default implementation using the Pool Service.
108
TurbineRunData data =
109                 (TurbineRunData) TurbinePool.getInstance(DefaultTurbineRunData.class);
110
111         // Cache some information that will be used elsewhere.
112
data.setRequest(req);
113         data.setResponse(res);
114
115         // Let the implementation to create messages on demand.
116
// data.setMessages(new FormMessages());
117

118         // data.context = this.getServletContext();
119

120         // Don't set this because if we want to output via
121
// res.getOutputStream() then we will get an
122
// IllegalStateException (already called getWriter()). The
123
// solution is to only do this if data.getOut() is called and
124
// data.out is null. -jss
125

126         // data.setOut(data.getResponse().getWriter());
127

128         String JavaDoc contextPath = req.getContextPath();
129
130         String JavaDoc scriptName = contextPath + data.getRequest().getServletPath();
131
132         // Sets the default cookie parser.
133
data.setCookieParser(new DefaultCookieParser());
134
135         // Contains all of the GET/POST parameters.
136
data.setParameterParser(new DefaultParameterParser());
137
138         // Get the HttpSession object.
139
data.setSession(data.getRequest().getSession(true));
140
141         // Set the servlet configuration in RunData for use in loading
142
// other servlets.
143
data.setServletConfig(config);
144
145         // Now set the ServerData.
146
data.setServerData(new ServerData(data.getRequest().getServerName(),
147                 data.getRequest().getServerPort(),
148                 data.getRequest().getScheme(),
149                 scriptName,
150                 contextPath));
151         return (RunData) data;
152     }
153
154     /**
155      * Returns the used RunData object back to the factory for recycling.
156      *
157      * @param data the used RunData object.
158      */

159     public static void putRunData(RunData data)
160     {
161         // Try to return the RunData implementation to the RunData Service.
162
if (tryRunDataService)
163         {
164             try
165             {
166                  TurbineRunDataFacade.putRunData(data);
167                 return;
168             }
169             catch (Exception JavaDoc x)
170             {
171             }
172         }
173
174         // Failed, use the Pool Service instead.
175
TurbinePool.putInstance(data);
176     }
177 }
178
Popular Tags