SpringBeanAdapter for Flex Flash Remoting

Alon Salant ·

The Spring Framework is a popular open source framework for managing enterprise Java application components. SpringBeanAdapter provides the ability to locate Flash Remoting services in a Spring application context. Additionally, it provides the ability for server-side developers to exercise custom control over the handling of server-side errors.

We have documented and released this implementation on our Sourceforge Open Source site.

SpringBeanAdapter is designed to be used with Macromedia Flash Remoting MX on any J2EE application server.
This adapter only works with versions of Flash Remoting that allow registering custom adapters, specifically, the version provided with Macromedia Flex.

SpringBeanAdapter is supported by Carbon Five.


Overview

Flash Remoting comes with a number of adapters for locating remote services implemented as Java classes, EJBs, servlets, and JMX Beans.

The Spring Framework is a popular open source framework for managing
enterprise Java application components.

SpringBeanAdapter provides the ability to locate Flash Remoting services in a Spring application context.

Features

  • Locate Remoting services by bean name in Spring application context
  • Customizable error handling of server-side exceptions

Download

The latest release of SpringBeanAdapter is available for download from SourceForge.net. For the adventurous, you can check out the latest changes from CVS.

Prerequisites

SpringBeanAdapter depends on:

Install

To install SpringBeanAdapter you must unzip springadapter-x.x.zip and copy the following jars to your web application’s WEB-INF/lib/ directory:

  • lib/springadapter-x.x.jar

Configure Flash Remoting to use SpringBeanAdapter by editing your Flex Flash Remoting configuration file located in WEB-INF/flex/gateway-config.xml.

<service-adapters>
  <adapter type="stateless-class">com.carbonfive.flash.spring.SpringBeanAdapter</adapter>
</service-adapters>

Usage

SpringBeanAdapter handles services named with a ‘spring://’ prefix. We recommend using named remote services in Flex by adding services to your Flex configuration in WEB-INF/flex/flex-config.xml:

The following is just a sample from a flex-config.xml file:

<remote-objects>
  <named>
    <object name="LocalServiceName">
      <source>spring://RemoteServiceName</source>
      <type>stateless-class</type>
      <allow-unnamed-access>false</allow-unnamed-access>
    </object>
  </named>
</remote-objects>

With this configuration, you can then use remote object services from your Flex application with:

<mx:RemoteObject id="serviceName" named="LocalServiceName"/>

Error Handling

SpreadBeanAdapter provides a nice feature for developers who want control over how errors that happen on the server are sent to the Flash client.

In the standard adapters provided by Macromedia, server errors are converted to a class, flashgateway.GatewayException, that contains the same properties as the fault object received by the Flash client:

  • type
  • code
  • message
  • details

SpringBeanAdapter allows you to provide a factory class to produce your own GatewayExceptions when an error occurs on the server. To use this feature, register a bean that implements the interface com.carbonfive.flash.spring.RemotingExceptionFactory with the name ‘remotingExceptionFactory’ in your Spring application context. SpringBeanAdapter will look for and use this class if found.

An example implementation of RemotingExceptionFactory looks like:

public class ExampleExceptionFactory
  implements java.io.Serializable, RemotingExceptionFactory
{
  public static String CUSTOM_CODE = "Server.Custom";

  public void throwException(Throwable t) throws Exception
  {
    if (t instanceof SecurityException)
    {
      GatewayException e = new GatewayException(t.getMessage());
      e.setRootCause(t);
      e.setCode(GatewayException.SERVER_AUTHORIZATION);
      throw e;
    }
    else if (t instanceof IndexOutOfBoundsException)
    {
      GatewayException e = new GatewayException(t.getMessage());
      e.setRootCause(t);
      e.setCode(CUSTOM_CODE);
      throw e;
    }

    if (t instanceof Exception) throw (Exception) t;
    else if (t instanceof Error) throw (Error) t;
  }
}

If you do not convert the exception to a GatewayException in your implementation of RemotingExceptionFactory, it will be handled by the standard Flash Remoting exception handling process.