You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Rapthor <ra...@lycos.de> on 2007/01/02 20:01:13 UTC

ANT task to process properties-files into static class

Hi,

I think this problem is very interesting. I have a web application that
needs various configuration. I'm using a properties file that is loaded into
a Properties-class.

The problem arrises when runtime exceptions are thrown
(NullPointerException) by using:

String value = props.getProperty("myProp");

(In this case the entry "myProp" is not available.)

The solution would be to process the properties file and put all keys and
values into a class with public static attributes:

class Props {
public static String myProp = "123";
.....
}

This processing should be done with ANT! It shall create a JAVA file before
the general compilation task. This would be great and runtime exceptions
concerning a lack of propertie-keys are extinguished forever.

Is there any tool for ANT that helps?

Thanks in advance.
-- 
View this message in context: http://www.nabble.com/ANT-task-to-process-properties-files-into-static-class-tf2909466.html#a8128739
Sent from the Ant - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: ANT task to process properties-files into static class

Posted by "Alexey N. Solofnenko" <A....@mdl.com>.
It seems a trivial task to generate a Java source  file from a property 
with <scripdef>.

- Alexey.

Rapthor wrote:
> Sorry, having confused you.
>
> I have a file named global.properties. This file is edited manually by me.
> The webapplication loads this file into a java.util.Properties-instance at
> start.
>
> Everywhere I use this Properties instance like "props.getProperty("myProp")"
> I have to check whether a "null" is returned or not. This check is at
> runtime! I wanted to see these at compile time. And that would be possible
> with a static class with attributes.
>   

-- 
------------------------------------------------------------------------
Alexey N. Solofnenko <http://trelony.cjb.net/>
Pleasant Hill, CA (GMT-8 usually)

Re: ANT task to process properties-files into static class

Posted by Rapthor <ra...@lycos.de>.
Sorry, having confused you.

I have a file named global.properties. This file is edited manually by me.
The webapplication loads this file into a java.util.Properties-instance at
start.

Everywhere I use this Properties instance like "props.getProperty("myProp")"
I have to check whether a "null" is returned or not. This check is at
runtime! I wanted to see these at compile time. And that would be possible
with a static class with attributes.
-- 
View this message in context: http://www.nabble.com/ANT-task-to-process-properties-files-into-static-class-tf2909466.html#a8131908
Sent from the Ant - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: ANT task to process properties-files into static class

Posted by "Scot P. Floess" <fl...@mindspring.com>.
Sorry, I'm not sure I follow your problem...  Do you mean Ant should 
somehow generate a properties file based upon your build.xml 
properties?  Who sets the properties and how is this null pointer being 
raised?  I am a "wee" bit confused ;)

Rapthor wrote:
> Scot P. Floess wrote:
>   
>> If a property does not exist, why is a null pointer thrown?  I assume 
>> you mean java.util.Properties?  If so, that does not raise a null 
>> pointer exception.
>>
>>     
>
> You are right ... the NullPointerException would be thrown if I relied on a
> value read from the Properties instance. (If no matching key was present in
> the file.)
>   

-- 
Scot P. Floess
27 Lake Royale
Louisburg, NC  27549

252-478-8087 (Home)
919-754-4592 (Work)

Chief Architect JPlate  http://sourceforge.net/projects/jplate
Chief Architect JavaPIM http://sourceforge.net/projects/javapim


Re: ANT task to process properties-files into static class

Posted by Rapthor <ra...@lycos.de>.

Scot P. Floess wrote:
> 
> If a property does not exist, why is a null pointer thrown?  I assume 
> you mean java.util.Properties?  If so, that does not raise a null 
> pointer exception.
> 

You are right ... the NullPointerException would be thrown if I relied on a
value read from the Properties instance. (If no matching key was present in
the file.)
-- 
View this message in context: http://www.nabble.com/ANT-task-to-process-properties-files-into-static-class-tf2909466.html#a8131316
Sent from the Ant - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: ANT task to process properties-files into static class

Posted by "Scot P. Floess" <fl...@mindspring.com>.
If a property does not exist, why is a null pointer thrown?  I assume 
you mean java.util.Properties?  If so, that does not raise a null 
pointer exception.

Rapthor wrote:
> Hi,
>
> I think this problem is very interesting. I have a web application that
> needs various configuration. I'm using a properties file that is loaded into
> a Properties-class.
>
> The problem arrises when runtime exceptions are thrown
> (NullPointerException) by using:
>
> String value = props.getProperty("myProp");
>
> (In this case the entry "myProp" is not available.)
>
> The solution would be to process the properties file and put all keys and
> values into a class with public static attributes:
>
> class Props {
> public static String myProp = "123";
> .....
> }
>
> This processing should be done with ANT! It shall create a JAVA file before
> the general compilation task. This would be great and runtime exceptions
> concerning a lack of propertie-keys are extinguished forever.
>
> Is there any tool for ANT that helps?
>
> Thanks in advance.
>   

