Flerry: Flex-Java bridge for Adobe AIR 2.0

2010 January 13
tags: , ,
by Piotr Walczyszyn

Today I published Flerry on Google Code, a Flex-Java bridge that uses NativeProcess API from Adobe AIR 2.0. Using it is very simple and similar to RemoteObject API in Flex. In fact I reused libraries from BlazeDS to do AMF de/serialization on Java side and on Flex side classes like AsyncToken, RemotingMessage, AcknowledgeMessage and ResultEvent/FaultEvent for eventing.

Here is short snippet how you can use it directly in MXML:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="300" minHeight="200" xmlns:flerry="net.riaspace.flerry.*">
    <fx:Script>
        <![CDATA[
            import flash.events.MouseEvent;
 
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
 
            protected function nativeObject_faultHandler(event:FaultEvent):void
            {
              Alert.show(event.message.toString());
            }
 
            protected function addMethod_resultHandler(event:ResultEvent):void
            {
              txtResult.text = event.result.toString();
            }
 
            protected function btnAdd_clickHandler(event:MouseEvent):void
            {
              nativeObject.add(parseInt(txtValue1.text), parseInt(txtValue2.text));
            }
            ]]>
    </fx:Script>
    <fx:Declarations>
 
    <!-- Declaring NativeObject that points to 
            net.riaspace.flerrydemo.MyJavaObject class in ./jars/flerry-demo.jar -->
    <flerry:NativeObject id="nativeObject" binPath="./jars/flerry-demo.jar" 
            source="net.riaspace.flerrydemo.MyJavaObject"
            fault="nativeObject_faultHandler(event)">
 
      <!-- Declaring NativeMethod with custom result handler, for this example 
              actually result from NativeObject could be used -->
      <flerry:NativeMethod id="addMethod" name="add" 
            result="addMethod_resultHandler(event)" />
 
    </flerry:NativeObject> 
 
  </fx:Declarations>
 
  <s:HGroup verticalAlign="middle" textAlign="center" horizontalCenter="0" verticalCenter="0">
    <s:TextInput id="txtValue1" text="2" width="30" />
    <s:Label text="+" width="30" />
    <s:TextInput id="txtValue2" text="3" width="30" />
    <s:Button id="btnAdd" label="=" width="30" click="btnAdd_clickHandler(event)" />
    <s:TextInput id="txtResult" width="30" editable="false" />
  </s:HGroup>
 
</s:WindowedApplication>

Some NativeObject properties:

  • binPath – points to jar file with your Java application
  • source – points to java class that this NativeObject maps to
  • singleton – sets source class object as singleton
  • startupInfoProvider – custom implementation of net.riaspace.flerry.IStartupInfoProvider, by default Flerry comes with JavaStartupInfoProvider that tries to detect where Java is installed on users machine, in other case Error is thrown

In following posts I will explain in details how it all works, for now you can download flerry Flash Builder project with all the source code from here.

59 Responses leave one →
  1. 2010 March 2
    Prashant permalink

    oh! thats great…

    ofcourse I (with some of my colleagues) would like to help you with that.
    and we will surely need your guidance throughout that and documentation support also.

    but simultaneously we also have to complete our peoject within 30-40 days.

    so what according to you, “should we implement it for our project also?
    or should we just find out some other way for our project and implement it without any context to our project ?”

    and what we would have to do now to help you?

  2. 2010 March 2

    I’m not quite surehow/why this is related to that “Error #2006: The supplied index is out of bounds.” error. But I get that error any time my Java code used Log4J. If I remove all calls to Log4J, the return value of my Java method is received without error in my AIR application. Interestingly, with the Log4J calls in place, the Java method still runs completely without error. The only error seems to be in the receipt of the return value.

  3. 2010 March 2
    Sangita permalink

    Hi Piotr

    I have installed Java on C:\Pogram Files\Java\jre1.6.0_01
    waiting for ur reply

    Thanks and Regards
    Sangita

  4. 2010 March 2

    This may be useful to others having errors. One thing that I fought with for a couple of hours was that, you need to make sure that you update the classpathTemplate to include any additional jar files that your Java classes require. For example, my application calls ANT from my Java code. In order to do that, I put the required ant jar files in the “jars” directory of my AIR app thinking that any jars in that directory would automatically get passed to the java executable as part of the classpath argument. That classpath argument is generated in the JavaStartupInfoProvider class. By default, the NativeObject creates a JavaStartupInfoProvider for you, but it only includes a few jar files on the classpath (flerry.jar, flex-messaging-core.jar and flex-messaging-common.jar). In order to make sure all my jar files were added to the classpath, I added some initialization code to my application to create my own JavaStartupInfoProvider instance in which I specify the classpathTemplate by looking at all jar files in the jars directory. Then I set the my instance into startupInfoProvider property of my NativeObject.

    Here is my init method:

    private function init() : void {
    //build classpath string – right now assumes that there are only .jar files in this directory
    var jarsDir:File = File.applicationDirectory.resolvePath(“jars”);
    var jars:Array = jarsDir.getDirectoryListing();
    classPath = “”;
    for (var i:int = 0; i < jars.length; i++) {
    classPath += "./jars/" + File(jars[i]).name + "{0}";
    }

    classPath += "{1}";

    var startupProvider:JavaStartupInfoProvider = new JavaStartupInfoProvider();
    startupProvider.classpathTemplate = classPath;
    nativeObject.startupInfoProvider = startupProvider;
    }

  5. 2010 March 2
    Sangita permalink

    Hi Piotr

    Thanks for ur help.

    I was able to get rid of the problem.

    Problem was related to jre.Actualy there was jdk1.5 and jdk 1.6 both installed.

    I have removed the earlier versions and have installed jre 1.6.

    After this I updated the flerry and imported the file again.

    Thanks and Regards
    Sangita

  6. 2010 March 4
    Sangita Patel permalink

    Hi Piotr

    I wanted to know how NativeObject.as in Flerry side is mapped with NativeObject.java

    I wanted to know the exact flow and where the constructor of NativeObject.java is called and how its members are initialised

    Waiting for ur Reply

    Thanks and Regards
    Sangita

Trackbacks & Pingbacks

  1. Java-AS3 serialization with AMF | Space of Flex/AIR technologies
  2. 使用Merapi来在AIR和Java中通信 | Color Hook
  3. MikeCann.co.uk » Blog Archive » On the Bleeding Edge

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS