Skip Navigation Links | |
Exit Print View | |
Oracle GlassFish Server 3.1 Add-On Component Development Guide |
1. Introduction to the Development Environment for GlassFish Server Add-On Components
3. Extending the Administration Console
4. Extending the asadmin Utility
About the Administrative Command Infrastructure of GlassFish Server
Representing an asadmin Subcommand as a Java Class
Specifying the Name of an asadmin Subcommand
Ensuring That an AdminCommand Implementation Is Stateless
Example of Adding an asadmin Subcommand
Adding Parameters to an asadmin Subcommand
Representing a Parameter of an asadmin Subcommand
Identifying a Parameter of an asadmin Subcommand
Specifying Whether a Parameter Is an Option or an Operand
Specifying the Name of an Option
Specifying the Long Form of an Option Name
Specifying the Short Form of an Option Name
Specifying the Acceptable Values of a Parameter
Specifying the Default Value of a Parameter
Specifying Whether a Parameter Is Required or Optional
Example of Adding Parameters to an asadmin Subcommand
Making asadmin Subcommands Cluster-Aware
Specifying asadmin Subcommand Execution
Subcommand Preprocessing and Postprocessing
Running a Command from Another Command
Adding Message Text Strings to an asadmin Subcommand
Enabling an asadmin Subcommand to Run
Setting the Context of an asadmin Subcommand
Changing the Brand in the GlassFish Server CLI
Implementing Create, Delete, and List Commands Using Annotations
Using Multiple Command Annotations
5. Adding Monitoring Capabilities
6. 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
Example 4-5 asadmin Subcommand With Empty execute Method
This example shows a class that represents the asadmin subcommand create-mycontainer.
The usage statement for this subcommand is as follows:
asadmin create-mycontainer --originator any-character-string [--description any-character-string] [--enabled {true|false}] any-character-string
This subcommand uses injection to specify that a running domain is required.
package com.example.mycontainer; import org.glassfish.api.admin.AdminCommand; import org.glassfish.api.admin.AdminCommandContext; import org.glassfish.api.I18n; import org.glassfish.api.Param; import org.jvnet.hk2.annotations.Service; import org.jvnet.hk2.annotations.Inject; import org.jvnet.hk2.annotations.Scoped; import org.jvnet.hk2.component.PerLookup; /** * Sample subcommand */ @Service(name="create-mycontainer") @Scoped(PerLookup.class) public Class CreateMycontainer implements AdminCommand { @Inject Domain domain; @Param String originator; @Param(name="description", optional=true) @I18n("mydesc") String mycontainerDescription @Param (acceptableValues="true,false", defaultValue="false", optional=true) String enabled @Param(primary=true) String containername; /** * Executes the subcommand with the subcommand parameters passed as Properties * where the keys are the paramter names and the values the parameter values * @param context information */ public void execute(AdminCommandContext context) { // domain and originator are not null // mycontainerDescription can be null. } }
The following message text strings are defined in the file LocalStrings.properties for use by the subcommand:
create-mycontainer.command=Creates a custom container create-mycontainer.command.originator=The originator of the container create-mycontainer.command.mydesc=A description of the container create-mycontainer.command.enabled=Whether the container is enabled or disabled create-mycontainer.command.containername=The container name
Example 4-6 asadmin Subcommand for Retrieving and Displaying Information
This example shows a class that represents the asadmin subcommand list-runtime-environment. The subcommand determines the operating system or runtime information for GlassFish Server.
The usage statement for this subcommand is as follows:
asadmin list-runtime-environment{runtime|os}
package com.example.env.cli; import org.glassfish.api.admin.AdminCommand; import org.glassfish.api.admin.AdminCommandContext; import org.glassfish.api.ActionReport; import org.glassfish.api.I18n; import org.glassfish.api.ActionReport.ExitCode; import org.glassfish.api.Param; import org.jvnet.hk2.annotations.Service; import org.jvnet.hk2.annotations.Inject; import org.jvnet.hk2.annotations.Scoped; import org.jvnet.hk2.component.PerLookup; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; /** * Demos asadmin CLI extension * */ @Service(name="list-runtime-environment") @Scoped(PerLookup.class) public class ListRuntimeEnvironmentCommand implements AdminCommand { // this value can be either runtime or os for our demo @Param(primary=true) String inParam; public void execute(AdminCommandContext context) { ActionReport report = context.getActionReport(); report.setActionExitCode(ExitCode.SUCCESS); // If the inParam is 'os' then this subcommand returns operating system // info and if the inParam is 'runtime' then it returns runtime info. // Both of the above are based on mxbeans. if ("os".equals(inParam)) { OperatingSystemMXBean osmb = ManagementFactory.getOperatingSystemMXBean(); report.setMessage("Your machine operating system name = " + osmb.getName()); } else if ("runtime".equals(inParam)) { RuntimeMXBean rtmb = ManagementFactory.getRuntimeMXBean(); report.setMessage("Your JVM name = " + rtmb.getVmName()); } else { report.setActionExitCode(ExitCode.FAILURE); report.setMessage("operand should be either 'os' or 'runtime'"); } } }
Example 4-7 asadmin Subcommand for Updating Configuration Data
This example shows a class that represents the asadmin subcommand configure-greeter-container. The subcommand performs a transaction to update configuration data for a container component. For more information about such transactions, see Creating a Transaction to Update Configuration Data.
The usage statement for this subcommand is as follows:
asadmin configure-greeter-container --instances instances [--language language] [--style style]
The acceptable values and default value of each option of the subcommand are shown in the following table. The table also indicates whether each option is optional or required.
|
Code for the container component is shown in Example of Adding Container Capabilities.
Code that defines the configuration data for the container component is shown in Examples of Adding Configuration Data for a Component.
package org.glassfish.examples.extension.greeter.config; import org.glassfish.api.admin.AdminCommand; import org.glassfish.api.admin.AdminCommandContext; import org.glassfish.api.Param; import org.jvnet.hk2.annotations.Service; import org.jvnet.hk2.annotations.Inject; import org.jvnet.hk2.config.Transactions; import org.jvnet.hk2.config.ConfigSupport; import org.jvnet.hk2.config.SingleConfigCode; import org.jvnet.hk2.config.TransactionFailure; import java.beans.PropertyVetoException; @Service(name = "configure-greeter-container") public class ConfigureGreeterContainerCommand implements AdminCommand { @Param(acceptableValues = "1,2,3,4,5,6,7,8,9,10", defaultValue = "5") String instances; @Param(acceptableValues = "english,norsk,francais", defaultValue = "norsk", optional = true) String language; @Param(acceptableValues = "formal,casual,expansive", defaultValue = "formal", optional = true) String style; @Inject GreeterContainerConfig config; public void execute(AdminCommandContext adminCommandContext) { try { ConfigSupport.apply(new SingleConfigCode<GreeterContainerConfig>() { public Object run(GreeterContainerConfig greeterContainerConfig) throws PropertyVetoException, TransactionFailure { greeterContainerConfig.setNumberOfInstances(instances); greeterContainerConfig.setLanguage(language); greeterContainerConfig.setStyle(style); return null; } }, config); } catch (TransactionFailure e) { } } }