BlazeDS adapter to invoke multiple classes on a Remoting destination

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🙂

9 Responses to BlazeDS adapter to invoke multiple classes on a Remoting destination

  1. Harish says:

    Damn! This thing rocks or what?

  2. Danilo says:

    Sound interesting. Well done!

  3. Would you like to contribute this to FlexServerLib (http://flexserverlib.googlecode.com) ?

  4. Sujit Reddy G says:

    Hi Andrew,

    Yes, I like to contribute.

  5. Dave says:

    This is a little off topic but being new to Flex/BlazeDS I have a question. What is the exact difference between scope=”session” and scope=”application”? It seems to me that what is missing is a scope that allows the client to create any number of server side object instances, each scoped to the life of the client object reference. Does this exist? If not why? (it seems like the most basic scope for a proxy).

  6. Sujit Reddy G says:

    Hi Dave,

    Value in the scope attribute defines instance life cycle. If you have your scope set to session, then when you send a Remoting request to this class only one instance of a class is used for a session of a user. If the scope is set to application, then the instance is used for requests any user. request is another value you can set to scope and is the default. In this case for each request a different instance of the class is created.

    Hope this helps.

  7. Dilip Reddy says:

    Hi Sujit, I am configuring flex app with blazeds.
    I developed two application with blazeds configuration.
    When I run these apps individually works fine, but if I run both the applications they are not working fine. May I think channels conflict(?). Can you give suggestion for me in this issue.

    Regards,
    Dilip

  8. Hi Dilip,

    Are you getting any error messages? If yes please share the error messages, that will help in finding out what might be going wrong.

    Hope this helps.

  9. Hi there may I reference some of the information from this blog if I reference you with a link back to your site?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: