Flash と JRun の併用

JRun で作成した機能は、Java メソッドを作成するだけで Flash に適用できます。JRun は、次の Java アプリケーションタイプと Flash のコネクティビティをサポートします。

Java アプリケーションを Flash で利用するには、クラスファイルを SERVER-INF ディレクトリに保存する必要があります。

次の表に、Java データタイプとそれらに対応する ActionScript をリストします。
Java データタイプ
ActionScript データタイプ
number
number (プリミティブデータタイプ)
boolean
Boolean (プリミティブデータタイプ)
string
string
map
ActionScript (AS) オブジェクト、サービス関数に渡される
唯一の引数としての AS オブジェクト、または AS XML
オブジェクト
null
null
array
array
date
date オブジェクト

Flash と通信する JavaBean の作成

Flash と通信するには、flashgateway パッケージの一部としてファイルを宣言します。public メソッドは、ActionScript 関数として自動的に Flash ムービーで利用できます。

Flash と通信する JavaBean を作成するには

  1. Java ファイルを作成し、WEB-INF ディレクトリの samples フォルダに
    FlashExampleBean.java として保存します。
  2. Java コードが次のように保存されるように、ファイルを修正します。
    // パッケージに名前を付けます。
    package flashgateway.samples;
    
    import java.util.Date;
    import java.io.Serializable;
    
    // クラスを宣言します。
    public class FlashExampleBean
            implements Serializable {
      private String message;
      private int count;
    public FlashExampleBean() 
      {
            message = "こんにちは JavaBean です。"; 
            count = 0;
      }
      // testBoolean メソッドを宣言します。
      public boolean testBoolean(boolean b) 
      {
        return b;
      }
      // testDate メソッドを宣言します。
      public Date testDate(Date d) 
      {
        return d;
      }
      // setMessage メソッドを宣言します。
      public void setMessage(String message) 
      {
        this.message = "はい " + message;
      }
      // getMessage メソッドを宣言します。
      public String getMessage() {
            count++;
            return message + " (count=" + count + ")";
        }
      // getCount メソッドを宣言します。
      public int getCount() 
      {
        count++;
        return count;
      }
      //setCount を宣言します。
      public void setCount(int count) 
      {
            this.count = count;
        }
    }
    
  3. ファイルを保存します。

JavaBean のメソッドが FlashExampleBean ActionScript 関数にトランスレートされます。次の表は、JavaBean で利用できるメソッドと、対応する ActionScript の関数を示したものです。
JavaBean のメソッド
ActionScript 関数
testBoolean
FlashExampleBean.testBoolean
testDate
FlashExampleBean.testDate
setMessage
FlashExampleBean.SetMessage
getMessage
FlashExampleBean.getMessage
setCount
FlashExampleBean.setCount
getCount
FlashExampleBean.getCount

ActionScript の考察

ActionScript で JavaBean のメソッドにアクセスする場合は、getService ActionScript 関数に、flashgateway.samples.FlashExampleBean のような完全修飾名を指定します。

フレームレベルで適用された次の ActionScript は、JavaBean のメソッドを呼び出します。

