SeamFramework.orgCommunity Documentation

Chapter 4. Developing a Plugin

4.1. Referencing the Forge APIs
4.1.1. Using Forge
4.1.2. With Maven)
4.2. Implementing the Plugin interface
4.3. Naming your plugin
4.4. Ensure all required classes are on the CLASSPATH
4.5. Make your Plugin available to Forge
4.6. Add commands to your plugin
4.6.1. Default commands
4.6.2. Named commands
4.7. Understanding command @Options
4.7.1. --named options
4.7.2. Ordered options
4.7.3. Combining --named and ordered options
4.7.4. Option attributes and configuration
4.8. Piping output between plugins

Part of Forge's architecture is to allow extensions to be created with extreme ease. This is done using the same programming model that you would use for any CDI or Java EE application, and you should quickly recognize the annotation-driven patterns and practices applied.

A Forge plugin could be as simple as a tool to print files to the console, or as complex as deploying an application to a server, 'tweet'ing the status of your latest source-code commit, or even sending commands to a home-automation system; the sky is the limit!

Because Forge is based on Maven, the easiest way to get started quickly writing a plugin is to create a new maven Java project. This can be done by hand, or using Forge's build in plugin project facet.

Your class must implement the org.jboss.seam.forge.shell.plugins.Plugin interface.

import org.jboss.seam.forge.shell.plugins.Plugin;
         
public class ExamplePlugin implements Plugin {
}

Each plugin should be given a name. This is done by adding the @org.jboss.seam.forge.shell.plugins.Alias annotation to your plugin class. By default, if no @Alias annotation is found, the lower-case Class name will be used; for instance, our ExamplePlugin, above, would be executed by typing:
$ exampleplugin 
Now we will add a name to our plugin.
@Alias("example")
public class ExamplePlugin implements Plugin {
   // commands
}
Our named @Alias("example") ExamplePlugin would be executed by typing:
$ example 

All imports must be available on the CLASSPATH. If your Plugin depends on classes that are not provided by Forge, then you must either package those classes in the JAR file containing your Plugin (for instance, using the maven shade plugin), or you must ensure that the required dependencies are also placed on the CLASSPATH (typically in the $FORGE_HOME/lib folder,) otherwise your plugin will *not* be loaded.

After following all of the steps in this section, you should now be ready to install your Plugin into the Forge environment. This is accomplished simply by packaging your Plugin in a JAR file with a CDI activator, otherwise referred to as a /META-INF/beans.xml file.

Tip

You must include a /META-INF/beans.xml file in your JAR, or none of the classes in your archive will be discovered; therefore, your Plugin will not be made available to Forge.

Now that you have implemented the Plugin interface, it's time to add some functionality. This is done by adding "Commands" to your plugin class. Commands are plain Java methods in your plugin Class. Plugin methods must be annotated as either a @DefaultCommand, the method to be invoked if the plugin is called by name (with no additional commands), or @Command(name="..."), in which case a the plugin name and command name must both be used to invoke the method.

Commands also accept @Options parameters as arguments. These are described in detail later in this section.

Once we have a command or two in our Plugin, it's time to give our users some control over what it does; to do this, we use @Option params; options enable users to pass information of various types into our commands.

Options can be named, in which case they are set by passing the --name followed immediately by the value, or if the option is a boolean flag, simply passing the flag will signal a `true` value. Named parameters may be passed into a command in any order, while unnamed parameters must be passed into the command in the order with which they were defined.

In addition to --named option parameters, as described above, parameters may also be passed on the command line by the order in which they are entered. These are called "ordered option parameters", and do not require any parameters other than help or description information.

@Option String value

The order of the options in the method signature controls how values are assigned from parsed Forge shell command statements.

For example, the following command accepts several options, named 'one', and 'two':

public class ExamplePlugin implements Plugin {
   @Command(name="perform")
   public void exampleCommand( 
                  @Option String one,
                  @Option String two,
                  PipeOut out) {
       out.println(">> first option equals: " + one);
       out.println(">> second option equals: " + two);
   }
}

The above command, when executed, would produce the following output:

$ example-plugin cat dog 
>> option one equals: cat
>> option two equals: dog

Both --named and ordered option parameters can be mixed in the same command; there are some constraints on how commands must be typed, but there is a great deal of flexibility as well.

@Option String value,
@Option(name="num") int number

The order of ordered options in the method signature controls how values are assigned from the command line shell, whereas the named options have no bearing on the order in which inputs are provided on the command line.

For example, the following command accepts several options, named 'one', 'two', and several more options that are not named:

