1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import javax.servlet.ServletConfig;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
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 264148 2005-08-29 14:21:04Z henning $
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 req,
65 HttpServletResponse res,
66 ServletConfig config)
67 throws TurbineException,
68 IllegalArgumentException
69 {
70
71
72
73
74
75
76
77 if (req == null ||
78 res == null ||
79 config == null)
80 {
81 throw new IllegalArgumentException(
82 "RunDataFactory fatal error: HttpServletRequest, " +
83 "HttpServletResponse or ServletConfig were null.");
84 }
85
86
87
88
89
90
91
92
93
94 if (tryRunDataService)
95 {
96 try
97 {
98 return TurbineRunDataFacade.getRunData(req, res, config);
99 }
100 catch (Exception x)
101 {
102 log.info("No Run Data Service available, not trying again!");
103 tryRunDataService = false;
104 }
105 }
106
107
108 TurbineRunData data =
109 (TurbineRunData) TurbinePool.getInstance(DefaultTurbineRunData.class);
110
111
112 data.setRequest(req);
113 data.setResponse(res);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 String contextPath = req.getContextPath();
129
130 String scriptName = contextPath + data.getRequest().getServletPath();
131
132
133 data.setCookieParser(new DefaultCookieParser());
134
135
136 data.setParameterParser(new DefaultParameterParser());
137
138
139 data.setSession(data.getRequest().getSession(true));
140
141
142
143 data.setServletConfig(config);
144
145
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
162 if (tryRunDataService)
163 {
164 try
165 {
166 TurbineRunDataFacade.putRunData(data);
167 return;
168 }
169 catch (Exception x)
170 {
171 }
172 }
173
174
175 TurbinePool.putInstance(data);
176 }
177 }