Beispiel #1
0
 private static ApplicationContext createGrailsApplicationContext(
     ApplicationContext parent, GrailsApplication application) {
   GrailsRuntimeConfigurator config = new GrailsRuntimeConfigurator(application, parent);
   MockServletContext servletContext = new MockServletContext(new MockResourceLoader());
   ConfigurableApplicationContext appCtx =
       (ConfigurableApplicationContext) config.configure(servletContext);
   servletContext.setAttribute(ApplicationAttributes.APPLICATION_CONTEXT, appCtx);
   Assert.notNull(appCtx);
   return appCtx;
 }
  public static WebApplicationContext configureWebApplicationContext(
      ServletContext servletContext, WebApplicationContext parent) {
    ServletContextHolder.setServletContext(servletContext);
    GrailsApplication application =
        (GrailsApplication) parent.getBean(GrailsApplication.APPLICATION_ID);

    if (LOG.isDebugEnabled()) {
      LOG.debug("[GrailsContextLoader] Configuring Grails Application");
    }

    if (application.getParentContext() == null) {
      application.setApplicationContext(parent);
    }

    GrailsRuntimeConfigurator configurator = null;
    if (parent.containsBean(GrailsRuntimeConfigurator.BEAN_ID)) {
      // get configurator from parent application context
      configurator = (GrailsRuntimeConfigurator) parent.getBean(GrailsRuntimeConfigurator.BEAN_ID);
    } else {
      // get configurator from servlet context
      configurator =
          determineGrailsRuntimeConfiguratorFromServletContext(application, servletContext, parent);
    }

    if (configurator == null) {
      // no configurator, use default
      configurator = new GrailsRuntimeConfigurator(application, parent);
      if (parent.containsBean(GrailsPluginManager.BEAN_NAME)) {
        GrailsPluginManager pluginManager =
            (GrailsPluginManager) parent.getBean(GrailsPluginManager.BEAN_NAME);
        configurator.setPluginManager(pluginManager);
      }
    }

    final GrailsPluginManager pluginManager = configurator.getPluginManager();

    // return a context that obeys grails' settings
    WebApplicationContext webContext = configurator.configure(servletContext);
    pluginManager.setApplicationContext(webContext);

    configureServletContextAttributes(servletContext, application, pluginManager, webContext);
    LOG.info("[GrailsContextLoader] Grails application loaded.");
    return webContext;
  }