Oracle GlassFish Server 3.0.1 Add-On Component Development Guide

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.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.net.URL;
...
    @Inject(optional=true)
    WombatContainerConfig config=null;
...
@Inject
    ConfigParser configParser;

    @Inject
    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, 
                   WombatContainerConfig.class);
            }
        }
        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.

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