Oracle GlassFish Server 3.0.1 Add-On Component Development Guide

Initializing a Component's Configuration Data

To ensure that a component's configuration data is added to the domain.xml file when the component is first instantiated, you must initialize the component's configuration data.

Initializing a component's configuration data involves the following tasks:

ProcedureTo Define a Component's Initial Configuration Data

  1. Create a plain-text file that contains an XML fragment to represent the configuration data.

    • Ensure that each XML element accurately represents the interface that is defined for the element.

    • Ensure that any subelements that you are initializing are correctly nested.

    • Set attributes of the elements to their required initial values.

  2. When you package the component, include the file that contains the XML fragment in the component's JAR file.

Example 6–7 XML Data Fragment

This example shows the XML data fragment for adding the wombat-container-config element to the domain.xml file. The wombat-container-config element contains the subelement wombat-element. The attributes of wombat-element are initialized as follows:

    <wombat-element foo="something" bar="anything"/>

ProcedureTo Write a Component's Initial Configuration Data to the domain.xml File

Add code to write the component's initial configuration data in the class that represents your add-on component. If your add-on component is a container, add this code to the sniffer class. For more information about adding a container, see Chapter 7, Adding Container Capabilities.

  1. Set an optional dependency on an instance of the class that represents the XML element that you are adding.

    1. Initialize the instance variable to null.

      If the element is not present in the domain.xml file when the add-on component is initialized, the instance variable remains null.

    2. Annotate the declaration of the instance variable with the org.jvnet.hk2.annotations.Inject annotation.

    3. Set the optional element of the @Inject annotation to true.

  2. Set a dependency on an instance of the following classes:

    • org.glassfish.api.admin.config.ConfigParser

      The ConfigParser class provides methods to parse an XML fragment and to write the fragment to the correct location in the domain.xml file.

    • org.jvnet.hk2.component.Habitat

  3. Invoke the parseContainerConfig method of the ConfigParser object only if the instance is null.

    If your add-on component is a container, invoke this method within the implementation of the setup method the sniffer class. When the container is first instantiated, GlassFish Server invokes the setup method.

    The test that the instance is null is required to ensure that the configuration data is added only if the data is not already present in the domain.xml file.

    In the invocation of the parseContainerConfig method, pass the following items as parameters:

    • The Habitat object on which you set a dependency

    • The URL to the file that contains the XML fragment that represents the configuration data

Example 6–8 Writing a Component's Initial Configuration Data to the domain.xml File

This example writes the XML fragment in the file init.xml to the domain.xml file. The fragment is written only if the domain.xml file does not contain the wombat-container-config-element.

The wombat-container-config element is represented by the WombatContainerConfig interface. An optional dependency is set on an instance of a class that implements WombatContainerConfig.

import org.glassfish.api.admin.config.ConfigParser;
import org.glassfish.examples.extension.config.WombatContainerConfig;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.component.Habitat;
import com.sun.enterprise.module.Module;

import java.util.logging.Logger;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
    WombatContainerConfig config=null;
    ConfigParser configParser;

    Habitat habitat;

    public Module[] setup(String containerHome, Logger logger) throws IOException {
        if (config==null) {
            URL url = this.getClass().getClassLoader().getResource("init.xml");
            if (url!=null) {
               configParser.parseContainerConfig(habitat, url, 
        return null;

Example 6–9 domain.xml File After Initialization

This example shows the domain.xml file after the setup method was invoked to add the wombat-container-config element under the config element.

    <config name="server-config">
      <wombat-container-config number-of-instances="5">
        <wombat-element foo="something" bar="anything" />