How to add a new cli command (asadmin utlilty) for performing administrative task in glassfish v3 server

Glassfish v3 application server is designed to be modular and extensible so that you can add/remove functionality as you require. In this blog i am going to talk about how easy it is to add a new admin cli command (asadmin utility) which can be used to perform administrative task for glassfish application server from command line and also give the dependencies required for the command implementation.

This blog has been written with reference to other blogs Sreeni Blog I am giving two examples in the blog. The first example is about adding some more functionality to the existing command list-applications. Currently list-applications lists all the applications with the type information. Now in my new command implementation, i adding some code to display the context root along with application status.

First Example


The new command is added in the module deployment-admin.

Declaration
The new command is implemented as a Service using annotation @Service named list-applications-detail and by implementing the AdminCommand interface. Scope of the command is defined as @Scoped, PerLookup, so that a new instance is created when the command is executed.

@Service(name = "list-applications-detail")
@Scoped(PerLookup.class)
Execute Method

ListComponentsCommand (available in deployment-admin module) is extended and the execute method of
AdminCommand interface is overriden for adding the new functionality.

public class ListApplicationsDetail extends ListComponentsCommand { 
@Inject
ApplicationRegistry appRegistry;
final private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(ListApplicationsDetail.class)
public void execute(AdminCommandContext context) {
final ActionReport report = context.getActionReport();
String status = "Disabled";
ActionReport.MessagePart part = report.getTopMessagePart();
int numOfApplications = 0;
for (Named module : applications.getModules()) {
if (module instanceof Application) {
final Application app = (Application) module;
if (app.getEnabled().equalsIgnoreCase("true"))
status = "Enabled";
if (app.getObjectType().equals("user")) {
if (type == null || isApplicationOfThisType(app, type)) {
ActionReport.MessagePart childPart = part.addChild();
childPart.setMessage(app.getName() + " " +
getAppSnifferEngines(app, true) + " " + getContextRoot(app)
+ " " + status);
numOfApplications++;
}
}
}
}
if (numOfApplications == 0) {
part.setMessage(localStrings.getLocalString("list.components.no.elements.to.list", "Nothing to List."));
}
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}

   private String getWebModuleName(List appModules) {
for (Module appMod : appModules) {
List engines = appMod.getEngines();
for (Engine engine : engines) {
if (engine.getSniffer().equals("web"))
return appMod.getName();
}
}
return null;
}

   private String getContextRoot(Application app) {
String moduleName = getWebModuleName(app.getModule());
ApplicationInfo appInfo = appRegistry.get(app.getName());
if (appInfo != null) {
com.sun.enterprise.deployment.Application appDeployObj = appInfo.getMetaData(com.sun.enterprise.deployment.Application.class);
if (appDeployObj != null) {
BundleDescriptor bundleDesc = appDeployObj.getModuleByUri(moduleName);
if (bundleDesc != null &&
bundleDesc instanceof WebBundleDescriptor) {
return ((WebBundleDescriptor) bundleDesc).getContextRoot();
}
}
}
return null;
}
}

Dependencies required

 <dependency>
<groupid>com.sun.enterprise</groupid>
<artifactid>hk2</artifactid>
</dependency>
<dependency>
<groupid>org.glassfish.common</groupid>
<artifactid>glassfish-api</artifactid>
<version>${project.version}</version>
</dependency>
<dependency>
<groupid>org.glassfish.deployment</groupid>
<artifactid>dol</artifactid>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>org.glassfish.deployment</groupid>
<artifactid>deployment-admin</artifactid>
<version>${project.version}</version>
</dependency>
The dependency required are hk2 for implementing the cli command as a service so that it can can be automatically and dynamically discovered by the runtime. The other dependency required is glassfish-api which has Public APIs of Glassfish V3 and deployment-dol which contains Deployment Object Library and deployment-admin which contains Deployment Related Admin Classes

Command Execution and Output

After you compile the deployment-admin module copy the deployment-admin.jar file to <glassfish-install>/modules directory and start your application server (asadmin start-domain).

asadmin list-commands|grep list-applications-detail
create-http-listener list-applications-detail
asadmin list-applications-detail
converter <ear, ejb,="" web,="" appclient=""> /converter Enabled
logging-helloworld <ear, ejb,="" web=""> logging-helloworld Enabled
jdbc-simple <ear, ejb,="" web=""> jdbc-simple Enabled
RosterApp <ear, ejb,="" web=""> /RosterClient Enabled
Command list-applications-detail executed successfully.
From the output you can observe that context root and application status has been displayed along with application deployed and type.

Second Example

This example is very generic to return hello world when you execute the command.

Declaration

The new command is implemented as a Service using annotation @Service named sampleclicommand and by implementing the AdminCommand interface. Scope of the command is defined as @Scoped, PerLookup, so that a new instance is created when the command is executed.

@Service(name="sampleclicommand")
@Scoped(PerLookup.class)
Execute Method

Implement the execute method of AdminCommand interface for providing the functionality of the command.ActionReport class obtained from AdminContext of glassfish-api is used to return the output and exit status to the client.

 public class SampleCliCommand implements AdminCommand {
public void execute(AdminCommandContext context) {
ActionReport report = context.getActionReport();
report.setMessage("Hello World!!!");
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
}
Dependencies required
<dependency>
<groupid>com.sun.enterprise</groupid>
<artifactid>hk2</artifactid>
</dependency>
<dependency>
<groupid>org.glassfish.common</groupid>
<artifactid>glassfish-api</artifactid>
<version>${project.version}</version>
</dependency>

The dependency required are hk2 for implementing the cli command as a service so that it can can be automatically and dynamically discovered by the runtime.The other dependency required is glassfish-api which has Public APIs of Glassfish V3.

Command Execution and Output

After you compile the module copy to <glassfish-install>/modules directory and start your application server (asadmin start-domain).

Execute following steps (with appropriate changes) to see if your command works 
asadmin list-commands |grep sampleclicommand
delete-network-listener sampleclicommand
asadmin sampleclicommand
Hello World!!!
Command sampleclicommand executed successfully.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Nachiappan Veerappan Nachiappan

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