[configuration] no exception on missing key

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

[configuration] no exception on missing key

rusty-11
The documentation says that getString() throws a
NoSuchElementException "if the key doesn't map to an existing object."
I'm not getting any exception when I tested this by deleting the key
from my .xml file.  For example, in the following, I deleted the whole
<url> line and didn't get an exception when I did a getString("ldap.url").

<?xml version="1.0" encoding="ISO-8859-1" ?>

<config>
    <ldap>
        <url>ldap://caldir.berkeley.edu:636</url>
        <base>ou=people, dc=berkeley, dc=edu</base>
        <uid>uid=name, ou=applications, dc=berkeley, dc=edu</uid>
        <password>secret</password>
    </ldap>
</config>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [configuration] no exception on missing key

Moran Ben-David
Try checking the value returned by
       
        AbstractConfiguratin.isThrowExceptionOnMissing()

If it is false, then that might explain why you weren't getting an
exception.  Try calling setThrowExceptionOnMissing(true) in that case to get
the behaviour you're looking for.

Moran



> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> Sent: Wednesday, August 24, 2005 1:30 PM
> To: [hidden email]
> Subject: [configuration] no exception on missing key
>
> The documentation says that getString() throws a
> NoSuchElementException "if the key doesn't map to an existing object."
> I'm not getting any exception when I tested this by deleting the key
> from my .xml file.  For example, in the following, I deleted the whole
> <url> line and didn't get an exception when I did a getString("ldap.url").
>
> <?xml version="1.0" encoding="ISO-8859-1" ?>
>
> <config>
>     <ldap>
> <url>ldap://caldir.berkeley.edu:636</url>
> <base>ou=people, dc=berkeley, dc=edu</base>
> <uid>uid=name, ou=applications, dc=berkeley, dc=edu</uid>
> <password>secret</password>
>     </ldap>
> </config>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [configuration] no exception on missing key

Oliver Heger-2
You are right, Moran, setting the throwExceptionOnMissing flag should do
the trick.

However we should rework the javadocs for the Configuration getter
methods. They in fact claim that an exception is always thrown for
missing properties!

Oliver

Moran Ben-David wrote:

>Try checking the value returned by
>
> AbstractConfiguratin.isThrowExceptionOnMissing()
>
>If it is false, then that might explain why you weren't getting an
>exception.  Try calling setThrowExceptionOnMissing(true) in that case to get
>the behaviour you're looking for.
>
>Moran
>
>
>
>  
>
>>-----Original Message-----
>>From: [hidden email] [mailto:[hidden email]]
>>Sent: Wednesday, August 24, 2005 1:30 PM
>>To: [hidden email]
>>Subject: [configuration] no exception on missing key
>>
>>The documentation says that getString() throws a
>>NoSuchElementException "if the key doesn't map to an existing object."
>>I'm not getting any exception when I tested this by deleting the key
>>from my .xml file.  For example, in the following, I deleted the whole
>><url> line and didn't get an exception when I did a getString("ldap.url").
>>
>><?xml version="1.0" encoding="ISO-8859-1" ?>
>>
>><config>
>>    <ldap>
>> <url>ldap://caldir.berkeley.edu:636</url>
>> <base>ou=people, dc=berkeley, dc=edu</base>
>> <uid>uid=name, ou=applications, dc=berkeley, dc=edu</uid>
>> <password>secret</password>
>>    </ldap>
>></config>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: [hidden email]
>>For additional commands, e-mail: [hidden email]
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [configuration] no exception on missing key

Kevin Klinemeier
In reply to this post by rusty-11
Can one set that flag in the config file or the composite file?  I
hoped something like this would work, but no luck:

<configuration>
 <!--  the order of prescidence is first-to-last -->
 <system/>
 <xml fileName="prodConfig.xml"
               optional="true"
               throwExceptionOnMissing="true"/>
</configuration>

On a related note, is there some kind of document that describes the
config options available in that file?  I feel like there's a doc page
I haven't found yet.

-Kevin
(initially sent to the wrong list, sorry for the cross-post)

Oliver Heger wrote:
> You are right, Moran, setting the throwExceptionOnMissing flag should do
> the trick.
>
> However we should rework the javadocs for the Configuration getter
> methods. They in fact claim that an exception is always thrown for
> missing properties!

> Oliver

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [configuration] no exception on missing key

Oliver Heger-2
Kevin Klinemeier wrote:

>Can one set that flag in the config file or the composite file?  I
>hoped something like this would work, but no luck:
>
><configuration>
> <!--  the order of prescidence is first-to-last -->
> <system/>
> <xml fileName="prodConfig.xml"
>               optional="true"
>               throwExceptionOnMissing="true"/>
></configuration>
>
>On a related note, is there some kind of document that describes the
>config options available in that file?  I feel like there's a doc page
>I haven't found yet.
>
>-Kevin
>(initially sent to the wrong list, sorry for the cross-post)
>  
>
I checked this and found out that there is a bug (or at least an
undesired behavior) in CompositeConfiguration:

