home *** CD-ROM | disk | FTP | other *** search
/ Computer Active Guide 2009 June / CAG06.ISO / Programos / TwonkyMediaManagerSetupStandard.exe / MediaManager / Japanese.swf / scripts / mx / modules / ModuleManager.as < prev    next >
Encoding:
Text File  |  2010-02-09  |  13.9 KB  |  540 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.       super();
  185.    }
  186.    
  187.    public function getModule(param1:String) : IModuleInfo
  188.    {
  189.       var _loc2_:ModuleInfo = moduleList[param1] as ModuleInfo;
  190.       if(!_loc2_)
  191.       {
  192.          _loc2_ = new ModuleInfo(param1);
  193.          moduleList[param1] = _loc2_;
  194.       }
  195.       return new ModuleInfoProxy(_loc2_);
  196.    }
  197.    
  198.    public function getAssociatedFactory(param1:Object) : IFlexModuleFactory
  199.    {
  200.       var m:Object = null;
  201.       var info:ModuleInfo = null;
  202.       var domain:ApplicationDomain = null;
  203.       var cls:Class = null;
  204.       var object:Object = param1;
  205.       var className:String = getQualifiedClassName(object);
  206.       for each(m in moduleList)
  207.       {
  208.          info = m as ModuleInfo;
  209.          if(info.ready)
  210.          {
  211.             domain = info.applicationDomain;
  212.             try
  213.             {
  214.                cls = Class(domain.getDefinition(className));
  215.                if(object is cls)
  216.                {
  217.                   return info.factory;
  218.                }
  219.             }
  220.             catch(error:Error)
  221.             {
  222.                continue;
  223.             }
  224.          }
  225.       }
  226.       return null;
  227.    }
  228. }
  229.  
  230. class ModuleInfo extends EventDispatcher
  231. {
  232.    private var _error:Boolean = false;
  233.    
  234.    private var loader:Loader;
  235.    
  236.    private var factoryInfo:FactoryInfo;
  237.    
  238.    private var limbo:Dictionary;
  239.    
  240.    private var _loaded:Boolean = false;
  241.    
  242.    private var _ready:Boolean = false;
  243.    
  244.    private var numReferences:int = 0;
  245.    
  246.    private var _url:String;
  247.    
  248.    private var _setup:Boolean = false;
  249.    
  250.    public function ModuleInfo(param1:String)
  251.    {
  252.       super();
  253.       _url = param1;
  254.    }
  255.    
  256.    private function clearLoader() : void
  257.    {
  258.       if(loader)
  259.       {
  260.          if(loader.contentLoaderInfo)
  261.          {
  262.             loader.contentLoaderInfo.removeEventListener(Event.INIT,initHandler);
  263.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
  264.             loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
  265.             loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  266.             loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  267.          }
  268.          try
  269.          {
  270.             if(loader.content)
  271.             {
  272.                loader.content.removeEventListener("ready",readyHandler);
  273.             }
  274.          }
  275.          catch(error:Error)
  276.          {
  277.          }
  278.          if(_loaded)
  279.          {
  280.             try
  281.             {
  282.                loader.close();
  283.             }
  284.             catch(error:Error)
  285.             {
  286.             }
  287.          }
  288.          try
  289.          {
  290.             loader.unload();
  291.          }
  292.          catch(error:Error)
  293.          {
  294.          }
  295.          loader = null;
  296.       }
  297.    }
  298.    
  299.    public function get size() : int
  300.    {
  301.       return !limbo && Boolean(factoryInfo) ? int(factoryInfo.bytesTotal) : 0;
  302.    }
  303.    
  304.    public function get loaded() : Boolean
  305.    {
  306.       return !limbo ? Boolean(_loaded) : false;
  307.    }
  308.    
  309.    public function release() : void
  310.    {
  311.       if(Boolean(_ready) && !limbo)
  312.       {
  313.          limbo = new Dictionary(true);
  314.          limbo[factoryInfo] = 1;
  315.          factoryInfo = null;
  316.       }
  317.       else
  318.       {
  319.          unload();
  320.       }
  321.    }
  322.    
  323.    public function get error() : Boolean
  324.    {
  325.       return !limbo ? Boolean(_error) : false;
  326.    }
  327.    
  328.    public function get factory() : IFlexModuleFactory
  329.    {
  330.       return !limbo && Boolean(factoryInfo) ? factoryInfo.factory : null;
  331.    }
  332.    
  333.    public function completeHandler(param1:Event) : void
  334.    {
  335.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.PROGRESS,param1.bubbles,param1.cancelable);
  336.       _loc2_.bytesLoaded = loader.contentLoaderInfo.bytesLoaded;
  337.       _loc2_.bytesTotal = loader.contentLoaderInfo.bytesTotal;
  338.       dispatchEvent(_loc2_);
  339.    }
  340.    
  341.    public function publish(param1:IFlexModuleFactory) : void
  342.    {
  343.       if(factoryInfo)
  344.       {
  345.          return;
  346.       }
  347.       if(_url.indexOf("published://") != 0)
  348.       {
  349.          return;
  350.       }
  351.       factoryInfo = new FactoryInfo();
  352.       factoryInfo.factory = param1;
  353.       _loaded = true;
  354.       _setup = true;
  355.       _ready = true;
  356.       _error = false;
  357.       dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  358.       dispatchEvent(new ModuleEvent(ModuleEvent.PROGRESS));
  359.       dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  360.    }
  361.    
  362.    public function initHandler(param1:Event) : void
  363.    {
  364.       var moduleEvent:ModuleEvent = null;
  365.       var event:Event = param1;
  366.       factoryInfo = new FactoryInfo();
  367.       try
  368.       {
  369.          factoryInfo.factory = loader.content as IFlexModuleFactory;
  370.       }
  371.       catch(error:Error)
  372.       {
  373.       }
  374.       if(!factoryInfo.factory)
  375.       {
  376.          moduleEvent = new ModuleEvent(ModuleEvent.ERROR,event.bubbles,event.cancelable);
  377.          moduleEvent.bytesLoaded = 0;
  378.          moduleEvent.bytesTotal = 0;
  379.          moduleEvent.errorText = "SWF is not a loadable module";
  380.          dispatchEvent(moduleEvent);
  381.          return;
  382.       }
  383.       loader.content.addEventListener("ready",readyHandler);
  384.       try
  385.       {
  386.          factoryInfo.applicationDomain = loader.contentLoaderInfo.applicationDomain;
  387.       }
  388.       catch(error:Error)
  389.       {
  390.       }
  391.       _setup = true;
  392.       dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  393.    }
  394.    
  395.    public function resurrect() : void
  396.    {
  397.       var _loc1_:Object = null;
  398.       if(!factoryInfo && Boolean(limbo))
  399.       {
  400.          var _loc2_:int = 0;
  401.          var _loc3_:* = limbo;
  402.          for(_loc1_ in _loc3_)
  403.          {
  404.             factoryInfo = _loc1_ as FactoryInfo;
  405.          }
  406.          limbo = null;
  407.       }
  408.       if(!factoryInfo)
  409.       {
  410.          if(_loaded)
  411.          {
  412.             dispatchEvent(new ModuleEvent(ModuleEvent.UNLOAD));
  413.          }
  414.          loader = null;
  415.          _loaded = false;
  416.          _setup = false;
  417.          _ready = false;
  418.          _error = false;
  419.       }
  420.    }
  421.    
  422.    public function errorHandler(param1:ErrorEvent) : void
  423.    {
  424.       _error = true;
  425.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.ERROR,param1.bubbles,param1.cancelable);
  426.       _loc2_.bytesLoaded = 0;
  427.       _loc2_.bytesTotal = 0;
  428.       _loc2_.errorText = param1.text;
  429.       dispatchEvent(_loc2_);
  430.    }
  431.    
  432.    public function get ready() : Boolean
  433.    {
  434.       return !limbo ? Boolean(_ready) : false;
  435.    }
  436.    
  437.    public function removeReference() : void
  438.    {
  439.       --numReferences;
  440.       if(numReferences == 0)
  441.       {
  442.          release();
  443.       }
  444.    }
  445.    
  446.    public function addReference() : void
  447.    {
  448.       ++numReferences;
  449.    }
  450.    
  451.    public function progressHandler(param1:ProgressEvent) : void
  452.    {
  453.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.PROGRESS,param1.bubbles,param1.cancelable);
  454.       _loc2_.bytesLoaded = param1.bytesLoaded;
  455.       _loc2_.bytesTotal = param1.bytesTotal;
  456.       dispatchEvent(_loc2_);
  457.    }
  458.    
  459.    public function load(param1:ApplicationDomain = null, param2:SecurityDomain = null) : void
  460.    {
  461.       if(_loaded)
  462.       {
  463.          return;
  464.       }
  465.       _loaded = true;
  466.       limbo = null;
  467.       if(_url.indexOf("published://") == 0)
  468.       {
  469.          return;
  470.       }
  471.       var _loc3_:URLRequest = new URLRequest(_url);
  472.       var _loc4_:LoaderContext = new LoaderContext();
  473.       _loc4_.applicationDomain = !!param1 ? param1 : new ApplicationDomain(ApplicationDomain.currentDomain);
  474.       _loc4_.securityDomain = param2;
  475.       if(param2 == null && Security.sandboxType == Security.REMOTE)
  476.       {
  477.          _loc4_.securityDomain = SecurityDomain.currentDomain;
  478.       }
  479.       loader = new Loader();
  480.       loader.contentLoaderInfo.addEventListener(Event.INIT,initHandler);
  481.       loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
  482.       loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
  483.       loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  484.       loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  485.       loader.load(_loc3_,_loc4_);
  486.    }
  487.    
  488.    public function get url() : String
  489.    {
  490.       return _url;
  491.    }
  492.    
  493.    public function get applicationDomain() : ApplicationDomain
  494.    {
  495.       return !limbo && Boolean(factoryInfo) ? factoryInfo.applicationDomain : null;
  496.    }
  497.    
  498.    public function readyHandler(param1:Event) : void
  499.    {
  500.       _ready = true;
  501.       factoryInfo.bytesTotal = loader.contentLoaderInfo.bytesTotal;
  502.       clearLoader();
  503.       dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  504.    }
  505.    
  506.    public function get setup() : Boolean
  507.    {
  508.       return !limbo ? Boolean(_setup) : false;
  509.    }
  510.    
  511.    public function unload() : void
  512.    {
  513.       clearLoader();
  514.       if(_loaded)
  515.       {
  516.          dispatchEvent(new ModuleEvent(ModuleEvent.UNLOAD));
  517.       }
  518.       limbo = null;
  519.       factoryInfo = null;
  520.       _loaded = false;
  521.       _setup = false;
  522.       _ready = false;
  523.       _error = false;
  524.    }
  525. }
  526.  
  527. class FactoryInfo
  528. {
  529.    public var bytesTotal:int = 0;
  530.    
  531.    public var factory:IFlexModuleFactory;
  532.    
  533.    public var applicationDomain:ApplicationDomain;
  534.    
  535.    public function FactoryInfo()
  536.    {
  537.       super();
  538.    }
  539. }
  540.