Author: matthieu Date: Tue Nov 3 16:48:26 2015 New Revision: 1712333 URL: http://svn.apache.org/viewvc?rev=1712333&view=rev Log: JAMES-1626 Implement an HierarchicalConfiguration reader for guice use
Added: james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ClassPathConfigurationProvider.java james/project/trunk/server/container/cassandra-guice/src/test/java/org/apache/james/utils/ClassPathConfigurationProviderTest.java james/project/trunk/server/container/cassandra-guice/src/test/resources/test.xml Added: james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ClassPathConfigurationProvider.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ClassPathConfigurationProvider.java?rev=1712333&view=auto ============================================================================== --- james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ClassPathConfigurationProvider.java (added) +++ james/project/trunk/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ClassPathConfigurationProvider.java Tue Nov 3 16:48:26 2015 @@ -0,0 +1,69 @@ +/**************************************************************** + * 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. * + ****************************************************************/ + +package org.apache.james.utils; + +import java.io.InputStream; +import java.util.List; +import java.util.Optional; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.HierarchicalConfiguration; +import org.apache.commons.configuration.XMLConfiguration; + +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import com.google.common.base.Strings; +import com.google.common.collect.Iterables; + +public class ClassPathConfigurationProvider { + + private static final String CONFIGURATION_FILE_SUFFIX = ".xml"; + + public HierarchicalConfiguration getConfiguration(String component) throws ConfigurationException { + Preconditions.checkNotNull(component); + List<String> configPathParts = Splitter.on(".").splitToList(component); + Preconditions.checkArgument(!configPathParts.isEmpty()); + HierarchicalConfiguration config = getConfig(retrieveConfigInputStream(configPathParts.get(0))); + return selectHierarchicalConfigPart(config, Iterables.skip(configPathParts, 1)); + } + + private InputStream retrieveConfigInputStream(String configurationFileWithoutExtension) throws ConfigurationException { + Preconditions.checkArgument(!Strings.isNullOrEmpty(configurationFileWithoutExtension), "The configuration file name should not be empty or null"); + return Optional.ofNullable(ClassLoader.getSystemResourceAsStream(configurationFileWithoutExtension + CONFIGURATION_FILE_SUFFIX)) + .orElseThrow(() -> new ConfigurationException("Unable to locate configuration file " + configurationFileWithoutExtension + CONFIGURATION_FILE_SUFFIX)); + } + + private XMLConfiguration getConfig(InputStream configStream) throws ConfigurationException { + XMLConfiguration config = new XMLConfiguration(); + config.setDelimiterParsingDisabled(true); + config.setAttributeSplittingDisabled(true); + config.load(configStream); + return config; + } + + private HierarchicalConfiguration selectHierarchicalConfigPart(HierarchicalConfiguration config, Iterable<String> configsPathParts) { + HierarchicalConfiguration currentConfig = config; + for (String nextPathPart : configsPathParts) { + currentConfig = currentConfig.configurationAt(nextPathPart); + } + return currentConfig; + } + +} Added: james/project/trunk/server/container/cassandra-guice/src/test/java/org/apache/james/utils/ClassPathConfigurationProviderTest.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cassandra-guice/src/test/java/org/apache/james/utils/ClassPathConfigurationProviderTest.java?rev=1712333&view=auto ============================================================================== --- james/project/trunk/server/container/cassandra-guice/src/test/java/org/apache/james/utils/ClassPathConfigurationProviderTest.java (added) +++ james/project/trunk/server/container/cassandra-guice/src/test/java/org/apache/james/utils/ClassPathConfigurationProviderTest.java Tue Nov 3 16:48:26 2015 @@ -0,0 +1,114 @@ +/**************************************************************** + * 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. * + ****************************************************************/ + +package org.apache.james.utils; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.HierarchicalConfiguration; +import org.junit.Before; +import org.junit.Test; + +public class ClassPathConfigurationProviderTest { + + private static final String CONFIG_KEY_1 = "test2"; + private static final String CONFIG_KEY_2 = "property"; + private static final String CONFIG_KEY_4 = "james"; + private static final String CONFIG_KEY_5 = "internal"; + private static final String VALUE_1 = "0"; + private static final String VALUE_2 = "awesome"; + private static final String VALUE_3 = "james"; + private static final String FAKE_CONFIG_KEY = "fake"; + private static final String ROOT_CONFIG_KEY = "test"; + private static final String CONFIG_SEPARATOR = "."; + + private ClassPathConfigurationProvider configurationProvider; + + @Before + public void setUp() { + configurationProvider = new ClassPathConfigurationProvider(); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyArgumentShouldThrow() throws Exception { + configurationProvider.getConfiguration(""); + } + + @Test(expected = NullPointerException.class) + public void nullArgumentShouldThrow() throws Exception { + configurationProvider.getConfiguration(null); + } + + @Test(expected = IllegalArgumentException.class) + public void configSeparatorArgumentShouldThrow() throws Exception { + configurationProvider.getConfiguration(CONFIG_SEPARATOR); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyFileNameShouldThrow() throws Exception { + configurationProvider.getConfiguration(CONFIG_SEPARATOR + ROOT_CONFIG_KEY); + } + + @Test + public void getConfigurationShouldLoadCorrespondingXMLFile() throws Exception { + HierarchicalConfiguration hierarchicalConfiguration = configurationProvider.getConfiguration(ROOT_CONFIG_KEY); + assertThat(hierarchicalConfiguration.getKeys()).containsOnly(CONFIG_KEY_1, + String.join(CONFIG_SEPARATOR, CONFIG_KEY_4, CONFIG_KEY_2), + String.join(CONFIG_SEPARATOR, CONFIG_KEY_4, CONFIG_KEY_5, CONFIG_KEY_2)); + assertThat(hierarchicalConfiguration.getProperty(CONFIG_KEY_1)).isEqualTo(VALUE_1); + } + + @Test + public void getConfigurationShouldLoadCorrespondingXMLFilePart() throws Exception { + HierarchicalConfiguration hierarchicalConfiguration = configurationProvider.getConfiguration( + String.join(CONFIG_SEPARATOR, ROOT_CONFIG_KEY, CONFIG_KEY_4)); + assertThat(hierarchicalConfiguration.getKeys()).containsOnly(CONFIG_KEY_2, + String.join(CONFIG_SEPARATOR, CONFIG_KEY_5, CONFIG_KEY_2)); + assertThat(hierarchicalConfiguration.getProperty(CONFIG_KEY_2)).isEqualTo(VALUE_2); + } + + @Test + public void getConfigurationShouldLoadCorrespondingXMLFileWhenAPathIsProvidedPart() throws Exception { + HierarchicalConfiguration hierarchicalConfiguration = configurationProvider.getConfiguration( + String.join(CONFIG_SEPARATOR, ROOT_CONFIG_KEY, CONFIG_KEY_4, CONFIG_KEY_5)); + assertThat(hierarchicalConfiguration.getKeys()).containsOnly(CONFIG_KEY_2); + assertThat(hierarchicalConfiguration.getProperty(CONFIG_KEY_2)).isEqualTo(VALUE_3); + } + + @Test + public void multiplesSeparatorsShouldBeTolerated() throws Exception { + HierarchicalConfiguration hierarchicalConfiguration = configurationProvider.getConfiguration( + ROOT_CONFIG_KEY + CONFIG_SEPARATOR + CONFIG_SEPARATOR + CONFIG_KEY_4); + assertThat(hierarchicalConfiguration.getKeys()).containsOnly(CONFIG_KEY_2, + String.join(CONFIG_SEPARATOR, CONFIG_KEY_5, CONFIG_KEY_2)); + assertThat(hierarchicalConfiguration.getProperty(CONFIG_KEY_2)).isEqualTo(VALUE_2); + } + + @Test(expected = ConfigurationException.class) + public void getConfigurationShouldThrowOnNonExistingXMLFile() throws Exception { + assertThat(configurationProvider.getConfiguration(FAKE_CONFIG_KEY)).isNotNull(); + } + + @Test(expected = IllegalArgumentException.class) + public void getConfigurationShouldThrowOnNonExistingXMLFilePart() throws Exception { + configurationProvider.getConfiguration(String.join(CONFIG_SEPARATOR, ROOT_CONFIG_KEY, FAKE_CONFIG_KEY)); + } + +} Added: james/project/trunk/server/container/cassandra-guice/src/test/resources/test.xml URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/cassandra-guice/src/test/resources/test.xml?rev=1712333&view=auto ============================================================================== --- james/project/trunk/server/container/cassandra-guice/src/test/resources/test.xml (added) +++ james/project/trunk/server/container/cassandra-guice/src/test/resources/test.xml Tue Nov 3 16:48:26 2015 @@ -0,0 +1,28 @@ +<?xml version="1.0"?> +<!-- + 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> + <test2>0</test2> + <james> + <property>awesome</property> + <internal> + <property>james</property> + </internal> + </james> +</test> \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org