BlazeDS adapter to invoke multiple classes on a Remoting destination

January 22, 2009

What is this?

In BlazeDS and LCDS Remoting service, JavaAdapter class allows us to invoke methods on a Java object.  If we want to invoke methods on a Java class, we declare destination for each class we want to be invoked from Flex applications in remoting-config.xml.

I extended the default JavaAdapter and added very very few lines of code so that Flex application can invoke Java classes by specifying the Java class name in the Flex application. Don’t worry, modified adapter allows you to control access to classes using regular expression and other ways.

This will be useful if you have lots of classes, which you want expose as service. Instead of declaring destinations for each class in the remoting-config.xml file, you can use this adapter and expose all required classes by declaring one destination.

What does this adapter let me do?

1.       Specify the name of the Java class in Flex application and invoke methods on that class

2.       You can control access to classes based on regular expression. If you want to allow access to classes in “com.adobe” package only, you can set the source of the destination to “com.adobe.*”.

3.       Declare a default Java class, which will be used if Flex application doesn’t specify the Java class to be used.

4.       From the list of classes allowed in a package, you can exclude few classes

5.       You can specify scope for each class. For example you want instances of 2 classes in the allowed package to be stored in a session or application scope, you can do that.

How to use this adapter?

Adding adapter class to your web application classpath

Download the MultiClassJavaAdapter .java file from this URL http://sujitreddy.g.googlepages.com/MultiClassJavaAdapter.java

Compile it and copy that under WEB-INF/classes folder of your web application in appropriate package structure.

Declaring in remoting-config.xml

The remoting-cong.xml file is explained below. You can download the completed remoting-config.xml file from this URL http://sujitreddy.g.googlepages.com/remoting-config.xml

In the configuration file (remoting-config.xml) file downloaded, you can find we have added our adapter (com.adobe.remoting.adapters.MultiClassJavaAdapter) to the adapters list as shown in the XML snippet below.

<adapters>

<adapter-definition id=”any-java-object” class=”com.adobe.remoting.adapters.MultiClassJavaAdapter”/>

</adapters>

We have destination with id “AnyJavaClass” declared. This destination has its adapter set to the adapter added above using the adapter element.

<destination id=”AnyJavaClass”>

<adapter ref=”any-java-object”/>

</destination>

You can give a regular expression in the configuration file, which will be used to evaluate if a Java class can be invoked by the Flex application on this destination. You will use source property to specify this regular expression as shown below.

You can declare a default Java class, which will be used if the Flex application doesn’t specify the name of the Java class to use. Set the default Java class name using default-source element as shown below.

<destination id=”AnyJavaClass”>

<properties>

<source>com.adobe.*</source>

<default-source>MultiClassAdapterTest</default-source>

</properties>   </destination>

As per declaration above, Flex application can invoke any class under “com/adobe” folder. You might want not want to allow access for few classes in this package. In that case you can declare list of classes you don’t want to exclude as shown below.

<destination id=”AnyJavaClass”>

<properties>

<exclude-classes>

<class name=”com.adobe.ExcludeClass1″/>

<class name=”com.adobe.ExcludeClass2″/>

</exclude-classes>

</properties>    </destination>

By default when there is a request to invoke method on a class, a new instance of that class is created in the request scope. You might want to keep the instances created in application or session scope. Since we are allowing multiple classes to be invoked using single destination, you can declare names of the classes and scope in which you want to store them as shown below.

<destination id=”AnyJavaClass”>

<properties>

<classes-scopes>

<class name=”MultiClassAdapterTest” scope=”session”/>

<class name=”com.adobe.TestClass” scope=”application”/>

</classes-scopes>

</properties>    </destination>

Flex application to test the adapter

Download the MXML file from this URL http://sujitreddy.g.googlepages.com/MultiClassAdapterTesting.mxml

In the file downloaded, you can see that we will use the source property of the RemoteObject class to specify the name of the class to invoke the method on.

That’s it, now you can declare one destination and invoke methods on multiple classes. Still you can control access to classes.

Please feel free to use/edit/delete this class. Any suggestions are most welcome. 🙂

Adobe rocks 🙂

Advertisements