ConfigurationFactory will load all configuration sources listed in the
configuration definition file and combine them into a single
configuration object, which is an instance of CompositeConfiguration, a
container for other Configuration objects.
So if factory points to an initialized ConfigurationFactory object, you
can do something like
CompositeConfiguration cc = (CompositeConfiguration)
factory.getConfiguration();
After that you can use cc like a normal configuration object for
querying properties. You can also access the original configuration
objects that are stored in this container. This can be done through
CompositeConfiguration's getConfiguration(int index) method. If you know
that the first configuration in your factory definition file is an
XMLConfiguration (loaded by the <xml> tag), you can safely cast it as
follows:
XMLConfiguration xmlConf = (XMLConfiguration) cc.getConfiguration(0);
Now you can pass this object to other components.
HTH
Oliver
Kfir Shay wrote:
Hi Borut,
take another look at the java doc for CompositeConfiguration
http://jakarta.apache.org/commons/configuration/apidocs/org/apache/commons/configuration/CompositeConfiguration.html
<http://jakarta.apache.org/commons/configuration/apidocs/org/apache/commons/configuration/CompositeConfiguration.html>
The method getConfiguration only take int argument so your example line -
CompositeConfiguration config = getConfiguration("configuration.xml");
simply won't work.
Also even if I create a CompositeConfiguration object manually using the
getConfiguration method will return a Configuration object which means
that doing something like this (this is your example)
XMLPropertiesConfiguration xc = (XMLPropertiesConfiguration)
config.getConfiguration(2);
will throw a class cast exception.
I am not trying to pick or anything but it seems like what you told me
doesn't work.
Thanks,
Kfir S.
On 1/17/06, *Borut Bolčina* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>
wrote:
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: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]