Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new cache manager for the serverGuid passed in.
        /// </summary>
        /// <param name="serverGuid"></param>
        public LocationCacheManager(Guid serverGuid, Guid serviceOwner, Uri connectionBaseUrl)
        {
            m_cacheAvailable = (serverGuid.Equals(Guid.Empty)) ? false : true;

            m_lastChangeId        = -1;
            m_cacheExpirationDate = DateTime.MinValue;

            if (serviceOwner == Guid.Empty)
            {
                // For a legacy server (which didn't return serviceOwner in the connectionData), let's not try to break anything
                // just use the old path.
                // We should be able to remove this case eventually.
                m_cacheFilePath = Path.Combine(Path.Combine(VssClientSettings.ClientCacheDirectory, serverGuid.ToString()),
                                               s_cacheFileName);
            }
            else
            {
                m_cacheFilePath = Path.Combine(Path.Combine(Path.Combine(VssClientSettings.ClientCacheDirectory, serverGuid.ToString()), serviceOwner.ToString()),
                                               s_cacheFileName);
            }

            m_cacheLocallyFresh = false;
            m_accessMappings    = new Dictionary <String, AccessMapping>(VssStringComparer.AccessMappingMoniker);
            m_services          = new Dictionary <String, Dictionary <Guid, ServiceDefinition> >(VssStringComparer.ServiceType);
            m_cachedMisses      = new HashSet <String>(VssStringComparer.ServiceType);

            m_connectionBaseUrl   = connectionBaseUrl;
            m_locationXmlOperator = new LocationXmlOperator(true);
        }
        /// <summary>
        /// Reads the service definitions from the provided document.
        /// For a specification of what the xml should look like, see the
        /// corresponding Write method.
        /// </summary>
        /// <param name="document">The document to read from.</param>
        /// <returns>A list of service definitions.</returns>
        public List <ServiceDefinition> ReadServices(XmlDocument document, Dictionary <String, AccessMapping> accessMappings)
        {
            List <ServiceDefinition> definitions = new List <ServiceDefinition>();

            XmlNodeList servicesNodeList = document.SelectNodes("//" + s_services);

            if (servicesNodeList == null)
            {
                return(definitions);
            }

            foreach (XmlNode servicesNode in servicesNodeList)
            {
                // Get all of the service definition nodes
                foreach (XmlNode definitionNode in servicesNode.SelectNodes("./" + s_serviceDefinition))
                {
                    ServiceDefinition definition = new ServiceDefinition();

                    // Get the service type - it must exist
                    XmlNode serviceTypeNode = definitionNode.SelectSingleNode("./" + s_serviceType);
                    LocationXmlOperator.CheckXmlNodeNullOrEmpty(serviceTypeNode, s_serviceType, definitionNode);
                    definition.ServiceType = serviceTypeNode.InnerText;

                    // Get the identifier if it exists - it must exist if this is the client cache
                    XmlNode identifierNode = definitionNode.SelectSingleNode("./" + s_identifier);
                    if (m_isClientCache)
                    {
                        LocationXmlOperator.CheckXmlNodeNullOrEmpty(identifierNode, s_identifier, definitionNode);
                    }
                    definition.Identifier = (identifierNode != null) ? XmlConvert.ToGuid(identifierNode.InnerText) : Guid.Empty;

                    // Get the display name - it must exist
                    XmlNode displayNameNode = definitionNode.SelectSingleNode("./" + s_displayName);
                    LocationXmlOperator.CheckXmlNodeNullOrEmpty(displayNameNode, s_displayName, definitionNode);
                    definition.DisplayName = displayNameNode.InnerText;

                    // Get the description if it exists
                    XmlNode descriptionNode = definitionNode.SelectSingleNode("./" + s_description);
                    definition.Description = (descriptionNode != null) ? descriptionNode.InnerText : String.Empty;

                    // Get the relativePath and the relativeTo setting
                    XmlNode relativePathNode = definitionNode.SelectSingleNode("./" + s_relativePath);
                    LocationXmlOperator.CheckXmlNodeNull(relativePathNode, s_relativePath, definitionNode);
                    definition.RelativePath = relativePathNode.InnerText;

                    // Get the relativeTo setting
                    XmlAttribute relativeToAttribute = relativePathNode.Attributes[s_relativeTo];
                    CheckXmlAttributeNullOrEmpty(relativeToAttribute, s_relativeTo, relativePathNode);
                    RelativeToSetting setting;
                    if (!RelativeToEnumCache.GetRelativeToEnums().TryGetValue(relativeToAttribute.InnerText, out setting))
                    {
                        throw new ConfigFileException(relativeToAttribute.InnerText);
                    }
                    definition.RelativeToSetting = setting;

                    // If the relativeToSetting is FullyQualified and the path is empty, set it to null
                    // to make the framework happy.
                    if (definition.RelativeToSetting == RelativeToSetting.FullyQualified && definition.RelativePath == String.Empty)
                    {
                        definition.RelativePath = null;
                    }

                    XmlNode parentServiceTypeNode = definitionNode.SelectSingleNode("./" + s_parentServiceType);
                    definition.ParentServiceType = (parentServiceTypeNode != null) ? parentServiceTypeNode.InnerText : null;

                    XmlNode parentIdentifierNode = definitionNode.SelectSingleNode("./" + s_parentIdentifier);
                    definition.ParentIdentifier = (parentIdentifierNode != null) ? XmlConvert.ToGuid(parentIdentifierNode.InnerText) : Guid.Empty;

                    // Get all of the location mappings
                    definition.LocationMappings = new List <LocationMapping>();
                    if (definition.RelativeToSetting == RelativeToSetting.FullyQualified)
                    {
                        XmlNodeList mappings = definitionNode.SelectNodes(".//" + s_locationMapping);

                        foreach (XmlNode mappingNode in mappings)
                        {
                            LocationMapping locationMapping = new LocationMapping();

                            // Get the accessMapping
                            XmlNode accessMappingNode = mappingNode.SelectSingleNode("./" + s_accessMapping);
                            LocationXmlOperator.CheckXmlNodeNullOrEmpty(accessMappingNode, s_accessMapping, mappingNode);
                            locationMapping.AccessMappingMoniker = accessMappingNode.InnerText;

                            // Only process the location code if this is the client cache and there better
                            // not be a location node if this isn't a client cache.
                            XmlNode locationNode = mappingNode.SelectSingleNode("./" + s_location);
                            if (m_isClientCache)
                            {
                                CheckXmlNodeNullOrEmpty(locationNode, s_location, mappingNode);
                            }

                            locationMapping.Location = (locationNode != null) ? locationNode.InnerText : null;

                            // We will let the caller build the proper location from the proper service definitions
                            // instead of doing it here.

                            definition.LocationMappings.Add(locationMapping);
                        }
                    }

                    // Get the resourceVersion
                    XmlNode resourceVersionNode = definitionNode.SelectSingleNode("./" + s_resourceVersion);
                    definition.ResourceVersion = (resourceVersionNode != null) ? XmlConvert.ToInt32(resourceVersionNode.InnerText) : 0;

                    // Get the minVersion
                    XmlNode minVersionNode = definitionNode.SelectSingleNode("./" + s_minVersion);
                    definition.MinVersionString = (minVersionNode != null) ? minVersionNode.InnerText : null;

                    // Get the maxVersion
                    XmlNode maxVersionNode = definitionNode.SelectSingleNode("./" + s_maxVersion);
                    definition.MaxVersionString = (maxVersionNode != null) ? maxVersionNode.InnerText : null;

                    // Get the releasedVersion
                    XmlNode releasedVersionNode = definitionNode.SelectSingleNode("./" + s_releasedVersion);
                    definition.ReleasedVersionString = (releasedVersionNode != null) ? releasedVersionNode.InnerText : null;

                    definitions.Add(definition);
                }
            }

            return(definitions);
        }