Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: Java  |  size: 1.48 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     @Override
  2.     public void contextInitialized(ServletContextEvent servletContextEvent) {
  3.         ServletContext context = servletContextEvent.getServletContext();
  4.         ScopedContainers containers = (ScopedContainers) context.getAttribute(ScopedContainers.class.getName());
  5.  
  6.         MutablePicoContainer appContainer = containers.getApplicationContainer();
  7.         MutablePicoContainer reqContainer = containers.getRequestContainer();
  8.  
  9.         addAdapters(appContainer, XML_CONFIGURATION_FILE_APP);
  10.         addAdapters(reqContainer, XML_CONFIGURATION_FILE_REQ);
  11.     }
  12.  
  13.     private void addAdapters(MutablePicoContainer container, final String xmlPath) {
  14.         InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(xmlPath);
  15.         if(inStream == null) {
  16.             throw new PicoContainerWebException("Configuration file not found.");
  17.         }
  18.  
  19.         ScriptedContainerBuilder containerBuilder = new XMLContainerBuilder(
  20.                 new InputStreamReader(inStream),
  21.                 Thread.currentThread().getContextClassLoader()
  22.         );
  23.         PicoContainer appCtnr = containerBuilder.buildContainer(null, null, true);
  24.         for (ComponentAdapter<?> adapter: appCtnr.getComponentAdapters()) {
  25.             container.addAdapter(adapter);
  26.         }
  27.  
  28.         try {
  29.             inStream.close();
  30.         } catch (IOException e) {
  31.             throw new PicoContainerWebException("Error while closing configuration file.");
  32.         }
  33.     }