-- 
Scot P. Floess
27 Lake Royale
Louisburg, NC  27549

252-478-8087 (Home)
919-754-4592 (Work)

Chief Architect JPlate  http://sourceforge.net/projects/jplate
Chief Architect JavaPIM http://sourceforge.net/projects/javapim


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: ANT task to process properties-files into static class

Posted by James Abley <ja...@volantis.com>.
If that's the sort of thing you're after, Log4J has a similar mechanism 
which can monitor the config file and polls to see if it needs to reload 
the configuration. [1]

I think that would provide a template for you. YMMV.

Cheers,

James

[1] 
http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/DOMConfigurator.html#configureAndWatch(java.lang.String,%20long)


Rapthor wrote:
> 
> Rapthor wrote:
>>
>> Donald McLean wrote:
>>> Really? That sounds like a case of "The cure is worse than the disease."
>>>
>>> I can't imagine a single case where something like that would be a 
>>> better solution than the three or four techniques for providing static 
>>> attributes that I'm already familiar with.
>>>
>>> However, of course, YMMV.
>>>
>> Hmm you mean I should leave the properties file out and create a static
>> class instead? That sounds useful ... On the first attempt I wanted to use
>> JAVAs Properties-class and so I used properties-files as base.
>>
>> Did I get you right?
>>
>>
> 
> Now I remember: I wanted to be able to change configuration values at
> runtime. I would just have to provide a mechanism to reload the Properties
> instance with the file. But the mechanism to create a static class does not
> confirm with that idea.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: ANT task to process properties-files into static class

Posted by Rapthor <ra...@lycos.de>.

Rapthor wrote:
> 
> 
> Donald McLean wrote:
>> 
>> Really? That sounds like a case of "The cure is worse than the disease."
>> 
>> I can't imagine a single case where something like that would be a 
>> better solution than the three or four techniques for providing static 
>> attributes that I'm already familiar with.
>> 
>> However, of course, YMMV.
>> 
> 
> Hmm you mean I should leave the properties file out and create a static
> class instead? That sounds useful ... On the first attempt I wanted to use
> JAVAs Properties-class and so I used properties-files as base.
> 
> Did I get you right?
> 
> 

Now I remember: I wanted to be able to change configuration values at
runtime. I would just have to provide a mechanism to reload the Properties
instance with the file. But the mechanism to create a static class does not
confirm with that idea.
-- 
View this message in context: http://www.nabble.com/ANT-task-to-process-properties-files-into-static-class-tf2909466.html#a8131373
Sent from the Ant - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: ANT task to process properties-files into static class

Posted by Donald McLean <dm...@stsci.edu>.
Rapthor wrote:
> 
> Donald McLean wrote:
>> Really? That sounds like a case of "The cure is worse than the disease."
>>
>> I can't imagine a single case where something like that would be a 
>> better solution than the three or four techniques for providing static 
>> attributes that I'm already familiar with.
>>
>> However, of course, YMMV.
>>
> 
> Hmm you mean I should leave the properties file out and create a static
> class instead? That sounds useful ... On the first attempt I wanted to use
> JAVAs Properties-class and so I used properties-files as base.
> 
> Did I get you right?

No.

Using a class with static attributes, either manually or automatically 
created, has only one advantage - you can verify at compile time that 
every attribute that you need to be defined is, in fact, defined.

On the other hand, the disadvantages of that approach are hideous.

If you're having problems using a .properties file with the Properties 
class then having better testing (to catch required missing properties) 
or subclassing Properties (to implement better handling for undefined 
properties) would be much better solutions.

Donald

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: ANT task to process properties-files into static class

Posted by Rapthor <ra...@lycos.de>.

Donald McLean wrote:
> 
> Really? That sounds like a case of "The cure is worse than the disease."
> 
> I can't imagine a single case where something like that would be a 
> better solution than the three or four techniques for providing static 
> attributes that I'm already familiar with.
> 
> However, of course, YMMV.
> 

Hmm you mean I should leave the properties file out and create a static
class instead? That sounds useful ... On the first attempt I wanted to use
JAVAs Properties-class and so I used properties-files as base.

Did I get you right?

-- 
View this message in context: http://www.nabble.com/ANT-task-to-process-properties-files-into-static-class-tf2909466.html#a8131286
Sent from the Ant - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: ANT task to process properties-files into static class

Posted by Donald McLean <dm...@stsci.edu>.
Really? That sounds like a case of "The cure is worse than the disease."

I can't imagine a single case where something like that would be a 
better solution than the three or four techniques for providing static 
attributes that I'm already familiar with.

However, of course, YMMV.

Rapthor wrote:
> 
> The solution would be to process the properties file and put all keys and
> values into a class with public static attributes:


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org