package stroom.startup;

import io.dropwizard.setup.Environment;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import stroom.Config;

public class ApplicationContexts {

    static AnnotationConfigWebApplicationContext applicationContext;

    static void configure() throws ClassNotFoundException {
        applicationContext = new AnnotationConfigWebApplicationContext();

        // We register all the configuration beans so they'll be loaded when we call applicationContext.refresh().
        applicationContext.register(stroom.security.spring.SecurityConfiguration.class,
                stroom.spring.ScopeConfiguration.class,
                stroom.spring.PersistenceConfiguration.class,
                stroom.spring.ServerComponentScanConfiguration.class,
                stroom.spring.ServerConfiguration.class,
                stroom.spring.CachedServiceConfiguration.class,
                stroom.logging.spring.EventLoggingConfiguration.class,
                stroom.index.spring.IndexConfiguration.class,
                stroom.search.spring.SearchConfiguration.class,
                stroom.script.spring.ScriptConfiguration.class,
                stroom.visualisation.spring.VisualisationConfiguration.class,
                stroom.dashboard.spring.DashboardConfiguration.class,
                stroom.spring.CoreClientConfiguration.class,
                stroom.statistics.spring.StatisticsConfiguration.class,
                Config.class
        );

        

        applicationContext.setConfigLocations(
                "stroom.spring.ScopeConfiguration",
                "stroom.spring.PersistenceConfiguration",
                "stroom.spring.ServerComponentScanConfiguration",
                "stroom.spring.ServerConfiguration",
                "stroom.spring.CachedServiceConfiguration",
                "stroom.logging.spring.EventLoggingConfiguration",
                "stroom.index.spring.IndexConfiguration",
                "stroom.search.spring.SearchConfiguration",
                "stroom.script.spring.ScriptConfiguration",
                "stroom.visualisation.spring.VisualisationConfiguration",
                "stroom.dashboard.spring.DashboardConfiguration",
                "stroom.spring.CoreClientConfiguration",
                "stroom.statistics.spring.StatisticsConfiguration",
                "stroom.security.spring.SecurityConfiguration");
        applicationContext.registerShutdownHook();



    }

    static void start(Environment environment, Config configuration){
        applicationContext.refresh();
        applicationContext.getBeanFactory().registerSingleton("dwConfiguration", configuration);
        applicationContext.getBeanFactory().registerSingleton("dwEnvironment", environment);
        applicationContext.getBeanFactory().registerSingleton("dwObjectMapper", environment.getObjectMapper());
        applicationContext.start();
    }
}

@@ -276,68 +276,198 @@
 xt;%0A
-%0A    static void configure() throws ClassNotFoundException %7B
+    static AnnotationConfigWebApplicationContext rootContext;%0A%0A    static void configure() throws ClassNotFoundException %7B%0A        rootContext = new AnnotationConfigWebApplicationContext();%0A
 %0A   
@@ -529,32 +529,84 @@
 ationContext();%0A
+        applicationContext.setParent(rootContext);%0A%0A
 %0A        // We r
@@ -1689,929 +1689,8 @@
 );%0A%0A
-        %0A%0A        applicationContext.setConfigLocations(%0A                %22stroom.spring.ScopeConfiguration%22,%0A                %22stroom.spring.PersistenceConfiguration%22,%0A                %22stroom.spring.ServerComponentScanConfiguration%22,%0A                %22stroom.spring.ServerConfiguration%22,%0A                %22stroom.spring.CachedServiceConfiguration%22,%0A                %22stroom.logging.spring.EventLoggingConfiguration%22,%0A                %22stroom.index.spring.IndexConfiguration%22,%0A                %22stroom.search.spring.SearchConfiguration%22,%0A                %22stroom.script.spring.ScriptConfiguration%22,%0A                %22stroom.visualisation.spring.VisualisationConfiguration%22,%0A                %22stroom.dashboard.spring.DashboardConfiguration%22,%0A                %22stroom.spring.CoreClientConfiguration%22,%0A                %22stroom.statistics.spring.StatisticsConfiguration%22,%0A                %22stroom.security.spring.SecurityConfiguration%22);%0A
     
@@ -1736,19 +1736,16 @@
 Hook();%0A
-%0A%0A%0A
     %7D%0A%0A 
@@ -1813,16 +1813,76 @@
 ation)%7B%0A
+        rootContext.refresh();%0A        rootContext.start();%0A
         

Add root context