wildfly: reading properties from configuration directory

EasterBunnyBugSmasher picture EasterBunnyBugSmasher · Jan 14, 2015 · Viewed 58.9k times · Source

I'm trying to read deployment specific information from a properties file in my wildfly configuration folder. I tried this:

@Singleton
@Startup
public class DeploymentConfiguration {

  protected Properties props;

  @PostConstruct
  public void readConfig() {

    props = new Properties();
    try {
      props.load(getClass().getClassLoader().getResourceAsStream("my.properties"));
    } catch (IOException e) {
      // ... whatever
    }
  }

But apparently this is not working since the configuration folder is not in the classpath anymore. Now I can't find an easy way to do it. My favorite would be something like this:

@InjectProperties("my.properties")
protected Properties props;

The only solution I found on the web so far involves making my own OSGi module, but I believe there must be an easier way to do it (one without OSGi!). Can anyone show me how?

Answer

John Ament picture John Ament · Jan 15, 2015

If you want to explicitly read a file from the configuration directory (e.g. $WILDFLY_HOME/standalone/configuration or domain/configuration) there's a system property with the path in it. Simply do System.getProperty("jboss.server.config.dir"); and append your file name to that to get the file.

You wouldn't read it as a resource though, so...

String fileName = System.getProperty("jboss.server.config.dir") + "/my.properties";
try(FileInputStream fis = new FileInputStream(fileName)) {
  properties.load(fis);
}

Then the file would be loaded for you.

Also, since WildFly doesn't ship with OSGi support anymore, I don't know how creating an OSGi module would help you here.