Friday, November 16, 2007

Cairngorm and the Framework

People have been asking me to show them how to use Cairngorm with the AWX Framework that is loosely based on OSGi. The interaction will most likely come in an ICommand implementation or a Delegate.  Here is an example that shows how to use the Find Service.
First, make sure that you have registered with the Framework the FindActivator.
<Framework apiKey="[your-api-key]">
  <FindActivator/>
</Framework>
Next, since a Command is always created and executed by the CairngormEventDispatcher,  you can always get a reference to the Framework through its static function getInstance() and then get a reference to systemBundleContext which is an IBundleContext implementation.

package com.esri.awx
{
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    import com.esri.aws.osgi.framework.Framework;
    import com.esri.aws.osgi.framework.IBundleContext;
    import com.esri.aws.osgi.framework.IServiceReference;
    import com.esri.aws.services.IFind;
   
    import flash.utils.getQualifiedClassName;
   
    import mx.collections.ItemResponder;

    public class FindCommand implements ICommand
    {
        public function execute(event:CairngormEvent):void
        {
            var findEvent : FindEvent = event as FindEvent;
            var context : IBundleContext = Framework.getInstance().systemContext;
            var ref : IServiceReference = context.getServiceReference(getQualifiedClassName(IFind));
            if( ref )
            {
                var find : IFind = context.getService( ref ) as IFind;
                find.findLocation( findEvent.location, null, new ItemResponder(
                function( data : Object, token : Object = null) : void {
                    // do something with data
                },
                function( info : Object, token : Object = null) : void {
                    // do something with info
                }
                ));
            }
        }
    }
}

And here is the FindEvent class:

package com.esri.awx
{
    import com.adobe.cairngorm.control.CairngormEvent;

    public class FindEvent extends CairngormEvent
    {
        public static const FIND : String = "find";
        
        public var location : String;
        
        public function FindEvent(
            type:String,
            bubbles:Boolean=false,
            cancelable:Boolean=false
        )
        {
            super(type, bubbles, cancelable);
        }
        
    }
}

Blogged with Flock

No comments: