home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / mx / messaging / config / ServerConfig.as < prev   
Encoding:
Text File  |  2010-06-23  |  15.4 KB  |  461 lines

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