last update: 06/29/2004 11:30:17

The configuration server

Managing configurations can get difficult. When you run your application in different environments for example. Or if you have a lot of clients that all share the same configuration. When you include the configuration in the client installation then every update is difficult.

The configuration can help a lot in these cases.

How does the server works

The configuration server is mainly a webserver. It uses the HTTP protocol to communicate between the client. The server has a strategy based on the incoming ip address and the configuration name to find the configuration. One important thing is how the server will actually
search for a configuration when a request comes in. First of all the server will try to find a
configuration file with the name based on the ip-address of the client and the requested
configuration name. So it will look for (example):
127.0.0.1_myconfig.xml
If not found it will cut off the last part of the ip-address and try again. It will repeat this until
there only the name left. The reason for this lookup mechanism is that you can move your
application from one server to another server and the config server will give you the correct
configuration. This makes moving an application from a test to a development server much
easier. The application does not change and does not have to take care of different
configurations.

Installing the server

First create a directory where the server should run in. Then you can create a subdirectory where the configuration files will be stored. This is optional. Copy the jconfig.jar file to this directory. Create the jconfig.properties file. Now you can run the server:
java -classpath .;jconfig.jar org.jconfig.server.ConfigurationServer

Running the server

In order to run the server it needs two parameters. The port and where the base directory is where all configurations are stored. You can define the parameters inside a file "jconfig.properties" and put this file in the classpath:
# the port that the server will listen on
server.port=8765
# the directory where the server will search the configuration files
server.docroot=c:\configserver\files

The ConfigServerHandler

In order to get a configuration from the server the client has to use the ConfigServerHandler. Here is an example:
import org.jconfig.*;
import org.jconfig.handler.*;

public class MyConfigLoader {

    public static void load() {
      ConfigServerHandler handler = new ConfigServerHandler();
      try {
        urlHandler.setURL("jconfig://myconfigserver:8765/serverconfig");
        ConfigurationManager cm = ConfigurationManager.getInstance();
        // we use the ConfigurationManager so it can store the config
        cm.load(urlHandler.load(),"MyConfig");
      }
      catch (ConfigurationManagerException cme) {
        // do something here since we did not get a configuration
      }
    }
}
In this example we have used the ConfigServerHandler to load the configuration. If we use the load method in the ConfigurationManager then the configuration will be stored and all other classes can access the configuration simply like this:
import org.jconfig.*;

public class MyApp {

    private static final Configuration config = ConfigurationManager.getConfiguration("MyConfig");

    public static void main(String[] args) {
      System.out.println("config:"+config.toString());
    }
}
We can also implement a fall back strategy with the first example. The way would be to load the configuration and save it to a file. If the next time we cannot load the configuration we have at least the backup of the last time. Here is an example:
import org.jconfig.*;
import org.jconfig.handler.*;
import java.io.File;

public class MyConfigLoader {

    public static void load() {
      ConfigServerHandler handler = new ConfigServerHandler();
      try {
        urlHandler.setURL("jconfig://myconfigserver:8765/serverconfig");
        ConfigurationManager cm = ConfigurationManager.getInstance();
        // we use the ConfigurationManager so it can store the config
        cm.load(urlHandler.load(),"MyConfig");
        File file = new File(System.getProperty("java.io.tmpdir")+"config_backup.xml");
        XMLFileHandler fileHandler = new XMLFileHandler();
        fileHandler.setFile(file);
        cm.store(fileHandler,"MyConfig");
      }
      catch (ConfigurationManagerException cme) {
        // we will use our backup file
        try {
          File file = new File(System.getProperty("java.io.tmpdir")+"config_backup.xml");
          XMLFileHandler fileHandler = new XMLFileHandler();
          fileHandler.setFile(file);
          cm.load(fileHandler.load(),"MyConfig");
        }
        catch (ConfigurationManagerException cme) {
          // ok, now we are doomed
        }
      }
    }
}