/* ***************************************************************************
 * Copyright (c) 2008 Brabenetz Harald, Austria.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 *****************************************************************************/
package org.settings4j.helper.spring;

import javax.servlet.ServletContext;

import org.settings4j.helper.web.DefaultPropertiesLoader;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.XmlWebApplicationContext;

/**
 * Spring Context Loader which can replaces Placeholders in contextConfigLocations like "${...}" with Values from
 * Settings4j.
 * 
 * @author brabenetz
 */
public class Settings4jContextLoader extends ContextLoader {

    /** General Logger for this Class. */
    private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
        .getLog(Settings4jContextLoader.class);

    /**
     * Name of servlet context parameter (i.e., "<code>contextConfigLocation</code>") that can specify the config
     * location for the root context, falling back to the implementation's default otherwise.
     * 
     * @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION
     */
    public static final String CONFIG_DEFAULT_VALUES = "settings4jContextConfigDefaultValues";

    /** {@inheritDoc} */
    protected void customizeContext(final ServletContext servletContext, final ConfigurableWebApplicationContext wac) {
        if (wac instanceof XmlWebApplicationContext) {
            // be sure that the DefaultPropertiesLoader is initialized:
            createDefaultPropertiesLoader().initDefaultProperties(servletContext);
            // replace Placeholders in configLocations.
            final String[] configLocations = wac.getConfigLocations();
            // TODO hbrabenetz 23.03.2012 : get Placeholder Prefix from Context!?
            for (int i = 0; i < configLocations.length; i++) {
                final String configLocation = configLocations[i];
                configLocations[i] = Settings4jPlaceholderConfigurer.parseStringValue(configLocation);
            }
            wac.setConfigLocations(configLocations);
        } else {
            LOG.warn(//
            "Settings4jContextLoader only works with an ApplicationContext from type XmlWebApplicationContext.");
        }
    }

    /**
     * Create the DefaultPropertiesLoader to use. Can be overridden in subclasses.
     * 
     * @return the new DefaultPropertiesLoader
     */
    protected DefaultPropertiesLoader createDefaultPropertiesLoader() {
        return new DefaultPropertiesLoader();
    }

}

@@ -1580,17 +1580,27 @@
  %22%3Ccode%3E
-c
+settings4jC
 ontextCo
@@ -1620,16 +1620,23 @@
 /code%3E%22)
+%0A     *
  that ca
@@ -1655,23 +1655,16 @@
 e config
-%0A     *
  locatio
@@ -1699,16 +1699,23 @@
 ing back
+%0A     *
  to the 
@@ -1750,24 +1750,16 @@
 erwise.%0A
-     * %0A
      * @
@@ -1894,29 +1894,40 @@
 ing 
-CONFIG_DEFAULT_VALUES
+SETTINGS4J_CONFIG_LOCATION_PARAM
  = %22
@@ -1953,24 +1953,23 @@
 nfig
-DefaultValues%22;%0A
+Location%22;%0A    
 %0A   
@@ -2392,22 +2392,14 @@
     
-final 
 String
-%5B%5D
  con
@@ -2417,207 +2417,173 @@
 s = 
-wac.getConfigLocations();%0A            // TODO hbrabenetz 23.03.2012 : get Placeholder Prefix from Context!?%0A            for (int i = 0; i %3C configLocations.length; i++) %7B%0A                final String
+servletContext.getInitParameter(SETTINGS4J_CONFIG_LOCATION_PARAM);%0A            if (LOG.isDebugEnabled()) %7B%0A                LOG.debug(%22settings4jContextConfigLocation
  con
@@ -2593,18 +2593,22 @@
 Location
- =
+s: %22 +
  configL
@@ -2615,19 +2615,17 @@
 ocations
-%5Bi%5D
+)
 ;%0A      
@@ -2627,24 +2627,47 @@
         
+   %7D%0A
        
-c
+     String parsedC
 onfigLoc
@@ -2676,11 +2676,8 @@
 ions
-%5Bi%5D
  = S
@@ -2738,16 +2738,168 @@
 Location
+s);%0A            if (LOG.isDebugEnabled()) %7B%0A                LOG.debug(%22settings4jContextConfigLocation parsed configLocations: %22 + parsedConfigLocations
 );%0A     
@@ -2899,32 +2899,45 @@
 ;%0A            %7D%0A
+            %0A
             wac.
@@ -2957,11 +2957,16 @@
 tion
-s(c
+(parsedC
 onfi

Fix Settings4j ContextLoader for Spring ApplicationContext in web.xml