View Javadoc
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 javax.servlet.ServletConfig; 58 import javax.servlet.http.HttpServletRequest; 59 import javax.servlet.http.HttpServletResponse; 60 import org.apache.turbine.services.TurbineServices; 61 import org.apache.turbine.services.pool.PoolService; 62 import org.apache.turbine.services.rundata.DefaultTurbineRunData; 63 import org.apache.turbine.services.rundata.RunDataService; 64 import org.apache.turbine.services.rundata.TurbineRunData; 65 import org.apache.turbine.util.parser.DefaultCookieParser; 66 import org.apache.turbine.util.parser.DefaultParameterParser; 67 68 /*** 69 * Creates instances of RunData for use within Turbine or 3rd party 70 * applications. 71 * 72 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a> 73 * @author <a href="mailto:burton@relativity.yi.org">Kevin A. Burton</a> 74 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 75 * @version $Id: RunDataFactory.java,v 1.2 2002/07/11 16:53:21 mpoeschl Exp $ 76 */ 77 public class RunDataFactory 78 { 79 /*** 80 * A flag for the RunData Service. 81 */ 82 private static boolean tryRunDataService = true; 83 84 /*** 85 * Open way to get RunData information across Turbine.. 86 * 87 * @param req An HttpServletRequest. 88 * @param res An HttpServletResponse. 89 * @param config A ServletConfig. 90 * @throws TurbineException. 91 */ 92 public static RunData getRunData( HttpServletRequest req, 93 HttpServletResponse res, 94 ServletConfig config ) 95 throws TurbineException, 96 IllegalArgumentException 97 { 98 /* 99 * NOTE: getRunData( HttpServletRequest req, 100 * HttpServletResponse res ) has been deprecated 3-3-2000. 101 * Wait a couple months (before Turbine 1.0) and remove this 102 * method. Also don't allow null for req, res, or config as 103 * these are now required by Turbine. Uncomment the below as 104 * this should include the necessary functionality when we are 105 * ready. 106 */ 107 if ( req == null || 108 res == null || 109 config == null ) 110 { 111 throw new IllegalArgumentException( 112 "RunDataFactory fatal error: HttpServletRequest, " + 113 "HttpServletResponse or ServletConfig were null." ); 114 } 115 116 // Create a new RunData object. This object caches all the 117 // information that is needed for the execution lifetime of a 118 // single request. A new RunData object is created for each 119 // and every request and is passed to each and every module. 120 // Since each thread has its own RunData object, it is not 121 // necessary to perform syncronization for the data within 122 // this object. 123 // Try to retrieve the RunData implementation from the RunData Service. 124 if (tryRunDataService) 125 { 126 try 127 { 128 RunDataService service = (RunDataService) 129 TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME); 130 return service.getRunData(req,res,config); 131 } 132 catch (Exception x) 133 { 134 tryRunDataService = false; 135 } 136 } 137 138 // Failed, create a default implementation using the Pool Service. 139 PoolService pool = (PoolService) 140 TurbineServices.getInstance().getService(PoolService.SERVICE_NAME); 141 TurbineRunData data = (TurbineRunData) 142 pool.getInstance(DefaultTurbineRunData.class); 143 144 // Cache some information that will be used elsewhere. 145 data.setRequest(req); 146 data.setResponse(res); 147 148 // Let the implementation to create messages on demand. 149 // data.setMessages(new FormMessages()); 150 151 // data.context = this.getServletContext(); 152 153 // Don't set this because if we want to output via 154 // res.getOutputStream() then we will get an 155 // IllegalStateException (already called getWriter()). The 156 // solution is to only do this if data.getOut() is called and 157 // data.out is null. -jss 158 159 // data.setOut(data.getResponse().getWriter()); 160 161 // Allow Turbine to work with both 2.2 (and 2.1) and 2.0 162 // Servlet API. 163 String contextPath = null; 164 Class jsdkClass = HttpServletRequest.class; 165 try 166 { 167 java.lang.reflect.Method meth = 168 jsdkClass.getDeclaredMethod("getContextPath", null); 169 contextPath = (String) meth.invoke(req, null); 170 } 171 catch (Exception ex) 172 { 173 // Ignore a NoSuchMethodException because it means we are 174 // using Servlet API 2.0. Make sure scriptName is not 175 // null. 176 contextPath = ""; 177 } 178 179 String scriptName = contextPath + data.getRequest().getServletPath(); 180 181 // Sets the default cookie parser. 182 data.setCookieParser(new DefaultCookieParser()); 183 184 // Contains all of the GET/POST parameters. 185 data.setParameterParser(new DefaultParameterParser()); 186 187 // Get the HttpSession object. 188 data.setSession( data.getRequest().getSession(true) ); 189 190 // Set the servlet configuration in RunData for use in loading 191 // other servlets. 192 data.setServletConfig(config); 193 194 // Now set the ServerData. 195 data.setServerData( new ServerData( data.getRequest().getServerName(), 196 data.getRequest().getServerPort(), 197 data.getRequest().getScheme(), 198 scriptName, 199 contextPath ) ); 200 return (RunData) data; 201 } 202 203 /*** 204 * Returns the used RunData object back to the factory for recycling. 205 * 206 * @param data the used RunData object. 207 */ 208 public static void putRunData( RunData data ) 209 { 210 // Try to return the RunData implementation to the RunData Service. 211 if (tryRunDataService) 212 { 213 try 214 { 215 RunDataService service = (RunDataService) 216 TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME); 217 service.putRunData(data); 218 return; 219 } 220 catch (Exception x) 221 { 222 } 223 } 224 225 // Failed, use the Pool Service instead. 226 PoolService pool = (PoolService) 227 TurbineServices.getInstance().getService(PoolService.SERVICE_NAME); 228 pool.putInstance(data); 229 } 230 }

This page was automatically generated by Maven