Simon is an Adobe Community Expert, an Adobe Certified Instructor, and a Senior Web Developer for e-bim.com. Working on several high profile projects ranging from a start-up athlete skin care shopping cart application to a full blown CMS requiring multiple workflows for publishing and editing several types of content, Simon has used many different technologies including ColdFusion, Flex, InDesign, AIR, and Blaze DS. Constantly learning, Simon is always looking for new challenges and reports his findings to user groups and on his website. Simon has posted 3 posts at DZone. View Full User Profile

Merapi : A Bridge Between AIR and Java

12.09.2009
| 34091 views |
  • submit to reddit

The Java Steps

So first things first – create a new Java project in eclipse, and add the necessary jar files to the library.  The list of required jar files can be viewed in the screen shot below.

 

 After the project is created, create a file called magicEightBallRunner.  This is the main application file and should reside in the default package.  Once it has been created, add the following code:

import merapi.Bridge;
import handlers.questionHandler;

public class magicEightballRunner {

public static void main( String[] args )
{
Bridge.open();
new questionHandler();
}
}

In this code we import the bridge class and the questionHandler class.  We then open the bridge and create a new instance of the questionHandler, thus subscribing the application to the feed of messages in the bridge.  The same bridge is used for all Merapi applications, so the application will have access to all messages, including both those that relate to the application and those that don’t.  It is the questionHandler that selects which messages the application will access and which it will not.  For anyone who has prior experience with technologies such as Blaze or LiveCycle, this subscriber/ producer approach will seem familiar.  These two lines of code have set up the application to be a subscriber.

The next file that we must create is the handler.  As stated before, the handler class contains most of the logic for the application.  A handler is created for every type of message that the application is going to read, and inside the handler is a handleMessage function.  This function is called when the handler recognizes a message in the bridge that matches its criteria.  Another way to think of handlers is to think of them as event listeners.  The handler "listens" to the bridge, and acts when it encounters a message.

So create a handlers package, and create a class called questionHandler inside the package.  Then add the following code:

package handlers;

import messages.questionMessage;
import merapi.handlers.MessageHandler;
import merapi.messages.IMessage;
import java.util.Random;

public class questionHandler extends MessageHandler{
public questionHandler()
{
super( questionMessage.ASK_IT );
}

public void handleMessage( IMessage message )
{
if ( message instanceof questionMessage )
{
questionMessage qMessage = ( questionMessage ) message;
Random generator = new Random();
int randomIndex = generator.nextInt( 10 );

String answer = null;

switch(randomIndex){
case 1: answer = "As I see it, yes"; break;
case 2: answer = "It is decidedly so"; break;
case 3: answer = "Without a doubt"; break;
case 4: answer = "Reply hazy, try again"; break;
case 5: answer = "Better not tell you now"; break;
case 6: answer = "Concentrate and ask again"; break;
case 7: answer = "Don't count on it"; break;
case 8: answer = "Outlook not so good"; break;
case 9: answer = "Yes - definitely"; break;
case 10: answer = "You may rely on it"; break;

}
qMessage.setAnswer(answer);
qMessage.send();
}
}
}

This class extends the MessageHandler class that is contained in Merapi.  At the top of the class, we import the necessary libraries.  When creating a handler, it is important to import the matching message class so that you are able to access information, such as message type, from the class.  It is also important to import the class if you are planning on sending any messages.  You must also import the IMessage and messageHandler classes from Merapi.  In this example we are also importing the java.util.Random class, since we will be randomly choosing what response to provide.  Most likely you will not need this in other examples.

The first part of this class is the constructor.  In the constructor, we call the super function and pass a static variable from the questionMessage class.  This variable is a string stating what type of message this handler will handle.  The string value can be whatever you wish but must match the string value in the AIR application. Otherwise, the handler will never pick up the messages from the bridge.

The second part of this class is the handleMessage function.  As stated before, every handler must have a handleMessage function. This function accepts an IMessage, which is the message object from the bridge.  This IMessage is then cast as the message type the handler is handling.  Once you have the message variable in the format that you need, you can use whatever logic you need to achieve your goal.  If you were creating an application that communicated with some 3rd party API, you would place that logic here.  In the case of this example, we are generating a random number which we are using in a switch statement to select the response we want to provide.  We then set the answer value of the questionMessage object that we created earlier and call the send function.  Calling the send function sends the message back to the bridge, where it will wait to be picked up by the AIR application.

Now let's take a look at the message class.  Create a new package called messages, and in that package create a new class called questionMessage.  Inside the class put the following code:

package messages;
import merapi.messages.Message;

public class questionMessage extends Message{

public static final String ASK_IT = "magicQuestion";

public questionMessage()
{
super();
}

public String getQuestion() { return __question; }
public void setQuestion( String val ) { __question = val; }

public String getAnswer() { return __answer; }
public void setAnswer( String val ) { __answer = val; }

private String __question = null;
private String __answer = null;

}

As you can see, this is a very simple class.  A variable called ASK_IT is created (you may remember that this is the variable we call from the handler class) and is set to the value magicQuestion.  This is the value used to differentiate between the different message types that can be in the bridge.  In the constructor class we call the super function, and then we set up the getters and setters that are needed in the object.  In this example the object can store both a question and an answer and also retrieve said values.

Now that you have all the necessary files, run the application and make sure there are no errors.  If all goes well, the application will run, and nothing will really happen.  This is one of the few times where seeing nothing is a good thing.

Legacy
Published at DZone with permission of its author, Simon Free.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Andrew Sweeney replied on Sat, 2010/01/02 - 3:47pm

I am very new to Java programming, SVN and eclipse. I have done very little svn downloading. In the first page you say to download the code but it just takes me to a site with the code list. How do I go about actually downloading the links listed. Thanks Andy

J S replied on Sun, 2011/06/05 - 11:57am

Didn't your momma teach you to capitalize your Java/AS class names?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.