1 16 package org.directwebremoting.impl; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Enumeration ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 25 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.ServletContext ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 32 import org.directwebremoting.Container; 33 import org.directwebremoting.ServerContextFactory.ServerContextBuilder; 34 import org.directwebremoting.WebContextFactory.WebContextBuilder; 35 import org.directwebremoting.dwrp.DefaultConverterManager; 36 import org.directwebremoting.dwrp.HtmlCallMarshaller; 37 import org.directwebremoting.dwrp.PlainCallMarshaller; 38 import org.directwebremoting.extend.AccessControl; 39 import org.directwebremoting.extend.AjaxFilterManager; 40 import org.directwebremoting.extend.Configurator; 41 import org.directwebremoting.extend.ConverterManager; 42 import org.directwebremoting.extend.Creator; 43 import org.directwebremoting.extend.CreatorManager; 44 import org.directwebremoting.extend.DebugPageGenerator; 45 import org.directwebremoting.extend.DwrConstants; 46 import org.directwebremoting.extend.Handler; 47 import org.directwebremoting.extend.PageNormalizer; 48 import org.directwebremoting.extend.Remoter; 49 import org.directwebremoting.extend.ScriptSessionManager; 50 import org.directwebremoting.extend.ServerLoadMonitor; 51 import org.directwebremoting.servlet.AboutHandler; 52 import org.directwebremoting.servlet.AuthHandler; 53 import org.directwebremoting.servlet.DwrWebContextFilter; 54 import org.directwebremoting.servlet.EngineHandler; 55 import org.directwebremoting.servlet.HtmlCallHandler; 56 import org.directwebremoting.servlet.HtmlPollHandler; 57 import org.directwebremoting.servlet.IndexHandler; 58 import org.directwebremoting.servlet.InterfaceHandler; 59 import org.directwebremoting.servlet.PathConstants; 60 import org.directwebremoting.servlet.PlainCallHandler; 61 import org.directwebremoting.servlet.PlainPollHandler; 62 import org.directwebremoting.servlet.TestHandler; 63 import org.directwebremoting.servlet.UrlProcessor; 64 import org.directwebremoting.servlet.UtilHandler; 65 import org.directwebremoting.servlet.WebworkUtilHandler; 66 import org.directwebremoting.util.LocalUtil; 67 import org.directwebremoting.util.Logger; 68 import org.xml.sax.SAXException ; 69 70 77 public class ContainerUtil 78 { 79 93 public static DefaultContainer createDefaultContainer(ServletConfig servletConfig) throws ServletException 94 { 95 try 96 { 97 String typeName = servletConfig.getInitParameter(Container.class.getName()); 98 if (typeName == null) 99 { 100 return new DefaultContainer(); 101 } 102 103 log.debug("Using alternate Container implementation: " + typeName); 104 Class type = LocalUtil.classForName(typeName); 105 return (DefaultContainer) type.newInstance(); 106 } 107 catch (Exception ex) 108 { 109 throw new ServletException (ex); 110 } 111 } 112 113 123 public static void setupDefaultContainer(DefaultContainer container, ServletConfig servletConfig) throws InstantiationException , IllegalAccessException 124 { 125 setupDefaults(container, servletConfig); 126 setupFromServletConfig(container, servletConfig); 127 container.setupFinished(); 128 } 129 130 137 public static void setupDefaults(DefaultContainer container, ServletConfig servletConfig) throws InstantiationException , IllegalAccessException 138 { 139 container.addParameter(AccessControl.class.getName(), DefaultAccessControl.class.getName()); 140 container.addParameter(ConverterManager.class.getName(), DefaultConverterManager.class.getName()); 141 container.addParameter(CreatorManager.class.getName(), DefaultCreatorManager.class.getName()); 142 container.addParameter(UrlProcessor.class.getName(), UrlProcessor.class.getName()); 143 container.addParameter(WebContextBuilder.class.getName(), DefaultWebContextBuilder.class.getName()); 144 container.addParameter(ServerContextBuilder.class.getName(), DefaultServerContextBuilder.class.getName()); 145 container.addParameter(AjaxFilterManager.class.getName(), DefaultAjaxFilterManager.class.getName()); 146 container.addParameter(Remoter.class.getName(), DefaultRemoter.class.getName()); 147 container.addParameter(DebugPageGenerator.class.getName(), DefaultDebugPageGenerator.class.getName()); 148 container.addParameter(PlainCallMarshaller.class.getName(), PlainCallMarshaller.class.getName()); 149 container.addParameter(HtmlCallMarshaller.class.getName(), HtmlCallMarshaller.class.getName()); 150 container.addParameter(PlainPollHandler.class.getName(), PlainPollHandler.class.getName()); 151 container.addParameter(HtmlPollHandler.class.getName(), HtmlPollHandler.class.getName()); 152 container.addParameter(ScriptSessionManager.class.getName(), DefaultScriptSessionManager.class.getName()); 153 container.addParameter(PageNormalizer.class.getName(), DefaultPageNormalizer.class.getName()); 154 155 if (servletConfig.getServletContext().getServerInfo().startsWith("jetty-6")) 156 { 157 container.addParameter(ServerLoadMonitor.class.getName(), ThreadDroppingServerLoadMonitor.class.getName()); 158 } 159 else 160 { 161 container.addParameter(ServerLoadMonitor.class.getName(), DefaultServerLoadMonitor.class.getName()); 162 } 163 164 createUrlMapping(container, "/index.html", "indexHandlerUrl", IndexHandler.class); 166 createUrlMapping(container, "/engine.js", "engineHandlerUrl", EngineHandler.class); 167 createUrlMapping(container, "/util.js", "utilHandlerUrl", UtilHandler.class); 168 createUrlMapping(container, "/auth.js", "authHandlerUrl", AuthHandler.class); 169 createUrlMapping(container, "/webwork/DWRActionUtil.js", "webworkUtilHandlerUrl", WebworkUtilHandler.class); 170 createUrlMapping(container, "/about", "aboutHandlerUrl", AboutHandler.class); 171 createUrlMapping(container, "/test/", "testHandlerUrl", TestHandler.class); 172 createUrlMapping(container, "/interface/", "interfaceHandlerUrl", InterfaceHandler.class); 173 174 createUrlMapping(container, "/call/plaincall/", "plainCallHandlerUrl", PlainCallHandler.class); 178 createUrlMapping(container, "/call/plainpoll/", "plainPollHandlerUrl", PlainPollHandler.class); 179 createUrlMapping(container, "/call/htmlcall/", "htmlCallHandlerUrl", HtmlCallHandler.class); 180 createUrlMapping(container, "/call/htmlpoll/", "htmlPollHandlerUrl", HtmlPollHandler.class); 181 } 182 183 196 public static void createUrlMapping(DefaultContainer container, String url, String propertyName, Class handler) throws InstantiationException , IllegalAccessException 197 { 198 container.addParameter(PathConstants.URL_PREFIX + url, handler.getName()); 199 container.addParameter(propertyName, url); 200 } 201 202 209 public static void setupFromServletConfig(DefaultContainer container, ServletConfig servletConfig) throws InstantiationException , IllegalAccessException 210 { 211 Enumeration en = servletConfig.getInitParameterNames(); 212 while (en.hasMoreElements()) 213 { 214 String name = (String ) en.nextElement(); 215 String value = servletConfig.getInitParameter(name); 216 container.addParameter(name, value); 217 } 218 } 219 220 229 public static void prepareForWebContextFilter(ServletContext context, ServletConfig config, Container container, WebContextBuilder webContextBuilder, HttpServlet servlet) 230 { 231 context.setAttribute(Container.class.getName(), container); 232 context.setAttribute(WebContextBuilder.class.getName(), webContextBuilder); 233 context.setAttribute(ServletConfig .class.getName(), config); 234 context.setAttribute(HttpServlet .class.getName(), servlet); 235 } 236 237 244 public static void configureFromSystemDwrXml(Container container) throws IOException , ParserConfigurationException , SAXException 245 { 246 DwrXmlConfigurator system = new DwrXmlConfigurator(); 247 system.setClassResourceName(DwrConstants.FILE_DWR_XML); 248 system.configure(container); 249 } 250 251 258 public static void configureFromDefaultDwrXml(Container container) throws IOException , ParserConfigurationException , SAXException 259 { 260 DwrXmlConfigurator local = new DwrXmlConfigurator(); 261 local.setServletResourceName(DwrConstants.DEFAULT_DWR_XML); 262 local.configure(container); 263 } 264 265 275 public static boolean configureFromInitParams(Container container, ServletConfig servletConfig) throws IOException , ParserConfigurationException , SAXException 276 { 277 Enumeration en = servletConfig.getInitParameterNames(); 278 boolean foundConfig = false; 279 while (en.hasMoreElements()) 280 { 281 String name = (String ) en.nextElement(); 282 String value = servletConfig.getInitParameter(name); 283 284 if (name.startsWith(INIT_CONFIG)) 286 { 287 foundConfig = true; 288 289 StringTokenizer st = new StringTokenizer (value, "\n,"); 290 while (st.hasMoreTokens()) 291 { 292 String fileName = st.nextToken().trim(); 293 DwrXmlConfigurator local = new DwrXmlConfigurator(); 294 local.setServletResourceName(fileName); 295 local.configure(container); 296 } 297 } 298 else if (name.equals(INIT_CUSTOM_CONFIGURATOR)) 299 { 300 foundConfig = true; 301 302 try 303 { 304 Configurator configurator = (Configurator) LocalUtil.classNewInstance(INIT_CUSTOM_CONFIGURATOR, value, Configurator.class); 305 configurator.configure(container); 306 log.debug("Loaded config from: " + value); 307 } 308 catch (Exception ex) 309 { 310 log.warn("Failed to start custom configurator", ex); 311 } 312 } 313 } 314 315 return foundConfig; 316 } 317 318 326 public static boolean configureFromAnnotations(Container container) 327 { 328 try 329 { 330 Class annotationCfgClass = LocalUtil.classForName("org.directwebremoting.annotations.AnnotationsConfigurator"); 331 332 Configurator configurator = (Configurator) annotationCfgClass.newInstance(); 333 configurator.configure(container); 334 335 log.debug("Java5 AnnotationsConfigurator enabled"); 336 return true; 337 } 338 catch (UnsupportedClassVersionError ex) 339 { 340 return false; 342 } 343 catch (ClassNotFoundException ex) 344 { 345 log.warn("AnnotationsConfigurator is missing. Are you running from within an IDE?"); 347 return false; 348 } 349 catch (Exception ex) 350 { 351 log.warn("Failed to start annotations", ex); 353 return false; 354 } 355 catch (Throwable ex) 356 { 357 if (ex.getClass().getName().equals(UnsupportedClassVersionError .class.getName())) 358 { 359 log.error("Caught impossible Throwable", ex); 360 return false; 361 } 362 else if (ex.getClass().getName().equals(LinkageError .class.getName())) 363 { 364 log.error("Caught impossible Throwable", ex); 365 return false; 366 } 367 else if (ex instanceof Error ) 368 { 369 log.fatal("Rethrowing Error:" + ex); 370 throw (Error ) ex; 371 } 372 else 373 { 374 log.error("Ilogical catch state", ex); 377 return false; 378 } 379 } 380 } 381 382 387 public static void configure(Container container, List configurators) 388 { 389 for (Iterator it = configurators.iterator(); it.hasNext();) 391 { 392 Configurator configurator = (Configurator) it.next(); 393 394 log.debug("** Adding config from " + configurator); 395 configurator.configure(container); 396 } 397 } 398 399 407 public static void configureContainerFully(Container container, ServletConfig servletConfig) throws IOException , ParserConfigurationException , SAXException 408 { 409 configureFromSystemDwrXml(container); 410 boolean foundConfig = configureFromInitParams(container, servletConfig); 411 412 boolean skip = Boolean.valueOf(servletConfig.getInitParameter(INIT_SKIP_DEFAULT)).booleanValue(); 414 IOException delayedIOException = null; 415 if (!foundConfig && !skip) 416 { 417 try 418 { 419 configureFromDefaultDwrXml(container); 420 } 421 catch (IOException ex) 422 { 423 delayedIOException = ex; 425 } 426 } 427 428 if (!configureFromAnnotations(container)) 429 { 430 log.debug("Java5 AnnotationsConfigurator disabled"); 431 432 if (delayedIOException != null) 433 { 434 throw delayedIOException; 435 } 436 } 437 } 438 439 446 public static void publishContainer(Container container, ServletConfig servletConfig) 447 { 448 ServletContext servletContext = servletConfig.getServletContext(); 449 450 String publishName = servletConfig.getInitParameter(INIT_PUBLISH_CONTAINER); 451 if (publishName != null) 452 { 453 servletContext.setAttribute(publishName, container); 454 } 455 456 List containers = (List ) servletContext.getAttribute(ATTRIBUTE_CONTAINER_LIST); 457 if (containers == null) 458 { 459 containers = new ArrayList (); 460 } 461 containers.add(container); 462 servletContext.setAttribute(ATTRIBUTE_CONTAINER_LIST, containers); 463 } 464 465 470 public static List getAllPublishedContainers(ServletContext servletContext) 471 { 472 List containers = (List ) servletContext.getAttribute(ATTRIBUTE_CONTAINER_LIST); 473 if (containers == null) 474 { 475 containers = new ArrayList (); 476 } 477 478 return containers; 479 } 480 481 488 public static void shutdownServerLoadMonitorsInContainerList(List containers, String title) 489 { 490 if (containers == null || containers.size() == 0) 491 { 492 log.debug("No containers to shutdown for: " + title); 493 return; 494 } 495 496 log.debug("Shutting down containers for: " + title); 497 for (Iterator it = containers.iterator(); it.hasNext();) 498 { 499 Container container = (Container) it.next(); 500 ServerLoadMonitor monitor = (ServerLoadMonitor) container.getBean(ServerLoadMonitor.class.getName()); 501 monitor.shutdown(); 502 } 503 } 504 505 509 public static void debugConfig(Container container) 510 { 511 if (log.isDebugEnabled()) 512 { 513 log.debug("Container"); 515 log.debug(" Type: " + container.getClass().getName()); 516 Collection beanNames = container.getBeanNames(); 517 for (Iterator it = beanNames.iterator(); it.hasNext();) 518 { 519 String name = (String ) it.next(); 520 Object object = container.getBean(name); 521 522 if (object instanceof String ) 523 { 524 log.debug(" Param: " + name + " = " + object + " (" + object.getClass().getName() + ")"); 525 } 526 else 527 { 528 log.debug(" Bean: " + name + " = " + object + " (" + object.getClass().getName() + ")"); 529 } 530 } 531 532 AccessControl accessControl = (AccessControl) container.getBean(AccessControl.class.getName()); 534 log.debug("AccessControl"); 535 log.debug(" Type: " + accessControl.getClass().getName()); 536 537 AjaxFilterManager ajaxFilterManager = (AjaxFilterManager) container.getBean(AjaxFilterManager.class.getName()); 539 log.debug("AjaxFilterManager"); 540 log.debug(" Type: " + ajaxFilterManager.getClass().getName()); 541 542 ConverterManager converterManager = (ConverterManager) container.getBean(ConverterManager.class.getName()); 544 log.debug("ConverterManager"); 545 log.debug(" Type: " + converterManager.getClass().getName()); 546 547 CreatorManager creatorManager = (CreatorManager) container.getBean(CreatorManager.class.getName()); 549 log.debug("CreatorManager"); 550 log.debug(" Type: " + creatorManager.getClass().getName()); 551 Collection creatorNames = creatorManager.getCreatorNames(); 552 for (Iterator it = creatorNames.iterator(); it.hasNext();) 553 { 554 String creatorName = (String ) it.next(); 555 Creator creator = creatorManager.getCreator(creatorName); 556 log.debug(" Creator: " + creatorName + " = " + creator + " (" + creator.getClass().getName() + ")"); 557 } 558 } 559 } 560 561 565 public static final String INIT_CONFIG = "config"; 566 567 570 public static final String INIT_SKIP_DEFAULT = "skipDefaultConfig"; 571 572 575 public static final String INIT_LOGLEVEL = "logLevel"; 576 577 581 public static final String INIT_PUBLISH_CONTAINER = "publishContainerAs"; 582 583 587 public static final String INIT_CUSTOM_CONFIGURATOR = "customConfigurator"; 588 589 592 public static final String ATTRIBUTE_CONTAINER_LIST = "org.directwebremoting.ContainerList"; 593 594 597 private static final Logger log = Logger.getLogger(ContainerUtil.class); 598 } 599 | Popular Tags |