@Override
  public void initialize(ConfigurableWebApplicationContext applicationContext) {

    Resource resource = null;
    ServletContext servletContext = applicationContext.getServletContext();
    WebApplicationContextUtils.initServletPropertySources(
        applicationContext.getEnvironment().getPropertySources(),
        servletContext,
        applicationContext.getServletConfig());

    ServletConfig servletConfig = applicationContext.getServletConfig();
    String locations =
        servletConfig == null
            ? null
            : servletConfig.getInitParameter(PROFILE_CONFIG_FILE_LOCATIONS);
    resource = getResource(servletContext, applicationContext, locations);

    if (resource == null) {
      servletContext.log(
          "No YAML environment properties from servlet.  Defaulting to servlet context.");
      locations = servletContext.getInitParameter(PROFILE_CONFIG_FILE_LOCATIONS);
      resource = getResource(servletContext, applicationContext, locations);
    }

    try {
      servletContext.log("Loading YAML environment properties from location: " + resource);
      YamlMapFactoryBean factory = new YamlMapFactoryBean();
      factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE);

      List<Resource> resources = new ArrayList<Resource>();

      String defaultLocation =
          servletConfig == null
              ? null
              : servletConfig.getInitParameter(PROFILE_CONFIG_FILE_DEFAULT);
      if (defaultLocation != null) {
        Resource defaultResource = new ClassPathResource(defaultLocation);
        if (defaultResource.exists()) {
          resources.add(defaultResource);
        }
      }

      resources.add(resource);
      factory.setResources(resources.toArray(new Resource[resources.size()]));

      Map<String, Object> map = factory.getObject();
      String yamlStr = (new Yaml()).dump(map);
      map.put(rawYamlKey, yamlStr);
      NestedMapPropertySource properties = new NestedMapPropertySource("servletConfigYaml", map);
      applicationContext.getEnvironment().getPropertySources().addLast(properties);
      applySpringProfiles(applicationContext.getEnvironment(), servletContext);
      applyLog4jConfiguration(applicationContext.getEnvironment(), servletContext);

    } catch (Exception e) {
      servletContext.log("Error loading YAML environment properties from location: " + resource, e);
    }
  }
Example #2
0
  // Notification that the web application is ready to process requests
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    if (null != servletContext) {
      return;
    }
    System.setProperty("red5.deployment.type", "war");

    servletContext = sce.getServletContext();
    String prefix = servletContext.getRealPath("/");

    long time = System.currentTimeMillis();

    logger.info("RED5 Server (http://www.osflash.org/red5)");
    logger.info("WAR loader");
    logger.debug("Path: " + prefix);

    try {
      // instance the context loader
      contextLoader = createContextLoader();
      applicationContext =
          (ConfigurableWebApplicationContext)
              contextLoader.initWebApplicationContext(servletContext);
      logger.debug("Root context path: " + applicationContext.getServletContext().getContextPath());

      ConfigurableBeanFactory factory = applicationContext.getBeanFactory();

      // register default
      factory.registerSingleton("default.context", applicationContext);

      // get the main factory
      parentFactory = (DefaultListableBeanFactory) factory.getParentBeanFactory();

    } catch (Throwable t) {
      logger.error("", t);
    }

    long startupIn = System.currentTimeMillis() - time;
    logger.info("Startup done in: " + startupIn + " ms");
  }