Adding an OSGi command (Equinox)

Adding OSGi commands using spring-osgi:

<osgi:reference id="utilities" interface="net.sourceforge.pebble.util.Utilities" />
<osgi:service interface="org.eclipse.osgi.framework.console.CommandProvider">
  <bean class="net.sourceforge.pebble.commands.PebbleCommandProvider" autowire="constructor" />
</osgi:service>

The OSGi console command is implemented by the PebbleCommandProvider. Configuration is reduced to a bare minimum using Spring DI (constructor autowire).

public class PebbleCommandProvider implements CommandProvider {
  private final Utilities utilities;
  private final BlogManager blogManager;

  public Object _reindex(CommandInterpreter commandInterpreter) {
    String blogName = commandInterpreter.nextArgument();
    if (blogName == null) {
      blogName = "default";
    }
    utilities.buildIndexes(blogManager.getBlog(blogName));
    return null;
  }

  public String getHelp() {
    StringBuffer buffer = new StringBuffer();
    buffer.append("---pebble commands---\n\t");
    buffer.append("reindex [blogName] - reindex a blog\n");
    return buffer.toString();
  }
}

A sample OSGi session:

osgi> help
---pebble commands---
reindex [blogName] - reindex a blog
---Eclipse Runtime commands---
...

This post is part of a series about an OSGi experiment: Migrate a monolith to a modular OSGi application architecture.

The Beginning of an OSGi Experiment

Building OSGi ready pebble with Maven

Third time it's a charm

Extracting the first OSGi bundle

Extract the DAO layer

Adding the listener aspect

Extracting the E-Mail Service

▶︎ Adding an OSGi command (Equinox)

The tale of a sitemesh experiment

▷ ...


Header cover Photo by Matthew Henry from Burst