Loading Modules in AIR application

September 5 20085 Commented

Categorized Under: Flex, RIA, Software Engineer, Technology

We had the task-force of loading Flex modules in AIR application at runtime. All our flex modules are developed already and they can run well in our website; but loading the Flex modules and make them run in AIR is the different story, the problem comes from the different security mechanism between AIR and Web Flex application. The Flex module can not be loaded by using class mx.modules.ModuleManager though we tried to modify crossdomain.xml file and modify some stuffs in flex module. We search the solutions on internet and there are only 3 valuable resources discuss exactly the problem we faces, they are:

The first work-around solution is tricky, we see it as the last solution if we can not seek the better one (within 2 days). We also do not prefer the third solution because we do not want to use AIR Update framework because we want to deploy our application as SaaS model, that means we do not know how many services we have to bundle them all at the first time also we would like to have right that force people update (such as security pack, bug fix pack like OS without user confirm whether it is necessary or not). The comments of third solution is exact our solution when we combine them with the solution of [2], they solve completely our problem in several lines of code!

I would like to summarize our steps to solve the problem (Copy from [2], temporarily not sign our modules and verify the the signature. They should be presented in our future works)

  1. Download the remote module and store them in AIR application storage:
    //Load the remote module base on moduleUri -
    //for example: http://www.esofthead.com/engroup/HrEmployeeModule.swf
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.BINARY;
    loader.addEventListener(Event.COMPLETE, moduleLoadedCompleteHandler);
    loader.addEventListener(IOErrorEvent.IO_ERROR, moduleLoaderErrorHandler);
    loader.load(new URLRequest(moduleUri))

    Save module into application storage when the module is completely downloaded

    private function moduleLoadedCompleteHandler(e:Event): void {
        var loader:URLLoader = URLLoader(e.target);
        var data:ByteArray = ByteArray(loader.data);
        var moduleFile:File = File.applicationStorageDirectory.resolvePath("HrEmployeeModule.swf");
        var fileStream:FileStream = new FileStream();
        fileStream.open(moduleFile, FileMode.WRITE);
        fileStream.writeBytes(data);
        fileStream.close();
    }
  2. Load the module (in application storage)
    var moduleLoader:Loader = new Loader();
    var context:LoaderContext = new LoaderContext();
    context.allowLoadBytesCodeExecution = true;
    context.applicationDomain = ApplicationDomain.currentDomain;    
     
    //data is byte array of module, it could be filled by loading 
     
    //bytes from local file (module file is stored in app storage)
    moduleLoader.loadBytes(data, context);
    moduleLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeLoader);

    We process module when it is already loaded

    private function completeLoader(e:Event): void {
        var moduleLoader:LoaderInfo = LoaderInfo(e.target);
        moduleLoader.content.addEventListener("ready", readyHandler);
    }
  3. Use module when the module is already loaded
    private function readyHandler(e:Event): void {
        var factory:IFlexModuleFactory = IFlexModuleFactory(e.target);
        var module:Module = factory.create() as Module;
        // Do as flex module as usual
        ...
    }

Note that you can control the loading remote module and cache module by using AIR embedded database. The database should keep url, version of module (our AIR only download the remote module if it does not exist in local or it has the newer version in server side). And we can develop the Service Management at client side to keep track the life cycle of each application services, that allows we can uninstall/install/update with/or without user knowledge.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • blinkbits
  • BlinkList
  • blogmarks
  • BlogMemes
  • BlogMemes Cn
  • BlogMemes Fr
  • BlogMemes Jp
  • BlogMemes Sp
  • Blogosphere News
  • Blogsvine
  • blogtercimlap
  • Book.mark.hu
  • Bumpzee
  • co.mments
  • connotea
  • De.lirio.us
  • Design Float
  • DotNetKicks
  • DZone
  • eKudos
  • email
  • Fark
  • Faves
  • feedmelinks
  • Fleck
  • Furl
  • GeenRedactie
  • Global Grind
  • Gwar
  • Haohao
  • HealthRanker
  • Hemidemi
  • Identi.ca
  • IndianPad
  • Internetmedia
  • kick.ie
  • Kirtsy
  • laaik.it
  • Leonaut
  • LinkaGoGo
  • LinkArena
  • LinkedIn
  • Linkter
  • Live
  • Ma.gnolia
  • Meneame
  • MisterWong
  • MisterWong.DE
  • muti
  • MyShare
  • MySpace
  • N4G
  • Netvibes
  • Netvouz
  • NewsVine
  • NuJIJ
  • Ping.fm
  • PlugIM
  • Pownce
  • ppnow
  • Print
  • Propeller
  • Ratimarks
  • Rec6
  • Reddit
  • SalesMarks
  • Scoopeo
  • scuttle
  • Segnalo
  • Shadows
  • Simpy
  • Slashdot
  • Smarking
  • Socialogs
  • SphereIt
  • Spurl
  • StumbleUpon
  • Symbaloo
  • Taggly
  • TailRank
  • Technorati
  • ThisNext
  • Tipd
  • Tumblr
  • TwitThis
  • Upnews
  • Webnews.de
  • Webride
  • Wikio
  • Wikio FR
  • Wikio IT
  • Wists
  • Wykop
  • Xerpi
  • Yahoo! Buzz
  • YahooMyWeb
  • Yigg

5 Responses to “Loading Modules in AIR application”

  1. [...] Loading Modules in AIR application: This solution worked for me and with some minor adjustments, I was also able to load a regular swf file (see code snippet). [...]

  2. ozeebee says:

    I had the same problem and came up with exactly the same solution as yours :
    1. load SWF bytes into mem with an URLLoader
    2. create a custom LoaderContext with the “allowLoadBytesCodeExecution” property set to true.
    3. use the Loader.loadBytes() method with the data and loaderContext.

    I think that it is currently the only solution to load modules from another location than the application sanbdox.

    I haven’t tried to sign the modules yet.

  3. [...] of loading remote modules within an Air application I would like to recommend the article ‘Loading Modules in Air application‘ at ESOFTHEAD’S BLOG. The code example on this site helped me creating a workaround of [...]

  4. Stray says:

    Hi,

    I came across your page while tackling the building of a modular air app – like you, we want to be able to add to and change out the modules after installation.

    Your loadBytes() solution worked great, but left an obvious security hole.

    I’ve now completed building a framework which allows loading of signed and non-signed swfs into different sandboxes. The loaded swfs must be in .air packages – renamed as .zip – signed with the same security certificate as the original app.

    All the source, as well as compiled test stuff and documentation is freely available here: http://flair-flash-flex-air.blogspot.com/2009/09/framework-for-modular-air-applications.html

    Thanks for your help in getting this working!

  5. Ethan Chee says:

    I’m developing a AIR Application Online game where i have 1 Game Lobby AIR App (Flex-Based AIR) and games AIR App (Flash CS4 Built Swf, Create AIR file using Flex to load the Swf file).

    The game have only 1 Lobby(air file) and Numbers of games (air files).
    Purpose to having multiple AIR file to make use of AIR auto updates features for the lobby and all games.

    I want to remove/unchecked “Add shortcut icon to my desktop” from the Installation Preferences of AIR Application Install dialogue.

    May i know how to do remove/unchecked “Add shortcut icon to my desktop” feature?

Leave a Reply