Using Flash with JRun

To expose functionality that you build in JRun to Flash, you build Java methods. JRun supports Flash connectivity with the following Java application types:

To make your Java applications available to Flash, you must save your class files in the SERVER-INF directory.

The following table contains the Java data types and their ActionScript equivalents:
Java data type
ActionScript data type
number
number (primitive data type)
boolean
Boolean (primitive data type)
string
string
map
ActionScript (AS) object, AS object as the only argument passed to a service function, or AS XML object
null
null
array
array
date
date object

Building a JavaBean that communicates with Flash

To communicate with Flash, you declare the file as part of the flashgateway package. The public methods are automatically available to Flash movies as ActionScript functions.

To create a JavaBean that communicates with Flash:

  1. Create a Java file and save it as FlashExampleBean.java in the samples folder in the WEB-INF directory.
  2. Modify the file so that the Java code appears as follows:
    // names package
    package flashgateway.samples;
    
    import java.util.Date;
    import java.io.Serializable;
    
    //declares class
    public class FlashExampleBean
            implements Serializable {
      private String message;
      private int count;
    
    public FlashExampleBean() 
      {
            message = "Hello World From JavaBean";
            count = 0;
      }
      //declares testBoolean method
      public boolean testBoolean(boolean b) 
      {
        return b;
      }
      //declares testDate method
      public Date testDate(Date d) 
      {
        return d;
      }
      //declares setMessage method
      public void setMessage(String message) 
      {
        this.message = "Hi " + message;
      }
      //declares getMessage method
      public String getMessage() {
            count++;
            return message + " (count=" + count + ")";
        }
      //declares getCount method
      public int getCount() 
      {
        count++;
        return count;
      }
      //declares setCount
      public void setCount(int count) 
      {
            this.count = count;
        }
    }
    
  3. Save the file.

The JavaBean methods translate to the FlashExampleBean ActionScript functions. The following table displays the methods available for JavaBean and the corresponding functions in ActionScript:
JavaBean method
ActionScript functions
testBoolean
FlashExampleBean.testBoolean
testDate
FlashExampleBean.testDate
setMessage
FlashExampleBean.SetMessage
getMessage
FlashExampleBean.getMessage
setCount
FlashExampleBean.setCount
getCount
FlashExampleBean.getCount

Looking at the ActionScript

When you access JavaBean methods in ActionScript, you must supply the fully qualified class name, such as flashgateway.samples.FlashExampleBean, in the getService ActionScript function.

The following ActionScript, applied at the frame level, calls the JavaBean methods:

#include "NetServices.as"
#include "NetDebug.as"
// --------------------------------------------------
// Handlers for user interaction events
// --------------------------------------------------
function runExample()
{
  setMessage();
  getMessage();
  testBoolean();
  testDate();
}
function setMessage()
{
  flashtestService.setMessage(messageInput.text);
}
function getMessage()
{
  flashtestService.getMessage();
}
function testBoolean()
{
  if (trueRadio.GetState())
  {
    flashtestService.testBoolean(true);
  }
  else
  {
    flashtestService.testBoolean(false);
  }    
}
function testDate()
{
  flashDate = new Date();
  dateInput.text = "" + flashDate;
  flashtestService.testDate(flashDate);
}
// --------------------------------------------------
// Handlers for data coming in from server
// --------------------------------------------------
function setMessage_Result( result )
{
}
function getMessage_Result( result )
{
  messageOutput.text = result;
}
function setMessage_Status( result )
{
  messageOutput.text = result.details;
}
function getMessage_Status( result )
{
  messageOutput.text = result.details;
}
function testBoolean_Result(result)
{
  boolOutput.text = "result: " + result;
}
function testBoolean_Status(result)
{
  boolOutput.text = "status: " + result.details;
}
function testDate_Result(result)
{
  flashDate = result;
  dateOutput.text = " " + flashDate;
}
function testDate_Status(result)
{
  dateOutput.text = "Status: " + result.details;
}
// --------------------------------------------------
// Start up the application
// --------------------------------------------------
if (inited == null)
{  
  // do this code only once
  inited = true;
  // set the default gateway URL (for use in authoring)
    NetServices.setDefaultGatewayUrl("http://localhost:8200/flashservices/gateway");
  // connect to the gateway
    gatewayConnnection = NetServices.createGatewayConnection();
  // get a reference to the JavaBean in my HTTP session (creating a new one the first time)
    flashtestService = gatewayConnnection.getService("flashgateway.samples.FlashJavaBean", this);
  flashDate = new Date();
  messageInput.text = "[Enter a Message]";
  dateInput.text = "" + flashDate;
}

Accessing EJB applications with Flash

You build EJBs for Salsa just like any other EJB. When you access the EJB methods exposed through Salsa, you use the JNDI EJB name in the getService function. The following ActionScript example calls two different EJBs according to the user's selection in the Flash movie:

