Helma Logo
main list history
previous version  overview  next version

Version 31 by earl on 27. September 2009, 22:54

With Google's [announcement][1] of [Java support][2] for App Engine there is all of a sudden a very attractive new option for hosting Java based web applications. Of course my first impulse was to check if it would run [Rhino] and Helma NG (I had little hopes for Helma 1 as it runs a lot of background threads and relies heavily on relational or file based database backends, both of which are not available on App Engine).

[1]: http://googleappengine.blogspot.com/2009/04/seriously-this-time-new-language-on-app.html
[2]: http://code.google.com/appengine/docs/java/overview.html
[rhino]: http://www.mozilla.org/rhino/

To my surprise, Rhino works perfectly out of the box, and I only had to make a few changes to Helma NG to get the demo app [running on App Engine][3]. This is somewhat in contradiction with what [others have found][ola], and I discovered only later that I was partly lucky for not stomping on any SecurityExceptions, but on the other hand it's by virtue of Rhino not doing a lot of tricks under the hood, and Helma NG doing all its file reading through a unified [resource framework][4].

[3]: http://helma-ng.appspot.com/
[ola]: http://olabini.com/blog/tag/gae/
[4]: http://github.com/hns/helma-ng/tree/master/src/org/helma/repository

This article covers two options, one using the Helma NG webapp framework, and one using the [Jack] interface for JavaScript web applications.

### Installing App Engine SDK and Helma NG

First, install the App Engine SDK for Java as described on the [Google pages][gae-install]. Whether you use the Eclipse plugin or the command line SDK shouldn't make a difference. I'm using Eclipse, but things should work in a similar fashion with the command line SDK.

[gae-install]: http://code.google.com/appengine/docs/java/gettingstarted/installing.html

Next, you need Helma NG, which is effectively Rhino with a thin layer of server-related functionality on top. Helma NG currently comes with its own copy of Rhino as we are using some experimental JS 1.8 features, but this may change as soon as these features are included in a Rhino release.

You can [download the latest release][download] or get the current snapshot [from Github][github], either by clicking the download button or using the following git command:

[download]: http://github.com/hns/helma-ng/downloads
[github]: http://github.com/hns/helma-ng/tree/master

`git clone git://github.com/hns/helma-ng.git`

You need [Apache Ant][ant] to build Helma NG. Change to the helma-ng directory you just checked out and run ant it with the "jar" `jar` target:

[ant]: http://ant.apache.org/

`ant jar`

If this worked then you should now have a file called lib/helma`lib/helma.jarjar`. 

### Creating a new project with the Eclipse plugin

Once you have everything in place, create a new, empty App Engine application. Use the new project wizard and select "Web Application Project" in the Google category. This will ask you for a project name and a package name. The package name doesn't matter unless you plan to write Java code, but it is required so just enter anything.

The new project should have two subdirectories, "src" `src` and "war"`war`. Again, the src `src` dir is only relevant for Java code, as we'll put our JavaScript code directly in war/WEB-INF `war/WEB-INF` and our jar files in war/WEB-INF/lib`war/WEB-INF/lib`.

Next, copy over some jar files from the Helma NG lib directory to the war/WEB-INF/lib `war/WEB-INF/lib` of your project. You'll probably need the following:

* `lib/js.jar`
* `lib/helma.jar`
* `lib/log4j-1.2.15.jar`

App Engine has its own web server of course, so you don't need the jetty or servlet jar files.

From here, you have two options: Run an app using the Helma NG webapp framework, or one using the [Jack] interface.

[jack]: http://github.com/tlrobinson/jack/tree/master

### Creating a new project from the command line

From the SDK directory, copy demos/new_project_template and rename it to your application's name (say "Demo`Demo`").

Open Demo/build`Demo/build.xmlxml`, locate the property appengine`appengine.sdk sdk` and modify it so that it points to the path of the SDK root directory on your machine.

Also add the following:

    <target name="copyjars"
            description="Copies the App Engine JARs to the WAR."
      <copy todir="${war.dir}/WEB-INF/lib"
        <fileset dir="${appengine.sdk}/lib/user">
          <include name="\*\*/*.jar" />

Again in build`build.xml xml` locate the "war" `war` target and add a dependency on the "copyjars" `copyjars` target so that it looks like:

    <target name="war" depends="enhance,copyjars"
            description="Assemble the application directory"

Run "ant" `ant` in the same directory as "build`build.xml" xml` so that the WEB-INF `WEB-INF` directory will be created.

Next, copy over some jar files from the Helma NG lib directory to the www/WEB-INF/lib `www/WEB-INF/lib` of your project. You'll probably need the following:

* `lib/js.jar`
* `lib/helma.jar`
* `lib/log4j-1.2.15.jar`

App Engine has its own web server of course, so you don't need the jetty or servlet jar files.

From here, you have two options: Run an app using the Helma NG webapp framework, or one using the [Jack] interface.

### Running Helma NG apps

To run the Helma NG *demo application|http://helma-ng.appspot.com/* follow these steps:

1. Copy the modules directory from the Helma NG directory to war/WEB-INF `war/WEB-INF` (or `www/WEB-INF`, if you created the project from the command-line) so that the resulting directory is called `WEB-INF/modules`.

2. Copy the demo app from apps/demo Helma NG's `apps/demo` to war/WEB-INF `war/WEB-INF` (or `www/WEB-INF`, respectively) so that the resulting directory is called `WEB-INF/demo`. Note that the demo app contains a symbolic link, which is not supported by Google App Engine. Copying files with `cp -rL` will take care of dereferncing dereferencing the link.

3. Copy or move the static directory from within the demo app two levels up to the war directory (war/static)(`war/static` or `www/static`).

4. Edit `WEB-INF/web.xml` to look [like the attached file](http://dev.helma.org/static/files/3318/web.xml), or just download and overwrite the file.

5. Edit `WEB-INF/appengine-web.xml` to look [like the attached file](http://dev.helma.org/static/files/3318/appengine-web.xml), or just download and overwrite the file.

That's it - save all your changes and click on the green Run button in Eclipse or run the dev_appserver.sh script `ant dev_appserver` to start the development server. If everything worked as planned you should be able to access the [demo app](http://helma-ng.appspot.com/) on http://localhost:8080/.

### Running Jack apps

Helma NG uses [Jack] to as web application connector, so you are not bound to use the Helma webapp infrastructure. If you prefer, you can use Helma NG as a container for Jack applications fully compatible with the [ServerJS Securable Modules proposal](https://wiki.mozilla.org/ServerJS/Modules/SecurableModules).

The setup is much the same as with the Helma NG demo application, only that you point the JackServlet to your own Jack web appliction in `WEB-INF/web.xml`.

For example, assuming you have the following simple Jack application in `app/main.js`:

    function handler(env) {
      return [200, {"Content-Type": "text/plain"}, ["Hello World!"]];

You would then set the `modulePath` servlet init-parameter in [web.xml](http://dev.helma.org/static/files/3318/web.xml) to `app`, `moduleName` to `main`, and `functionName` to `handler`.

After starting the development server you should be able to access your Jack app at http://localhost:8080/.

### Attachments

<% this.attachments %>