Hello World - GlassFish-V3 Module

GlassFish v3 has a modular and extensible architecture. Hence, it provides a lot of opportunities to add more and more functionality to it. So, why not explore its modular architecture with a simple hello world module !!!
To start with we need to be familiar with the HK2 component services and OSGi module management subsystem.
In our example we will extend the asadmin command to add our own hello command to the admin command utility.

Method Decleration:

AdminCommand is exposed by glassfish through annotation @contract, this has to be implemented in order to make use of the asadmin command. Hello command will be implemented as a service using the annotation @Service, which helps in the runtime discovery of the command.The command name is specified with the @Service annotation.

@Service(name="hello")
public class HelloCommand implements AdminCommand{
    ......
}

Command Execution:
To enable an asadmin command to run, we need to implement the execute method of the AdminCommand interface. The execute method is declared as:

public void execute(AdminCommandContext context){
   ....
}
The ActionReport which is obtained from AdminCommandContext is used to return the command status or error message to the client, hence the complete hello world execute method will be:

public void execute(AdminCommandContext context){
        ActionReport report = context.getActionReport();
        report.setMessage("Hello World !!!");
        report.setActionExitCode(ExitCode.SUCCESS);
}

Writing pom.xml for the project:

Our project structure is:
    hello.command
        |---> pom.xml
        |---> hello-command
              |--->pom.xml
              |--->src/main/java/testcomponent/HelloCommand.java

The pom.xml under hello.command is the top level pom.xml with the packaging type pom. Which will be more useful is case there are multiple modules.

pom.xml under hello-command includes:


Identification of the module.
    ....   
    <artifactId>hello-command</artifactId>
      <packaging>hk2-jar</packaging>
      <version>${pom.parent.version}</version>
      <name>Hello Command Sample</name>
    ....

Required Plugins, maven-bundle-plugin and hk2-maven-plugin, are specified as:
    ....
    <build>
        <plugins>
        <plugin>
                   <groupId>org.apache.felix</groupId>
                   <artifactId>maven-bundle-plugin</artifactId>
                    ....
        </plugin>
        <plugin>
                   <groupId>com.sun.enterprise</groupId>
                   <artifactId>hk2-maven-plugin</artifactId>
                    ....
        </plugin>
        </plugins>
    </build>
    ....

The Dependencies are mentioned as follows:
    ....
    <dependencies>
          <dependency>
            <groupId>com.sun.enterprise</groupId>
            <artifactId>hk2</artifactId>
            <version>${hk2.version}</version>
          </dependency>
          <dependency>
            <groupId>org.glassfish.common</groupId>
            <artifactId>common-util</artifactId>
            <version>3.0-SNAPSHOT</version>
          </dependency>
      </dependencies>
    ....

Playing with the example:

1. The complete example can be downloaded here.
2. Unzip the module in your favourite directory.
3. cd hello.command
4. mvn clean install
5. copy hello-command/target/hello-command-1.0-SNAPSHOT.jar to Glassfish-v3-installation-dir/modules
6. Start the server.
7. Execute the command : asadmin hello
8. Output: Hello World !!!

I hope it will be useful for the beginners like me.
Cheers !!!

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Mohit Gupta

Search

Categories
Archives
« April 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
   
       
Today