Author: oheger Date: Fri Oct 2 20:25:58 2009 New Revision: 821160 URL: http://svn.apache.org/viewvc?rev=821160&view=rev Log: Added support for capabilities to the ConfigurationSource interface.
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceEventWrapper.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/FlatNodeSourceAdapter.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/HierarchicalSourceAdapter.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/InMemoryConfigurationSource.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/SubConfiguration.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceEventWrapper.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestFlatNodeSourceAdapter.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestHierarchicalSourceAdapter.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestSubConfiguration.java Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSource.java Fri Oct 2 20:25:58 2009 @@ -75,4 +75,31 @@ * implemented */ boolean removeConfigurationSourceListener(ConfigurationSourceListener l); + + /** + * <p> + * Returns the capability of the the specified type. + * </p> + * <p> + * Beyond the basic set of methods defined by the {...@code + * ConfigurationSource} interface and its sub interfaces a concrete source + * implementation can provide additional features (e.g. persistence + * operations). The interfaces required for controlling these features can + * be queried using this generic method. + * </p> + * <p> + * This is an application of the <em>capability pattern</em>. It allows + * keeping the basic interfaces for configuration sources lean, but + * flexible. Additional functionality can be added later without the need to + * extend the interfaces. Note that this method can return <b>null</b> if + * the capability requested is not available. Callers should always check + * for <b>null</b> results. + * </p> + * + * @param <T> the type of the capability requested + * @param cls the class of the capability interface + * @return the object implementing the desired capability or <b>null</b> if + * this capability is not supported + */ + <T> T getCapability(Class<T> cls); } Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceEventWrapper.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceEventWrapper.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceEventWrapper.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/ConfigurationSourceEventWrapper.java Fri Oct 2 20:25:58 2009 @@ -254,6 +254,20 @@ } /** + * Returns the capability of the the specified type. This implementation + * delegates to the wrapped source. + * + * @param <T> the type of the capability requested + * @param cls the class of the capability interface + * @return the object implementing the desired capability or <b>null</b> if + * this capability is not supported + */ + public <T> T getCapability(Class<T> cls) + { + return getWrappedSource().getCapability(cls); + } + + /** * Helper method for sending an event to all listeners currently registered * at this object. * Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/FlatNodeSourceAdapter.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/FlatNodeSourceAdapter.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/FlatNodeSourceAdapter.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/FlatNodeSourceAdapter.java Fri Oct 2 20:25:58 2009 @@ -187,6 +187,19 @@ } /** + * Queries this object for the specified capability. This implementation + * delegates to the original source. + * + * @param <T> the type of the capability + * @param cls the class of the capability interface + * @return the object implementing this capability or <b>null</b> + */ + public <T> T getCapability(Class<T> cls) + { + return getOriginalSource().getCapability(cls); + } + + /** * Creates a hierarchy of {...@code FlatNode} objects that corresponds to the * data stored in the wrapped {...@code ConfigurationSource}. This * implementation relies on the method {...@code getKeys()} of the wrapped Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/HierarchicalSourceAdapter.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/HierarchicalSourceAdapter.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/HierarchicalSourceAdapter.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/HierarchicalSourceAdapter.java Fri Oct 2 20:25:58 2009 @@ -308,6 +308,20 @@ } /** + * Returns the capability of the the specified type. This implementation + * delegates to the transformed source. + * + * @param <T> the type of the capability requested + * @param cls the class of the capability interface + * @return the object implementing the desired capability or <b>null</b> if + * this capability is not supported + */ + public <T> T getCapability(Class<T> cls) + { + return getOriginalSource().getCapability(cls); + } + + /** * Notifies this object about a change of a monitored {...@code * ConfigurationSource}. If this {...@code HierarchicalSourceAdapter} was * constructed with the {...@code monitorChanges} flag set to <b>true</b>, it Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/InMemoryConfigurationSource.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/InMemoryConfigurationSource.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/InMemoryConfigurationSource.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/InMemoryConfigurationSource.java Fri Oct 2 20:25:58 2009 @@ -116,4 +116,10 @@ // TODO Auto-generated method stub throw new UnsupportedOperationException("Not yet implemented!"); } + + public <T> T getCapability(Class<T> cls) + { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Not yet implemented!"); + } } Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java Fri Oct 2 20:25:58 2009 @@ -319,6 +319,12 @@ throw new UnsupportedOperationException("Not implemented!"); } + public <T> T getCapability(Class<T> cls) + { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Not yet implemented!"); + } + /** * Returns the underlying map, in which the data of this {...@code * MapConfigurationSource} is stored. This method is intended to be used by Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/SubConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/SubConfiguration.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/SubConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/SubConfiguration.java Fri Oct 2 20:25:58 2009 @@ -275,6 +275,22 @@ } /** + * Returns the capability with the specified class. A {...@code + * SubConfiguration} does not inherit the capabilities of the {...@code + * ConfigurationSource} used by its parent. So this implementation always + * returns <b>null</b>. + * + * @param <T> the type of the capability requested + * @param cls the class of the capability interface + * @return the object implementing the desired capability or <b>null</b> if + * this capability is not supported + */ + public <C> C getCapability(Class<C> cls) + { + return null; + } + + /** * Returns a hierarchical configuration object for the given sub node. * This implementation will ensure that the returned * <code>SubConfiguration</code> object will have the same parent as Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceEventWrapper.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceEventWrapper.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceEventWrapper.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestConfigurationSourceEventWrapper.java Fri Oct 2 20:25:58 2009 @@ -296,6 +296,20 @@ } /** + * Tests the implementation of getCapability(). + */ + public void testGetCapability() + { + final Object cap = new Object(); + EasyMock.expect(wrappedSource.getCapability(Object.class)).andReturn( + cap); + EasyMock.replay(wrappedSource); + assertEquals("Wrong capability", cap, wrapper + .getCapability(Object.class)); + EasyMock.verify(wrappedSource); + } + + /** * A test configuration source listener implementation used for testing the * events generated by the source. */ Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestFlatNodeSourceAdapter.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestFlatNodeSourceAdapter.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestFlatNodeSourceAdapter.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestFlatNodeSourceAdapter.java Fri Oct 2 20:25:58 2009 @@ -276,4 +276,21 @@ assertEquals("Property not found in source", NEW_VALUE, adapter .getOriginalSource().getProperty(NEW_KEY)); } + + /** + * Tests whether capabilities can be requested. + */ + public void testGetCapability() + { + FlatConfigurationSource src = createMockSource(); + ConfigurationSource cap = EasyMock + .createMock(ConfigurationSource.class); + EasyMock.expect(src.getCapability(ConfigurationSource.class)) + .andReturn(cap); + EasyMock.replay(src, cap); + FlatNodeSourceAdapter adapter = new FlatNodeSourceAdapter(src); + assertEquals("Wrong capability", cap, adapter + .getCapability(ConfigurationSource.class)); + EasyMock.verify(src, cap); + } } Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestHierarchicalSourceAdapter.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestHierarchicalSourceAdapter.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestHierarchicalSourceAdapter.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestHierarchicalSourceAdapter.java Fri Oct 2 20:25:58 2009 @@ -543,6 +543,22 @@ } /** + * Tests the implementation of getCapability(). + */ + public void testGetCapability() + { + HierarchicalSourceAdapterTestImpl adapter = new HierarchicalSourceAdapterTestImpl(); + final Object cap = new Object(); + EasyMock + .expect(adapter.getOriginalSource().getCapability(Object.class)) + .andReturn(cap); + EasyMock.replay(adapter.getOriginalSource()); + assertEquals("Wrong capability", cap, adapter + .getCapability(Object.class)); + EasyMock.verify(adapter.getOriginalSource()); + } + + /** * A specialized implementation of {...@code HierarchicalSourceAdapter} that * overrides some methods to inject mock objects. */ Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestSubConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestSubConfiguration.java?rev=821160&r1=821159&r2=821160&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestSubConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestSubConfiguration.java Fri Oct 2 20:25:58 2009 @@ -377,6 +377,16 @@ } /** + * Tests the implementation of getCapability(). This is just a dummy which + * does not return any concrete capabilities. + */ + public void testGetCapability() + { + setUpSubnodeConfig(); + assertNull("Got a capability", config.getCapability(Object.class)); + } + + /** * Initializes the parent configuration. This method creates the typical * structure of tables and fields nodes. *