ApplicationDomain学习


ApplicationDomain学习
2011年03月11日
  flash.system 包下  final类 继承自Object
  ApplicationDomain类是分散的类定义组的一个容器。应用程序域用于划分位于同一个安全域中的类。它们允许同一个类存在多个定义,并且允许子级重用父级定义。
  在通过Loader类加载外部SWF文件时会使用应用程序域。加载的SWF文件中的所有Actionscript 3.0定义都存储在由LoaderContext对象的applicationDomain属性指定的应用程序域中,此对象是您为Loader对象的load()()或loadBytes()方法传递的context参数。LoaderInfo对象还包含一个只读的applicationDomain属性。
  SWF文件中的所有代码被定义为存在于应用程序域中。主应用程序就在当前的应用程序域中运行。系统域中包含所有应用程序域(包括当前域),这意味着它包含所有Flash Player类。
  除系统域以外,每个应用程序域都有一个关联的父域。主应用程序的应用程序域的父域是系统域。已加载的类仅在其父级中没有相关定义时才进行定义。不能用较新的定义覆盖已加载的类定义。
  公共属性
  currentDomain : ApplicationDomain 【静态】【只读(read-only)】
  获取正在其中执行代码的当前应用程序域。
  domainMemory : ByteArray
  获取并设置将在此ApplicationDomain中对其执行域全局内存操作的对象。
  MIN_DOMAIN_MEMORY_LENGTH : uint [静态][只读]
  获取用作ApplicationDomain.domainMemory所需的最小内存对象长度。
  parentDomain : ApplicationDomain [只读]
  获取该应用程序域的父域。
  公共方法
  ApplicationDomain(parentDomain : ApplicationDomain = null)
  创建一个新的应用程序域
  参数:
  parentDomain :ApplicationDomain(default = null)
  如果未传入父域,此应用程序域将使用系统域作为其父域。
  getDefinition(name : String) : Object
  从指定的应用程序域获取一个公共定义。该定义可以是一个类、一个命名空间或一个函数的定义。
  参数:
  name :String 定义的名称
  返回:
  Object :与此定义关联的对象。
  引发:
  ReferenceError :不存在具有指定名称的公共定义。
  hasDefinition(name : String) : Boolean
  检查指定的应用程序域之内是否存在一个公共定义。该定义可以是一个类、一个命名空间或一个函数的定义。
  参数:
  name :String 定义的名称
  返回:
  Boolean :如果指定的定义存在,则返回true,否则,返回false
  演示:加载运行时的类,调用位于另一个 SWF 中的类的公共方法。
  注意:1.由于ClassLoader类要加载SWF文件,因此需要文件系统级别的本地安全性。
  2.若要运行此示例,在ApplicationDomainExample.swf文件所在的文件夹中必须存在一个名为RuntimeClasses.swf的SWF文件。
  首先,使用下面的代码创建RuntimeClasses.swf的SWF文件。
  package ApplicationDomainTest
  {
  import flash.display.Sprite;
  public class RuntimeClasses extends Sprite
  {
  public function RuntimeClasses()
  {
  }
  public function greet() : String
  {
  return ("Hello World");
  }
  }
  }
  package ApplicationDomainTest
  {
  import flash.display.Sprite;
  import flash.errors.IllegalOperationError;
  import flash.events.Event;
  import flash.text.TextField;
  public class ApplicationDomainExample extends Sprite
  {
  private var loader : ClassLoader;
  private var tf : TextField = new TextField();
  public function ApplicationDomainExample()
  {
  addChild(tf);
  tf.text = "I am a textfield";
  loader = new ClassLoader();
  loader.addEventListener(ClassLoader.LOAD_ERROR, loadErrorHandler);
  loader.addEventListener(ClassLoader.CLASS_LOADED, classLoadedHandler);
  loader.load("RuntimeClasses.swf");
  }
  private function loadErrorHandler(e : Event) : void
  {
  tf.text = "Load failed";
  throw new IllegalOperationError("Cannot load the specified file.");
  }
  private function classLoadedHandler(e : Event) : void
  {
  var runtimeClassRef : Class = loader.getClass("RuntimeClasses");
  var greeter : Object = new runtimeClassRef();
  tf.text = greeter.greet();
  }
  }
  }
  package ApplicationDomainTest
  {
  import flash.display.Loader;
  import flash.errors.IllegalOperationError;
  import flash.events.Event;
  import flash.events.EventDispatcher;
  import flash.events.IOErrorEvent;
  import flash.events.SecurityErrorEvent;
  import flash.net.URLRequest;
  import flash.system.ApplicationDomain;
  import flash.system.LoaderContext;
  public class ClassLoader extends EventDispatcher
  {
  public static var CLASS_LOADED : String = "classLoaded";
  public static var LOAD_ERROR : String = "loadError";
  private var loader : Loader;
  private var swfLib : String;
  private var request : URLRequest;
  private var loadedClass : Class;
  public function ClassLoader()
  {
  loader = new Loader();
  loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, completeHandler);
  loader.contentLoaderInfo.addEventListener(IOErrorE vent.IO_ERROR, ioErrorHandler);
  loader.contentLoaderInfo.addEventListener(Security ErrorEvent.SECURITY_ERROR, securityErrorHandler);
  }
  public function load(lib : String) : void
  {
  swfLib = lib;
  request = new URLRequest(swfLib);
  var context : LoaderContext = new LoaderContext();
  context.applicationDomain = ApplicationDomain.currentDomain;
  loader.load(request, context);
  }
  public function getClass(className : String) : Class
  {
  try{
  return loader.contentLoaderInfo.applicationDomain.getDefi nition(className) as Class;
  }
  catch(e : Error)
  {
  throw new IllegalOperationError(className + " definition not found in " + swfLib);
  }
  return null;
  }
  private function completeHandler(e : Event) : void
  {
  dispatchEvent(new Event(ClassLoader.CLASS_LOADED));
  }
  private function ioErrorHandler(e : Event) : void
  {
  dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
  }
  private function securityErrorHandler(e : Event) : void
  {
  dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
  }
  }
  }

猜你喜欢

转载自xus799ov.iteye.com/blog/1359575