Since the advent of BlazeDS we can deploy a Flex application on any JEE Application Server. I personally like to place all the resources like web services endpoints, usernames, etc in Java system properties. Then I can simply get them back by doing a System.getProperty('propertyName"). If you want to know how to set and use Java system properties in JBoss, read one my previous blog entries: Setting Java System Properties in JBoss .
If your Flex application is being served from a JEE application server, it would be nice to be able to read the Java system properties of that server. On alternative is to have a POJO in the back end that reads the Java system properties in the back end, then the Flex client queries that information from the POJO using remoting. This is the option to use if you also need modify Java system properties from the Flex application. If you don't need to modify the Java system properties, there is an easier alternative.
With Flex you have the ability to pass variables to the application at startup via flashVars. A Flex application gets compiled to a Flash (SWF) file. That file usually is served embedded in an HTML page. So, if you want to pass variables to the application you create an index.html page to serve the application that looks like this:
<html>
<body>
<object id='flexApp' height='100%' width='100%'>
<param name='stockCheckEndPoint' value='http://foo/stockEndPoint/>
<param name='ldapServer' value='ldapServerName>'/>
<embed name='flexApp' src='flexApp.swf' pluginspage='http://www.adobe.com/go/getflashplayer'
height='100%' width='100%'/>
</object>
</body>
</html>
This page passed the values of stockServiceEndpoint and ldapServer to the Flex application. To read those values in Action Script Flex provides the following instructions:
stockWebServiceEndpoint = Application.application.parameters.stockCheckEndPoint;
ldapServerName = Application.application.parameters.ldapServer;
Now the values of the flashVars are in couple ActionScript variables ready to be used. This still does not solve the problem of reading Java system properties in a Flex application. The solution involves using the flashVars as described above, but instead of serving the Flex application from a static html page, to serve it from a JSP page that reads the Java system properties.
<html>
<body>
<%
String stockCheckEndPoint = System.getProperty("stockCheckEndPoint");
String ldapServer = System.getProperty("ldapServer");
%>
<object id='flexApp' height='100%' width='100%'>
<param name='stockCheckEndPoint' value='<%= stockCheckEndPoint %>'/>
<param name='ldapServer' value='<%= ldapServer %>'/>
<embed name='flexApp' src='flexApp.swf' pluginspage='http://www.adobe.com/go/getflashplayer'
height='100%' width='100%'/>
</object>
</body>
</html>
As you can see, instead of creating an index.html file to serve the Flex application, I created a JSP file (index.jsp) to server the Flex application. I read the Java system properties and placed it in the variables stockCheckEndPoint and ldapServer. Then I passed the value of these variables as flashVars. Now I can access the values of those variables with ActionScript as mentioned before. Use this formula, so you never have to hard code variables in Flex again.