JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle GlassFish Server 3.1 Add-On Component Development Guide
search filter icon
search icon

Document Information

Preface

1.   Introduction to the Development Environment for GlassFish Server Add-On Components

2.  Writing HK2 Components

3.  Extending the Administration Console

4.  Extending the asadmin Utility

5.  Adding Monitoring Capabilities

6.  Adding Configuration Data for a Component

How GlassFish Server Stores Configuration Data

Defining an Element

To Define an Element

Defining an Attribute of an Element

Representing an Attribute of an Element

Specifying the Data Type of an Attribute

Identifying an Attribute of an Element

Specifying the Name of an Attribute

Specifying the Default Value of an Attribute

Specifying Whether an Attribute Is Required or Optional

Example of Defining an Attribute of an Element

Defining a Subelement

To Define a Subelement

Validating Configuration Data

Initializing a Component's Configuration Data

To Define a Component's Initial Configuration Data

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

Creating a Transaction to Update Configuration Data

To Create a Transaction to Update Configuration Data

Dotted Names and REST URLs of Configuration Attributes

Examples of Adding Configuration Data for a Component

7.  Adding Container Capabilities

8.  Creating a Session Persistence Module

9.  Packaging, Integrating, and Delivering an Add-On Component

A.  Integration Point Reference

Index

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:

To 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-container-config>
    <wombat-element foo="something" bar="anything"/>
</wombat-container-config>

To 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>