Forgot to include my private method getConfiguration(), it does what
Oliver suggests:
On 17.1.2006 20:50, 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
The method getConfiguration only take int argument so your example line
-
CompositeConfiguration config = getConfiguration("configuration.xml");
simply won't work.
private CompositeConfiguration getConfiguration(String confDefinition) {
ConfigurationFactory factory = new
ConfigurationFactory(confDefinition);
try {
CompositeConfiguration config = (CompositeConfiguration)
factory.getConfiguration();
return config;
} catch (ConfigurationException e) {
e.printStackTrace();
}
return null;
}
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.
If you get class cast exception then check your configuration.xml. Fist
entry is index 0, not 1. Another check can be done by
if (config == null || !(config.getConfiguration(2) instanceof
XMLConfiguration)) {
throw new Exception("Please check configuration.xml. Third
entry should be XML conf file."); // or similar action
}
I am not trying to pick or anything but it seems like what you told me
doesn't work.
That's ok, I was a bit sloppy.
--bb
Thanks,
Kfir S.
On 1/17/06, Borut Bolčina <[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]
--
--
Naključna
izjava
tedna iz tednika Mladina:
--

--
Naključna izjava
tedna iz tednika Mladina:
|