commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r653100 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/ src/main/java/org/apache/commons/configuration2/beanutils/ src/test/java/org/apache/commons/configuration2/ xd...
Date Sat, 03 May 2008 17:48:15 GMT
Author: ebourg
Date: Sat May  3 10:48:15 2008
New Revision: 653100

URL: http://svn.apache.org/viewvc?rev=653100&view=rev
Log:
Added support for the <database/> element in the configuration descriptor (CONFIGURATION-313)

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDatabaseConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java?rev=653100&r1=653099&r2=653100&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java
Sat May  3 10:48:15 2008
@@ -26,6 +26,8 @@
 import java.util.List;
 import java.util.Map;
 import java.util.logging.Logger;
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
 
 import org.apache.commons.configuration2.beanutils.BeanDeclaration;
 import org.apache.commons.configuration2.beanutils.BeanFactory;
@@ -299,10 +301,14 @@
     private static final ConfigurationProvider SYSTEM_PROVIDER =
             new ConfigurationProvider(SystemConfiguration.class);
 
-    /** Constant for the provider for environment properties. */
+    /** Constant for the provider for environment variables. */
     private static final ConfigurationProvider ENVIRONMENT_PROVIDER =
             new ConfigurationProvider(EnvironmentConfiguration.class);
 
+    /** Constant for the provider for environment variables. */
+    private static final ConfigurationProvider DATABASE_PROVIDER =
+            new DatabaseConfigurationProvider();
+
     /** Constant for the provider for plist files. */
     private static final ConfigurationProvider PLIST_PROVIDER = new FileExtensionConfigurationProvider(
             "org.apache.commons.configuration2.plist.XMLPropertyListConfiguration",
@@ -314,12 +320,12 @@
 
     /** An array with the names of the default tags. */
     private static final String[] DEFAULT_TAGS =
-    {"properties", "xml", "hierarchicalXml", "jndi", "system", "environment", "plist", "configuration"};
+    {"properties", "xml", "hierarchicalXml", "jndi", "system", "environment", "plist", "configuration",
"database"};
 
     /** An array with the providers for the default tags. */
     private static final ConfigurationProvider[] DEFAULT_PROVIDERS =
     {PROPERTIES_PROVIDER, XML_PROVIDER, XML_PROVIDER, JNDI_PROVIDER,
-            SYSTEM_PROVIDER, ENVIRONMENT_PROVIDER, PLIST_PROVIDER, BUILDER_PROVIDER};
+            SYSTEM_PROVIDER, ENVIRONMENT_PROVIDER, PLIST_PROVIDER, BUILDER_PROVIDER, DATABASE_PROVIDER};
 
     /**
      * The serial version UID.
@@ -1444,4 +1450,36 @@
             return new CombinedConfiguration();
         }
     }
+
+    /**
+     * A specialized configuration provider for database configuration.
+     * The datasource is specified as a JNDI key for the default context.
+     */
+    static class DatabaseConfigurationProvider extends ConfigurationProvider
+    {
+        public AbstractConfiguration getConfiguration(ConfigurationDeclaration decl) throws
Exception
+        {
+            Map attributes = decl.getBeanProperties();
+            String jndi = (String) attributes.get("jndi");
+            DataSource datasource = (DataSource) new InitialContext().lookup(jndi);
+
+            String table = (String) attributes.get("table");
+            String nameColumn = (String) attributes.get("nameColumn");
+            String name = (String) attributes.get("name");
+            String keyColumn = (String) attributes.get("keyColumn");
+            String valueColumn = (String) attributes.get("valueColumn");
+
+            DatabaseConfiguration config;
+            if (name == null) {
+                config = new DatabaseConfiguration(datasource, table, keyColumn, valueColumn);
+            } else {
+                config = new DatabaseConfiguration(datasource, table, nameColumn, keyColumn,
valueColumn, name);
+            }
+
+            // initialize the other properties
+            BeanHelper.initBean(config, decl, true);
+
+            return config;
+        }
+    }
 }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java?rev=653100&r1=653099&r2=653100&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
