3

I have started reading the Apache commons documentation but its very extensive so I am hoping someone can answer a simple question so I don't need to read all of it just to start using it for basic configuration. I am losing patience really quick with it - there is no "quick start chapter" and I don't need to know every detail before I decide if I want to use the library or not.

I want (what I think is a common use-case) a class with static methods that provides look up of properties.

E.g. in class Foo i can use

Settings.config.getString("paramter"); 

Where

 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.DefaultConfigurationBuilder;
 /**
  * Settings configuration class
  */
 public class Settings  {

     private static final DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder("config.xml"); 
     public static final Configuration config= factory.getConfiguration();

}

The problem is that the factory method can throw an exception! So this code does not compile, a class cannot throw an exception either so I suspect that I need to do a lot more coding.

I suspect that there is a simple solution to this. But it surely cannot be calling

DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder("config.xml"); 
Configuration config= factory.getConfiguration();

In every class where I want to read configurations?


I have tried:

public class Settings  {
 public static final Configuration config;
 static {
     try {
    DefaultConfigurationBuilder factory; 
    factory = new DefaultConfigurationBuilder("config.xml") ; 
         config = factory.getConfiguration();
     }
     catch (ConfigurationException e) {
         // Deal with the exception
    config=null;
    System.exit(1);
     }
 }
}

But I get the compilation error:

 error: variable config might already have been assigned
   [javac]      config=null;
user1443778
  • 581
  • 1
  • 5
  • 20

1 Answers1

4

You could put the code to initialize config in a static initializer block and deal with the exception there. For example:

public class Settings {
    public static final Configuration config;

    static {
        Configuration c = null;
        try {
            DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder("config.xml");
            c = factory.getConfiguration();
        }
        catch (SomeException e) {
            // Deal with the exception
            c = null;
        }

        config = c;
    }
}
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Does not compile: Settings.java:9: error: variable config might not have been initialized – user1443778 Nov 05 '12 at 09:05
  • @user1443778 That was not a complete example - you'll have to finish it yourself. Since `config` is `final`, it must be initialized. In case of an exception, you'll have to initialize it to something. For example, set it to `null` or some other appropriate value (maybe an object with default settings?) in the `catch` block. – Jesper Nov 05 '12 at 09:07
  • Settings.java:22: error: variable config might already have been assigned [javac] config=null; So It can't be final? – user1443778 Nov 05 '12 at 09:08
  • @user1443778 I changed the answer (see above) so that you don't get this error. – Jesper Nov 05 '12 at 10:09