Listing 1 ServerUtility Remote Interface

package org.sofu.ejb;

import javax.ejb.Remote;
/**
 * This is the business interface for ServerUtility enterprise bean.
 */
@Remote
public interface ServerUtilityRemote {

}

Listing 1a ServerUtility bean class

package org.sofu.ejb;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.Stateless;

@Stateless
public class ServerUtilityBean implements ServerUtilityRemote {

    /** Creates a new instance of ServerUtilityBean */
    public ServerUtilityBean() {
    }
}

Listing 2 EJB method implementation

package org.sofu.ejb;
...
@Stateless
public class ServerUtilityBean implements ServerUtilityRemote {
...
    /* get server date information */
    public String getServerDate() {
        Date now = new Date();
        SimpleDateFormat format = newSimpleDateFormat();
        format.applyPattern("dd MMMM yyyy");

        return format.format(new Date());
    }

    /* current age from birthday parameter */
    public String currentAge(String birthday) {
        String result = null;
        SimpleDateFormat format = newSimpleDateFormat();
        try {
            format.applyPattern("dd/MM/yyyy");
            Date birthdate = format.parse(birthday);

            long age = (System.currentTimeMillis() -
               birthdate.getTime()) / 315360000001;
           result = "You Are " + age + " Years old";
        } catch (ParseException e) {
            result = "Invalid birthday format";
        }
        return result;
    }
}

Listing 3 Command class

package org.sofu.cgw;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.sofu.ejb.ServerUtilityRemote;

public class Command {
    private Context ctx = null;
    private ServerUtilityRemote server = null;

    /** Creates a new instance of Command */
    public Command() {
           Properties env = new Properties();
           env.setProperty(
                Context.INITIAL_CONTEXT_FACTORY,
               "com.sun.enterprise. _                           
                      naming.SerialInitContextFactory");

           env.setProperty(
                Context.URL_PKG_PREFIXES,
                "com.sun.enterprise.naming");

           env.setProperty(
              Context.STATE_FACTORIES,
             "com.sun.corba.ee.impl.presentation. _
              rmi.JNDIStateFactoryImpl");

            env.setProperty(Context.PROVIDER_URL,
                                  "iiop://localhost:3700");
            ctx = new InitialContext(env);
        try {
            ctx = new InitialContext(env);
            server = (ServerUtilityRemote)
                 ctx.lookup(
                   "org.sofu.ejb.ServerUtilityRemote");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
    /* command server to get the
       current server date */
 public String cmdGetServerDate() {
        return server.getServerDate();
    }
    /* command server to calculate the 
       current age based on birthday  */
 public String cmdCurrentAge(String birthday) {
        return server.currentAge(birthday);
    }
}

Listing 4 Importing a Java library and defining a class in JavaFX

import java.lang.*;
import javafx.ui.*;
import javafx.ui.canvas.*;
import javafx.ui.filter.*;
import java.awt.Dimension;

import org.sofu.cgw.*;

class Window {
    attribute clientWidth:Number;
    attribute clientHeight:Number;
    attribute clientBackground:ImageView;
    attribute clientPanel:FlowPanel;
    attribute clientCanvas:Canvas;
    attribute clientLblDateTitle:Text;
    attribute clientLblDateValue:Text;
    attribute clientBtnCalcAge:View;
    attribute clientLblBirthTitle:Text;
    attribute clientTxtBirthday:View;
    attribute clientLblFormat:Text;
    attribute clientLblAgeValue:Text;
}
...

Listing 5 JavaFX variable initialization

...
var cmd:Command = new Command();
var win:Window = new Window();
var txtBirthday:String = new String();
var resultAge:String = new String();

win.clientWidth = 345;
win.clientHeight = 245;
...

Listing 6 Calling the Java method from JavaFX

...
win.clientBtnCalcAge = View {
    transform: translate (100, 125)
    content : Button {
        text: "Calculate My Age"
        font: Font {
            face: VERDANA
            size: 10
            style : [BOLD]
        }
        action: operation() {
            resultAge = 
                cmd.cmdCurrentAge(txtBirthday);
        }
    }
};
...

Listing 7 JavaFX UI composition

...
win.clientCanvas = Canvas {
    content: [win.clientBackground, 
              win.clientLblDateTitle,
              win.clientLblDateValue,
              win.clientLblBirthTitle,
              win.clientTxtBirthday,
              win.clientLblFormat,
              win.clientBtnCalcAge,
              win.clientLblAgeValue]
};

win.clientPanel = FlowPanel {
    content: win.clientCanvas
};

var ui = Frame {
    title  : "Communicating JavaFX with EJB 3.0"
    width  : win.clientWidth
    height : win.clientHeight
    content: win.clientPanel 
    visible:true, centerOnScreen: true
};