#include "NetServices.as"
#include "NetDebug.as"
// --------------------------------------------------
// ユーザーとの対話イベントハンドラ
// --------------------------------------------------
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);
}
// --------------------------------------------------
// サーバから受信するデータハンドラ
// --------------------------------------------------
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 = "ステータス:" + result.details;
}
function testDate_Result(result)
{
  flashDate = result;
  dateOutput.text = " " + flashDate;
}
function testDate_Status(result)
{
  dateOutput.text = "ステータス:" + result.details;
}
// --------------------------------------------------
// アプリケーションを起動します。
// --------------------------------------------------
if (inited == null)
{  
  // このコードは 1 度だけ実行します。
  inited = true;
  // デフォルトゲートウェイ URL を設定します (オーサリングで使用するため)。
    NetServices.setDefaultGatewayUrl("http://localhost:8200/
flashservices/gateway");
  // ゲートウェイに接続します。
    gatewayConnnection = NetServices.createGatewayConnection();
  // 自身の HTTP セッションの JavaBean へのリファレンスを取得します (初めて新規のリ
ファレンスを作成します)。
    flashtestService = 
gatewayConnnection.getService("flashgateway.samples.FlashJava
Bean", this);
  flashDate = new Date();
  messageInput.text = "[Enter a Message]";
  dateInput.text = "" + flashDate;
}

Flash による EJB アプリケーションへのアクセス

他の EJB とまったく同じように Salsa 用の EJB を作成します。Salsa を通じて示された EJB メソッドにアクセスするには、getService 関数で JNDI EJB 名を使用します。次の ActionScript の例では、Flash ムービーでユーザーが行う選択に応じて、2 種類の EJB を呼び出します。

function getBiggerString()
{
  if (useStateless.selected) 
  {
    flashStatelessEJB.getBiggerString( stringInput.text );
    stringOutput.text = "[ステートレス EJB を呼び出し中...]";
  }
  else 
  {
    flashStatefulEJB.getBiggerString( stringInput.text );
    stringOutput.text = "[ステートフル EJB を呼び出し中...]";
  }
}

EJB アプリケーションと Flash を併用する場合、ActionScript 関数は EJBHome メソッドと EJBObject メソッドにマッピングされます。

ActionScript の考察

次の ActionScript の例では、EJB メソッドを呼び出し、結果を Flash に表示します。

#include "NetServices.as"
#include "NetDebug.as"
// --------------------------------------------------
// ユーザーとの対話イベントハンドラ
// --------------------------------------------------
function runExample()
{
  // 最初に選択された EJB タイプへのリファレンスを取り出します。
  getEJBs();
}
// ---------------------------------------------------
// ビジネスメソッド
// ---------------------------------------------------
function getBiggerString()
{
  if (useStateless.selected) 
  {
    flashStatelessEJB.getBiggerString( stringInput.text );
    stringOutput.text = "[ステートレス EJB を呼び出し中...]";
  }
  else 
  {
    flashStatefulEJB.getBiggerString( stringInput.text );
    stringOutput.text = "[ステートフル EJB を呼び出し中...]";
  }
}
function getBiggerInt()
{
  if (useStateless.selected) 
  {
    flashStatelessEJB.getBiggerInt( (new 
Number(numInput.getSelectedItem().data)).valueOf() );
    stringOutput.text = "[ステートレス EJB を呼び出し中...]";
  }
  else 
  {
    flashStatefulEJB.getBiggerInt( (new 
Number(numInput.getSelectedItem().data)).valueOf() );
    stringOutput.text = "[ステートフル EJB を呼び出し中...]";
  }                          
}
// --------------------------------------------------
// サーバから受信するデータハンドラ
// --------------------------------------------------
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();
}
// このメソッドは、create を呼び出し中にエラーが発生した場合に呼び出されます。
function create_Status( result )
{
  stringOutput.text = result.details;  
}
function getEJBs() 
{
  // EJB へのリファレンスを取得します。
  // ステートレス:
  if (useStateless.selected) 
  {
    flashstatelessHome = 
gatewayConnection.getService("FlashSampleStatelessEJB", 
this);
    flashstatelessHome.create();
  }
  // ステートフル:
  else
  {
    if (flashstatefulHome == undefined)
    {
        flashstatefulHome = 
gatewayConnection.getService("FlashSampleStatefulEJB", this);
      flashstatefulHome.create( "stateful message" ); 
    }
    else
    {
      getBiggerString();
      getBiggerInt();
    }
  }
  
}
// --------------------------------------------------
// アプリケーションを起動します。
// --------------------------------------------------
if (inited == null)
{  
  // このコードは 1 度だけ実行します。
  inited = true;
  // デフォルトゲートウェイ URL を設定します (オーサリングで使用するため)。
    NetServices.setDefaultGatewayUrl("http://localhost:8200/
flashservices/gateway");
  // ゲートウェイに接続します。
    gatewayConnection = NetServices.createGatewayConnection();
  // カスタムの認証と認可用のセキュリティ情報を設定します。
  //gatewayConnection.setCredentials("Flash", "Flashpass");
  stringInput.text = "[Enter a Word]";
  numInput.setSelectedIndex(0);
}

JMX と Flash の併用

Flash を使用して JMX を通して JRun アプリケーションを呼び出すには、MBean メソッドを呼び出します。ActionScript の getService 関数で MBean オブジェクト名を使用して、JRun の JMX オブジェクトに接続します。

ActionScript の考察

次の ActionScript の例では、MBean メソッドを呼び出し、結果を Flash に表示します。

#include "NetServices.as"
#include "NetDebug.as"
// --------------------------------------------------
// ユーザーとの対話イベントハンドラ
// --------------------------------------------------
function runExample()
{
  jrunDeployerMBean.getServerName();
  jrunDeployerMBean.getEARs();
}
// --------------------------------------------------
// サーバから受信するデータハンドラ
// --------------------------------------------------
function getServerName_Result(result)
{
  serverName.text = result;
}
function getEARs_Result(result)
{
  numDeployed.text = result.length;
}
function onStatus(result)
{
  serverName.text = "status triggered";
}
// --------------------------------------------------
// アプリケーションを起動します。
// --------------------------------------------------
if (inited == null)
{  
  // このコードは 1 度だけ実行します。
  inited = true;
  // デフォルトゲートウェイ URL を設定します (オーサリングで使用するため)。
    NetServices.setDefaultGatewayUrl("http://localhost:8200/
flashservices/
gateway");
  // ゲートウェイに接続します。
    gatewayConnnection = NetServices.createGatewayConnection();
  // リモート MBean へのリファレンスを取得します。
  jrunDeployerMBean =
gatewayConnnection.getService("DefaultDomain:service=
DeployerService", this);
}