Author: oheger
Date: Sat Dec 15 20:39:08 2012
New Revision: 1422349

URL: http://svn.apache.org/viewvc?rev=1422349&view=rev
Log:
CombinedConfigurationBuilder now supports the declaration of a file system in 
the definition configuration. This property is inherited by included file-base 
configuration sources.
The base path is now also inherited by file-based configurations.

Added:
    commons/proper/configuration/trunk/src/test/resources/testCCFileSystem.xml  
 (with props)
Modified:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java?rev=1422349&r1=1422348&r2=1422349&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedConfigurationBuilder.java
 Sat Dec 15 20:39:08 2012
@@ -43,8 +43,10 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.builder.BuilderParameters;
 import org.apache.commons.configuration.builder.ConfigurationBuilder;
 import org.apache.commons.configuration.builder.FileBasedBuilderParametersImpl;
+import org.apache.commons.configuration.builder.FileBasedBuilderProperties;
 import org.apache.commons.configuration.builder.FileBasedConfigurationBuilder;
 import org.apache.commons.configuration.builder.XMLBuilderParametersImpl;
+import org.apache.commons.configuration.builder.XMLBuilderProperties;
 import org.apache.commons.configuration.resolver.CatalogResolver;
 import org.apache.commons.configuration.tree.DefaultExpressionEngine;
 import org.apache.commons.configuration.tree.OverrideCombiner;
@@ -499,9 +501,6 @@ public class CombinedConfigurationBuilde
     /** The current XML parameters object. */
     private XMLBuilderParametersImpl currentXMLParameters;
 
-    /** Stores the base path to the configuration sources to load. */
-    private String configurationBasePath;
-
     /**
      * Creates a new instance of {@code CombinedConfigurationBuilder}. No 
parameters
      * are set.
@@ -535,33 +534,6 @@ public class CombinedConfigurationBuilde
     }
 
     /**
-     * Returns the base path for the configuration sources to load. This path 
is
-     * used to resolve relative paths in the configuration definition file.
-     *
-     * @return the base path for configuration sources
-     */
-    public String getConfigurationBasePath()
-    {
-        return configurationBasePath;
-    }
-
-    /**
-     * Sets the base path for the configuration sources to load. Normally a 
base
-     * path need not to be set because it is determined by the location of the
-     * configuration definition file to load. All relative paths in this file
-     * are resolved relative to this file. Setting a base path makes sense if
-     * such relative paths should be otherwise resolved, e.g. if the
-     * configuration file is loaded from the class path and all sub
-     * configurations it refers to are stored in a special config directory.
-     *
-     * @param configurationBasePath the new base path to set
-     */
-    public void setConfigurationBasePath(String configurationBasePath)
-    {
-        this.configurationBasePath = configurationBasePath;
-    }
-
-    /**
      * Returns the configuration provided by this builder. If the boolean
      * parameter is <b>true</b>, the configuration definition file will be
      * loaded. It will then be parsed, and instances for the declared
@@ -577,7 +549,6 @@ public class CombinedConfigurationBuilde
     public CombinedConfiguration getConfiguration(boolean load)
             throws ConfigurationException
     {
-        initFileSystem();
         registerConfiguredLookups();
 
 //        CombinedConfiguration result = createResultConfiguration();
@@ -773,8 +744,9 @@ public class CombinedConfigurationBuilde
         setUpCurrentParameters();
         initNodeCombinerListNodes(result, config, KEY_OVERRIDE_LIST);
         registerConfiguredProviders(config);
-        initSystemProperties(config);
         setUpCurrentXMLParameters();
+        currentXMLParameters.setFileSystem(initFileSystem(config));
+        initSystemProperties(config, getBasePath());
         configureEntityResolver(config, currentXMLParameters);
 
         ConfigurationSourceData data = getSourceData();
@@ -833,14 +805,26 @@ public class CombinedConfigurationBuilde
 //        }
     }
 
-    protected void initFileSystem() throws ConfigurationException
+    /**
+     * Creates and initializes a default {@code FileSystem} if the definition
+     * configuration contains a corresponding declaration. The file system
+     * returned by this method is used as default for all file-based child
+     * configuration sources.
+     *
+     * @param config the definition configuration
+     * @return the default {@code FileSystem} (may be <b>null</b>)
+     * @throws ConfigurationException if an error occurs
+     */
+    protected FileSystem initFileSystem(HierarchicalConfiguration config)
+            throws ConfigurationException
     {
-//        if (getMaxIndex(FILE_SYSTEM) == 0)
-//        {
-//            HierarchicalConfiguration config = configurationAt(FILE_SYSTEM);
-//            XMLBeanDeclaration decl = new XMLBeanDeclaration(config);
-//            setFileSystem((FileSystem) BeanHelper.createBean(decl));
-//        }
+        if (config.getMaxIndex(FILE_SYSTEM) == 0)
+        {
+            XMLBeanDeclaration decl =
+                    new XMLBeanDeclaration(config, FILE_SYSTEM);
+            return (FileSystem) BeanHelper.createBean(decl);
+        }
+        return null;
     }
 
     /**
@@ -849,18 +833,19 @@ public class CombinedConfigurationBuilde
      * properties are added to the system properties.
      *
      * @param config the definition configuration
+     * @param basePath the base path defined for this builder (may be
+     *        <b>null</b>)
      * @throws ConfigurationException if an error occurs.
      */
