Mind the Flex

Using the GraniteDS and Cairngorm together

April 13th, 2008 | Posted by Martin
Filed under Flex, Java, Spring |

I have been using the Cairngorm framework in my application from the day I started to develop it. It has helped me a lot in understanding the Flex as it is somewhat different that is done in Java. At some point I wanted to start using the GraniteDS because of Spring but at the same time I didn't want to give up Cairngorm. To make the co-existence of these two great frameworks possible, I changed the initial Services.mxml that looked like this:

<?xml version="1.0" encoding="utf-8"?>
<cairngorm:ServiceLocator
   xmlns:mx="http://www.macromedia.com/2003/mxml"
   xmlns:cairngorm="http://www.iterationtwo.com/cairngorm">
    <mx:RemoteObject
    	id="productService"
    	destination="product-service"
	showBusyCursor="true"/>
</cairngorm:ServiceLocator>

to this:

<?xml version="1.0" encoding="utf-8"?>
<cairngorm:ServiceLocator
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:cairngorm="com.adobe.cairngorm.business.*"
    xmlns:granite="org.granite.rpc.remoting.mxml.*">
   
    <granite:SecureRemoteObject
        id="productService"
        destination="product-service"
        showBusyCursor="true"/>
</cairngorm:ServiceLocator>

This allows me to keep querying the remote service as before:

 productService=ServiceLocator.getInstance().getRemoteObject("productService");

but in addition to that you got the possibility to use the SecurityEvent

productService.addEventListener(SecurityEvent.ALL, onSecurityEvent);   

in onSecurityEvent method as below. This is pretty useful if you are using Acegi secured Spring service beans as this way you get the correct event from authentication or authorization event and don't have to deal with parsing the usual event message by yourself.

public function onSecurityEvent(event:SecurityEvent):void {                           
  switch (event.type) {
   case SecurityEvent.INVALID_CREDENTIALS:
     model.loginStatusMessage = "Invalid username or password";
     break;
   case SecurityEvent.NOT_LOGGED_IN:
     model.loginStatusMessage = "Not logged in";
     break;
   case SecurityEvent.SESSION_EXPIRED:
    model.loginStatusMessage = "Session expired";
    break;
   case SecurityEvent.ACCESS_DENIED:
    Alert.show("You don't have required rights to execute this action");
    break;
  }
}       

2 Responses to “Using the GraniteDS and Cairngorm together”

  1. jarek Says:

    Hi,
    i am having very serious problem while using method you proposed. I think the problem is in the way I get SecureRemoteObject in delegate, i am using code like:
    service = SecureRemoteObject(ServiceLocator.getInstance().getRemoteObject( “testService”));
    (…) doing mapping of fault i result
    service.addEventListener(SecurityEvent.ALL, onSecurityEvent);
    the problem is that every event related to credentials is mapped to FaultEvent(mx.rpc.events.ResultEvent) and NOT the SecurityEvent(org.granite.events.SecurityEvent), i Think this is some related to the way we get SecureRemoteObject, from ServiceLocator from Cairngorm and not direct creating of SecureRemoteObject (from granite namespace). Would you be so kind, and public some simple and WORKING delegate code? I think this will explain difference between my situation (not working) and your situation(working). Thanks in advance

  2. Using the GraniteDS and Cairngorm together « dailytuts.net Says:

    […] See the original post here:  Using the GraniteDS and Cairngorm together […]

Leave a Reply