Implementaion Design - GShell

Overview

The Grimoires Shell (GShell) is a simple command line client for the Grimoires Web Service registry. The GShell provides a shell environment to interact with Grimoires service, within which a group of commands can be invoked for users to publish/inquire business/service/wsdl/metadata.

In terms of implementation, GShell consists of a Shell class that implements the shell environment, and a group of Command classes, each of which implements a registry task, for instance, publishing a service. Below we explain the Shell class and Command classes respectively.

Shell explained

The Shell class has implemented the following functionalities:

  • The Shell class is responsible for sending and receiving SOAP messages to/from the Grimoires service through the Grimoires client API.
  • The Shell class has provided some built-in utilities:
    • help: display a help message.
    • ls: list all available commands. As a convention, all classes in the package whose names end with "Command" are commands. The Shell class will search for all commands and list them automatically.
    • run command_name: invoke a command. The Shell class uses Java Reflection API to create an instance of the specified Command class, and invokes its "run" method.
    • exit: exit GShell.
    • In addition, the Shell class also supports "environment variables". When you see [$n]=VALUE (where n is an integer) in the output, it means the a variable, with name n, is recorded in GShell and you may reuse them by typing "$n" to denote VALUE.

The Shell class need not be touched to implement a new command.

Command explained

All commands extend from the Shell class in order to inherit the shell environment, for instance, the capability to communicate with the Grimoires service.

All commands implements the Command interface, which is quite simple:

public interface Command {
    public void run() throws Exception;
}

All commands should provide a nullary constructor, which will be used by the Shell class to create an instance. Then the shell class will invoke the run() method of the created instance to execute the command.

In order to be runnable outside the shell, a command should also provide a public main() method. A sample command looks like:

public class PublishBusinessCommand extends Shell implements Command {
    
    private String name;
    private String description;
    private BusinessDetail response;

    public PublishBusinessCommand(String name, String desc) {
	this.name = name;
        this.description = desc;
    }
    
    public PublishBusinessCommand() {
        
    }

    private void expect() throws Exception {
        out.println("==== Publish a business ====");
	out.println("Input the business name:");
        name = readAndParseLine();
        out.println("Input the business description:");
        description = readAndParseLine();
     }

    private void execute() throws Exception {
        Save_business request = new Save_business();
        request.setAuthInfo("AUTHINFO");
        BusinessEntity[] businessEntities = new BusinessEntity[1];
        businessEntities[0] = new BusinessEntity();
        businessEntities[0].setName(new Name[] {
	    new Name(name)
        });         
        businessEntities[0].setDescription(new Description[] {
            new Description(description)
        });
        
        request.setBusinessEntity(businessEntities);
        response = publishImpl.save_business(request);
    }

    private void present() throws Exception {
        String businessKey = response.getBusinessEntity(0).getBusinessKey();
        printEnv("Business key", businessKey);
    }
    
    public void run() throws Exception {
    	expect();
        execute();
        present();
    }
    
    public static void main(String[] args) throws Exception {
        if (args.length != 3) {
            help();
            System.exit(1);
        }
        
        setUpStubs(args[0]);
        PublishBusinessCommand cmd = new PublishBusinessCommand(args[1], args[2]);
        cmd.execute();
        cmd.present();
    }

    private static void help() {
        System.out.println("java PubishBusinessCommand Grimoires_URL business_name business_description");
    }
}