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:
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.
When you package the component, include the file that contains the XML fragment in the component's JAR file.
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:
The foo attribute is set to something.
The bar attribute is set to anything.
<wombat-container-config> <wombat-element foo="something" bar="anything"/> </wombat-container-config>
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.
Set an optional dependency on an instance of the class that represents the XML element that you are adding.
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
.
Annotate the declaration of the instance variable with the org.jvnet.hk2.annotations.Inject annotation.
Set the optional element of the @Inject annotation to true.
Set a dependency on an instance of the following classes:
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
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; } ...
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>