Flash Remoting Mx and Java connection

Tomcat 5 + Flash Remoting Mx 2004 + Java + ASTranslator (Carbonfive)

Flash General Setup

1. Install the macromedia Flash mx 2004 program

2. Go to www.macromedia.com/software/flashremoting/downloads/components, download and install the Flash Remoting Components. This step will modify the Flash IDE to support remoting.

3. Go to www.macromedia.com/downloads and download/install the Flash Remoting MX for J2EE. I installed it at c:\program files\macromedia\Flash Remoting MX\. During the installation i selected only to install flashgateway.war

4. Those are all the steps, now let's go to Tomcat.


TOMCAT Setup

1. Tomcat should already be configured and running. $TOMCAT_HOME is where your tomcat server is installed. Copy the file flashgateway.war (from the previous steps) into $TOMCAT_HOME/webapps. Then, you should probably restart tomcat. Then, in webapps a new folder must have been created called flashgateway, which is the resulting folder after tomcat automatically deployed flashgateway.war.

I am assuming that i am working in a project whose context is located at $TOMCAT_HOME/webapps/project1 and i want to expose one of it's classes using flash remoting.

2. To do that, copy the file $TOMCAT_HOME/webapps/flashgateway/WEB-INF/lib/flashgateway.jar to $TOMCAT_HOME/webapps/project1/WEB-INF/lib.

3. Now we have to modify the web.xml of our context, which is located at: $TOMCAT_HOME/webapps/project1/WEB-INF/web.xml. Add the following lines where they should go:

<servlet>
    <servlet-name>FlashGatewayServlet</servlet-name>
    <display-name>Flash Remoting Servlet</display-name>
    <description>Servlet-based plugin to Flash Remoting</description>
    <servlet-class>flashgateway.controller.GatewayServlet</servlet-class>
    <load-on-startup>10</load-on-startup>
</servlet>

  
<servlet-mapping>
    <servlet-name>FlashGatewayServlet</servlet-name>
    <url-pattern>/gateway</url-pattern>
</servlet-mapping>

What those lines do is to make Flash remoting available for your context at: http://localhost:8080/project1/gateway, which will be the url that flash needs to know (later).

Tomcat 5 Setup of ASTranslator

There is one class, developed by Carbonfive, called the ASTranslator, which has some extra features when sending objects from java and Flash and viceversa. To install this class go to http://carbonfive.sourceforge.net/astranslator and follow the instructions. When you donwload the zip file ( i got one zip file with the sources) you will probably have to deploy it to obtain the astranslator-X.X.X.jar. That means compiling the .java files and jarring them using the java jar.

Follow the instructions there and copy the following jars to $TOMCAT_HOME/webapps/project1/WEB-INF/lib

-astranslator-1.5.8.jar (the one you deployed)
-commons-beanutils-1.7.0.jar
-commons-collections-3.1.jar
-commons-logging-1.0.4.jar

Although ASTranslator is not necessary to begin using remoting i am using it for some examples, so if you don't want to install it just comment out the code(below) that refers to it.


First Example, sending Objects from Java to Flash Mx 2004 using remoting.

-Well, i assume that my project context is located at:

$TOMCAT_HOME/webapps/project1/


- I also assume that i have a package structure of the form com.tungra, whose classes reside at $TOMCAT_HOME/webapps/project1/WEB-INF/classes, in the appropiate folders.
-I will be using the com.tungra.Player and com.tungra.Service classes.

com.tungra.Player code (this is the object i will pass to flash)

------------------------------------------------------------------------------

package com.tungra;


import java.io.Serializable;

public class Player implements Serializable{


  private String name;
  private int age;

  private double height;

  public Player(String e){
    name = e;
    age = 11;

    height = 5.3;
  }

  public String getName()
  {
    return name;
  }
  public int getAge(){

    return age;

  }



}


		  

------------------------------------------------------------------------------

now, here is the com.tungra.Service code:

------------------------------------------------------------------------------

package com.tungra;

import flashgateway.io.ASObject;


import com.carbonfive.flash.ASTranslator;  // Only if you want to use ASTranslator!

import java.io.*;

public class Service
{


/* Returns a string to Flash */

public String getString()
{

  return "string returned";
}

/* Returns a int to flash */

public int getNumber (){

  return 1234;
}

/* Returns an object using ASObject */
// Comment out this method  if you dont't want to use ASTranslator!

public ASObject getPlayer(String received )
{

 ASTranslator translator = new ASTranslator();
 ASObject aso = (ASObject) translator.toActionScript( new Player( received + "modified by java" ) );

 return aso;
}


public Player getPlayer2(){

 return new Player("Sonya");
}


}

		  

Well, restart tomcat if necessary for the changes to take effect.

 

The Action Script to receive the java object.
#include "NetServices.as"
	  
// This is for flash remoting debugging. The debugging information can be seen 
// by showing the NetConnectionDebugger from Window>Other Panels> NetConnectionDebugger
#include "NetDebug.as"  


NetServices.setDefaultGatewayUrl("http://localhost:8080/project1/gateway");
var gatewayConnection = NetServices.createGatewayConnection();
var service = gatewayConnection.getService("com.tungra.Service", this);

// Use Flash REmoting to actually connect to the J2EE server (tomcat) and
// call the methods
service.getNumber(); service.getString(); service.getPlayer("Peter "); service.getPlayer2(); /* //General Responder function this.onResult = function (result) { // player is an ActionScript object of type Player trace ( result ); _root.text1.text = "this is " + result; } */ /* Responders for getString */ function getNumber_Result( result ){ _root.text2.text = result; } function getNumber_Status(error){ trace("there was an error " + error.description ); } /* Responders for getString */ function getString_Result( result){ trace ( "string received + result); } function getString_Status(error){ trace("there was an error " + error.description ); } /* Responders for Player object (if using ASTranslator, otherwise uncomment this and the above method call*/ // We see here that result.height is undefined because is a private method of the Player class. This is one // of the advantages of using ASTranslator, it only show those properties which has getters/setters, like // age or name, and hides those properties which are private, preserving the object encapsulation. function getPlayer_Result( result){ trace ( "Player one's name: " + result.name + " , age " + result.age + ", height: " + result.height); } /* Responders for Player object 2 */ // Here we are not using ASTranslator, so the height property, although is private, can be accessed from Flash!!. function getPlayer2_Result( result){ trace ( "Player two " + result.name + ", height: " + result.height); }

Everytime you call a service function, like service.getNumber(), you can have a responder function which is a function that is called when flash remoting receives a response from the J2ee server. For instance, for the method getNumber we have the responder method getNumber_Result( result). Also, we have a responder function to deal with possible errors, by appending _Status: getNumber_Status( error) { ... }