CRaSH is a great piece of software that brings the power of the command line to any application running on the JVM. It allows you to browse internals of your application without a debuger or logs. See the official website for more information.
Guice is a google library that brings strong typing to the dependency injection world. It helps a lot when you build modular applications.
This project makes it easy to embed the CRaSH shell into any Guice application.
<dependency>
<groupId>com.linagora</groupId>
<artifactId>crsh.shell.embed.guice</artifactId>
<version>0.3</version>
</dependency>
public class SampleApplication extends AbstractModule {
@Override
protected void configure() {
install(new CrashGuiceSupport()); //import crash in my app
... bind some objects ...
}
}
You must include the right dependency in your Maven pom.xml, it will be autodetected by crash.
<dependency>
<groupId>org.crsh</groupId>
<artifactId>crsh.shell.ssh</artifactId>
<version>1.2.7</version>
</dependency>
You have to provide a CrashGuiceConfiguration
that sets the SSHPlugin.AUTH
property.
public class SampleApplication extends AbstractModule {
@Override
protected void configure() {
install(new CrashGuiceSupport()); //import crash in my app
//... bind some objects ...
}
@Provides
public CrashGuiceConfiguration crashConfiguration() {
return CrashGuiceConfiguration.builder()
.property(AuthenticationPlugin.AUTH.getName(), "simple")
.property(SimpleAuthenticationPlugin.SIMPLE_USERNAME.name, "admin")
.property(SimpleAuthenticationPlugin.SIMPLE_PASSWORD.name, "password")
.build();
}
}
Injection works in lot of cases :
-
you can inject a plugin you wrote into Crash without having to use the ServiceLoader
-
you can inject running plugins into your code to interact with
-
all your Guice managed singleton are available into CRaSH context to build crash command with
To declare a CRaSHPlugin
you just have to add it to guice Multibinder
. It will automatically be imported into CRaSH at plugin discovery time.
CRaSHPlugin
Multibinder<CRaSHPlugin<?>> pluginBinder = Multibinder.newSetBinder(binder(), new TypeLiteral<CRaSHPlugin<?>>(){});
pluginBinder.addBinding().to(ObmSyncAuthenticationPlugin.class);
You can retrieve a CRaSHPlugin into your Guice managed object for whatever reason. You only need to @Inject
it.
@Singleton
public class SampleServlet extends HttpServlet {
@Inject TelnetPlugin plugin;
// ...
}
Singletons are put into context.attributes.beans
and is usable by your crash commands.
@Usage("Perform action on Guice singletons") class guice extends CRaSHCommand { @Usage("display a Guice Singleton property") @Command Object print(@Usage("The full class name") @Required @Argument String type, @Usage("The property") @Option(names=["p", "property"]) String property) { def singleton = context.attributes.beans[type]; if (singleton != null) { if (property != null) { return singleton[property]; } else { return singleton; } } return "No such type : " + type; } }
All standards commands usually shipped with CRaSH are available in CRaSH Guice Module. Some guice specific commands are provided to make it possible to interact with guice managed objects.
display
It allows you to display any member of a guice singleton by calling toString on it.
invoke
It allows to call argument-less methods on guice singleton and display the result by calling toString on it.