View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * 
16   *****************************************************************************/
17  package org.settings4j.helper.spring;
18  
19  import javax.servlet.ServletContext;
20  
21  import org.settings4j.helper.web.DefaultPropertiesLoader;
22  import org.springframework.web.context.ConfigurableWebApplicationContext;
23  import org.springframework.web.context.ContextLoader;
24  import org.springframework.web.context.support.XmlWebApplicationContext;
25  
26  /**
27   * Spring Context Loader which can replaces Placeholders in contextConfigLocations like "${...}" with Values from
28   * Settings4j.
29   * <p>
30   * This Implementation replaces the {@link ContextLoader}
31   * <p>
32   * See configuration Example: {@link Settings4jContextLoaderListener}.
33   * 
34   * @author brabenetz
35   */
36  public class Settings4jContextLoader extends ContextLoader {
37  
38      /** General Logger for this Class. */
39      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(Settings4jContextLoader.class);
40  
41      /**
42       * Name of servlet context parameter (i.e., "<code>settings4jContextConfigLocation</code>")
43       * that can specify the config location for the root context, falling back
44       * to the implementation's default otherwise.
45       * @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION
46       */
47      public static final String SETTINGS4J_CONFIG_LOCATION_PARAM = "settings4jContextConfigLocation";
48      
49      /** {@inheritDoc} */
50      @Override
51      protected void customizeContext(final ServletContext servletContext, final ConfigurableWebApplicationContext wac) {
52          if (wac instanceof XmlWebApplicationContext) {
53              // be sure that the DefaultPropertiesLoader is initialized:
54              createDefaultPropertiesLoader().initDefaultProperties(servletContext);
55              // replace Placeholders in configLocations.
56              final String configLocations = servletContext.getInitParameter(SETTINGS4J_CONFIG_LOCATION_PARAM);
57              LOG.debug("settings4jContextConfigLocation configLocations: {}", configLocations);
58              final String parsedConfigLocations = Settings4jPlaceholderConfigurer.parseStringValue(configLocations);
59              LOG.debug("settings4jContextConfigLocation parsed configLocations: {}", parsedConfigLocations);
60              
61              wac.setConfigLocation(parsedConfigLocations);
62          } else {
63              LOG.warn(//
64              "Settings4jContextLoader only works with an ApplicationContext from type XmlWebApplicationContext.");
65          }
66      }
67  
68      /**
69       * Create the DefaultPropertiesLoader to use. Can be overridden in subclasses.
70       * 
71       * @return the new DefaultPropertiesLoader
72       */
73      protected DefaultPropertiesLoader createDefaultPropertiesLoader() {
74          return new DefaultPropertiesLoader();
75      }
76  
77  }