1 23 24 package com.sun.enterprise.tools.guiframework.view; 25 26 import com.iplanet.jato.ApplicationServletBase; 27 import com.iplanet.jato.RequestContext; 28 import com.iplanet.jato.RequestContextImpl; 29 import com.iplanet.jato.RequestManager; 30 import com.iplanet.jato.view.View; 31 import com.iplanet.jato.view.ViewBean; 32 import com.iplanet.jato.NavigationException; 33 import com.iplanet.jato.util.RootCauseException; 34 35 import com.sun.enterprise.tools.guiframework.event.descriptors.EventDescriptor; 36 import com.sun.enterprise.tools.guiframework.exception.FrameworkError; 37 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 38 import com.sun.enterprise.tools.guiframework.exception.ViewDescriptorHolder; 39 import com.sun.enterprise.tools.guiframework.util.LogUtil; 40 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 41 import com.sun.enterprise.tools.guiframework.view.event.ErrorEvent; 42 43 import java.io.IOException ; 44 import java.io.PrintWriter ; 45 import java.io.StringWriter ; 46 import java.net.URL ; 47 import java.util.EventObject ; 48 49 import javax.servlet.ServletConfig ; 50 import javax.servlet.ServletContext ; 51 import javax.servlet.ServletException ; 52 import javax.servlet.ServletRequest ; 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletResponse ; 55 56 import org.xml.sax.EntityResolver ; 57 58 59 public abstract class BaseServlet extends ApplicationServletBase { 60 61 73 protected abstract void handleUncaughtException(ErrorEvent errorEvent); 74 75 76 80 protected abstract URL getViewXMLURL(); 81 82 83 88 protected abstract String getJSPRoot(); 89 90 91 94 protected abstract String getPackageName(); 95 96 97 103 protected String getDTDURLBase(ServletConfig config) { 104 String base = config.getInitParameter(DTD_URL_BASE); 106 if ((base == null) && LogUtil.isLoggable(LogUtil.FINER)) { 107 LogUtil.log(LogUtil.FINER, 108 "framework.servletInitNotFound", DTD_URL_BASE); 109 } 110 return base; 111 } 112 113 114 120 121 protected EntityResolver getViewXMLEntityResolver() { 122 return null; 123 } 124 125 128 public void init(ServletConfig config) throws ServletException { 129 super.init(config); 130 131 ViewDescriptorManager mgr = ViewDescriptorManager.getInstance(); 133 mgr.setDTDURLBase(getDTDURLBase(config)); 134 mgr.setViewDescriptorURL(getViewXMLURL()); 135 mgr.setViewXMLEntityResolver(getViewXMLEntityResolver()); 136 mgr.setJSPRoot(getJSPRoot()); 138 } 139 140 141 151 protected DescriptorViewManager newViewManagerInstance(RequestContext rc) { 152 return new DescriptorViewManager(rc, getPackageName()); 153 } 154 155 156 159 protected void initializeRequestContext(RequestContext rc) { 160 super.initializeRequestContext(rc); 161 162 DescriptorViewManager vm = newViewManagerInstance(rc); 166 ((RequestContextImpl)rc).setViewBeanManager(vm); 167 } 168 169 170 173 protected String getPageName(RequestContext rc) { 174 HttpServletRequest request = rc.getRequest(); 175 String pageName = request.getParameter(PARAM_HANDLER_BEAN); 176 if (pageName == null) { 177 pageName = getPageName(request, request.getPathInfo()); 178 } 179 return pageName; 180 } 181 182 185 protected String getPageName(HttpServletRequest request, String pathInfo) { 186 String pageName = null; 187 if (pathInfo != null) { 188 pageName = parsePathInfo(pathInfo); 189 } 190 191 if (pageName == null) { 193 pageName = getDefaultHandlerName(request); 194 } 195 return pageName; 196 } 197 198 199 202 protected void processRequest(String pageName, HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 203 try { 204 super.processRequest(pageName, request, response); 205 } catch (Throwable ex) { 206 try { 207 handleException(RequestManager.getRequestContext(), ex); 208 } catch (Throwable ex2) { 209 handleException(RequestManager.getRequestContext(), ex2); 210 } 211 } 212 } 213 214 215 218 protected void onUncaughtException(RequestContext rc, Exception ex) throws ServletException , IOException { 219 try { 220 handleException(rc, ex); 221 } catch (Throwable ex2) { 222 handleException(rc, ex2); 223 } 224 } 225 226 227 234 protected Throwable getRootCause(Throwable ex) { 235 if (ex instanceof RootCauseException) { 236 Throwable jatoRoot = ((RootCauseException)ex).getRootCause(); 239 try { 240 ex.initCause(jatoRoot); 241 } catch (Exception ignore) { 242 } 244 ex = jatoRoot; 245 } if (ex instanceof ServletException ) { 246 Throwable servletRoot = ((ServletException )ex).getRootCause(); 248 try { 249 ex.initCause(servletRoot); 250 } catch (Exception ignore) { 251 } 253 ex = servletRoot; 254 } else { 255 ex = ex.getCause(); 256 } 257 return ex; 258 } 259 260 261 264 protected void handleException(RequestContext rc, Throwable ex) { 265 View view = null; 269 ViewDescriptor desc = null; 270 Throwable tmpEx = ex; 271 while (tmpEx != null) { 272 if (tmpEx instanceof ViewDescriptorHolder) { 273 ViewDescriptorHolder vdh = (ViewDescriptorHolder)tmpEx; 274 desc = vdh.getResponsibleViewDescriptor(); 275 view = vdh.getResponsibleView(); 276 break; 277 } 278 tmpEx = getRootCause(tmpEx); 279 } 280 281 if (view == null) { 283 String pageName = getPageName(rc); 284 if (pageName != null) { 285 try { 286 view = rc.getViewBeanManager().getViewBean(pageName); 287 } catch (Exception ex2) { 288 } 290 } 291 } 292 293 if ((desc == null) && (view != null)) { 295 if (view instanceof DescriptorContainerView) { 296 desc = ((DescriptorContainerView)view).getViewDescriptor(); 297 } 298 } 299 300 handleException(rc, ex, desc, view); 302 } 303 304 305 319 public void handleException(RequestContext rc, Throwable ex, ViewDescriptor desc, View view) { 320 View vb = view; 322 HttpServletRequest request = rc.getRequest(); 323 if (vb == null) { 324 if (desc != null) { 325 ViewDescriptor topDesc = desc; 326 while (topDesc.getParent() != null) { 327 topDesc = topDesc.getParent(); 328 } 329 try { 330 vb = topDesc.getView(rc); 331 } catch (Exception ignoreEx) { 332 } 334 } 335 } 336 337 while ((vb != null) && !(vb instanceof ViewBean)) { 339 vb = vb.getParent(); 340 } 341 if (vb == null) { 342 String pageName = getPageName(request, request.getPathInfo()); 346 try { 348 vb = rc.getViewBeanManager().getViewBean(pageName); 349 } catch (Exception notFoundException) { 350 } 352 } 353 if (vb == null) { 354 String msg = "Top View was not a ViewBean!"; 356 if (desc != null) { 357 ViewDescriptor topDesc = desc; 359 while (topDesc.getParent() != null) { 360 topDesc = topDesc.getParent(); 361 } 362 msg = "Top View (as described by ViewDescriptor: '"+ 363 topDesc.getName()+"') was not a ViewBean!"; 364 } 365 ex = new FrameworkError(msg, ex); 366 } 367 368 Throwable root = ex; 370 Throwable next = ex; 371 while (next != null) { 372 root = next; next = getRootCause(next); 374 } 375 376 String rootMessage = (root == ex) ? null : root.getMessage(); 379 String rootClass = (root == null) ? null : root.getClass().getName(); 380 381 StackTraceElement elts[] = root.getStackTrace(); 383 int len = elts.length; 384 StringBuffer fullTrace = new StringBuffer (); 385 for (int count=0; count<len; count++) { 386 fullTrace.append("\t"+elts[count].toString()+"\n"); 387 } 388 389 StringWriter writer = new StringWriter (); 391 ex.printStackTrace(new PrintWriter (writer)); 392 String regTrace = writer.toString(); 393 394 if (desc == null) { 396 View tmpView = view; 397 String childName = null; 398 while ((tmpView != null) && !(tmpView instanceof DescriptorContainerView)) { 399 childName = tmpView.getName(); 400 tmpView = tmpView.getParent(); 401 } 402 if (tmpView != null) { 403 desc = ((DescriptorContainerView)tmpView).getViewDescriptor(); 404 if (childName != null) { 405 if (desc.getChildDescriptor(childName) != null) { 409 desc = desc.getChildDescriptor(childName); 410 } 411 } 412 } 413 } 414 if (desc != null) { 415 ViewDescriptor handlerDesc = desc; 418 ViewDescriptor tmpDesc = desc; 419 while ((handlerDesc != null) && (handlerDesc.getEventDescriptor(EventDescriptor.TYPES.ERROR) == null)) { 420 tmpDesc = handlerDesc.getParent(); 422 if ((tmpDesc == null) && (vb != null) && (vb instanceof DescriptorContainerView)) { 423 tmpDesc = ((DescriptorContainerView)vb).getViewDescriptor(); 427 } 428 handlerDesc = tmpDesc; 429 } 430 if (handlerDesc != null) { 431 View descView = null; 432 try { 434 descView = handlerDesc.getView(rc); 435 } catch (Throwable ignoreThis) { 436 ex = new FrameworkError( 437 "Unable to get View for ViewDescriptor '"+ 438 handlerDesc.getName()+"'", ex); 439 } 440 ErrorEvent errorEvent = new ErrorEvent( 441 descView, handlerDesc, (ViewBean)vb, ex, view, desc, 442 ex.getMessage(), ex.getClass().getName(), rootMessage, 443 rootClass, fullTrace.toString(), regTrace); 444 DescriptorViewHelper.dispatchEvent( 445 rc, descView, handlerDesc, 446 handlerDesc.getEventDescriptor( 447 EventDescriptor.TYPES.ERROR), 448 errorEvent); 449 return; 450 } 451 } 452 453 ErrorEvent errorEvent = new ErrorEvent( 456 null, null, (ViewBean)vb, ex, view, desc, 457 ex.getMessage(), ex.getClass().getName(), rootMessage, 458 rootClass, fullTrace.toString(), regTrace); 459 handleUncaughtException(errorEvent); 460 } 461 462 490 491 496 public static final String DTD_URL_BASE = "dtdURLBase"; 497 498 503 public static final String PATH_INFO_ATTRIBUTE = 504 "javax.servlet.include.path_info"; 505 } 506 | Popular Tags |