public void contextInitialized(final ServletContextEvent sce) {
    final ServletContext context = sce.getServletContext();

    // create a container if there is none yet
    if (context.getAttribute(PlexusConstants.PLEXUS_KEY) == null) {
      try {
        appContext =
            createContainerContext(
                context, (AppContext) context.getAttribute(AppContext.APPCONTEXT_KEY));

        final ContainerConfiguration plexusConfiguration =
            new DefaultContainerConfiguration()
                .setName(context.getServletContextName())
                .setContainerConfigurationURL(plexusXmlFile.toURI().toURL())
                .setContext((Map) appContext.flatten())
                .setAutoWiring(true)
                .setClassPathScanning(PlexusConstants.SCANNING_INDEX)
                .setComponentVisibility(PlexusConstants.GLOBAL_VISIBILITY);

        final ArrayList<Module> modules = new ArrayList<Module>(2);
        modules.add(new NexusWebModule(sce.getServletContext()));
        modules.add(new AppContextModule(appContext));

        // Add support for @Timed
        modules.add(new TimingModule());

        final Module[] customModules = (Module[]) context.getAttribute(CUSTOM_MODULES);

        if (customModules != null) {
          modules.addAll(Arrays.asList(customModules));
        }

        plexusContainer =
            new DefaultPlexusContainer(
                plexusConfiguration, modules.toArray(new Module[modules.size()]));

        context.setAttribute(PlexusConstants.PLEXUS_KEY, plexusContainer);

        context.setAttribute(AppContext.APPCONTEXT_KEY, appContext);
      } catch (PlexusContainerException e) {
        sce.getServletContext().log("Could not start Plexus container!", e);

        throw new IllegalStateException("Could not start Plexus container!", e);
      } catch (IOException e) {
        sce.getServletContext().log("Could not start Plexus container!", e);

        throw new IllegalStateException("Could not start Plexus container!", e);
      }

      // fire startable
      appContext.getLifecycleManager().invokeHandler(Startable.class);
    }
  }
 @Override
 protected void tearDown() throws Exception {
   try {
     appContext.getLifecycleManager().invokeHandler(Stoppable.class);
   } finally {
     super.tearDown();
   }
 }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   cacheManagerComponent = lookup(CacheManagerComponent.class);
   appContext
       .getLifecycleManager()
       .registerManaged(new CacheManagerLifecycleHandler(cacheManagerComponent));
 }
 public void contextDestroyed(final ServletContextEvent sce) {
   if (plexusContainer != null) {
     plexusContainer.dispose();
   }
   if (appContext != null) {
     appContext.getLifecycleManager().invokeHandler(Stoppable.class);
   }
 }