Flex: Loading external properties file

Flex is a nice platform which provides many out of box feature and that's why more and more Java developers are bending towards it. When I started using flex very soon I started comparing it with Java features. Java provides a features where you can externalize your application configurations in to a configuration (.properties) file external to application, by doing this we can change the configuration without actually changing the application code. Flex is also having same concept where you can externalize the application configurations in to a file. Here is a sample action script class using which you can load a (.properties) file in Flex application. 

public class PropertyFileLoader
  {
    [Bindable]
    private var VARIABLES_URL:String = "default.properties";
    [Bindable]
    public var vars:HashTable = new HashTable();
    private var urlReq:URLRequest;
    private var urlLdr:URLLoader;
    public function getValue(key:String):String
    {
      if(key != null && key.length > 0)
      {
        return vars.getItem(key);
      }
      return null;
    }

    public function PropertyFileLoader(fileName:String) {
      /* Initialize the two ArrayCollections objects with empty arrays. */
      VARIABLES_URL = fileName;
      /* Initialize the URLRequest object with the URL to the file of name/value pairs. */
      urlReq = new URLRequest(VARIABLES_URL);
      /* Initialize the URLLoader object, assign the various event listeners, and load the specified URLRequest object. */
      urlLdr = new URLLoader();
      urlLdr.addEventListener(Event.COMPLETE, doEvent);
      urlLdr.load(urlReq);
    }

    private function doEvent(evt:Event):void  

   {
      switch (evt.type) {
        case Event.COMPLETE:
          if(vars.length <= 0)
          {
            var ldr:URLLoader = evt.currentTarget as URLLoader;
            var lines:Array = ( ldr.data as String ).split( "\n" );

            for each ( var line:String in lines ) {
              var pair:Array = line.split( "=" );
              vars.addItem(pair[0], pair[1]);
            }

             break;
          }
      }
    }
  }


And this is how you'll use above class to load the external file (sample.properties) and use the configuration values in you application: 

sample.properties file: 
service.endpoint=http://surya:8080/erp/messagebroker/amf

Code to access above property:
public var configuration:PropertyFileLoader = new PropertyFileLoader("sample.properties");

public var configValue:String = configuration.getValue("service.endpoint");

Apart from above code we also need check sandbox security options which should allow flash player (flex) to access the sample.properties file. For more information follow this link http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html and update your sandbox security options accordingly.

Comments

Hello sir,i am new in flex so, in my application i hv multiple language support means selecting the english all the label should be display in english or if i choose hindi or marathi so all the label are display in hindi or marathi through out in my application..
for that i hv used properties file for each..english ,marathi and hindi.so in my dropdownlist has english,marathi,hindi.when user select one of the language and fill user name and password at login event i want to change all the label according to language selection.

please give me the solution for it.
plz mail me on w.shankar@gmail.com
Anonymous said…
Using resource manager you can do it.

Popular posts from this blog

Sling Authentication

CQ Development - OSGi bundles and Components

Multiple log files using log4j appender

Create an AEM (CQ) project using Maven