Sat May  3 10:48:15 2008
@@ -161,12 +161,29 @@
      */
     public static void initBean(Object bean, BeanDeclaration data) throws ConfigurationRuntimeException
     {
+        initBean(bean, data, false);
+    }
+
+    /**
+     * Initializes the passed in bean. This method will obtain all the bean's
+     * properties that are defined in the passed in bean declaration. These
+     * properties will be set on the bean. If necessary, further beans will be
+     * created recursively.
+     *
+     * @param bean    the bean to be initialized
+     * @param data    the bean declaration
+     * @param lenient ignore missing and read only properties found in the BeanDeclaration,
+     *                throws an exception otherwise
+     * @throws ConfigurationRuntimeException if a property cannot be set
+     */
+    public static void initBean(Object bean, BeanDeclaration data, boolean lenient) throws
ConfigurationRuntimeException
+    {
         Map<String, Object> properties = data.getBeanProperties();
         if (properties != null)
         {
             for (String propName : properties.keySet())
             {
-                initProperty(bean, propName, properties.get(propName));
+                initProperty(bean, propName, properties.get(propName), lenient);
             }
         }
 
@@ -175,7 +192,7 @@
         {
             for (String propName : nestedBeans.keySet())
             {
-                initProperty(bean, propName, createBean((BeanDeclaration) nestedBeans.get(propName),
null));
+                initProperty(bean, propName, createBean((BeanDeclaration) nestedBeans.get(propName),
null), lenient);
             }
         }
     }
@@ -183,23 +200,30 @@
     /**
      * Sets a property on the given bean.
      *
-     * @param bean the bean
+     * @param bean     the bean
      * @param propName the name of the property
-     * @param value the property's value
-     * @throws ConfigurationRuntimeException if the property is not writeable or
-     * an error occurred
+     * @param value    the property's value
+     * @param lenient  ignore a missing or read only property, throws an exception otherwise
+     * @throws ConfigurationRuntimeException if the property is not writeable or an error
occurred
      */
