Singleton-Configuration-Loader and Interfaces with configuration-constants

Hi!

In order to simplify the first version of my application, I have been using Interfaces to define configuration constants. E.g. InterfaceDataConstants: publicfinallong NOT_SET = -1;

Now I would like to expand my application using a singleton ConfigHandler to load the configuration settings as System Properties.

publicstatic String getConfigurationProperty(String property_name)throws ConfigurationError{

if (configarationManager ==null)

configarationManager =new ConfigurationManager();

return System.getProperty(property_name);

}

The constructor ConfigurationManager() calls the init()-method, which reads the configuration file and sets the system properties:

privatevoid init()throws ConfigurationError{

String configFilePath = DataConstants.CONFIG_FILE_NAME_UNIX;

properties =new Properties(System.getProperties());

try{

// Read configuration

properties.load(new BufferedInputStream(new FileInputStream(configFilePath)));

}catch (FileNotFoundException e1){

thrownew ConfigurationError(ErrorConstants.CONFIGURATION_ERROR,"--","Couldn抰 find " +configFilePath+"!");

}catch (IOException e){

thrownew ConfigurationError(ErrorConstants.CONFIGURATION_ERROR,"--"," Couldn抰 read ?configFilePath+"!");

}

// Set Properties

System.setProperties(properties);

}

I hoped, I could just set the constants by calling the static method getConfigurationProperty() and carry on using the constant-interface in my classes. E.g.:

publicfinallong NOT_SET =new Long(ConfigurationManager.getConfigurationProperty("NOT_SET")).longValue();

But I get problems handling possible Exceptions.

Is there any possibility to keep the interfaces? Surely I could use e.g.publiclong user_id = = ConfigurationManager.getConfigurationProperty("NOT_SET");

insead of

publiclong user_id = DataConstants.NOT_SET;

in my classes, but then I have to modify a great number of code lines, which I'd like to prevent. Do you have any advice? Thanks for any help.

Lena

[3700 byte] By [Lena_a] at [2007-10-2 15:24:53]
# 1
as far as handling contants in the interfaces is concerned, I wouldnot do that. Thats not what interfaces are for, I would rather use a final class as as specified in http://www.javapractices.com/Topic2.cjp
kilyasa at 2007-7-13 14:39:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

> I hoped, I could just set the constants by calling

> the static method getConfigurationProperty() and

> carry on using the constant-interface in my classes.

> E.g.: public final long NOT_SET = new Long(ConfigurationManager.getConfigurationProperty("NOT_SET")).longValue();

> But I get problems handling possible Exceptions.

You can try making the constants blank finals, and initializing them in a static initializer block. In this block you can handle the exceptions (but probably not recover, so you'll have to decide on a conservative measure when the exception prevents initialization of the "constants", such as warn the user and exit - N.B.: the static initializer will not be run once more in the same JVM).

Incidentally, I agree with the previous poster that the constants should be declared in a dedicated class, not interface.

Generally people that go the interface way are motivated by dropping the interface name (e.g. in your case their classes would inherit DataConstants, and you could write public long user_id = NOT_SET;

instead of public long user_id = DataConstants.NOT_SET;

).

What a big deal. AFAIC I find the first line LESS readable.

As you've chosen the second option anyway, make DataConstants a class, so as to prevent the interface being used awkwardly. That won't affect the other classes that are already written the correct way.

jdupreza at 2007-7-13 14:39:21 > top of Java-index,Other Topics,Patterns & OO Design...