I proposed to extend DCmd to allow Java developers to customize their own 
diagnostic commands last week.

At present, I have implemented a preliminary version.

In the current implementation, I provided some simple APIs in the Java layer 
(under sun.management.cmd) for defining and registering commands.

- Executable interface
  User-defined commands need to implement this interface, the interface only 
contains a simle method:

    /**
     * @param output the output when this executable is running
     */
    void execute(PrintWriter output);


- Add two annotations (@Command and @Parameter) to describe the command meta 
info

- Use Factory API to register command, the following forms are supported

@Command(name = "My.Echo", description = "Echo description")
class Echo implements Executable {

    @Parameter(name = "text", ordinal=0, isMandatory = true)
    String text;

    @Parameter(name = "repeat", isMandatory = true, defaultValue = "1")
    int repeat;

    @Override
    public void execute(PrintWriter out) {
        for (int i = 0 ; i < repeat; i++) {
            out.println(text);
        }
    }
}

Factory.register(Echo.class);



Factory.register("My.Date", output -> {
    output.println(new Date());
});


- When the command is running, the VM will call `Executor.executeCommand` to 
execute the command. In the implementation of Executor, I introduced a simple 
timeout mechanism to prevent the command channel from being blocked.

At the VM layer, I extended the existing DCmd framework(implemented JavaDCmd 
and JavaDCmdFactoryImpl) to be compatible with existing functions (jmx, help, 
etc.).

In terms of security, considering that the SecurityManager will be deprecated, 
there are not too many considerations for the time being.

Any input is appreciated.

Thanks,
Denghui

-------------

Commit messages:
 - 8275259: Add support for Java level DCmd

Changes: https://git.openjdk.java.net/jdk/pull/5938/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=5938&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8275259
  Stats: 1159 lines in 12 files changed: 1158 ins; 0 del; 1 mod
  Patch: https://git.openjdk.java.net/jdk/pull/5938.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/5938/head:pull/5938

PR: https://git.openjdk.java.net/jdk/pull/5938

Reply via email to