commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Borut Bolčina <...@najdi.si>
Subject Re: Configuration
Date Tue, 17 Jan 2006 07:21:10 GMT
Hello,

if you want to have access to several configuration files from one 
place, then you should use
CompositeConfiguration config = getConfiguration("configuration.xml");

In configuration.xml you enumerate the config files you intend to use
<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
  <properties fileName="Some.properties"/>
  <properties fileName="Another.properties"/>
  <xml filename="YetAnotherProperties.xml"/>
</configuration>

In the above case you can access values in all three property files 
through the same method calls, like
valueOfTypeLong = config.getLong("someKey", 300000);
valueOfTypeString = config.getString("anotherKey", "default value if 
this key not present");

If you want values from XML configuration file 
(YetAnotherProperties.xml) then you (for example):
List<String> keywords = config.getList("blogs.keyword");

You will get a collection of all values (value1, anotherValue) if XML 
config looks like
<feeds>
    <news>
        <keyword>http://www.aaa.com/feeds/12.xml</keyword>
        <keyword>http://www.bbb.com/feeds/16.xml</keyword>
        <keyword>http://www.ccc.com/feeds/18.xml</keyword>
    </news>
    <blogs>
        <keyword>value1</keyword>
        <keyword>anotherValue</keyword>
    </blogs>
</feeds>

Now, if you want an explicit configuration out of CompositeConfiguration 
then you can by
PropertiesConfiguration pc = (PropertiesConfiguration) 
config.getConfiguration(0);
or
XMLPropertiesConfiguration xc = (XMLPropertiesConfiguration) 
config.getConfiguration(2);
if using the above configuration.xml file.

Hope this helps,
Borut

On 17.1.2006 5:26, Kfir Shay wrote:
> Hi,
>
> I am a bit confused about the documentation inconsistencies in the commons
> configuration package.
>
> In the "ConfigurationFactory Howto" document in the section called "The
> configuration definition file" it is stated that the xml element creates an
> instance of XMLConfiguration:
> "The xml element can be used to load XML configuration files. It also uses
> the fileName attribute to determine the name of the file to load and creates
> an instance of XMLConfiguration."  Yet earlier in the "Accessing properties"
> section it is stated that ConfigurationFactory only return a configuration
> object via the getConfiguration()  method: "Whatever way we used to load the
> configuration factory, we should now have a Configuration object that was
> returned by the factory's getConfiguration()  method. This object is
> actually an instance of the CompositeConfiguration class"
>
> c\Currently my configuration consist of only one xml file but I want to use
> the ConfigurationFactory and leverage the config.xml file for the case that
> more configuration files will be added in the future (most probably all will
> be in xml format) but at the same time I need to get a XMLConfiguration
> object not the more general Configuration object can someone please clarify
> how to achieve this.
>
> This is my first post to the list, I hope some of you will take the time to
> give me an answer.
>
> Thanks,
> Kfir S.
> AIM:kingink24
> Email: kfir.shay@gmail.com
>
>   

-- 
bLOG <http://www.delo.si/blog/borutb/>
--
Naključna *izjava tedna* iz tednika Mladina:

Mime
View raw message