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 interface provide additional commands, for example 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 ] [ 1] [active ] ...

Interacting with the OSGi environment

An instance of 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 - mvn:org.apache.clerezza/ 2 - mvn:org.apache.clerezza/ 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 <> <>., org.apache.clerezza.rdf.core.BNode@49ec9b79 <> <>., org.apache.clerezza.rdf.core.BNode@49ec9b79 <> "0"., <> <> <>., <> <> "Getting Started"^^<>., org.apache.clerezza.rdf.core.BNode@297c074e <http://www.w3.... zz>cg.add(new TripleImpl(new UriRef(""), 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 import 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("" -> true) args: scala.collection.mutable.Map[java.lang.String,Boolean] = Map((,true)) zz>bundleContext.registerService(classOf[Object].getName, new Foo(), args) res38: org.osgi.framework.ServiceRegistration = org.apache.felix.framework.ServiceRegistrationImpl@5bf89f46 zz>