-    private static void initProperty(Object bean, String propName, Object value) throws ConfigurationRuntimeException
+    private static void initProperty(Object bean, String propName, Object value, boolean
lenient) throws ConfigurationRuntimeException
     {
         try
         {
             // find the descriptor for the property requested
-            PropertyDescriptor descriptor =  getPropertyDescriptor(bean.getClass(), propName);
+            PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(), propName);
 
             // check if the property is writeable
             if (descriptor == null || descriptor.getWriteMethod() == null)
             {
-                throw new ConfigurationRuntimeException("Property " + propName + " cannot
be set!");
+                if (lenient)
+                {
+                    return;
+                }
+                else
+                {
+                    throw new ConfigurationRuntimeException("Property " + propName + " cannot
be set!");
+                }
             }
 
             // set the property

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDatabaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDatabaseConfiguration.java?rev=653100&r1=653099&r2=653100&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDatabaseConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestDatabaseConfiguration.java
Sat May  3 10:48:15 2008
@@ -17,22 +17,26 @@
 
 package org.apache.commons.configuration2;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
-
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.spi.InitialContextFactory;
 import javax.sql.DataSource;
 
-import junit.framework.TestCase;
-
-import org.apache.commons.configuration2.AbstractConfiguration;
-import org.apache.commons.configuration2.Configuration;
-import org.apache.commons.configuration2.DatabaseConfiguration;
 import org.apache.commons.configuration2.event.ConfigurationErrorListener;
 import org.apache.commons.configuration2.test.HsqlDB;
 import org.apache.commons.dbcp.BasicDataSource;
+
+import junit.framework.TestCase;
+import org.codehaus.spice.jndikit.DefaultNameParser;
+import org.codehaus.spice.jndikit.DefaultNamespace;
+import org.codehaus.spice.jndikit.memory.MemoryContext;
 import org.dbunit.database.DatabaseConnection;
 import org.dbunit.database.IDatabaseConnection;
 import org.dbunit.dataset.IDataSet;
@@ -90,9 +94,8 @@
 
         if (hsqlDB == null)
         {
-            hsqlDB = new HsqlDB(DATABASE_URL, DATABASE_DRIVER,
-                    ConfigurationAssert.getTestFile("testdb.script")
-                            .getAbsolutePath());
+            File script = ConfigurationAssert.getTestFile("testdb.script");
+            hsqlDB = new HsqlDB(DATABASE_URL, DATABASE_DRIVER, script.getAbsolutePath());
         }
 
         BasicDataSource datasource = new BasicDataSource();
@@ -104,10 +107,8 @@
         this.datasource = datasource;
 
         // prepare the database
-        IDatabaseConnection connection = new DatabaseConnection(datasource
-                .getConnection());
-        IDataSet dataSet = new XmlDataSet(new FileInputStream(
-                ConfigurationAssert.getTestFile("dataset.xml")));
+        IDatabaseConnection connection = new DatabaseConnection(datasource.getConnection());
+        IDataSet dataSet = new XmlDataSet(new FileInputStream(ConfigurationAssert.getTestFile("dataset.xml")));
 
         try
         {
@@ -190,11 +191,8 @@
     private void checkErrorListener(int type, String key, Object value)
     {
         listener.verify(type, key, value);
-        assertTrue(
-                "Wrong event source",
-                listener.getLastEvent().getSource() instanceof DatabaseConfiguration);
-        assertTrue("Wrong exception",
-                listener.getLastEvent().getCause() instanceof SQLException);
+        assertTrue("Wrong event source", listener.getLastEvent().getSource() instanceof DatabaseConfiguration);
+        assertTrue("Wrong exception", listener.getLastEvent().getCause() instanceof SQLException);
         listener = null; // mark as checked
     }
 
@@ -462,6 +460,48 @@
     }
 
     /**
+     * Test instanciating a DatabaseConfiguration from a configuration descriptor.
+     */
+    public void testConfigurationBuilder() throws Exception
+    {
+        // bind the datasource in the JNDI context
+        TestInitialContextFactory.datasource = datasource;
+        System.setProperty("java.naming.factory.initial", TestInitialContextFactory.class.getName());
+
+        File testFile = ConfigurationAssert.getTestFile("testDatabaseConfiguration.xml");
+        ConfigurationBuilder builder = new DefaultConfigurationBuilder(testFile);
+        Configuration config = builder.getConfiguration();
+
+        assertNotNull("null configuration", config);
+
+        assertEquals("property1", "value1", config.getProperty("conf1.key1"));
+        assertEquals("property2", "value2", config.getProperty("conf1.key2"));
+        assertEquals("unknown property", null, config.getProperty("conf1.key3"));
+        assertEquals("list property", 3, config.getList("conf1.keyMulti").size());
+
+        assertEquals("property1", "value1", config.getProperty("conf2.key1"));
+        assertEquals("property2", "value2", config.getProperty("conf2.key2"));
+        assertEquals("unknown property", null, config.getProperty("conf2.key3"));
+    }
+
+    /**
+     * JNDI Context factory that returns the test datasource bound on the java:comp/env/jdbc/configuration
key.
+     */
+    public static class TestInitialContextFactory implements InitialContextFactory
+    {
+        public static DataSource datasource;
+
+        public Context getInitialContext(Hashtable environment) throws NamingException
+        {
+            DefaultNamespace namespace = new DefaultNamespace(new DefaultNameParser());
+            MemoryContext context = new MemoryContext(namespace, new Hashtable(), null);
+            context.createSubcontext("java:comp").createSubcontext("env").createSubcontext("jdbc").bind("configuration",
datasource);
+
+            return context;
+        }
+    }
+
+    /**
      * A specialized database configuration implementation that can be
      * configured to throw an exception when obtaining a connection. This way
      * database exceptions can be simulated.

Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml?rev=653100&r1=653099&r2=653100&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml Sat
May  3 10:48:15 2008
@@ -51,8 +51,12 @@
         ConfigurationFactory has been removed, DefaultConfigurationBuilder should be used
instead.
       </action>
       <action dev="ebourg" type="add">
-        The &lt;environment/> tag can be specified in a configuration descriptor
-        in order to create an EnvironmentConfiguration.          
+        The &lt;environment/> element can be specified in a configuration descriptor
to create
+        an EnvironmentConfiguration.
+      </action>
+      <action dev="ebourg" type="add">
+        The &lt;database/> element can be specified in a configuration descriptor
to create
+        a DatabaseConfiguration. The datasource is specified as a JNDI property.
       </action>
       <action dev="ebourg" type="update">
         DefaultConfigurationBuilder no longer requires commons-beanutils.
@@ -76,6 +80,10 @@
     </release>
 
     <release version="1.6" date="in SVN" description="">
+      <action dev="ebourg" type="fix" issue="CONFIGURATION-322">
+        ConfigurationDynaBean now works properly with indexed properties
+        stored internally in the underlying configuration as arrays.
+      </action>
       <action dev="ebourg" type="fix" issue="CONFIGURATION-320">
         XMLPropertyListConfiguration is no longer limited to 32 bits integers.
       </action>



Mime
View raw message