private void applyLog4jConfiguration( ConfigurableEnvironment environment, ServletContext servletContext) { String log4jConfigLocation = "classpath:log4j.properties"; if (environment.containsProperty("logging.file")) { String location = environment.getProperty("logging.file"); servletContext.log("Setting LOG_FILE: " + location); System.setProperty("LOG_FILE", location); } else if (environment.containsProperty("logging.path")) { String location = environment.getProperty("logging.path"); servletContext.log("Setting LOG_PATH: " + location); System.setProperty("LOG_PATH", location); } else if (environment.containsProperty("logging.config")) { log4jConfigLocation = environment.getProperty("logging.config"); } try { servletContext.log("Loading log4j config from location: " + log4jConfigLocation); Log4jConfigurer.initLogging(log4jConfigLocation); } catch (FileNotFoundException e) { servletContext.log("Error loading log4j config from location: " + log4jConfigLocation, e); } MDC.put("context", servletContext.getContextPath()); }
private void applySpringProfiles( ConfigurableEnvironment environment, ServletContext servletContext) { if (environment.containsProperty("spring_profiles")) { String profiles = environment.getProperty("spring_profiles"); servletContext.log("Setting active profiles: " + profiles); environment.setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles)); } }
@Bean public ContainerAttributes containerAttributes() { final ContainerAttributes containerAttributes = new ContainerAttributes(); setConfiguredContainerAttributes(containerAttributes); final String containerIp = environment.getProperty(CONTAINER_ATTRIBUTES_PREFIX + ContainerAttributes.IP_ADDRESS_KEY); final String containerHostname = environment.getProperty(CONTAINER_ATTRIBUTES_PREFIX + ContainerAttributes.HOST_KEY); containerAttributes.setIp( StringUtils.hasText(containerIp) ? containerIp : RuntimeUtils.getIpAddress()); containerAttributes.setHost( StringUtils.hasText(containerHostname) ? containerHostname : RuntimeUtils.getHost()); containerAttributes.setPid(RuntimeUtils.getPid()); return containerAttributes; }
/** @param containerAttributes */ private void setConfiguredContainerAttributes(ContainerAttributes containerAttributes) { Map<String, String> attributes = new HashMap<String, String>(); for (PropertySource<?> propertySource : environment.getPropertySources()) { if (propertySource instanceof EnumerablePropertySource) { EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) propertySource; for (String key : ps.getPropertyNames()) { if (key.startsWith(CONTAINER_ATTRIBUTES_PREFIX)) { String attributeKey = key.replaceAll(CONTAINER_ATTRIBUTES_PREFIX, ""); attributes.put(attributeKey, environment.getProperty(key)); } } } } containerAttributes.putAll(attributes); }
@SuppressWarnings("unchecked") private List<ApplicationListener<ApplicationEvent>> getListeners(ConfigurableEnvironment env) { String classNames = env.getProperty(PROPERTY_NAME); List<ApplicationListener<ApplicationEvent>> listeners = new ArrayList<ApplicationListener<ApplicationEvent>>(); if (StringUtils.hasLength(classNames)) { for (String className : StringUtils.commaDelimitedListToSet(classNames)) { try { Class<?> clazz = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader()); Assert.isAssignable( ApplicationListener.class, clazz, "class [" + className + "] must implement ApplicationListener"); listeners.add((ApplicationListener<ApplicationEvent>) BeanUtils.instantiateClass(clazz)); } catch (Exception ex) { throw new ApplicationContextException( "Failed to load context listener class [" + className + "]", ex); } } } AnnotationAwareOrderComparator.sort(listeners); return listeners; }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); environment.setActiveProfiles("alarms-" + environment.getProperty("httpd.alarms.db.type")); }