home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Adobe Air 1.5 / AdobeAIRInstaller.exe / setup.swf / scripts / mx / modules / ModuleManager.as < prev    next >
Encoding:
Text File  |  2008-10-29  |  13.9 KB  |  541 lines

  1. package mx.modules
  2. {
  3.    import mx.core.IFlexModuleFactory;
  4.    import mx.core.mx_internal;
  5.    
  6.    use namespace mx_internal;
  7.    
  8.    public class ModuleManager
  9.    {
  10.       mx_internal static const VERSION:String = "3.0.0.0";
  11.       
  12.       public function ModuleManager()
  13.       {
  14.          super();
  15.       }
  16.       
  17.       public static function getModule(param1:String) : IModuleInfo
  18.       {
  19.          return getSingleton().getModule(param1);
  20.       }
  21.       
  22.       private static function getSingleton() : Object
  23.       {
  24.          if(!ModuleManagerGlobals.managerSingleton)
  25.          {
  26.             ModuleManagerGlobals.managerSingleton = new ModuleManagerImpl();
  27.          }
  28.          return ModuleManagerGlobals.managerSingleton;
  29.       }
  30.       
  31.       public static function getAssociatedFactory(param1:Object) : IFlexModuleFactory
  32.       {
  33.          return getSingleton().getAssociatedFactory(param1);
  34.       }
  35.    }
  36. }
  37.  
  38. import flash.display.Loader;
  39. import flash.events.ErrorEvent;
  40. import flash.events.Event;
  41. import flash.events.EventDispatcher;
  42. import flash.events.IOErrorEvent;
  43. import flash.events.ProgressEvent;
  44. import flash.events.SecurityErrorEvent;
  45. import flash.net.URLRequest;
  46. import flash.system.ApplicationDomain;
  47. import flash.system.LoaderContext;
  48. import flash.system.Security;
  49. import flash.system.SecurityDomain;
  50. import flash.utils.Dictionary;
  51. import flash.utils.getQualifiedClassName;
  52. import mx.core.IFlexModuleFactory;
  53. import mx.events.ModuleEvent;
  54.  
  55. class ModuleInfoProxy extends EventDispatcher implements IModuleInfo
  56. {
  57.    private var _data:Object;
  58.    
  59.    private var info:ModuleInfo;
  60.    
  61.    private var referenced:Boolean = false;
  62.    
  63.    public function ModuleInfoProxy(param1:ModuleInfo)
  64.    {
  65.       super();
  66.       this.info = param1;
  67.       param1.addEventListener(ModuleEvent.SETUP,moduleEventHandler,false,0,true);
  68.       param1.addEventListener(ModuleEvent.PROGRESS,moduleEventHandler,false,0,true);
  69.       param1.addEventListener(ModuleEvent.READY,moduleEventHandler,false,0,true);
  70.       param1.addEventListener(ModuleEvent.ERROR,moduleEventHandler,false,0,true);
  71.       param1.addEventListener(ModuleEvent.UNLOAD,moduleEventHandler,false,0,true);
  72.    }
  73.    
  74.    public function get loaded() : Boolean
  75.    {
  76.       return info.loaded;
  77.    }
  78.    
  79.    public function release() : void
  80.    {
  81.       if(referenced)
  82.       {
  83.          info.removeReference();
  84.          referenced = false;
  85.       }
  86.    }
  87.    
  88.    public function get error() : Boolean
  89.    {
  90.       return info.error;
  91.    }
  92.    
  93.    public function get factory() : IFlexModuleFactory
  94.    {
  95.       return info.factory;
  96.    }
  97.    
  98.    public function publish(param1:IFlexModuleFactory) : void
  99.    {
  100.       info.publish(param1);
  101.    }
  102.    
  103.    public function set data(param1:Object) : void
  104.    {
  105.       _data = param1;
  106.    }
  107.    
  108.    public function get ready() : Boolean
  109.    {
  110.       return info.ready;
  111.    }
  112.    
  113.    public function load(param1:ApplicationDomain = null, param2:SecurityDomain = null) : void
  114.    {
  115.       var _loc3_:ModuleEvent = null;
  116.       info.resurrect();
  117.       if(!referenced)
  118.       {
  119.          info.addReference();
  120.          referenced = true;
  121.       }
  122.       if(info.error)
  123.       {
  124.          dispatchEvent(new ModuleEvent(ModuleEvent.ERROR));
  125.       }
  126.       else if(info.loaded)
  127.       {
  128.          if(info.setup)
  129.          {
  130.             dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  131.             if(info.ready)
  132.             {
  133.                _loc3_ = new ModuleEvent(ModuleEvent.PROGRESS);
  134.                _loc3_.bytesLoaded = info.size;
  135.                _loc3_.bytesTotal = info.size;
  136.                dispatchEvent(_loc3_);
  137.                dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  138.             }
  139.          }
  140.       }
  141.       else
  142.       {
  143.          info.load(param1,param2);
  144.       }
  145.    }
  146.    
  147.    private function moduleEventHandler(param1:ModuleEvent) : void
  148.    {
  149.       dispatchEvent(param1);
  150.    }
  151.    
  152.    public function get url() : String
  153.    {
  154.       return info.url;
  155.    }
  156.    
  157.    public function get data() : Object
  158.    {
  159.       return _data;
  160.    }
  161.    
  162.    public function get setup() : Boolean
  163.    {
  164.       return info.setup;
  165.    }
  166.    
  167.    public function unload() : void
  168.    {
  169.       info.unload();
  170.       info.removeEventListener(ModuleEvent.SETUP,moduleEventHandler);
  171.       info.removeEventListener(ModuleEvent.PROGRESS,moduleEventHandler);
  172.       info.removeEventListener(ModuleEvent.READY,moduleEventHandler);
  173.       info.removeEventListener(ModuleEvent.ERROR,moduleEventHandler);
  174.       info.removeEventListener(ModuleEvent.UNLOAD,moduleEventHandler);
  175.    }
  176. }
  177.  
  178. class ModuleManagerImpl extends EventDispatcher
  179. {
  180.    private var moduleList:Object;
  181.    
  182.    public function ModuleManagerImpl()
  183.    {
  184.       moduleList = {};
  185.       super();
  186.    }
  187.    
  188.    public function getModule(param1:String) : IModuleInfo
  189.    {
  190.       var _loc2_:ModuleInfo = moduleList[param1] as ModuleInfo;
  191.       if(!_loc2_)
  192.       {
  193.          _loc2_ = new ModuleInfo(param1);
  194.          moduleList[param1] = _loc2_;
  195.       }
  196.       return new ModuleInfoProxy(_loc2_);
  197.    }
  198.    
  199.    public function getAssociatedFactory(param1:Object) : IFlexModuleFactory
  200.    {
  201.       var m:Object = null;
  202.       var info:ModuleInfo = null;
  203.       var domain:ApplicationDomain = null;
  204.       var cls:Class = null;
  205.       var object:Object = param1;
  206.       var className:String = getQualifiedClassName(object);
  207.       for each(m in moduleList)
  208.       {
  209.          info = m as ModuleInfo;
  210.          if(info.ready)
  211.          {
  212.             domain = info.applicationDomain;
  213.             try
  214.             {
  215.                cls = Class(domain.getDefinition(className));
  216.                if(object is cls)
  217.                {
  218.                   return info.factory;
  219.                }
  220.             }
  221.             catch(error:Error)
  222.             {
  223.                continue;
  224.             }
  225.          }
  226.       }
  227.       return null;
  228.    }
  229. }
  230.  
  231. class ModuleInfo extends EventDispatcher
  232. {
  233.    private var _error:Boolean = false;
  234.    
  235.    private var loader:Loader;
  236.    
  237.    private var factoryInfo:FactoryInfo;
  238.    
  239.    private var limbo:Dictionary;
  240.    
  241.    private var _loaded:Boolean = false;
  242.    
  243.    private var _ready:Boolean = false;
  244.    
  245.    private var numReferences:int = 0;
  246.    
  247.    private var _url:String;
  248.    
  249.    private var _setup:Boolean = false;
  250.    
  251.    public function ModuleInfo(param1:String)
  252.    {
  253.       super();
  254.       _url = param1;
  255.    }
  256.    
  257.    private function clearLoader() : void
  258.    {
  259.       if(loader)
  260.       {
  261.          if(loader.contentLoaderInfo)
  262.          {
  263.             loader.contentLoaderInfo.removeEventListener(Event.INIT,initHandler);
  264.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
  265.             loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
  266.             loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  267.             loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  268.          }
  269.          try
  270.          {
  271.             if(loader.content)
  272.             {
  273.                loader.content.removeEventListener("ready",readyHandler);
  274.             }
  275.          }
  276.          catch(error:Error)
  277.          {
  278.          }
  279.          if(_loaded)
  280.          {
  281.             try
  282.             {
  283.                loader.close();
  284.             }
  285.             catch(error:Error)
  286.             {
  287.             }
  288.          }
  289.          try
  290.          {
  291.             loader.unload();
  292.          }
  293.          catch(error:Error)
  294.          {
  295.          }
  296.          loader = null;
  297.       }
  298.    }
  299.    
  300.    public function get size() : int
  301.    {
  302.       return !limbo && Boolean(factoryInfo) ? int(factoryInfo.bytesTotal) : 0;
  303.    }
  304.    
  305.    public function get loaded() : Boolean
  306.    {
  307.       return !limbo ? Boolean(_loaded) : false;
  308.    }
  309.    
  310.    public function release() : void
  311.    {
  312.       if(Boolean(_ready) && !limbo)
  313.       {
  314.          limbo = new Dictionary(true);
  315.          limbo[factoryInfo] = 1;
  316.          factoryInfo = null;
  317.       }
  318.       else
  319.       {
  320.          unload();
  321.       }
  322.    }
  323.    
  324.    public function get error() : Boolean
  325.    {
  326.       return !limbo ? Boolean(_error) : false;
  327.    }
  328.    
  329.    public function get factory() : IFlexModuleFactory
  330.    {
  331.       return !limbo && Boolean(factoryInfo) ? factoryInfo.factory : null;
  332.    }
  333.    
  334.    public function completeHandler(param1:Event) : void
  335.    {
  336.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.PROGRESS,param1.bubbles,param1.cancelable);
  337.       _loc2_.bytesLoaded = loader.contentLoaderInfo.bytesLoaded;
  338.       _loc2_.bytesTotal = loader.contentLoaderInfo.bytesTotal;
  339.       dispatchEvent(_loc2_);
  340.    }
  341.    
  342.    public function publish(param1:IFlexModuleFactory) : void
  343.    {
  344.       if(factoryInfo)
  345.       {
  346.          return;
  347.       }
  348.       if(_url.indexOf("published://") != 0)
  349.       {
  350.          return;
  351.       }
  352.       factoryInfo = new FactoryInfo();
  353.       factoryInfo.factory = param1;
  354.       _loaded = true;
  355.       _setup = true;
  356.       _ready = true;
  357.       _error = false;
  358.       dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  359.       dispatchEvent(new ModuleEvent(ModuleEvent.PROGRESS));
  360.       dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  361.    }
  362.    
  363.    public function initHandler(param1:Event) : void
  364.    {
  365.       var moduleEvent:ModuleEvent = null;
  366.       var event:Event = param1;
  367.       factoryInfo = new FactoryInfo();
  368.       try
  369.       {
  370.          factoryInfo.factory = loader.content as IFlexModuleFactory;
  371.       }
  372.       catch(error:Error)
  373.       {
  374.       }
  375.       if(!factoryInfo.factory)
  376.       {
  377.          moduleEvent = new ModuleEvent(ModuleEvent.ERROR,event.bubbles,event.cancelable);
  378.          moduleEvent.bytesLoaded = 0;
  379.          moduleEvent.bytesTotal = 0;
  380.          moduleEvent.errorText = "SWF is not a loadable module";
  381.          dispatchEvent(moduleEvent);
  382.          return;
  383.       }
  384.       loader.content.addEventListener("ready",readyHandler);
  385.       try
  386.       {
  387.          factoryInfo.applicationDomain = loader.contentLoaderInfo.applicationDomain;
  388.       }
  389.       catch(error:Error)
  390.       {
  391.       }
  392.       _setup = true;
  393.       dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  394.    }
  395.    
  396.    public function resurrect() : void
  397.    {
  398.       var _loc1_:Object = null;
  399.       if(!factoryInfo && Boolean(limbo))
  400.       {
  401.          var _loc2_:int = 0;
  402.          var _loc3_:* = limbo;
  403.          for(_loc1_ in _loc3_)
  404.          {
  405.             factoryInfo = _loc1_ as FactoryInfo;
  406.          }
  407.          limbo = null;
  408.       }
  409.       if(!factoryInfo)
  410.       {
  411.          if(_loaded)
  412.          {
  413.             dispatchEvent(new ModuleEvent(ModuleEvent.UNLOAD));
  414.          }
  415.          loader = null;
  416.          _loaded = false;
  417.          _setup = false;
  418.          _ready = false;
  419.          _error = false;
  420.       }
  421.    }
  422.    
  423.    public function errorHandler(param1:ErrorEvent) : void
  424.    {
  425.       _error = true;
  426.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.ERROR,param1.bubbles,param1.cancelable);
  427.       _loc2_.bytesLoaded = 0;
  428.       _loc2_.bytesTotal = 0;
  429.       _loc2_.errorText = param1.text;
  430.       dispatchEvent(_loc2_);
  431.    }
  432.    
  433.    public function get ready() : Boolean
  434.    {
  435.       return !limbo ? Boolean(_ready) : false;
  436.    }
  437.    
  438.    public function removeReference() : void
  439.    {
  440.       --numReferences;
  441.       if(numReferences == 0)
  442.       {
  443.          release();
  444.       }
  445.    }
  446.    
  447.    public function addReference() : void
  448.    {
  449.       ++numReferences;
  450.    }
  451.    
  452.    public function progressHandler(param1:ProgressEvent) : void
  453.    {
  454.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.PROGRESS,param1.bubbles,param1.cancelable);
  455.       _loc2_.bytesLoaded = param1.bytesLoaded;
  456.       _loc2_.bytesTotal = param1.bytesTotal;
  457.       dispatchEvent(_loc2_);
  458.    }
  459.    
  460.    public function load(param1:ApplicationDomain = null, param2:SecurityDomain = null) : void
  461.    {
  462.       if(_loaded)
  463.       {
  464.          return;
  465.       }
  466.       _loaded = true;
  467.       limbo = null;
  468.       if(_url.indexOf("published://") == 0)
  469.       {
  470.          return;
  471.       }
  472.       var _loc3_:URLRequest = new URLRequest(_url);
  473.       var _loc4_:LoaderContext = new LoaderContext();
  474.       _loc4_.applicationDomain = !!param1 ? param1 : new ApplicationDomain(ApplicationDomain.currentDomain);
  475.       _loc4_.securityDomain = param2;
  476.       if(param2 == null && Security.sandboxType == Security.REMOTE)
  477.       {
  478.          _loc4_.securityDomain = SecurityDomain.currentDomain;
  479.       }
  480.       loader = new Loader();
  481.       loader.contentLoaderInfo.addEventListener(Event.INIT,initHandler);
  482.       loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
  483.       loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
  484.       loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  485.       loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  486.       loader.load(_loc3_,_loc4_);
  487.    }
  488.    
  489.    public function get url() : String
  490.    {
  491.       return _url;
  492.    }
  493.    
  494.    public function get applicationDomain() : ApplicationDomain
  495.    {
  496.       return !limbo && Boolean(factoryInfo) ? factoryInfo.applicationDomain : null;
  497.    }
  498.    
  499.    public function readyHandler(param1:Event) : void
  500.    {
  501.       _ready = true;
  502.       factoryInfo.bytesTotal = loader.contentLoaderInfo.bytesTotal;
  503.       clearLoader();
  504.       dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  505.    }
  506.    
  507.    public function get setup() : Boolean
  508.    {
  509.       return !limbo ? Boolean(_setup) : false;
  510.    }
  511.    
  512.    public function unload() : void
  513.    {
  514.       clearLoader();
  515.       if(_loaded)
  516.       {
  517.          dispatchEvent(new ModuleEvent(ModuleEvent.UNLOAD));
  518.       }
  519.       limbo = null;
  520.       factoryInfo = null;
  521.       _loaded = false;
  522.       _setup = false;
  523.       _ready = false;
  524.       _error = false;
  525.    }
  526. }
  527.  
  528. class FactoryInfo
  529. {
  530.    public var bytesTotal:int = 0;
  531.    
  532.    public var factory:IFlexModuleFactory;
  533.    
  534.    public var applicationDomain:ApplicationDomain;
  535.    
  536.    public function FactoryInfo()
  537.    {
  538.       super();
  539.    }
  540. }
  541.