Getting Started

Installation

We begin by assuming that you have unzipped 'seppia.zip' in the following directory

C:\seppia
 

Below are the files and directories you can now see.


c:\seppia\StartUp.class      java class to launch the application
c:\seppia\jars\              folder with the jars needed by any seppia application
c:\seppia\jars\seppia.jar 
c:\seppia\jars\rhinojs-1.6.jar 
c:\seppia\jars\xbean-1.3.1.jar 
c:\seppia\modules\ 
c:\seppia\modules\org.seppia.bootstrap\
c:\seppia\modules\org.seppia.bootstrap\javascripts\StartUp.js 


This is it: any Seppia Application is going to look like this whether it is going to be a financial application or a game. Only it might have more modules (folders in the modules directory), more jar files (jar files under the 'jars' folder in each module), and more javascripts.

Running The Seppia Application

If you want to run it just move into the directory and type


c:\seppia>java -cp . StartUp
Seppia successfully installed at URL file:/C:/seppia/

We are obviously assuming that java is pointing to the java virtual machine. Depending on which machine you are working (Windows, Unix, Linux ,...) you might decide to write the appropriate batch/script. But your batch/script should not do anything more than that. Any Seppia Application infact must be started in that way drastically reducing the need for a batch or shell script to launch a Java application.

The classic "Hello World" in Seppia

To do this we need to create a new module with the a new javascript that does the job.
Let's call the new module "org.seppia.tutorial.helloworld" 
Let's write the new javascript "HelloWorld.js" with the following content:


function main()
{
   java.lang.System.out.println("Hello World");
}

This is what we should see now:


c:\seppia\StartUp.class      java class to launch the application
c:\seppia\jars\              folder with the jars needed by any seppia application
c:\seppia\jars\seppia.jar 
c:\seppia\jars\rhinojs-1.6.jar 
c:\seppia\jars\xbean-1.3.1.jar 
c:\seppia\modules\ 
c:\seppia\modules\org.seppia.bootstrap\
c:\seppia\modules\org.seppia.bootstrap\javascripts\StartUp.js 
c:\seppia\modules\org.seppia.tutorial.helloworld\javascripts\HelloWorld.js

Ok now the final bit is to redirect "org.seppia.bootstrap.StartUp.js" so to run "HelloWorld.js".

So "StartUp.js" becomes:


function main()
{
   // redirect the script to another script.
   // the first argument is the name of the module. 
   // the second argument is the name of the script.
   run("org.seppia.tutorial.helloworld","HelloWorld");
}

Again this is a classical operation in Seppia, i.e.: every seppia application
redirects org.seppia.bootstrap.StartUp.js to launch another script.

This is what we should see now:


c:\seppia>java -cp . StartUp.class
Hello World

 

Another Example

The next example is going to show you how Seppia let your javascript interact with your java code.
We suppose you have written (or downloaded from the net) a basic geometrical package for the jdk.
The package might have classes like Line, Circle, Triangle, Rectangle, Shape and so on.
So let's suppose the Rectangle class looks like this:


package com.geometricalstuff.shape;

public class Rectangle
{
   public Rectangle(double side1,double side2)
   {
     ...
   } 

   public double getArea()
   {
     ...
   }
}

The classes Circle and Triangle are going to have the method called 'getArea()' as well.
Circle will have a constructor which receives a double (its radius) while Triangle will have a constructor with three parameters: one for each side.

The first step is to package your classes into a jar file. Let's call it: "geomtool.jar". This jar file needs to be placed into the jars folder of the module.Let's call our module "org.seppia.tutorial.geom"

Then one or more javascript files can be written to work with it. For the purpose of the simple program we want to write we could go away with just one javascript instead we will write two javascripts to have an opportunity to explain more about their interaction.

File: AreaCalculator.js


// Importing the classes...
var System = java.lang.System;
var Triangle = Packages.com.geometricalstuff.shape.Triangle;
var Rectangle =Packages.com.geometricalstuff.shape.Rectangle;
var Circle = Packages.com.geometricalstuff.shape.Circle;

//
// the main function creates a javascript object
// with four properties:
// four functions to calculate the area of different shapes.
//
public function main()
{
   var obj = new Object();
   obj.calcTriangleArea = calcTriangleArea;
   obj.calcRectangleArea = calcRectangleArea
   obj.calcCircleArea = calcCircleArea;
   obj.calcSquareArea = function f(a) { calcRectangleArea(a,a); return obj; }
}

function calcTriangleArea(a,b,c)
{
   var t = new Triangle(a,b,c);
   return t.getArea();
}

function calcRectangleArea(a,b)
{
   var r = new Rectangle(a,b);
   return r.getArea();
}

function calcCircleArea(radius)
{
   var c = new Circle(radius);
   return c.getArea();
}



File: GeomRunner.js


// this javascript launches the AreaCalculator
// and then performs a few geometrical calculations...
//

var System = java.lang.System;

function main()
{
   var ac = run("AreaCalculator");
   System.out.println("The area of a rectangle 4 by 6 is "+ ac.calcRectangleArea(4,6));
   System.out.println("The area of a circle of radius 5 is "+ ac.calcCircleArea(5));
}

 

org.seppia.bootstrap.StartUp is redirected to launch GeomRunner


function main()
{
   run("org.seppia.tutorial.geom","GeomRunner);
}

This is the structure of the files we should see now:


c:\seppia\StartUp.class      java class to launch the application
c:\seppia\jars\              folder with the jars needed by any seppia application
c:\seppia\jars\seppia.jar 
c:\seppia\jars\rhinojs-1.6.jar 
c:\seppia\jars\xbean-1.3.1.jar 
c:\seppia\modules\ 
c:\seppia\modules\org.seppia.bootstrap\
c:\seppia\modules\org.seppia.bootstrap\javascripts\StartUp.js 
c:\seppia\modules\org.seppia.tutorial.geom\jars\geomtool.jar 
c:\seppia\modules\org.seppia.tutorial.geom\javascripts\AreaCalculator.js
c:\seppia\modules\org.seppia.tutorial.geom\javascripts\GeomRunner.js

and this is what happens when we launch it:


c:\seppia>java -cp . StartUp.class
The area of a rectangle 4 by 6 is 24.0
The area of a circle of radius 5 is 49.33

At this point we hope that your reaction is a "Wow !!!" as you begin to see how javascripts and jars work together within the seppia Framework. 
The module org.seppia.tutorial.geom infact identifies a mostly self-contained part of your system deployed a single,coherent unit with its jars folder and its javascripts folder

AreaCalculator is a javascript that when executed builds a javascript object to offer some services. By being able to access the classes in the geomtool.jar the implementation of its services is relatively straight-forward.
Those services (the returned javascript object) could then be used by another module.

GeomRunner is the entry point of the module a tiny program  that prints out the areas of a circle and a rectangle. It runs "AreaCalculator.js" and then relies on its services to perform its task. As GeomRunner does not rely upon geomtool.jar it could easily deployed in another module.

Conclusions

The scope of this tutorial was to provide you with a quick introduction to the Seppia Technology.
In order to keep it very concise we obviously omitted many aspects of it.
We hope you can see beyond the simplicity of the tutorial's examples and see where you can use Seppia in your projects.
At this stage we would recommend you to continue to familiarize yourself with the concepts behind Seppia, by downloading some sample modules (if you have not done it already) and by writing a few yourself.
So remember: Seppia is just a simple framework to produce component-based software with java and javascript.