1 package org.apache.turbine.services.rundata; 2 3 56 57 import java.lang.reflect.Method ; 58 import java.util.HashMap ; 59 import java.util.Iterator ; 60 61 import javax.servlet.ServletConfig ; 62 import javax.servlet.http.HttpServletRequest ; 63 import javax.servlet.http.HttpServletResponse ; 64 65 import org.apache.fulcrum.BaseService; 66 import org.apache.fulcrum.InitializationException; 67 import org.apache.fulcrum.ServiceException; 68 import org.apache.fulcrum.TurbineServices; 69 import org.apache.fulcrum.parser.CookieParser; 70 import org.apache.fulcrum.parser.ParameterParser; 71 import org.apache.fulcrum.parser.DefaultParameterParser; 72 import org.apache.fulcrum.pool.PoolException; 73 import org.apache.fulcrum.pool.PoolService; 74 import org.apache.fulcrum.upload.UploadService; 75 import org.apache.turbine.RunData; 76 import org.apache.turbine.services.yaaficomponent.YaafiComponentService; 77 78 88 public class TurbineRunDataService 89 extends BaseService 90 implements RunDataService 91 { 92 95 public static final String RUN_DATA = "run.data"; 96 97 100 public static final String PARAMETER_PARSER = "parameter.parser"; 101 102 105 public static final String COOKIE_PARSER = "cookie.parser"; 106 107 110 private static final String DEFAULT_RUN_DATA = 111 "org.apache.fulcrum.rundata.DefaultTurbineRunData"; 112 private static final String DEFAULT_PARAMETER_PARSER = 113 "org.apache.fulcrum.parser.DefaultParameterParser"; 114 private static final String DEFAULT_COOKIE_PARSER = 115 "org.apache.fulcrum.parser.DefaultCookieParser"; 116 117 120 private HashMap configurations = new HashMap (); 121 122 125 private Method getContextPath; 126 127 130 public TurbineRunDataService() 131 { 132 try 134 { 135 getContextPath = 136 HttpServletRequest .class.getDeclaredMethod("getContextPath",null); 137 } 138 catch (NoSuchMethodException x) 139 { 140 } 143 } 144 145 150 public void init() 151 throws InitializationException 152 { 153 String [] def = new String [] 155 { 156 DEFAULT_RUN_DATA, 157 DEFAULT_PARAMETER_PARSER, 158 DEFAULT_COOKIE_PARSER 159 }; 160 configurations.put(DEFAULT_CONFIG,def.clone()); 161 162 if (getConfiguration() != null) 163 { 164 String key,value; 165 String [] config; 166 String [] plist = new String [] 167 { 168 RUN_DATA, 169 PARAMETER_PARSER, 170 COOKIE_PARSER 171 }; 172 for (Iterator i = getConfiguration().getKeys(); i.hasNext();) 173 { 174 key = (String ) i.next(); 175 value = getConfiguration().getString(key); 176 for (int j = 0; j < plist.length; j++) 177 { 178 if (key.endsWith(plist[j]) && 179 (key.length() > (plist[j].length() + 1))) 180 { 181 key = key.substring(0,key.length() - plist[j].length() - 1); 182 config = (String []) configurations.get(key); 183 if (config == null) 184 { 185 config = (String []) def.clone(); 186 configurations.put(key,config); 187 } 188 config[j] = value; 189 break; 190 } 191 } 192 } 193 } 194 setInit(true); 195 } 196 197 206 public RunData getRunData(HttpServletRequest req, 207 HttpServletResponse res, 208 ServletConfig config) 209 throws ServiceException 210 { 211 return getRunData(DEFAULT_CONFIG,req,res,config); 212 } 213 214 225 public RunData getRunData(String key, 226 HttpServletRequest req, 227 HttpServletResponse res, 228 ServletConfig config) 229 throws ServiceException, 230 IllegalArgumentException 231 { 232 if (req == null || res == null || config == null) 239 { 240 throw new IllegalArgumentException ( 241 "RunDataFactory fatal error: HttpServletRequest, HttpServletResponse or ServletConfig was null."); 242 } 243 244 String [] cfg = (String []) configurations.get(key); 246 if (cfg == null) 247 { 248 throw new ServiceException("RunTime configuration '" + key + 249 "' is undefined"); 250 } 251 252 TurbineRunData data; 253 try 254 { 255 YaafiComponentService yaafi = (YaafiComponentService)TurbineServices.getInstance().getService(YaafiComponentService.SERVICE_NAME); 257 258 PoolService pool = (PoolService)yaafi.lookup(PoolService.class.getName()); 259 data = (TurbineRunData) pool.getInstance(Class.forName(cfg[0])); 260 data.setParameterParser((ParameterParser) pool.getInstance(Class.forName(cfg[1]))); 261 if( data.getParameterParser() instanceof DefaultParameterParser){ 262 DefaultParameterParser parser = (DefaultParameterParser)data.getParameterParser(); 263 UploadService uploadService = (UploadService)yaafi.lookup(UploadService.class.getName()); 264 parser.setUploadService(uploadService); 265 } 266 data.setCookieParser((CookieParser) pool.getInstance(Class.forName(cfg[2]))); 267 } 268 catch (ClassNotFoundException cnfe) 269 { 270 throw new ServiceException("Missing class", cnfe); 271 } 272 catch (ClassCastException x) 273 { 274 throw new ServiceException("RunData configuration '" + key + 275 "' is illegal", x); 276 } 277 catch (PoolException pe) 278 { 279 throw new ServiceException("Pooling Exception", pe); 280 } 281 catch (Exception e) 282 { 283 throw new ServiceException("Lookup Exception", e); 284 } 285 286 data.setRequest(req); 288 data.setResponse(res); 289 290 data.setSession(data.getRequest().getSession(true)); 292 293 data.setServletConfig(config); 295 296 String contextPath; 298 299 try 300 { 301 contextPath = getContextPath != null ? 302 (String ) getContextPath.invoke(req,null) : ""; 303 } 304 catch (Exception x) 305 { 306 contextPath = ""; 307 } 308 String scriptName = contextPath + req.getServletPath(); 309 310 data.setServerName(req.getServerName()); 311 data.setServerPort(req.getServerPort()); 312 data.setServerScheme(req.getScheme()); 313 data.setScriptName(scriptName); 314 data.setContextPath(contextPath); 315 316 return (RunData) data; 317 } 318 319 325 public boolean putRunData(RunData data) 326 { 327 if (data instanceof TurbineRunData) 328 { 329 YaafiComponentService yaafi = (YaafiComponentService)TurbineServices.getInstance().getService(YaafiComponentService.SERVICE_NAME); 330 try { 331 PoolService pool = (PoolService)yaafi.lookup(PoolService.class.getName()); 332 333 pool.putInstance(((TurbineRunData) data).getParameterParser()); 334 pool.putInstance(((TurbineRunData) data).getCookieParser()); 335 return pool.putInstance(data); 336 } 337 catch (Exception e){ 338 throw new RuntimeException ("Problem looking up pool service",e); 339 } 340 } 341 else 342 { 343 return false; 344 } 345 } 346 } 347 | Popular Tags |