Esempio n. 1
0
        /// <summary>
        /// The unique identifier for the service owner. This property will attempt to return
        /// a cached value, if possible.
        /// </summary>
        public async Task <Guid> GetInstanceTypeAsync(
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!NeedToConnect(ConnectOptions.None))
            {
                // We've already made a Connect call and have the authoritative service owner ID.
                return(m_serviceOwner);
            }
            else
            {
                ServerMapData serverData = LocationServerMapCache.ReadServerData(m_fullyQualifiedUrl);
                Guid          toReturn   = serverData.ServiceOwner;

                if (Guid.Empty != toReturn)
                {
                    // We do. Return it.
                    return(toReturn);
                }

                // We do not. Make a Connect call and retrieve the service owner ID.
                await EnsureConnectedAsync(ConnectOptions.None, cancellationToken).ConfigureAwait(false);

                return(m_serviceOwner);
            }
        }
Esempio n. 2
0
        public VssServerDataProvider(
            VssConnection connection,
            HttpMessageHandler pipeline,
            String fullyQualifiedUrl)
        {
            m_connection        = connection;
            m_baseUri           = connection.Uri;
            m_fullyQualifiedUrl = fullyQualifiedUrl;
            m_locationClient    = new LocationHttpClient(m_baseUri, pipeline, false);

            // Try to get the guid for this server
            ServerMapData serverData = LocationServerMapCache.ReadServerData(m_fullyQualifiedUrl);

            m_locationDataCacheManager = new LocationCacheManager(serverData.ServerId, serverData.ServiceOwner, m_baseUri);
        }
Esempio n. 3
0
        /// <summary>
        /// If this call is not a change, nothing will be done.
        /// </summary>
        /// <param name="location"></param>
        /// <param name="serverId"></param>
        /// <param name="serviceOwner"></param>
        /// <returns>True if this is the first time the mapping was written.</returns>
        public static Boolean EnsureServerMappingExists(String location, Guid serverId, Guid serviceOwner)
        {
            try
            {
                EnsureCacheLoaded();
                s_accessLock.EnterWriteLock();

                // See if this is an update or an add to optimize writing the disk.
                Boolean       isNew = true;
                ServerMapData storedData;
                if (s_serverMappings.TryGetValue(location, out storedData))
                {
                    if (storedData.ServerId.Equals(serverId) &&
                        storedData.ServiceOwner.Equals(serviceOwner))
                    {
                        return(false);
                    }
                    isNew = false;
                }

                // Make the change in the cache
                s_serverMappings[location] = new ServerMapData(serverId, serviceOwner);

                s_accessLock.ExitWriteLock();

                // Persist the change
                return(TryWriteMappingToDisk(location, serverId, serviceOwner, isNew));
            }
            finally
            {
                if (s_accessLock.IsWriteLockHeld)
                {
                    s_accessLock.ExitWriteLock();
                }
            }
        }
Esempio n. 4
0
        private static void EnsureCacheLoaded()
        {
            if (s_cacheFreshLocally || s_cacheUnavailable)
            {
                return;
            }

            FileStream file = null;

            try
            {
                s_accessLock.EnterWriteLock();

                if (s_cacheFreshLocally)
                {
                    return;
                }

                // actually load the cache from disk
                // Open the file, allowing concurrent reads.
                // Do not create the file if it does not exist.
                XmlDocument document = XmlUtility.OpenXmlFile(out file, FilePath, FileShare.Read, false);

                if (document != null)
                {
                    // This is an existing document, get the root node
                    XmlNode documentNode = document.ChildNodes[0];

                    // Load all of the mappings
                    foreach (XmlNode mappingNode in documentNode.ChildNodes)
                    {
                        String location = mappingNode.Attributes[s_locationAttribute].InnerText;
                        Guid   guid     = XmlConvert.ToGuid(mappingNode.Attributes[s_guidAttribute].InnerText);

                        // Legacy server case: Don't error out if the existing file doesn't have the owner attribute.
                        // Once the server is updated the next connect call should update this record
                        Guid serviceOwner = Guid.Empty;
                        if (mappingNode.Attributes[s_ownerAttribute] != null)
                        {
                            serviceOwner = XmlConvert.ToGuid(mappingNode.Attributes[s_ownerAttribute].InnerText);
                        }

                        // If the service owner is absent, then the server is on-prem
                        if (Guid.Empty == serviceOwner)
                        {
                            serviceOwner = ServiceInstanceTypes.TFSOnPremises;
                        }

                        s_serverMappings[location] = new ServerMapData(guid, serviceOwner);
                    }
                }

                // Hook up the file system watcher so we know if we need to invalidate our cache
                if (s_fileWatcher == null)
                {
                    String directoryToWatch = VssClientSettings.ClientCacheDirectory;

                    // Ensure the directory exists, otherwise FileSystemWatcher will throw.
                    if (!Directory.Exists(directoryToWatch))
                    {
                        Directory.CreateDirectory(directoryToWatch);
                    }

                    s_fileWatcher = new FileSystemWatcher(directoryToWatch, s_fileName);
                    s_fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
                    s_fileWatcher.Changed     += new FileSystemEventHandler(s_fileWatcher_Changed);
                }
            }
            catch (Exception)
            {
                // It looks like something is wrong witht he cahce, lets just hide this
                // exception and work without it.
                s_cacheUnavailable = true;
            }
            finally
            {
                s_cacheFreshLocally = true;

                if (file != null)
                {
                    file.Close();
                }

                if (s_accessLock.IsWriteLockHeld)
                {
                    s_accessLock.ExitWriteLock();
                }
            }
        }