WebHostScriptMappingsInstallComponent.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / Tools / xws_reg / System / ServiceModel / Install / WebHostScriptMappingsInstallComponent.cs / 2 / WebHostScriptMappingsInstallComponent.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.ServiceModel.Install 
{
    using System; 
    using System.Collections.Generic; 
    using System.DirectoryServices;
    using System.Globalization; 
    using System.Runtime.InteropServices;
    using System.Text;

    internal class WebHostScriptMappingsInstallComponent : ServiceModelInstallComponent 
    {
        const string IISRoot = WebHostScriptMappingsInstallComponent.LocalHost + "W3SVC"; 
        const string LocalHost = "IIS://localhost/"; 
        bool recursive;
        DirectoryEntry rootPath; 
        const string ScriptMapOptions = @",1,GET,HEAD,POST,DEBUG";
        bool updateExisting;
        bool isReinstalling;
        Queue reinstallPaths; 

        private WebHostScriptMappingsInstallComponent() 
            : this(WebHostScriptMappingsInstallComponent.IISRoot, true, true) 
        {
        } 

        internal WebHostScriptMappingsInstallComponent(string rootPath, bool recursive, bool updateExisting)
        {
            if (!IisHelper.ShouldInstallIis) 
            {
                throw new IisNotInstalledException(SR.GetString(SR.IisNotInstalled, SR.GetString(SR.WebHostScriptMappingsName))); 
            } 
            else
            { 
                if (!String.IsNullOrEmpty(rootPath))
                {
                    if (rootPath.StartsWith("/LM/", StringComparison.OrdinalIgnoreCase))
                    { 
                        // Replace "/LM/" with "IIS://localhost/" to accept
                        // root paths like "/LM/W3SVC/1/root" 
                        rootPath = WebHostScriptMappingsInstallComponent.LocalHost + rootPath.Substring(4); 
                    }
 
                    if (!rootPath.StartsWith(WebHostScriptMappingsInstallComponent.LocalHost, StringComparison.OrdinalIgnoreCase))
                    {
                        rootPath = WebHostScriptMappingsInstallComponent.LocalHost + rootPath;
                    } 

                    if (rootPath.EndsWith("/", StringComparison.OrdinalIgnoreCase)) 
                    { 
                        rootPath = rootPath.Remove(rootPath.LastIndexOf('/'), 1);
                    } 

                    try
                    {
                        // No need to call ValidateDirectoryEntry here because DirectoryEntry.Exists forces a bind 
                        // We won't get the retry logic of ValidateDirectoryEntry, but that's okay because this code path is not used in the MSI install
                        if (!DirectoryEntry.Exists(rootPath)) 
                        { 
                            throw new ApplicationException(SR.GetString(SR.WebHostScriptMappingsInvalidScriptMapPath, rootPath));
                        } 
                        else
                        {
                            this.rootPath = new DirectoryEntry(rootPath);
                        } 
                    }
                    catch (COMException e) 
                    { 
                        switch (e.ErrorCode)
                        { 
                            case (COMErrors.E_ADS_BAD_PATHNAME):
                            case (COMErrors.E_UNSPECIFIED_ERRROR):
                                throw new ApplicationException(SR.GetString(SR.WebHostScriptMappingsInvalidScriptMapPath, rootPath), e);
 
                            default:
                                throw IisHelper.WrapIisError(e); 
                        } 
                    }
                } 
                else
                {
                    try
                    { 
                        this.rootPath = new DirectoryEntry(WebHostScriptMappingsInstallComponent.IISRoot);
                        IisHelper.ValidateDirectoryEntry(this.rootPath); 
                    } 
                    catch (COMException e)
                    { 
                        throw IisHelper.WrapIisError(e);
                    }
                }
 
                this.recursive = recursive;
                this.updateExisting = updateExisting; 
            } 
        }
 
        internal override string DisplayName
        {
            get { return SR.GetString(SR.WebHostScriptMappingsName); }
        } 

        string ExtensionValuesDisplay 
        { 
            get
            { 
                StringBuilder extensionValuesDisplay = new StringBuilder();
                for (int i = 0; i < ServiceModelInstallStrings.IISExtensionValues.Length; i++)
                {
                    string extensionValue = ServiceModelInstallStrings.IISExtensionValues[i]; 
                    extensionValuesDisplay = extensionValuesDisplay.Append(extensionValue);
 
                    if (i + 1 < ServiceModelInstallStrings.IISExtensionValues.Length) 
                    {
                        extensionValuesDisplay = extensionValuesDisplay.Append(", "); 
                    }
                }

                return extensionValuesDisplay.ToString(); 
            }
        } 
 
        string GenerateScriptMapping(string extensionValue, string isapiFilter)
        { 
            return String.Format(CultureInfo.CurrentCulture,
                "{0},{1}{2}",
                extensionValue,
                isapiFilter, 
                WebHostScriptMappingsInstallComponent.ScriptMapOptions);
        } 
 
        protected override string InstallActionMessage
        { 
            get
            {
                if (!this.IsInstalled)
                { 
                    return SR.GetString(SR.WebHostScriptMappingsInstall, this.ExtensionValuesDisplay);
                } 
                else 
                {
                    return SR.GetString(SR.WebHostScriptMappingsUpdate, this.ExtensionValuesDisplay); 
                }
            }
        }
 
        internal string[] InstalledPaths
        { 
            get 
            {
                List installedPaths = BuildInstalledPathsRecursive(this.rootPath); 

                return installedPaths.ToArray();
            }
        } 

        internal override string[] InstalledVersions 
        { 
            get
            { 
                List installedVersions = new List();

                PropertyValueCollection scriptMapCollection = this.rootPath.Properties[ServiceModelInstallStrings.ScriptMaps];
                foreach (string scriptMap in scriptMapCollection) 
                {
                    string lowerScriptMap = scriptMap.ToLower(CultureInfo.InvariantCulture); 
                    foreach (string extensionValue in ServiceModelInstallStrings.IISExtensionValues) 
                    {
                        if (lowerScriptMap.Contains(extensionValue.ToLower(CultureInfo.InvariantCulture))) 
                        {
                            installedVersions.Add(scriptMap);
                            break;
                        } 
                    }
                } 
 
                return installedVersions.ToArray();
            } 
        }

        internal override bool IsInstalled
        { 
            get
            { 
                return IsInstalledAtPath(this.rootPath); 
            }
        } 

        protected override string ReinstallActionMessage
        {
            get { return SR.GetString(SR.WebHostScriptMappingsReinstall, this.ExtensionValuesDisplay); } 
        }
 
        protected override string UninstallActionMessage 
        {
            get { return SR.GetString(SR.WebHostScriptMappingsUninstall, this.ExtensionValuesDisplay); } 
        }

        List BuildInstalledPathsRecursive(DirectoryEntry webSite)
        { 
            List installedPaths = new List();
 
            if (IsInstalledAtPath(webSite)) 
            {
                installedPaths.Add(webSite.Path); 

                PropertyValueCollection scriptMapCollection = webSite.Properties[ServiceModelInstallStrings.ScriptMaps];
                foreach (string scriptMap in scriptMapCollection)
                { 
                    foreach (string extensionValue in ServiceModelInstallStrings.IISExtensionValues)
                    { 
                        if (scriptMap.ToLower(CultureInfo.InvariantCulture).Contains(extensionValue.ToLower(CultureInfo.InvariantCulture))) 
                        {
                            installedPaths.Add("\t" + scriptMap); 
                            break;
                        }
                    }
                } 
            }
 
            foreach (DirectoryEntry child in webSite.Children) 
            {
                installedPaths.AddRange(BuildInstalledPathsRecursive(child)); 
            }

            return installedPaths;
        } 

        List CurrentInstalledScriptMaps(DirectoryEntry path) 
        { 
            List installedScriptMaps = new List();
 
            PropertyValueCollection scriptMapCollection = path.Properties[ServiceModelInstallStrings.ScriptMaps];
            foreach (string scriptMap in scriptMapCollection)
            {
                foreach (string extensionValue in ServiceModelInstallStrings.IISExtensionValues) 
                {
                    if (scriptMap.StartsWith(extensionValue, StringComparison.OrdinalIgnoreCase)) 
                    { 
                        installedScriptMaps.Add(scriptMap);
                        break; 
                    }
                }
            }
 
            return installedScriptMaps;
        } 
 
        internal override void Install(OutputLevel outputLevel)
        { 
            if (!IisHelper.ShouldInstallScriptMaps)
            {
                EventLogger.LogWarning(SR.GetString(SR.AspNetNotInstalled, SR.GetString(SR.WebHostScriptMappingsName)), (OutputLevel.Verbose == outputLevel));
            } 
            else
            { 
                int numberScriptMapsInstalled = InstallScriptMapToPath(this.rootPath, outputLevel); 
                if (this.isReinstalling && !this.recursive)
                { 
                    while (this.reinstallPaths.Count > 0)
                    {
                        DirectoryEntry path = this.reinstallPaths.Dequeue();
                        if (path != this.rootPath) 
                        {
                            numberScriptMapsInstalled += InstallScriptMapToPath(path, outputLevel); 
                        } 
                    }
                } 

                if (0 == numberScriptMapsInstalled)
                {
                    EventLogger.LogWarning(SR.GetString(SR.WebHostScriptMappingsAlreadyExists, this.ExtensionValuesDisplay), (OutputLevel.Verbose == outputLevel)); 
                }
            } 
        } 

        internal override void Reinstall(OutputLevel outputLevel) 
        {
            isReinstalling = true;
            this.reinstallPaths = new Queue();
            base.Reinstall(outputLevel); 
            isReinstalling = false;
        } 
 
        int InstallScriptMapToPath(DirectoryEntry path, OutputLevel outputLevel)
        { 
            int numberScriptMapsInstalled = 0;

            if (PathContainsScriptMaps(path))
            { 
                PropertyValueCollection scriptMapCollection = path.Properties[ServiceModelInstallStrings.ScriptMaps];
 
                if (IsInstalledAtPath(path) && this.updateExisting) 
                {
                    foreach (string scriptMap in this.CurrentInstalledScriptMaps(path)) 
                    {
                        scriptMapCollection.Remove(scriptMap);
                    }
                } 

                if (!IsInstalledAtPath(path)) 
                { 
                    if (OutputLevel.Verbose == outputLevel)
                    { 
                        EventLogger.WriteMsiStyleLogEntry(SR.GetString(SR.WebHostScriptMappingsAddMap, path.Path));
                        EventLogger.LogToConsole(SR.GetString(SR.WebHostScriptMappingsAddMap, path.Path));
                    }
 
                    foreach (string extensionValue in ServiceModelInstallStrings.IISExtensionValues)
                    { 
                        scriptMapCollection.Add(this.GenerateScriptMapping(extensionValue, InstallHelper.IsapiFilter)); 
                    }
 
                    path.CommitChanges();
                    numberScriptMapsInstalled++;
                }
            } 

            if (this.recursive) 
            { 
                foreach (DirectoryEntry childPath in path.Children)
                { 
                    numberScriptMapsInstalled += InstallScriptMapToPath(childPath, outputLevel);
                }
            }
 
            return numberScriptMapsInstalled;
        } 
 
        bool IsInstalledAtPath(DirectoryEntry path)
        { 
            bool isInstalled = false;

            if (PathContainsScriptMaps(path))
            { 
                PropertyValueCollection scriptMapCollection = path.Properties[ServiceModelInstallStrings.ScriptMaps];
                foreach (string scriptMap in scriptMapCollection) 
                { 
                    string lowerScriptMap = scriptMap.ToLower(CultureInfo.InvariantCulture);
                    foreach (string extensionValue in ServiceModelInstallStrings.IISExtensionValues) 
                    {
                        if (lowerScriptMap.Contains(extensionValue.ToLower(CultureInfo.InvariantCulture)))
                        {
                            isInstalled = true; 
                            break;
                        } 
                    } 

                    if (isInstalled) 
                    {
                        break;
                    }
                } 
            }
 
            return isInstalled; 
        }
 
        bool PathContainsScriptMaps(DirectoryEntry path)
        {
            bool containsScriptMaps = false;
 
            try
            { 
                if (path.Properties.Contains(ServiceModelInstallStrings.ScriptMaps)) 
                {
                    containsScriptMaps = true; 
                }
            }
            catch (COMException e)
            { 
                if (COMErrors.E_ADS_PROPERTY_NOT_SUPPORTED == e.ErrorCode ||
                    COMErrors.MD_ERROR_DATA_NOT_FOUND == e.ErrorCode || 
                    COMErrors.COR_E_DIRECTORYNOTFOUND == e.ErrorCode) 
                {
                    containsScriptMaps = false; 
                }
                else
                {
                    Environment.ExitCode = e.ErrorCode; 
                    throw IisHelper.WrapIisError(e);
                } 
            } 

            return containsScriptMaps; 
        }

        int IndexOfScriptMap(PropertyValueCollection scriptMapCollection, string entry)
        { 
            int scriptMapIndex = scriptMapCollection.IndexOf(entry);
 
            if (-1 == scriptMapIndex) 
            {
                for (int i = 0; i < scriptMapCollection.Count; i++) 
                {
                    string scriptMap = (string)scriptMapCollection[i];
                    if (scriptMap.Equals(entry, StringComparison.OrdinalIgnoreCase))
                    { 
                        scriptMapIndex = i;
                        break; 
                    } 
                }
            } 

            return scriptMapIndex;
        }
 
        internal override void Uninstall(OutputLevel outputLevel)
        { 
            if (this.IsInstalled) 
            {
                UninstallScriptMapFromPath(this.rootPath, outputLevel); 
            }
            else
            {
                EventLogger.LogWarning(SR.GetString(SR.WebHostScriptMappingsNotInstalled, this.ExtensionValuesDisplay), (OutputLevel.Verbose == outputLevel)); 
            }
        } 
 
        void UninstallScriptMapFromPath(DirectoryEntry path, OutputLevel outputLevel)
        { 
            if (PathContainsScriptMaps(path))
            {
                bool displayedUninstallMessage = false;
                PropertyValueCollection scriptMapCollection = path.Properties[ServiceModelInstallStrings.ScriptMaps]; 

                if (this.updateExisting) 
                { 
                    string newIsapiFilter = null;
                    string otherIndigoVersionDirectory = InstallHelper.GetHighestOtherWcfRuntimeInstallPath(); 

                    if (!String.IsNullOrEmpty(otherIndigoVersionDirectory))
                    {
                        newIsapiFilter = InstallHelper.IsapiFilter.Replace(InstallHelper.GetWcfRuntimeInstallPath(), otherIndigoVersionDirectory); 
                    }
 
                    foreach (string extensionValue in ServiceModelInstallStrings.IISExtensionValues) 
                    {
                        int scriptMapIndex = this.IndexOfScriptMap(scriptMapCollection, this.GenerateScriptMapping(extensionValue, InstallHelper.IsapiFilter)); 
                        if (-1 != scriptMapIndex)
                        {
                            if (!displayedUninstallMessage)
                            { 
                                if (OutputLevel.Verbose == outputLevel)
                                { 
                                    EventLogger.WriteMsiStyleLogEntry(SR.GetString(SR.WebHostScriptMappingsRemoveMap, path.Path)); 
                                    EventLogger.LogToConsole(SR.GetString(SR.WebHostScriptMappingsRemoveMap, path.Path));
                                } 

                                displayedUninstallMessage = true;
                            }
 
                            if (this.isReinstalling && !this.recursive)
                            { 
                                // This path has the script map explicitly set, which means that it won't inherit 
                                // script maps from its parent. So on reinstall, we need to explicit re-add it.
                                this.reinstallPaths.Enqueue(path); 
                            }

                            scriptMapCollection.RemoveAt(scriptMapIndex);
 
                            if (!String.IsNullOrEmpty(newIsapiFilter))
                            { 
                                scriptMapCollection.Add(this.GenerateScriptMapping(extensionValue, newIsapiFilter)); 
                            }
                        } 
                    }
                }
                else
                { 
                    foreach (string scriptMap in this.CurrentInstalledScriptMaps(path))
                    { 
                        if (!displayedUninstallMessage) 
                        {
                            if (OutputLevel.Verbose == outputLevel) 
                            {
                                EventLogger.WriteMsiStyleLogEntry(SR.GetString(SR.WebHostScriptMappingsRemoveMap, path.Path));
                                EventLogger.LogToConsole(SR.GetString(SR.WebHostScriptMappingsRemoveMap, path.Path));
                            } 

                            displayedUninstallMessage = true; 
                        } 

                        scriptMapCollection.Remove(scriptMap); 
                    }
                }

                path.CommitChanges(); 
            }
 
            if (this.recursive || this.isReinstalling) 
            {
                foreach (DirectoryEntry childPath in path.Children) 
                {
                    UninstallScriptMapFromPath(childPath, outputLevel);
                }
            } 
        }
 
        internal override InstallationState VerifyInstall() 
        {
            InstallationState installState = InstallationState.Unknown; 

            if (this.IsInstalled)
            {
                installState = InstallationState.InstalledDefaults; 
            }
            else 
            { 
                installState = InstallationState.NotInstalled;
            } 

            return installState;
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK