home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Darbas / kidoz_v1.air / kidoz.swf / scripts / mx / messaging / config / ServerConfig.as < prev   
Encoding:
Text File  |  2009-05-06  |  14.2 KB  |  431 lines

  1. package mx.messaging.config
  2. {
  3.    import flash.utils.getDefinitionByName;
  4.    import mx.collections.ArrayCollection;
  5.    import mx.core.mx_internal;
  6.    import mx.messaging.Channel;
  7.    import mx.messaging.ChannelSet;
  8.    import mx.messaging.MessageAgent;
  9.    import mx.messaging.errors.InvalidChannelError;
  10.    import mx.messaging.errors.InvalidDestinationError;
  11.    import mx.resources.IResourceManager;
  12.    import mx.resources.ResourceManager;
  13.    import mx.utils.ObjectUtil;
  14.    
  15.    use namespace mx_internal;
  16.    
  17.    public class ServerConfig
  18.    {
  19.       private static var _resourceManager:IResourceManager;
  20.       
  21.       private static var _configFetchedChannels:Object;
  22.       
  23.       public static var serverConfigData:XML;
  24.       
  25.       public static const CLASS_ATTR:String = "type";
  26.       
  27.       public static const URI_ATTR:String = "uri";
  28.       
  29.       private static var _channelSets:Object = {};
  30.       
  31.       private static var _clusteredChannels:Object = {};
  32.       
  33.       private static var _unclusteredChannels:Object = {};
  34.       
  35.       public function ServerConfig()
  36.       {
  37.          super();
  38.       }
  39.       
  40.       private static function convertToXML(param1:ConfigMap, param2:XML) : void
  41.       {
  42.          var _loc3_:Object = null;
  43.          var _loc4_:Object = null;
  44.          var _loc5_:Object = null;
  45.          var _loc6_:XML = null;
  46.          var _loc7_:Array = null;
  47.          var _loc8_:int = 0;
  48.          var _loc9_:XML = null;
  49.          var _loc10_:XML = null;
  50.          for(_loc3_ in param1)
  51.          {
  52.             _loc4_ = param1[_loc3_];
  53.             if(_loc4_ is String)
  54.             {
  55.                if(_loc3_ == "")
  56.                {
  57.                   _loc5_ = param2.localName();
  58.                   _loc6_ = param2.parent();
  59.                   _loc6_[_loc5_] = _loc4_;
  60.                }
  61.                else
  62.                {
  63.                   param2[_loc3_] = _loc4_;
  64.                }
  65.             }
  66.             else if(_loc4_ is ArrayCollection || _loc4_ is Array)
  67.             {
  68.                if(_loc4_ is ArrayCollection)
  69.                {
  70.                   _loc7_ = ArrayCollection(_loc4_).toArray();
  71.                }
  72.                else
  73.                {
  74.                   _loc7_ = _loc4_ as Array;
  75.                }
  76.                _loc8_ = 0;
  77.                while(_loc8_ < _loc7_.length)
  78.                {
  79.                   _loc9_ = new XML("<" + _loc3_ + "></" + _loc3_ + ">");
  80.                   param2.appendChild(_loc9_);
  81.                   convertToXML(_loc7_[_loc8_] as ConfigMap,_loc9_);
  82.                   _loc8_++;
  83.                }
  84.             }
  85.             else
  86.             {
  87.                _loc10_ = new XML("<" + _loc3_ + "></" + _loc3_ + ">");
  88.                param2.appendChild(_loc10_);
  89.                convertToXML(_loc4_ as ConfigMap,_loc10_);
  90.             }
  91.          }
  92.       }
  93.       
  94.       mx_internal static function getChannelIdList(param1:String) : Array
  95.       {
  96.          var _loc2_:XML = getDestinationConfig(param1);
  97.          return !!_loc2_ ? getChannelIds(_loc2_) : getDefaultChannelIds();
  98.       }
  99.       
  100.       public static function getChannel(param1:String, param2:Boolean = false) : Channel
  101.       {
  102.          var _loc3_:Channel = null;
  103.          if(!param2)
  104.          {
  105.             if(param1 in _unclusteredChannels)
  106.             {
  107.                return _unclusteredChannels[param1];
  108.             }
  109.             _loc3_ = createChannel(param1);
  110.             _unclusteredChannels[param1] = _loc3_;
  111.             return _loc3_;
  112.          }
  113.          if(param1 in _clusteredChannels)
  114.          {
  115.             return _clusteredChannels[param1];
  116.          }
  117.          _loc3_ = createChannel(param1);
  118.          _clusteredChannels[param1] = _loc3_;
  119.          return _loc3_;
  120.       }
  121.       
  122.       mx_internal static function needsConfig(param1:Channel) : Boolean
  123.       {
  124.          var _loc2_:Array = null;
  125.          var _loc3_:int = 0;
  126.          var _loc4_:int = 0;
  127.          var _loc5_:Array = null;
  128.          var _loc6_:int = 0;
  129.          var _loc7_:int = 0;
  130.          if(_configFetchedChannels == null || _configFetchedChannels[param1.endpoint] == null)
  131.          {
  132.             _loc2_ = param1.channelSets;
  133.             _loc3_ = int(_loc2_.length);
  134.             _loc4_ = 0;
  135.             while(_loc4_ < _loc3_)
  136.             {
  137.                _loc5_ = ChannelSet(_loc2_[_loc4_]).messageAgents;
  138.                _loc6_ = int(_loc5_.length);
  139.                _loc7_ = 0;
  140.                while(_loc7_ < _loc6_)
  141.                {
  142.                   if(MessageAgent(_loc5_[_loc7_]).mx_internal::needsConfig)
  143.                   {
  144.                      return true;
  145.                   }
  146.                   _loc7_++;
  147.                }
  148.                _loc4_++;
  149.             }
  150.          }
  151.          return false;
  152.       }
  153.       
  154.       public static function getChannelSet(param1:String) : ChannelSet
  155.       {
  156.          var _loc2_:XML = getDestinationConfig(param1);
  157.          return internalGetChannelSet(_loc2_,param1);
  158.       }
  159.       
  160.       public static function get xml() : XML
  161.       {
  162.          if(serverConfigData == null)
  163.          {
  164.             serverConfigData = <services/>;
  165.          }
  166.          return serverConfigData;
  167.       }
  168.       
  169.       mx_internal static function updateServerConfigData(param1:ConfigMap, param2:String = null) : void
  170.       {
  171.          var newServices:XML = null;
  172.          var newService:XML = null;
  173.          var newChannels:XMLList = null;
  174.          var oldServices:XMLList = null;
  175.          var oldService:XML = null;
  176.          var newDestination:XML = null;
  177.          var oldDestinations:XMLList = null;
  178.          var oldChannels:XML = null;
  179.          var serverConfig:ConfigMap = param1;
  180.          var endpoint:String = param2;
  181.          if(serverConfig != null)
  182.          {
  183.             if(endpoint != null)
  184.             {
  185.                if(_configFetchedChannels == null)
  186.                {
  187.                   _configFetchedChannels = {};
  188.                }
  189.                _configFetchedChannels[endpoint] = true;
  190.             }
  191.             newServices = <services></services>;
  192.             convertToXML(serverConfig,newServices);
  193.             xml["default-channels"] = newServices["default-channels"];
  194.             for each(newService in newServices..service)
  195.             {
  196.                oldServices = xml.service.(@id == newService.@id);
  197.                if(oldServices.length() != 0)
  198.                {
  199.                   oldService = oldServices[0];
  200.                   for each(newDestination in newService..destination)
  201.                   {
  202.                      oldDestinations = oldService.destination.(@id == newDestination.@id);
  203.                      if(oldDestinations.length() != 0)
  204.                      {
  205.                         delete oldDestinations[0];
  206.                      }
  207.                      oldService.appendChild(newDestination);
  208.                   }
  209.                }
  210.                else
  211.                {
  212.                   xml.appendChild(newService);
  213.                }
  214.             }
  215.             newChannels = newServices.channels;
  216.             if(newChannels.length() > 0)
  217.             {
  218.                oldChannels = xml.channels[0];
  219.                if(oldChannels == null || oldChannels.length() == 0)
  220.                {
  221.                   xml.appendChild(newChannels);
  222.                }
  223.             }
  224.          }
  225.       }
  226.       
  227.       private static function internalGetChannelSet(param1:XML, param2:String) : ChannelSet
  228.       {
  229.          var _loc3_:Array = null;
  230.          var _loc4_:Boolean = false;
  231.          var _loc6_:String = null;
  232.          var _loc7_:ChannelSet = null;
  233.          if(param1 == null)
  234.          {
  235.             _loc3_ = getDefaultChannelIds();
  236.             if(_loc3_.length == 0)
  237.             {
  238.                _loc6_ = resourceManager.getString("messaging","noChannelForDestination",[param2]);
  239.                throw new InvalidDestinationError(_loc6_);
  240.             }
  241.             _loc4_ = false;
  242.          }
  243.          else
  244.          {
  245.             _loc3_ = getChannelIds(param1);
  246.             _loc4_ = param1.properties.network.cluster.length() > 0 ? true : false;
  247.          }
  248.          var _loc5_:String = _loc3_.join(",") + ":" + _loc4_;
  249.          if(_loc5_ in _channelSets)
  250.          {
  251.             return _channelSets[_loc5_];
  252.          }
  253.          _loc7_ = new ChannelSet(_loc3_,_loc4_);
  254.          if(_loc4_)
  255.          {
  256.             _loc7_.initialDestinationId = param2;
  257.          }
  258.          _channelSets[_loc5_] = _loc7_;
  259.          return _loc7_;
  260.       }
  261.       
  262.       private static function getDefaultChannelIds() : Array
  263.       {
  264.          var _loc1_:Array = [];
  265.          var _loc2_:XMLList = xml["default-channels"].channel;
  266.          var _loc3_:int = int(_loc2_.length());
  267.          var _loc4_:int = 0;
  268.          while(_loc4_ < _loc3_)
  269.          {
  270.             _loc1_.push(_loc2_[_loc4_].@ref.toString());
  271.             _loc4_++;
  272.          }
  273.          return _loc1_;
  274.       }
  275.       
  276.       private static function createChannel(param1:String) : Channel
  277.       {
  278.          var message:String = null;
  279.          var channels:XMLList = null;
  280.          var channelConfig:XML = null;
  281.          var className:String = null;
  282.          var uri:String = null;
  283.          var channel:Channel = null;
  284.          var channelClass:Class = null;
  285.          var channelId:String = param1;
  286.          channels = xml.channels.channel.(@id == channelId);
  287.          if(channels.length() == 0)
  288.          {
  289.             message = resourceManager.getString("messaging","unknownChannelWithId",[channelId]);
  290.             throw new InvalidChannelError(message);
  291.          }
  292.          channelConfig = channels[0];
  293.          className = channelConfig.attribute(CLASS_ATTR).toString();
  294.          uri = channelConfig.endpoint[0].attribute(URI_ATTR).toString();
  295.          channel = null;
  296.          try
  297.          {
  298.             channelClass = getDefinitionByName(className) as Class;
  299.             channel = new channelClass(channelId,uri);
  300.             channel.applySettings(channelConfig);
  301.             if(LoaderConfig.parameters.WSRP_ENCODED_CHANNEL != null)
  302.             {
  303.                channel.url = LoaderConfig.parameters.WSRP_ENCODED_CHANNEL;
  304.             }
  305.          }
  306.          catch(e:ReferenceError)
  307.          {
  308.             message = resourceManager.getString("messaging","unknownChannelClass",[className]);
  309.             throw new InvalidChannelError(message);
  310.          }
  311.          return channel;
  312.       }
  313.       
  314.       public static function getProperties(param1:String) : XMLList
  315.       {
  316.          var destination:XMLList = null;
  317.          var message:String = null;
  318.          var destinationId:String = param1;
  319.          destination = xml..destination.(@id == destinationId);
  320.          if(destination.length() > 0)
  321.          {
  322.             return destination.properties;
  323.          }
  324.          message = resourceManager.getString("messaging","unknownDestination",[destinationId]);
  325.          throw new InvalidDestinationError(message);
  326.       }
  327.       
  328.       private static function getChannelIds(param1:XML) : Array
  329.       {
  330.          var _loc2_:Array = [];
  331.          var _loc3_:XMLList = param1.channels.channel;
  332.          var _loc4_:int = int(_loc3_.length());
  333.          var _loc5_:int = 0;
  334.          while(_loc5_ < _loc4_)
  335.          {
  336.             _loc2_.push(_loc3_[_loc5_].@ref.toString());
  337.             _loc5_++;
  338.          }
  339.          return _loc2_;
  340.       }
  341.       
  342.       public static function set xml(param1:XML) : void
  343.       {
  344.          serverConfigData = param1;
  345.          _channelSets = {};
  346.          _clusteredChannels = {};
  347.          _unclusteredChannels = {};
  348.       }
  349.       
  350.       private static function getDestinationConfig(param1:String) : XML
  351.       {
  352.          var destinations:XMLList = null;
  353.          var destinationCount:int = 0;
  354.          var destinationId:String = param1;
  355.          destinations = xml..destination.(@id == destinationId);
  356.          destinationCount = int(destinations.length());
  357.          if(destinationCount == 0)
  358.          {
  359.             return null;
  360.          }
  361.          return destinations[0];
  362.       }
  363.       
  364.       mx_internal static function fetchedConfig(param1:String) : Boolean
  365.       {
  366.          return _configFetchedChannels != null && _configFetchedChannels[param1] != null;
  367.       }
  368.       
  369.       mx_internal static function channelSetMatchesDestinationConfig(param1:ChannelSet, param2:String) : Boolean
  370.       {
  371.          var csUris:Array = null;
  372.          var csChannels:Array = null;
  373.          var i:uint = 0;
  374.          var ids:Array = null;
  375.          var dsUris:Array = null;
  376.          var dsChannels:XMLList = null;
  377.          var channelConfig:XML = null;
  378.          var j:uint = 0;
  379.          var channelSet:ChannelSet = param1;
  380.          var destination:String = param2;
  381.          if(channelSet != null)
  382.          {
  383.             if(ObjectUtil.compare(channelSet.channelIds,mx_internal::getChannelIdList(destination)) == 0)
  384.             {
  385.                return true;
  386.             }
  387.             csUris = [];
  388.             csChannels = channelSet.channels;
  389.             i = 0;
  390.             while(i < csChannels.length)
  391.             {
  392.                csUris.push(csChannels[i].uri);
  393.                i++;
  394.             }
  395.             ids = mx_internal::getChannelIdList(destination);
  396.             dsUris = [];
  397.             j = 0;
  398.             while(j < ids.length)
  399.             {
  400.                dsChannels = xml.channels.channel.(@id == ids[j]);
  401.                channelConfig = dsChannels[0];
  402.                dsUris.push(channelConfig.endpoint[0].attribute(URI_ATTR).toString());
  403.                j++;
  404.             }
  405.             return ObjectUtil.compare(csUris,dsUris) == 0;
  406.          }
  407.          return false;
  408.       }
  409.       
  410.       public static function checkChannelConsistency(param1:String, param2:String) : void
  411.       {
  412.          var _loc3_:Array = mx_internal::getChannelIdList(param1);
  413.          var _loc4_:Array = mx_internal::getChannelIdList(param2);
  414.          if(ObjectUtil.compare(_loc3_,_loc4_) != 0)
  415.          {
  416.             throw new ArgumentError("Specified destinations are not channel consistent");
  417.          }
  418.       }
  419.       
  420.       private static function get resourceManager() : IResourceManager
  421.       {
  422.          if(!_resourceManager)
  423.          {
  424.             _resourceManager = ResourceManager.getInstance();
  425.          }
  426.          return _resourceManager;
  427.       }
  428.    }
  429. }
  430.  
  431.