Configuration Management in JavaEE

My goal

I have a JavaEE environment (in my particular case, this is a Glassfish web profile) and I want a container-independent way to configure my application with the following functions:

  • The default configuration when nothing is specified (inside the WAR file)
  • User configuration (outside the WAR file) in two layers:
    • Specific parameters of the node (in an external properties file, for example, in some working directory)
    • Special application settings (in the database, for example, mailbox size)

My desire would be to have as few preconditions as possible (the only JNDI data source now) to deploy and run my application (configure the JNDI data source, expand the WAR file, have an additional .properties file in some configuration folder and - done )

This leads me to my first question: Is this a normal / good / useful installation or is it too complex and / or very exotic?

My idea (bye)

Default configuration

The default configuration will be in the properties file:

src/main/resources/config/default.properties

The bean application area reads these properties during initialization, as described here :

@Named
@ApplicationScoped
public class Configuration implements Serializable {

    ...

    @PostConstruct
    public void initConfiguration() {
        loadDefaultConfiguration();
    }

    private void loadDefaultConfiguration() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        try (InputStream input = classLoader.getResourceAsStream("/config/default.properties")) {
            properties.load(input);
        } catch(IOException ex) {
            LOGGER.error(...);
        }
    }

}

Application Specific Parameters

. EntityManager, , JPA :). , .

@Named
@ApplicationScoped
public class Configuration implements Serializable {

    ...

    public T getProperty(final PropertyKeyEnum key, final Class<T> type) {
        if (key.getSource() == PropertySourceEnum.DATABASE) {
            return configurationDao.getByKey(key.getKey(), type);
        }

        ...
    }
}

, :

? myAppName.properties , ( , ).

  • JavaSE 7
  • JavaEE 6
  • Glassfish 3.1.2 Web ( ;))

Update

Glassfish, , :

System.getProperty("myApp.propertyName");

.properties, , ,

  • , ( JavaEE) ​​
  • -.
+5
2

() .

, . (, MYAPP_HOME), , , (, <user.home>/.myapp):

private Path discoverRootDirectory() {
    String myAppHome = System.getenv("MYAPP_HOME");

    if (myAppHome == null) {
        return Paths.get(System.getProperty("user.home"), ".myapp");
    } else {
        return Paths.get(myAppHome);
    }
}

, :

private void loadConfiguration() {
    properties = new Properties();
    // ...
    try (InputStream inputStream = Files.newInputStream(discoverRootDirectory())) {
        properties.load(inputStream);
    } catch (FileAccessException | IOException ex) {
        // ...
    }
}
+1

, , , (, ), (Glassfish, JBoss, WebSphere ..) , . , , , , , , . , . , , , , script (, ..). ? , , . . .

, , (- ).

-1

All Articles