-    protected void initSystemProperties(HierarchicalConfiguration config)
-            throws ConfigurationException
+    protected void initSystemProperties(HierarchicalConfiguration config,
+            String basePath) throws ConfigurationException
     {
         String fileName = config.getString(KEY_SYSTEM_PROPS);
         if (fileName != null)
         {
             try
             {
-                SystemConfiguration.setSystemProperties(
-                        getConfigurationBasePath(), fileName);
+                SystemConfiguration.setSystemProperties(basePath, fileName);
             }
             catch (Exception ex)
             {
@@ -956,9 +941,13 @@ public class CombinedConfigurationBuilde
      */
     protected void initChildBuilderParameters(BuilderParameters params)
     {
-        if (params instanceof XMLBuilderParametersImpl)
+        if (params instanceof XMLBuilderProperties<?>)
+        {
+            initChildXMLParameters((XMLBuilderProperties<?>) params);
+        }
+        if(params instanceof FileBasedBuilderProperties<?>)
         {
-            initChildXMLParameters((XMLBuilderParametersImpl) params);
+            initChildFileBasedParameters((FileBasedBuilderProperties<?>) 
params);
         }
     }
 
@@ -979,20 +968,65 @@ public class CombinedConfigurationBuilde
      * related to XML and file-based configurations during creation of the
      * result configuration. The properties stored in this object can be
      * inherited to child configurations.
+     *
+     * @throws ConfigurationException if an error occurs
      */
-    private void setUpCurrentXMLParameters()
+    private void setUpCurrentXMLParameters() throws ConfigurationException
     {
         currentXMLParameters = new XMLBuilderParametersImpl();
+        initDefaultBasePath();
+    }
+
+    /**
+     * Initializes the default base path for all file-based child configuration
+     * sources. The base path can be explicitly defined in the parameters of
+     * this builder. Otherwise, if the definition builder is a file-based
+     * builder, it is obtained from there.
+     *
+     * @throws ConfigurationException if an error occurs
+     */
+    private void initDefaultBasePath() throws ConfigurationException
+    {
+        assert currentParameters != null : "Current parameters undefined!";
+        if (currentParameters.getBasePath() != null)
+        {
+            currentXMLParameters.setBasePath(currentParameters.getBasePath());
+        }
+        else
+        {
+            ConfigurationBuilder<? extends HierarchicalConfiguration> 
defBuilder =
+                    getDefinitionBuilder();
+            if (defBuilder instanceof FileBasedConfigurationBuilder<?>)
+            {
+                currentXMLParameters
+                        .setBasePath(((FileBasedConfigurationBuilder<?>) 
defBuilder)
+                                .getFileHandler().getBasePath());
+            }
+        }
+    }
+
+    /**
+     * Initializes a parameters object for a file-based configuration with
+     * properties already set for this parent builder. This method handles
+     * properties like a default file system or a base path.
+     *
+     * @param params the parameters object
+     */
+    private void initChildFileBasedParameters(
+            FileBasedBuilderProperties<?> params)
+    {
+        params.setBasePath(getBasePath());
+        params.setFileSystem(currentXMLParameters.getFileHandler()
+                .getFileSystem());
     }
 
     /**
      * Initializes a parameters object for an XML configuration with properties
-     * already set for this parent builder. Only properties that are not set in
-     * the parameters object are updated.
+     * already set for this parent builder.
      *
      * @param params the parameters object
      */
-    private void initChildXMLParameters(XMLBuilderParametersImpl params)
+    private void initChildXMLParameters(XMLBuilderProperties<?> params)
     {
         params.setEntityResolver(currentXMLParameters.getEntityResolver());
     }
@@ -1037,6 +1071,17 @@ public class CombinedConfigurationBuilde
     }
 
     /**
+     * Returns the current base path of this configuration builder. This is 
used
+     * for instance by all file-based child configurations.
+     *
+     * @return the base path
+     */
+    private String getBasePath()
+    {
+        return currentXMLParameters.getFileHandler().getBasePath();
+    }
+
+    /**
      * Registers providers defined in the configuration.
      *
      * @param defConfig the definition configuration

Added: 
commons/proper/configuration/trunk/src/test/resources/testCCFileSystem.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/testCCFileSystem.xml?rev=1422349&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/testCCFileSystem.xml 
(added)
+++ commons/proper/configuration/trunk/src/test/resources/testCCFileSystem.xml 
Sat Dec 15 20:39:08 2012
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.
+-->
+<!-- Test configuration definition file that contains a declaration of a
+     custom file system.
+     $Id$
+-->
+
+<configuration>
+  <header>
+    <fileSystem
+      
config-class="org.apache.commons.configuration.builder.combined.TestCombinedConfigurationBuilder$FileSystemTestImpl"/>
+  </header>
+  <xml fileName="test.xml" config-name="xml"/>
+</configuration>

Propchange: 
commons/proper/configuration/trunk/src/test/resources/testCCFileSystem.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
commons/proper/configuration/trunk/src/test/resources/testCCFileSystem.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: 
commons/proper/configuration/trunk/src/test/resources/testCCFileSystem.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml


Reply via email to