Extending GlassFish v3 Prelude, As Easy As 1-2-3

Now it is possible, with more ease than ever to write your own add-on and extend the functionality of GlassFish v3 Prelude application server.

From scratch, GlassFish v3 is designed to be modular, this combined with HK2 component services and OSGi module system made extensibility more natural for Prelude version.

For example, to extend the GlassFish server functionality by providing an add-on, one would need support for configuration, runtime, monitoring and administration. It is much better if it allows smoother integration like GlassFish modules themselves. Precisely, this is what the pluggable infrastructure provides.

In this series, I plan to explain various pluggability features provided by v3 with examples of working code where appropriate.

Some of the pluggability features available presently are:

  • Admin CLI (Command Line Interface)
  • Monitoring
  • Deployment
  • Container
  • Configuration
  • OEM Branding

In this blog, I'll discuss about Admin CLI pluggability. The asadmin utility is a command-line tool for configuring and administering the application server. By using CLI pluggability, the add-on could implement its own CLI commands similar to application server.

Declaration of CLI Command


@Service(name="mycommand")
@Scoped(PerLookup.class)
public class CLIPluggabilityCommand implements AdminCommand {
...
}

The CLI command will be implemented as a service using the annotation @Service and by implementing the AdminCommand interface. This enables run time discovery of the command. The command name is mycommand which is declared as part of @Service annotation. The scope is defined by the annotation @Scoped, PerLookup means whenever the command is looked up a new instance of the command is created and Singelton retains the same command for the entire session.

Parameters

The CLI command usually includes options and operands which are passed as parameters. For example, asadmin mycommand os has operand os which is paased to the command as @Param annotation.


    ...
    // this value can be either runtime or os for our demo
    @Param(primary=true)
    String inParam;
    ...

Execute method

    public void execute(AdminCommandContext context) {

        ActionReport report = context.getActionReport();
        report.setActionExitCode(ExitCode.SUCCESS);

        // If the inParam is 'os' then this command 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'");
        }

Implement the execute method based on inParam. The ActionReport which is obtained from AdminCommandContext is used to return the command status or error message to the client.

Complete Example

To work with the example, following are the steps:

  • Checkout and build GlassFish v3
  • Download pluggability-cli.zip and expand it under GlassFish v3 source directory
  • cd v3/pluggability/cli
  • mvn clean install
  • copy target/cli-pluggability-example-3.0-SNAPSHOT.jar to glassfish-install-location/modules
  • start glassfish
  • asadmin mycommand os should display the operating system name of the machine on which application server is run

Example log


punit[146]: ./asadmin list-commands | grep mycommand
delete-jvm-options                      mycommand

punit[150]: ./asadmin help mycommand

Administration Commands                        mycommand (1)

NAME
     mycommand - command to get application server os/runtime info.
...

punit[136]: ./asadmin mycommand abc
remote failure: operand should be either 'os' or 'runtime'

Command mycommand failed.

punit[137]: ./asadmin mycommand os
Your machine operating system name = SunOS

Command mycommand executed successfully.

punit[138]: ./asadmin mycommand runtime
Your JVM name = Java HotSpot(TM) Server VM

...

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

msreddy

Search

Categories
Archives
« July 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today