public class ExamplePlugin implements Plugin {
   @Command(name="perform")
   public void exampleCommand( 
                  @Option(name="one") String one,
                  @Option(name="two") String two,
                  @Option String three,
                  @Option String four,
                  PipeOut out) {
       out.println(">> first option equals: " + one);
       out.println(">> second option equals: " + two);
       out.println(">> third option equals: " + three);
       out.println(">> fourth option equals: " + four);
   }
}

The above command, when executed, would produce the following output:

$ example-plugin --one cat --two dog bird lizard  
>> option one equals: cat
>> option two equals: dog 
>> option one equals: bird
>> option two equals: lizard

However, we could also achieve the same result by re-arranging parameters, and as long as the name-value pairs remain together, and the ordered values are passed in the correct order, interpretation will remain the same:

$ example-plugin --two dog bird --one cat lizard  
>> option one equals: cat
>> option two equals: dog 
>> option one equals: bird
>> option two equals: lizard

This table describes all of the available @Option(...) attributes and their usage.

AttributeTypeDescriptionDefault
nameString If specified, defines the --name with which this option may be passed on the command line. If left blank, this option will be ordered (not named,) and will be passed in the order with which it was written in the Method signature.
@Option(name="exampleName")
none
requiredboolean Options may be declared as required when they must be supplied in order for proper command execution. The shell will enforce this requirement by prompting the user for valid input if the option is omitted when the command is executed.
@Option(required="false")
false
shortNameString If specified, defines the short name by which this option may be called on the command line. Short names must be one character in length. short name with which this option may be passed on the command line. none
    

Much like a standard UNIX-style shell, the Forge shell supports piping IO between executables; however in the case of forge, piping actually occurs between plugins, commands, for example:

$ cat /home/username/.forge/config | grep automatic 
@/* Automatically generated config file */;

This might look like a typical BASH command, but if you run forge and try it, you may be surprised to find that the results are the same as on your system command prompt, and in this example, we are demonstrating the pipe: '|'

In order to enable piping in your plugins, you must use one or both of the @PipeIn InputStream stream or PipeOut out command arguments. Notice that PipeOut is a java type that must be used as a Method parameter, whereas @PipeIn is an annotation that must be placed on a Java InputStream Method parameter.

`PipeOut out` - by default - is used to print output to the shell console; however, if the plugin on the left-hand-side is piped to a secondary plugin on the command line, the output will be written to the `@PipeIn InputStream stream` of the plugin on the right-hand-side:

$ left | right

Or in terms of pipes, this could be thought of as a flow of data from left to right:

$ PipeOut out -> @PipeIn InputStream stream

Notice that you can pipe output between any number of plugins as long as each uses both a @PipeIn InputStream and PipeOut:

$ first command | second command | third command

Take the 'grep' command itself, for example, which supports two methods of invocation: Invocation on one or more Resource<?> objects, or invocation on a piped InputStream.

@Alias("grep")
@Topic("File & Resources")
@Help("print lines matching a pattern")
public class GrepPlugin implements Plugin
{
   @DefaultCommand
   public void run(
         @PipeIn final InputStream pipeIn,
         @Option(name = "ignore-case", shortName = "i", flagOnly = true) boolean ignoreCase,
         @Option(name = "regexp", shortName = "e") String regExp,
         @Option(description = "PATTERN") String pattern,
         @Option(description = "FILE ...") Resource<?>[] resources, 
         final PipeOut pipeOut
   ) throws IOException
   {
      Pattern matchPattern = /* determine pattern (omitted for space) */;

      if (resources != null) {
      
         /* User passed file(s) on the command line; grep those. */
      
         for (Resource<?> r : resources) {
            InputStream inputStream = r.getResourceInputStream();
            try {
               match(inputStream, matchPattern, pipeOut, ignoreCase);
            }
            finally {
               inputStream.close();
            }
         }
      }
      else if (pipeIn != null) {
      
         /* No files were passed on the command line; check for a 
          * piped InputStream and use that.
          */
      
         match(pipeIn, matchPattern, pipeOut, ignoreCase);
      }
      else {
      
         /* No input was passed to the plugin. */
      
         throw new RuntimeException("Error: arguments required");
      }
   }

   private void match(InputStream instream, Pattern pattern, PipeOut pipeOut, boolean caseInsensitive) throws IOException {
      StringAppender buf = new StringAppender();

      int c;
      while ((c = instream.read()) != -1) { /* Read from the given stream. */
         switch (c) {
         case '\r':
         case '\n':
            String s = caseInsensitive ? buf.toString().toLowerCase() : buf.toString();

            if (pattern.matcher(s).matches()) {
               pipeOut.println(s); /* Write to the output pipe. */
            }
            buf.reset();
            break;
         default:
            buf.append((char) c);
            break;
         }
      }
   }
}