Helma Logo
main list history
previous version  overview  next version

Version 7 by tobi on 19. January 2008, 12:47

=== Introduction

Inspired by Massimiliano Mirra's <a href="http://hyperstruct.net/projects/mozrepl">MozRepl</a> project I quickly coded a similar tool, for now dubbed HopRepl.

HopRepl actually is similar to <a href="https://dev.helma.org/trac/helma/browser/apps/helmaTools/trunk/Global/helma.shell.js">Helma Shell</a> with the tiny difference that it does not run in a browser window but in a terminal environment (aka command line).

<small>Note: Chris Langreiter was here years ago already when he published <a href="http://www.langreiter.com/space/HopShell">HopShell</a>, a REPL written in Rebol.</small>

The four letters <a href="http://en.wikipedia.org/wiki/REPL">REPL</a> actually are in fact used synonymously for a shell.

Thus, you can enter simple commands to inspect and manipulate Helma space. And commands are of course JavaScript statements!

This is an alpha version, ie. you will get excepctions, stuck and not always what you expected.

Thanks to Robert and Hannes for their help; it wouldn't be even alpha without them.

Here's what you can do with it:

=== Installation And Setup

First, move the two files attached to this wiki as repl.zip and repl.jar into your application directory. I recommend the manage application because it already provides some interesting objects to explore.

<% this.attachments %>

To be sure that the HopRepl code is really compiled into the application a Helma restart won't hurt.

Now you can start the HopRepl server by navigating to the "repl" action in your browser. In my case the location is http://localhost:8080/manage/repl

There you will see one simple line saying "HopREPL server (stopped)" followed by two links "Start" and "Restart".

Click on the "Start" link and your HopRepl should be started. Simultaneously, the link will change to "Stop" and the port number will be displayed in brackets.

(If not you might check if anything else is running behind that port on your computer already. Use the application property replPort to change the port number.)

=== Using HopRepl

Let's check if you can connect from the command line shell to the HopRepl server by entering

  telnet localhost 1234

(Be sure to use the desired port number if you should have changed it in the previous step.)

If everyhing goes well HopRepl should welcome you with a short message and the REPL prompt:

  Hello! This is HopRepl Alpha (c) 2008 Tobi Schäfer

After the prompt you can enter any JavaScript statement you like. It will be (R)ead, (E)valuated and the result will be (P)rinted in your terminal. Finally, the (L)oop starts again with your next input. That's the generic principle behind REPL.

  REPL> new Date
  Thu Jan 17 2008 12:21:57 GMT+0100 (CET)
  REPL> Math
  [object Math]
  REPL> this
  [object GlobalObject]

There is one special object added to the JavaScript as defined by Rhino and Helma. It is called repl and acts as a helper object providing methods to make navigating and exploring the namespace more convenient.

  REPL> repl
  HopREPL server (port 1234)

To get a clue where in Helma space you are right now you can use the whereAmI() method:

  REPL> repl.whereAmI()
  [object GlobalObject]

The look() method prints a list of all available properties and methods in the current context:

  REPL> repl.look()
  this.Repl = [function]
  this.Server = [function]
  this.Thread = [function]
  this.Xml = {class helma.scripting.rhino.extensions.XmlObject}
  this.app = {class helma.framework.core.ApplicationBean}
  this.appStat = [function]
  this.checkAddress = [function]
  this.checkAuth = [function]
  this.createAddressFilter = [function]
  this.req = {class helma.framework.RequestBean}
  this.res = {class helma.framework.ResponseBean}
  this.root = {class helma.main.Server}
  this.scheduler = [function]
  this.session = {class helma.framework.core.SessionBean}
  this.skin_macro = [function]
  this.sortByName = [function]
  this.sortProps = [function]

For convenience you don't have to always type in "this" to access a property or a method. HopRepl provides all available properties of the current context as global variables:

  REPL> res
  REPL> session
  [Anonymous Session]
  REPL> skin_macro
  function skin_macro(par) {
      if (par && par.name) {

The enter() method changes the context to another object:

  REPL> repl.enter(root)
  REPL> repl.look()
  this.application = undefined
  this.applications = {class [Ljava.lang.Object;}
  this.appsHome = {class java.io.File}
  this.appsProperties = undefined
  this.checkAppManager = [function]
  this.class = {class java.lang.Class}
  this.dbHome = {class java.io.File}
  REPL> repl.enter(checkAppManager)
  function checkAppManager() {/*
      void checkAppManager(int)

To get back to the previous context the back() method is at hand:

  REPL> repl.back()

And the home() method brings you even further back, straight up to the top-level context:

  REPL> repl.home()
  [object GlobalObject]

If you should have enough of tinkering with HopRepl the quit() method closes the connection to the server:

  REPL> repl.quit()
  Connection closed by foreign host.

=== Getting Nifty

Of course you can also call methods, not only enter their contextes. Here's a more elaborated example of using HopRepl to stop an application:

  REPL> repl.home()
  [object GlobalObject]
  REPL> repl.enter(root)
  REPL> repl.enter(getApplication("test"))
  [Application test]
  REPL> isRunning()
  REPL> stop()
  REPL> isRunning()

Further manipulation is possible, too. Let's change a HopObject's property:

  REPL> repl.home()
  [object GlobalObject]
  REPL>> repl.enter(root)
  REPL> repl.enter(getApplication("antville"))
  [Application antville]
  REPL> repl.enter(dataRoot)
  HopObject Site 14
  REPL> repl.enter(creator)
  HopObject p3k
  REPL> repl.look()
  this.comments = [object]
  this.created = [object]
  this.email = nobody@nowhere.com
  this.files = [object]
  REPL> email = "somebody@somewhere.com"

Please note that the e-mail address is not changed in the database, yet. To achieve this one could add a <code>res.commit()</code> at this point. After invalidating the application – which of course could be done in HopRepl as well by invoking <code>app.clearCache()</code> – the new value will also be displayed when its property is requested.requested:

  REPL> res.commit()
  REPL> repl.back(); repl.back() // Back to the application's context
  [Application antville]
  REPL> clearCache()

That's it so far. Maybe this first attempt will grow and if anyone is interested in picking up the pieces, please contact me via the helma-dev mailing list.

=== To Do

* Eat those nasty java.lang.Nullpointer exceptions (could be something with entering Java class context)
* <s>Allow modifications of properties to trickle down to the database</s> Works with <code>res.commit()</code>

// *tobi*