DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 20.17 Receiving Typed Objects with Java

20.17.1 Problem

You want to handle typed objects using a J2EE service function.

20.17.2 Solution

Use JavaBean classes that correspond to the ActionScript classes, and use the third-party ASTranslator class to perform conversions between datatypes.

20.17.3 Discussion

Unlike the .NET version, Flash Remoting for J2EE does not automatically convert typed ActionScript objects into corresponding custom Java types. Instead, the gateway converts the ActionScript objects into ASObject types. The flashgateway.io.ASObject class (included in flashgateway.jar) is a subclass of java.util.HashMap to which two additional methods (getType( ) and setType( )) have been added. The properties of the ActionScript object are stored as key/value pairs in the ASObject. You can create your own proprietary code to convert between an ASObject and the appropriate Java datatype. However, Carbon Five has already created an excellent utility named ASTranslator to do this for you.

ASTranslator is freely available for download from http://www.carbonfive.com/opensource-main-1.html. It converts between ASObject and custom Java datatypes using JavaBean-style introspection. Therefore, it requires that your custom Java datatypes adhere to the JavaBean rules (namely, they must implement java.io.Serializable, use get/set methods for all publicly accessible properties, and have a constructor that requires no parameters).

Here is how to use ASTranslator to receive typed objects and convert them to custom Java types:

  1. Download the ASTranslator.jar file and place it in your web application's WEB-INF/lib directory.

  2. Create a JavaBean class that corresponds to the registered ActionScript class. The JavaBean class should have the same name as the name to which the ActionScript class is registered. Also, the JavaBean class should have get and set methods for each property in the ActionScript class. Here is an example ActionScript class:

    function MyClass(a, b) {
      this.a = a;
      this.b = b;
    }
    MyClass.prototype.a;
    MyClass.prototype.b;
    
    Object.registerClass("MyClass", MyClass);

    Here is the code for the corresponding JavaBean class:

    public class MyClass implements java.io.Serializable{
    
      private string _a;
      private string _b;
    
      public MyClass(  ) {}
    
      public void setA(string a) {
        this._a = a;
      }
    
      public void setB(string b) {
        this._b = b;
      }
      
      public string getA(  ) {
        return this._a;
      }
    
      public string getB(  ) {
        return this._b;
      }
    
    }
  3. The Flash Remoting service function (servlet/JSP, Java class, etc.) should accept a parameter of type ASObject. Then you can convert the ASObject into a JavaBean using the fromActionScript( ) method of an ASTranslator object. Here is an example of a Java class method that does this:

    public void myMethod(ASObject aso) {
    
      // Create the ASTranslator instance.
      com.carbonfive.flash.ASTranslator ast = new 
                                              com.carbonfive.flash.ASTranslator(  );
    
      // Convert and cast the ASObject to a MyClass object using the
      // fromActionScript(  ) method.
      MyClass myCls = (MyClass) ast.fromActionScript(aso);
    
      // Now you can do something with the properly cast Java object.
    }

20.17.4 See Also

Recipe 20.20

    [ Team LiB ] Previous Section Next Section