The Hydra Project
XML-based Software Property Management Tool for Java
previous section:
Using Hydra 2.0
Building a configuration file with the Hydra application

Using Hydra 2.0: Integrating Hydra in Java applications

In this section we will take look at how to integrate an existing Hydra configuration into your Java application. Normally, you would use the Hydra application to generate the configuration, store it as file or as String in a database and let the user make a choice between existing keys and values. Here we will use the settings from Hydra itself as an example.

Content

Requirements

Hydra 2.0 requires:

The required libraries are included in the release.

Loading the Hydra configuration

The Hydra settings can be found in the etc/ folder of the release. To load the ConfigRoot, use the following code:

try {
	FileInputStream in = new FileInputStream("etc/settings.hydra");
	//not configuring log4j by passing false
	ConfigRoot config = HydraFactory.createConfigRoot(false,in);
	//read the active value for the logging level
	String level = HydraAPI.getActiveValue(config,"/LOGGING/LEVEL"); 
	//read the active value for the display of the logging console
	String show = HydraAPI.getActiveValue(config,"/LOGGING/SHOW_CONSOLE");
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (XMLException e) {
	e.printStackTrace();
} catch (IllegalLabelException e) {
	e.printStackTrace();
} catch (IllegalSelectionException e) {
	e.printStackTrace();
} catch (IllegalPathException e) {
	e.printStackTrace();
} catch (NoActiveValueAvailableException e) {
	e.printStackTrace();
} catch (IllegalSettingsException e) {
	e.printStackTrace();
}

Creating a settings dialog

After reading the ConfigRoot from the settings file, the resulting ConfigRoot is visualized by the Hydra panel for applications:

The dialog provides the panel visualizing the Hydra configuration and three actions: Apply, Save and Close, of which Close only closes the dialog. The Hydra panel for applications can be obtained with the following code:

HydraPanel p = HydraGUIFactory.getHydraPanel(config); //config is the ConfigRoot

The actionPerformed method of the Apply button executes this code:

try {
	//read the active value for the logging level
	//and adjust the logging level
	String level = HydraAPI.getActiveValue(config,"/LOGGING/LEVEL"); //config is the ConfigRoot
	if (level.equals("DEBUG")) 
  		Logger.getLogger("org.ontoware.hydra").setLevel(Level.DEBUG);
   	else if (level.equals("INFO"))
   		Logger.getLogger("org.ontoware.hydra").setLevel(Level.INFO);
   	else if (level.equals("WARN"))
   		Logger.getLogger("org.ontoware.hydra").setLevel(Level.WARN);
   	else if (level.equals("ERROR"))
   		Logger.getLogger("org.ontoware.hydra").setLevel(Level.ERROR);
   	else if (level.equals("FATAL"))
   		Logger.getLogger("org.ontoware.hydra").setLevel(Level.FATAL);

	logger.info("set logging level: "+level);
	
	//read the active value for the display of the logging console
	//and add or remove the console from the graphical interface of the Hydra application
	String show = HydraAPI.getActiveValue(config,"/LOGGING/SHOW_CONSOLE"); //config is the ConfigRoot
	if (show.equals("YES")) {
		this.view.addConsole();
	}
	else if (show.equals("NO")) {
		this.view.removeConsole();
	}
	
	logger.info("show logging console: "+show);
			
} catch (IllegalPathException e) {
	logger.error(e.getMessage());
} catch (NoActiveValueAvailableException e) {
	logger.error(e.getMessage());
}

The actionPerformed method of the Save button executes this code:

try {
	FileOutputStream out = new FileOutputStream("etc/settings.hydra");
	config.write(out,true); //write the Hydra configuration to the settings file
	logger.info("saving settings");
} catch (FileNotFoundException e) {
	logger.error(e.getMessage());
} catch (IOException e) {
	logger.error(e.getMessage());
}

Summary

With just a little effort it is easy to integrate Hydra into your application. It simply involves a few steps.

Purpose Hydra methods
Build the configuration with the Hydra application --
Load the Hydra configuration HydraFactory.createConfigRoot(boolean log, InputStream in)
If desired, visualize the Hydra configuration, using the Hydra panel for applications HydraGUIFactory.getHydraPanel(ConfigRoot root)
Allow the user to manipulate the configuration --
Read out the ConfigKeys and ConfigValues you need Use the appropriate methods from the class HydraAPI
Save the configuration Use the write(OutputStream out, boolean close) method from the ConfigRoot object
previous section:
Using Hydra 2.0
Building a configuration file with the Hydra application
© 2003-2006 AIFB - OntoWare Team