1 15 package org.apache.tapestry.engine; 16 17 import java.io.IOException ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 import java.util.Locale ; 21 22 import javax.servlet.RequestDispatcher ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletException ; 25 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.hivemind.ApplicationRuntimeException; 30 import org.apache.hivemind.ClassResolver; 31 import org.apache.hivemind.util.Defense; 32 import org.apache.hivemind.util.ToStringBuilder; 33 import org.apache.tapestry.ApplicationServlet; 34 import org.apache.tapestry.Constants; 35 import org.apache.tapestry.IEngine; 36 import org.apache.tapestry.IPage; 37 import org.apache.tapestry.IRequestCycle; 38 import org.apache.tapestry.PageRedirectException; 39 import org.apache.tapestry.RedirectException; 40 import org.apache.tapestry.StaleLinkException; 41 import org.apache.tapestry.StaleSessionException; 42 import org.apache.tapestry.TapestryConstants; 43 import org.apache.tapestry.listener.ListenerMap; 44 import org.apache.tapestry.request.RequestContext; 45 import org.apache.tapestry.services.DataSqueezer; 46 import org.apache.tapestry.services.Infrastructure; 47 import org.apache.tapestry.spec.IApplicationSpecification; 48 import org.apache.tapestry.web.WebRequest; 49 import org.apache.tapestry.web.WebResponse; 50 51 96 97 public abstract class AbstractEngine implements IEngine 98 { 99 private static final Log LOG = LogFactory.getLog(AbstractEngine.class); 100 101 106 private Infrastructure _infrastructure; 107 108 private ListenerMap _listeners; 109 110 113 114 private Locale _locale; 115 116 120 121 public static final String VISIT_CLASS_PROPERTY_NAME = "org.apache.tapestry.visit-class"; 122 123 126 127 protected void activateExceptionPage(IRequestCycle cycle, Throwable cause) 128 { 129 _infrastructure.getExceptionPresenter().presentException(cycle, cause); 130 } 131 132 137 138 public void reportException(String reportTitle, Throwable ex) 139 { 140 _infrastructure.getRequestExceptionReporter().reportRequestException(reportTitle, ex); 141 } 142 143 147 148 protected void cleanupAfterRequest(IRequestCycle cycle) 149 { 150 151 } 152 153 157 158 public Locale getLocale() 159 { 160 return _locale; 161 } 162 163 169 170 public IEngineService getService(String name) 171 { 172 return _infrastructure.getServiceMap().getService(name); 173 } 174 175 176 177 public IApplicationSpecification getSpecification() 178 { 179 return _infrastructure.getApplicationSpecification(); 180 } 181 182 183 184 public ISpecificationSource getSpecificationSource() 185 { 186 return _infrastructure.getSpecificationSource(); 187 } 188 189 193 194 protected void redirect(String pageName, IRequestCycle cycle, 195 ApplicationRuntimeException exception) throws IOException 196 { 197 IPage page = cycle.getPage(pageName); 198 199 cycle.activate(page); 200 201 renderResponse(cycle); 202 } 203 204 208 209 public void renderResponse(IRequestCycle cycle) throws IOException 210 { 211 _infrastructure.getResponseRenderer().renderResponse(cycle); 212 } 213 214 217 218 public void service(WebRequest request, WebResponse response) throws IOException 219 { 220 IRequestCycle cycle = null; 221 IMonitor monitor = null; 222 IEngineService service = null; 223 224 if (_infrastructure == null) 225 _infrastructure = (Infrastructure) request.getAttribute(Constants.INFRASTRUCTURE_KEY); 226 227 try 228 { 229 try 230 { 231 cycle = _infrastructure.getRequestCycleFactory().newRequestCycle(this); 232 233 monitor = cycle.getMonitor(); 234 service = cycle.getService(); 235 236 monitor.serviceBegin(service.getName(), _infrastructure.getRequest() 237 .getRequestURI()); 238 239 241 service.service(cycle); 242 243 return; 244 } 245 catch (PageRedirectException ex) 246 { 247 handlePageRedirectException(cycle, ex); 248 } 249 catch (RedirectException ex) 250 { 251 handleRedirectException(cycle, ex); 252 } 253 catch (StaleLinkException ex) 254 { 255 handleStaleLinkException(cycle, ex); 256 } 257 catch (StaleSessionException ex) 258 { 259 handleStaleSessionException(cycle, ex); 260 } 261 } 262 catch (Exception ex) 263 { 264 monitor.serviceException(ex); 265 266 270 if (LOG.isDebugEnabled()) 271 LOG.debug("Uncaught exception", ex); 272 273 activateExceptionPage(cycle, ex); 274 } 275 finally 276 { 277 if (service != null) 278 monitor.serviceEnd(service.getName()); 279 280 try 281 { 282 cycle.cleanup(); 283 _infrastructure.getApplicationStateManager().flush(); 284 } 285 catch (Exception ex) 286 { 287 reportException(EngineMessages.exceptionDuringCleanup(ex), ex); 288 } 289 } 290 } 291 292 301 302 protected void handlePageRedirectException(IRequestCycle cycle, PageRedirectException exception) 303 throws IOException 304 { 305 List pageNames = new ArrayList (); 306 307 String pageName = exception.getTargetPageName(); 308 309 while (true) 310 { 311 if (pageNames.contains(pageName)) 312 { 313 pageNames.add(pageName); 314 315 throw new ApplicationRuntimeException(EngineMessages.validateCycle(pageNames)); 316 } 317 318 320 pageNames.add(pageName); 321 322 try 323 { 324 326 cycle.activate(pageName); 327 328 break; 329 } 330 catch (PageRedirectException secondRedirectException) 331 { 332 pageName = secondRedirectException.getTargetPageName(); 333 } 334 } 335 336 renderResponse(cycle); 337 } 338 339 359 360 protected void handleStaleLinkException(IRequestCycle cycle, StaleLinkException exception) 361 throws IOException 362 { 363 _infrastructure.getStaleLinkExceptionPresenter() 364 .presentStaleLinkException(cycle, exception); 365 } 366 367 379 380 protected void handleStaleSessionException(IRequestCycle cycle, StaleSessionException exception) 381 { 382 _infrastructure.getStaleSessionExceptionPresenter().presentStaleSessionException( 383 cycle, 384 exception); 385 } 386 387 390 391 public void setLocale(Locale value) 392 { 393 Defense.notNull(value, "locale"); 394 395 _locale = value; 396 397 399 if (_infrastructure != null) 400 _infrastructure.setLocale(value); 401 } 402 403 406 407 public ClassResolver getClassResolver() 408 { 409 return _infrastructure.getClassResolver(); 410 } 411 412 418 419 public String toString() 420 { 421 ToStringBuilder builder = new ToStringBuilder(this); 422 423 builder.append("locale", _locale); 424 425 return builder.toString(); 426 } 427 428 436 437 public Object getVisit() 438 { 439 return _infrastructure.getApplicationStateManager().get("visit"); 440 } 441 442 public void setVisit(Object visit) 443 { 444 _infrastructure.getApplicationStateManager().store("visit", visit); 445 } 446 447 452 453 public Object getVisit(IRequestCycle cycle) 454 { 455 return getVisit(); 456 } 457 458 public boolean getHasVisit() 459 { 460 return _infrastructure.getApplicationStateManager().exists("visit"); 461 } 462 463 473 474 public Object getGlobal() 475 { 476 return _infrastructure.getApplicationStateManager().get("global"); 477 } 478 479 public IScriptSource getScriptSource() 480 { 481 return _infrastructure.getScriptSource(); 482 } 483 484 489 490 public ListenerMap getListeners() 491 { 492 if (_listeners == null) 493 _listeners = _infrastructure.getListenerMapSource().getListenerMapForObject(this); 494 495 return _listeners; 496 } 497 498 506 507 protected void handleRedirectException(IRequestCycle cycle, RedirectException redirectException) 508 { 509 String location = redirectException.getRedirectLocation(); 510 511 if (LOG.isDebugEnabled()) 512 LOG.debug("Redirecting to: " + location); 513 514 _infrastructure.getRequest().forward(location); 515 } 516 517 520 521 public DataSqueezer getDataSqueezer() 522 { 523 return _infrastructure.getDataSqueezer(); 524 } 525 526 527 528 public IPropertySource getPropertySource() 529 { 530 return _infrastructure.getApplicationPropertySource(); 531 } 532 533 534 public Infrastructure getInfrastructure() 535 { 536 return _infrastructure; 537 } 538 539 public String getOutputEncoding() 540 { 541 return _infrastructure.getOutputEncoding(); 542 } 543 } | Popular Tags |