Search code examples
javaapache-flexweb-applicationsconfigurationblazeds

Externalizing client ChannelSet configuration for Flex/Java web application


I am looking for an approach that will allow me to (somehow) dynamically pass the server name, server port, and web context to my Flex client so it can create a ChannelSet for it's RemoteObjects to use. These three properties are, of course, readily available to my Java server-side code so I just need a way to get them to the client.

By default, Adobe says you should compile your Flex application against the server configuration file "services-config.xml". This is a highly inflexible practice that Spring says should be avoided (I agree).

One popular approach is to use Flex's http service to download an XML configuration file. I like this idea, but I don't want to hard-code an XML file and keep it inside my WAR file. Is there a way to dynamically generate this from Java code?

Another idea I had is to somehow use flashvars to pass the properties in from the containing HTML page to the SWF file. But again, I don't want to hard code them into the HTML page. Is there a way (maybe with Javascript?) to dynamically set the value of these when the page loads?


Solution

  • This is how I do it. I hope you'll find it useful:

    public static function getRemoteObject(destination:String, channelName:String,
        showBusyCursor:Boolean=true):RemoteObject{
        var remoteService:RemoteObject=new RemoteObject(destination);
        var channelSet:ChannelSet=new ChannelSet();
        var url:String = Application(Application.application).url;
        var secure:Boolean = URLUtil.isHttpsURL(url);
        var protocol:String = URLUtil.getProtocol(url);
        var amf:AMFChannel;
        if (secure){
            amf = new SecureAMFChannel(channelName, protocol +
                "://{server.name}:{server.port}" +
                (Application.application as Application).parameters.contextRoot +
                "/graniteamf/amf");
        }else{
            amf = new AMFChannel(channelName, protocol +
                "://{server.name}:{server.port}" +
                (Application.application as Application).parameters.contextRoot
                + "/graniteamf/amf");
        }
        channelSet.addChannel(amf);
        remoteService.channelSet=channelSet;
        remoteService.showBusyCursor=showBusyCursor;
        return remoteService;
    }
    

    So as you can see the only things you need to provide are destination - which must be configured in server side XML and contextRoot - passed as flashVar. Passing as flashVar in my case (through JSP) looks like this:

    String flashVariables = "contextRoot=" + request.getContextPath() +
        "&locale=" + request.getLocale().getLanguage();