function getBiggerString()
{
  if (useStateless.selected) 
  {
    flashStatelessEJB.getBiggerString( stringInput.text );
    stringOutput.text = "[Invoking Stateless EJB...]";
  }
  else 
  {
    flashStatefulEJB.getBiggerString( stringInput.text );
    stringOutput.text = "[Invoking Stateful EJB...]";
  }
}

When using Flash with EJB applications, ActionScript functions map to EJBHome and EJBObject methods.

Looking at the ActionScript

The following ActionScript example calls the EJB methods and displays the results in Flash:

#include "NetServices.as"
#include "NetDebug.as"
// --------------------------------------------------
// Handlers for user interaction events
// --------------------------------------------------
function runExample()
{
  // retrieve reference to initially-selected EJB type
  getEJBs();
}
// ---------------------------------------------------
// Business Methods
// ---------------------------------------------------
function getBiggerString()
{
  if (useStateless.selected) 
  {
    flashStatelessEJB.getBiggerString( stringInput.text );
    stringOutput.text = "[Invoking Stateless EJB...]";
  }
  else 
  {
    flashStatefulEJB.getBiggerString( stringInput.text );
    stringOutput.text = "[Invoking Stateful EJB...]";
  }
}
function getBiggerInt()
{
  if (useStateless.selected) 
  {
    flashStatelessEJB.getBiggerInt( (new Number(numInput.getSelectedItem().data)).valueOf() );
    stringOutput.text = "[Invoking Stateless EJB...]";
  }
  else 
  {
    flashStatefulEJB.getBiggerInt( (new Number(numInput.getSelectedItem().data)).valueOf() );
    stringOutput.text = "[Invoking Stateful EJB...]";
  }                          
}
// --------------------------------------------------
// Handlers for data coming in from server
// --------------------------------------------------
function getBiggerString_Result ( result )
{
  stringOutput.text = result;
}
function getBiggerInt_Result ( result )
{
  numOutput.text = result;
}
function create_Result( result )
{
  if (useStateless.selected) 
  {
    flashStatelessEJB = result;
  }
  else 
  {
    flashStatefulEJB = result;
  }
  
  getBiggerString();
  getBiggerInt();
}
// this method will be called if an error occurs while invoking 'create'
function create_Status( result )
{
  stringOutput.text = result.details;  
}
function getEJBs() 
{
  // get a reference to the EJBs
  // stateless:
  if (useStateless.selected) 
  {
    flashstatelessHome = gatewayConnection.getService("FlashSampleStatelessEJB", this);
    flashstatelessHome.create();
  }
  // stateful:
  else
  {
    if (flashstatefulHome == undefined)
    {
        flashstatefulHome = gatewayConnection.getService("FlashSampleStatefulEJB", this);
      flashstatefulHome.create( "stateful message" );
    }
    else
    {
      getBiggerString();
      getBiggerInt();
    }
  }
  
}
// --------------------------------------------------
// Start up the application
// --------------------------------------------------
if (inited == null)
{  
  // do this code only once
  inited = true;
  // set the default gateway URL (for use in authoring)
    NetServices.setDefaultGatewayUrl("http://localhost:8200/flashservices/gateway");
  // connect to the gateway
    gatewayConnection = NetServices.createGatewayConnection();
  // set security information for custom authentication and authorization
  //gatewayConnection.setCredentials("Flash", "Flashpass");
  stringInput.text = "[Enter a Word]";
  numInput.setSelectedIndex(0);
}

Using JMX with Flash

To call JRun application functionality through JMX using Flash, you call MBean methods. You connect to JMX objects in JRun using the MBean object name in the ActionScript getService function.

Looking at the ActionScript

The following ActionScript example calls the MBean methods and displays the results in Flash:

#include "NetServices.as"
#include "NetDebug.as"
// --------------------------------------------------
// Handlers for user interaction events
// --------------------------------------------------
function runExample()
{
  jrunDeployerMBean.getServerName();
  jrunDeployerMBean.getEARs();
}
// --------------------------------------------------
// Handlers for data coming in from server
// --------------------------------------------------
function getServerName_Result(result)
{
  serverName.text = result;
}
function getEARs_Result(result)
{
  numDeployed.text = result.length;
}
function onStatus(result)
{
  serverName.text = "status triggered";
}
// --------------------------------------------------
// Start up the application
// --------------------------------------------------
if (inited == null)
{  
  // do this code only once
  inited = true;
  // set the default gateway URL (for use in authoring)
    NetServices.setDefaultGatewayUrl("http://localhost:8200/flashservices/
gateway");
  // connect to the gateway
    gatewayConnnection = NetServices.createGatewayConnection();
  // get a reference to the remote MBean
  jrunDeployerMBean =
gatewayConnnection.getService("DefaultDomain:service=
DeployerService", this);
}

 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/usingSalsaJ3.htm