I need to read the URL that the browser shows when a Flex application is called because I would to reference it in a mxml configuring Cairngorm remote objects.
The goal I would reach is to automatically configure Cairngorm services from environment to environment (dev,test,qa,prod) without statically set the value in the mxml or other ActionScript. Since the Flex client is deployed in the root of the war of the webapp, it's enough to read where the browser is pointing.
I have written a class that is doing so:
public class ConfigServer {
public function ConfigServer() {
var loaderUrl:String = FlexGlobals.topLevelApplication.loaderInfo.loaderURL;
var urlToSet:String = <loaderURL-string-manipulation>;
_serverUrl = urlToSet;
}
private var _serverUrl:String = '';
public function get serverUrl():String
{
return _serverUrl;
}
}
In my mxml I would do so:
<mx:Script>
<![CDATA[
import org.fao.fapda.util.ConfigServer;
private var configuration:ConfigServer = new ConfigServer();
]]>
</mx:Script>
<mx:RemoteObject
id="userService"
destination="userService"
endpoint= "{configuration.serverUrl}/messagebroker/amf"
showBusyCursor="true"
requestTimeout="100"
/>
But whenever I call the ConfigServer constructor and for every (known to me) technique I applied (statics or singletons or public ro so on), I have always had the same error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at org.fao.fapda.util::ConfigServer()[C:\dev\workspaces\FAPDA\trunk\FAPDA-client\src\org\fao\fapda\util\ConfigServer.as:8]
Cairngorm services initialization is done as follow:
<fx:Declarations>
<cut/>
<services:FAPDAServices id="services"/>
<cut/>
</fx:Declarations>
and the problem is that FAPDAServices.mxml is read runs before FlexGlobals is valid...
Is there a point in the Flex Application lifecycle where such loaderURL is defined so that I can construct ConfigServer? When in startup events that initialization in done?
I confess I'm a Flex epic rookie, so it's possible I'm completely wrong on this.
Best regards