package com.ericfeminella.air.cairngorm.business
{
import com.adobe.cairngorm.CairngormError;
import com.adobe.cairngorm.CairngormMessageCodes;
import com.adobe.cairngorm.business.ServiceLocator;
import mx.core.Application;
import mx.events.FlexEvent;
/**
*
* <code>AIRServiceLocator</code> provides a mechanism from which AIR APIs,
* specifically <code>SQLConnection</code>, <code>SQLStatement</code>) can
* be located (referenced) from the Cairngorm <code>ServiceLocator</code>
* as a Service via <code>SQLService</code>.
*
* <p>
* <code>AIRServiceLocator</code> allows for Adobe AIR specific APIs to be
* retrieved from a Cairngorm <code>ServiceLocator</code> implementation in the
* same way one would access RPC Services such as <code>HTTPService</code>,
* <code>WebService</code> etc. from the <code>ServiceLocator</code>
* </p>
*
* @example The following example demonstrates a typical implementation of
* <code>AIRServiceLocator</code>
*
* <listing version="3.0">
*
* <?xml version="1.0" encoding="utf-8"?>
* <services:AIRServiceLocator xmlns:mx = "http://www.adobe.com/2006/mxml"
* xmlns:services = "com.ericfeminella.air.cairngorm.business.*\" >
* <mx:Script>
* <![CDATA[
* import mx.events.FlexEvent;
*
* public static const RDBMS:String = "rdbms";
*
* protected override function preinitialize(event:FlexEvent) : void
* {
* rdbms.open();
* }
* ]]>
* </mx:Script>
*
* <services:SQLService id = "rdbms"
* localDatabaseFilePath = "{ File.desktopDirectory.nativePath }"
* localDatabaseFileName = "air.cairngorm.example.db"
* />
*
* </services:AIRServiceLocator>
*
* </listing>
*
* @see com.adobe.cairngorm.business.ServiceLocator
*
*/
public class AIRServiceLocator extends ServiceLocator
{
/**
*
* Defines the Singleton instance of <code>AIRServiceLocator</code>
*
*/
protected static var instance:AIRServiceLocator;
/**
*
* Defines the <code>SQLServices</code> instance which contains a reference
* to all <code>SQLService</code> defined on a <code>AIRServiceLocator</code>
* instance
*
*/
protected var services:SQLServices;
/**
*
* <code>AIRServiceLocator</code> constructor Instantiates the Singleton
* instance of <code>AIRServiceLocator</code> and adds an listener for the
* <code>FlexEvent.PREINITIALIZE</code> event
*
* @throws <code>CairngormMessageCodes.SINGLETON_EXCEPTION</code>
*
*/
public function AIRServiceLocator()
{
if ( instance != null )
{
throw new CairngormError( CairngormMessageCodes.SINGLETON_EXCEPTION, "AIRServiceLocator" );
}
instance = this;
Application( Application.application ).addEventListener( FlexEvent.PREINITIALIZE, preinitialize );
}
/**
*
* Retrieves the Singleton instance of the <code>AIRServiceLocator</code>
*
* @return singleton instance of <code>AIRServiceLocator</code>
*
*/
public static function getInstance() : AIRServiceLocator
{
if ( instance == null )
{
instance = new AIRServiceLocator();
}
return instance;
}
/**
*
* Retrieves a specific <code>SQLService</code> which has been defined
* by the <code>AIRServiceLocator</code> instance
*
* @example The following example demonstrates how a <code>SQLService</code>
* instance can be referenced via <code>AIRServiceLocator.getSQLService</code>
*
* <listing version="3.0">
*
* var service:SQLService;
* service = AIRServiceLocator.getInstance().getSQLService( Services.SERVICE );
*
* </listing>
*
* @param unique identifier of the <code>SQLService</code> instance
* @return <code>SQLService</code> instance with the specified name
*
*/
public function getSQLService(name:String) : SQLService
{
if ( services == null )
{
services = new SQLServices();
services.register( this );
}
return SQLService( services.getService( name ) );
}
/**
*
* Determines if the specified <code>SQLService</code> has been defined
* on a <code>AIRServiceLocator</code> instance
*
* @param unique identifier of the <code>SQLService</code> instance
* @return true if the service exists, otherwise false
*
*/
public function hasSQLService(name:String) : Boolean
{
return getSQLService( name ) != null;
}
/**
*
* Abstract method which is invoked when <code>Application.preinitialize</code>
* event is dispatched.
*
* <p>
* This method is provided in order to allow clients to override in order to
* initialize services at startup of the application.
* </p>
*
* @param <code>FlexEvent.PREINITIALIZE</code>
*
*/
protected function preinitialize(event:FlexEvent) : void
{
}
}
}