Getting started with the Clerezza Shell

One way to interact with Clerezza is via the shell. The shell allows administrative tasks like installing bundles but it is also support the Scala langauge providing a way to interactively invoke services as well as to run scripts.

Commands and Expressions

The Clerezza Shell is based on the interactive Scala Shell and as such any line that does not start with a colon is evaluated as Scala:
zz>def greet { | out println "hello world" | } greet: Unit zz>greet hello world zz>
Commands by contrast are not interpreted as scala. Build in commands as well as commands provided by services are available.
zz>:help This is a scala based console, it supports any Scala expression, as well as the command described below. ...
Services exposing the org.apache.clerezza.shell.ShellCommand interface provide additional commands, for example org.apache.felix.shell.ShellService allows to have lines evaluated by the felix shell. Like all commands the command :felix can be abbreviated as long as there's no ambuigity:
zz>:f scr list Id State Name [ 0] [active ] org.apache.clerezza.platform.security.BundlePermissionManager [ 1] [active ] org.apache.clerezza.platform.security.SecurityActivator ...

Interacting with the OSGi environment

An instance of org.apache.clerezza.shell.OsgiDsl is initialized and part of the default imports. This object allows access to the bundleContext. The following assigns the bundle 0 to a variable and prints its symbolic name to the shell output
zz>val systemBundle = bundleContext.getBundle(0) systemBundle: org.osgi.framework.Bundle = org.apache.felix.framework [0] zz>out println systemBundle.getSymbolicName org.apache.felix.framework
The OsgiDsl will be enhanced to support shortcuts for many frequent tasks, currently only a few are available.
zz>ps 0 - org.apache.felix.framework System Bundle 1 - org.apache.clerezza.platform.security.conditions mvn:org.apache.clerezza/org.apache.clerezza.platform.security.conditions/0.5-incubating-SNAPSHOT 2 - org.apache.clerezza.platform.security mvn:org.apache.clerezza/org.apache.clerezza.platform.security/0.8-incubating-SNAPSHOT 3 - org.apache.felix.bundlerepository mvn:org.apache.felix/org.apache.felix.bundlerepository/1.6.4 4 - org.apache.felix.configadmin mvn:org.apache.felix/org.apache.felix.configadmin/1.2.8 zz>start("mvn:org.apache.clerezza/org.apache.clerezza.rdf.stable.serializer") res13: org.osgi.framework.Bundle = org.apache.clerezza.rdf.stable.serializer [128] zz>res13.uninstall zz>

Accessing Services

The shell provides a shortcut to access available services, the method $ with the service type as argument. The following ste the inital bundle startlevel to 10:
zz>import org.osgi.service.startlevel.StartLevel import org.osgi.service.startlevel.StartLevel zz>$[StartLevel] setInitialBundleStartLevel 10 zz>
The following adds a triple to the content graph retrieved via ContentGraphProvider:
zz>import rdf.core._ import rdf.core._ zz>import rdf.core.impl._ import rdf.core.impl._ zz>import rdf.ontologies._ import rdf.ontologies._ zz>import platform.graphprovider.content.ContentGraphProvider import platform.graphprovider.content.ContentGraphProvider zz>val cg = $[ContentGraphProvider] getContentGraph cg: org.apache.clerezza.rdf.core.access.LockableMGraph = [org.apache.clerezza.rdf.core.BNode@49ec9b79 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://discobits.org/ontology#Entry>., org.apache.clerezza.rdf.core.BNode@49ec9b79 <http://discobits.org/ontology#holds> <http://clerezza.apache.org/getting-started/index-title>., org.apache.clerezza.rdf.core.BNode@49ec9b79 <http://discobits.org/ontology#pos> "0"., <http://clerezza.apache.org/getting-started/index-title> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://discobits.org/ontology#XHTMLInfoDiscoBit>., <http://clerezza.apache.org/getting-started/index-title> <http://discobits.org/ontology#infoBit> "Getting Started"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>., org.apache.clerezza.rdf.core.BNode@297c074e <http://www.w3.... zz>cg.add(new TripleImpl(new UriRef("http://clerezza.apache.org/getting-started/shell/"), RDF.`type`, HIERARCHY.Collection)) res20: Boolean = true zz>

Creating and Registering Services

All variables and objects created on the Shell are lost after a restart this is true also for objects registered as service. Nevertheless being able t o quickly create and register a service might often come in handy.
The following registers a "hello world" root resource at the uri-path /foo/bar
zz>import javax.ws.rs._ import javax.ws.rs._ zz>@Path("foo/bar") class Foo { @GET def get() = { "hello world" } } defined class Foo zz>import scala.collection.JavaConversions.asDictionary import scala.collection.JavaConversions.asDictionary zz>val args = scala.collection.mutable.Map("javax.ws.rs" -> true) args: scala.collection.mutable.Map[java.lang.String,Boolean] = Map((javax.ws.rs,true)) zz>bundleContext.registerService(classOf[Object].getName, new Foo(), args) res38: org.osgi.framework.ServiceRegistration = org.apache.felix.framework.ServiceRegistrationImpl@5bf89f46 zz>