When the included configurations are loaded by the ConfigurationFactory,
their properties, including the throwExceptionOnMissing flag, are
correctly set. But when they are added to the resulting
CompositeConfiguration, this flag is reset because the code looks as
follows:

public void addConfiguration(Configuration config)
{
  ...
  if (config instanceof AbstractConfiguration)
  {
      ((AbstractConfiguration)
config).setThrowExceptionOnMissing(isThrowExceptionOnMissing());
  }
}

The purpose is that the configurations contained in a
CompositeConfiguration should have the same exception behavior as the
composite itself. Unfortunately when loaded from a ConfigurationFactory,
it is not possible to define the CompositeConfiguration's exception
flag, so it is always false.

I guess this should be fixed, or at least the
setThrowExceptionOnMissing() method overloaded in CompositeConfiguration
to set the flags on the contained configs. Then it would be possible to
turn this on after it was loaded by the factory.

Oliver

>Oliver Heger wrote:
>  
>
>>You are right, Moran, setting the throwExceptionOnMissing flag should do
>>the trick.
>>
>>However we should rework the javadocs for the Configuration getter
>>methods. They in fact claim that an exception is always thrown for
>>missing properties!
>>    
>>
>
>  
>
>>Oliver
>>    
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [configuration] no exception on missing key

Emmanuel Bourg-3
Oliver Heger wrote:

> The purpose is that the configurations contained in a
> CompositeConfiguration should have the same exception behavior as the
> composite itself. Unfortunately when loaded from a ConfigurationFactory,
> it is not possible to define the CompositeConfiguration's exception
> flag, so it is always false.
>
> I guess this should be fixed, or at least the
> setThrowExceptionOnMissing() method overloaded in CompositeConfiguration
> to set the flags on the contained configs. Then it would be possible to
> turn this on after it was loaded by the factory.

Does it really make sense to change the flag on only one of the
configurations ? What about supporting a syntax like this instead:

<configuration throwExceptionOnMissing="true">
   <system/>
   <xml fileName="prodConfig.xml" optional="true"/>
</configuration>

Emmanuel Bourg


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [configuration] no exception on missing key

Oliver Heger
Emmanuel Bourg schrieb:

> Oliver Heger wrote:
>
>> The purpose is that the configurations contained in a
>> CompositeConfiguration should have the same exception behavior as the
>> composite itself. Unfortunately when loaded from a
>> ConfigurationFactory, it is not possible to define the
>> CompositeConfiguration's exception flag, so it is always false.
>>
>> I guess this should be fixed, or at least the
>> setThrowExceptionOnMissing() method overloaded in
>> CompositeConfiguration to set the flags on the contained configs. Then
>> it would be possible to turn this on after it was loaded by the factory.
>
>
> Does it really make sense to change the flag on only one of the
> configurations ? What about supporting a syntax like this instead:
>
> <configuration throwExceptionOnMissing="true">
>   <system/>
>   <xml fileName="prodConfig.xml" optional="true"/>
> </configuration>
>
> Emmanuel Bourg
>

Yes, on the long run this would surely be better.

I just found out that the value of the flag in the configurations
contained in a CompositeConfiguration does not matter at all because a
getProperty() call is only performed if a containsKey() call returns
true. So it should be sufficient to set the composite's flag to get the
desired behavior.

So, Kevin, as a workaround for your current problem you can do something
like this:

Configuration config = factory.getConfiguration();
((AbstractConfiguration) config).setThrowExceptionOnMissing(true);

Oliver

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [configuration] no exception on missing key

Kevin Klinemeier
Thanks, that worked pretty well.  I needed a static class to read the
file once anyway, so not a terrible approach.

-Kevin

On 9/1/05, Oliver Heger <[hidden email]> wrote:

> Emmanuel Bourg schrieb:
> > Oliver Heger wrote:
> >
> >> The purpose is that the configurations contained in a
> >> CompositeConfiguration should have the same exception behavior as the
> >> composite itself. Unfortunately when loaded from a
> >> ConfigurationFactory, it is not possible to define the
> >> CompositeConfiguration's exception flag, so it is always false.
> >>
> >> I guess this should be fixed, or at least the
> >> setThrowExceptionOnMissing() method overloaded in
> >> CompositeConfiguration to set the flags on the contained configs. Then
> >> it would be possible to turn this on after it was loaded by the factory.
> >
> >
> > Does it really make sense to change the flag on only one of the
> > configurations ? What about supporting a syntax like this instead:
> >
> > <configuration throwExceptionOnMissing="true">
> >   <system/>
> >   <xml fileName="prodConfig.xml" optional="true"/>
> > </configuration>
> >
> > Emmanuel Bourg
> >
>
> Yes, on the long run this would surely be better.
>
> I just found out that the value of the flag in the configurations
> contained in a CompositeConfiguration does not matter at all because a
> getProperty() call is only performed if a containsKey() call returns
> true. So it should be sufficient to set the composite's flag to get the
> desired behavior.
>
> So, Kevin, as a workaround for your current problem you can do something
> like this:
>
> Configuration config = factory.getConfiguration();
> ((AbstractConfiguration) config).setThrowExceptionOnMissing(true);
>
> Oliver
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]