GlassFish 中的 命令框架 (CLI) 实现

GlassFish中的命令框架(CLI)

[Command Line Interface框架的工作原理]

GlassFish开源服务器中间,提供了CMD命令行机能,服务器的使用者可以通过CMD来发布更新自己的jar包,配置服务器的JDBC连接等等。

那么,到底GlassFish是怎么做到这一步的呢?

我们看到GlassFish实际上是引用了一个开源的命令行框架--- Command Line Interface框架

我们具体可以参照以下URL:

https://glassfish.dev.java.net/javaee5/admin-cli/admin-cli.html

CLI框架的工作流程如下:

1.      在这个框架内部,使用者发送的命令以及参数,会通过CMD窗口传给常驻内存的CLI框架的核心类CLIMain

2.       CLIMain通过查找一个名叫[CLIDescriptor.xml]的配置文件,在里面查找具体的执行类,一般来说GlassFish中的执行类都被叫做XXXXCommand之类的名字

3.       XXXXCommand之类的执行类,开始访问MBeanServer调用相对应的MBean,访问资源(一般都是config.xml),配置服务器

MBean相关知识请看我的另外一篇文章

配置工作中会涉及到对config.xml的容错性检查,这是通过rng技术来实现,这里就不讲了

 

[实践 自己动手建立一个Command]

   长话短说,我们来自己建立一个Command

1.      找到以下目录,在其中增加自己的Command执行实体类

[glassfish/admin-cli/commands/src/java/com/sun/enterprise/cli/commands]

public class NewListComponentsCommand extends S1ASCommand {

       /**

        * An abstract method that validates the options on the specification in the

        * xml properties file This method verifies for the correctness of number of

        * operands and if all the required options are supplied by the client.

        *

        * @return boolean returns true if success else returns false

        */

       public boolean validateOptions() throws CommandValidationException {

              return super.validateOptions();

       }

       /**

        * An abstract method that Executes the command

        *

        * @throws CommandException

        */

       public void runCommand() throws CommandException,

                     CommandValidationException {

              // if validateOptions is false, then it must be that --help option

              // is provided and there is no need to execute the command since

              // either manpage or usage text is displayed

              if (!validateOptions())

                     return;

              String objectName = getObjectName();

              Object[] params = getParamsInfo();

              String operationName = getOperationName();

              String[] types = getTypesInfo();

              CLILogger.getInstance().printMessage("It is a test From Jiang Biao");

       }

 

}

上面的Command唯一干的事情就是在CMD窗口上打出"It is a test From Jiang Biao"这句话

2.      我们找到[CLIDescriptor.xml]文件,在其中加入我们自己的Command配置

<Command name="new-list-components" classname="com.sun.enterprise.cli.commands.NewListComponentsCommand" numberofoperands="1" usage-text="list-components [--terse=false] [--echo=false] [--interactive=true] [--user admin_user] [--passwordfile file_name] [--help] [--type application|ejb|web|connector|webservice] target">

                                          <ValidOption name="user"/>

                                          <ValidOption name="password"/>

                                          <ValidOption name="passwordfile"/>

                                          <ValidOption name="host"/>

                                          <ValidOption name="port"/>

                                          <ValidOption name="secure"/>

                                          <ValidOption name="interactive"/>

                                          <ValidOption name="terse"/>

                                          <ValidOption name="echo"/>

                                          <ValidOption name="type"/>

                                          <properties>

                                                        <property name="objectname">

                                                                      <value>com.sun.appserv:type=applications,category=config</value>

                                                        </property>

                                                        <property name="operation">

                                                                      <value>getAllUserDeployedComponents</value>

                                                        </property>

                                                        <property name="params">

                                                                <value>{#1}</value>                      

                                                        </property>

                                                        <property name="paramtypes">                  

                                                                <value>java.lang.String</value>

                                                        </property>

                                                        <property name="displaytype">

                                                                      <value>name</value>

                                                        </property>

                                                        <property name="manpage">

                                                                      <value>com.sun.enterprise.tools.cli.help</value>

                                                        </property>

                                                        <property name="command-type">

                                                                      <value>list</value>

                                                        </property>

                                          </properties>

                            </Command>

以上配置文件定义了我们自己的Command类,并且规定了控制台上访问该类的命令是[new-list-components]

3.      将我们的修改打成jar包,替换到glassfish的运行环境,我们就能在控制台上运行自己的command命令咯~~~~~

 

 

你可能感兴趣的:(GlassFish 中的 命令框架 (CLI) 实现)