Add your own commands to TomEE SSH console


We saw how to install TomEE SSH console and which commands were available…but do you know it is easy to add your own commands?

This article will help you creating the hello command ;).

First add openejb-common-cli dependency. If you use maven it looks like:

<dependency>
  <groupId>org.apache.openejb</groupId>
  <artifactId>openejb-common-cli</artifactId>
  <version>4.0.0-beta-3-SNAPSHOT</version>
  <scope>provided</scope>
</dependency>

Then create a class HelloCommand. It is not mandatory but it is common to extend org.apache.openejb.server.cli.command.AbstractCommand. With this command you’ll be able to use “streamManager” attribute to write information to the client. It has a lookup method utility too but you shouldn’t need often to lookup EJB too often in this part of the server.

Then to configure your command simply use the @org.apache.openejb.server.cli.command.Command annotation. You can configure through this annotation the command name, the description of the command and the usage text (use \n to go on a new line).

So here is our hello command:

package fr.rmannibucau.command;

import org.apache.commons.lang3.StringUtils;
import org.apache.openejb.server.cli.command.AbstractCommand;
import org.apache.openejb.server.cli.command.Command;

@Command(name = "hello", usage = "hello <name>", description = "Print hello <name>")
public class HelloCommand extends AbstractCommand {
    @Override
    public void execute(String args) {
        streamManager.writeOut("Hello " + StringUtils.capitalize(args));
    }
}

Finally to make your command available add a file META-INF/scan.xml containing either the command classes or the packages where commands are. Here is the syntax:

<?xml version="1.0" encoding="UTF-8"?>
<scan>
  <packages>
    <package>fr.rmannibucau.command</package>
    <package>fr.rmannibucau.commanduser</package>
    <package>fr.rmannibucau.commandadmin</package>
  </packages>
  <classes>
    <class>fr.rmannibucau.other.OtherCommand</class>
    <class>fr.rmannibucau.foo.BarCommand</class>
  </classes>
</scan>

To install it simply build your jar and paste it in tomee lib directory.
Then connect to your server as described in previous articles.
If you type “help” command you’ll get the list of available command and your command was added:

And you can use it:

Isn’t it simple? Now you can add all the utility commands you need in your TomEE!

Note: if your command is generic enough feel free to send it back to OpenEJB/TomEE community to be integrated.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s