// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #nullable enable using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Management.Automation.Internal; using Microsoft.PowerShell.Telemetry; namespace System.Management.Automation.Subsystem { /// /// Class used to represent the metadata and state of a subsystem. /// public abstract class SubsystemInfo { #region "Metadata of a Subsystem (public)" /// /// Gets the kind of a concrete subsystem. /// public SubsystemKind Kind { get; } /// /// Gets the type of a concrete subsystem. /// public Type SubsystemType { get; } /// /// Gets a value indicating whether the subsystem allows to unregister an implementation. /// public bool AllowUnregistration { get; private set; } /// /// Gets a value indicating whether the subsystem allows to have multiple implementations registered. /// public bool AllowMultipleRegistration { get; private set; } /// /// Gets the names of the required cmdlets that have to be implemented by the subsystem implementation. /// public ReadOnlyCollection RequiredCmdlets { get; private set; } /// /// Gets the names of the required functions that have to be implemented by the subsystem implementation. /// public ReadOnlyCollection RequiredFunctions { get; private set; } // /// // /// A subsystem may depend on or more other subsystems. // /// Maybe add a 'DependsOn' member? // /// This can be validated when registering a subsystem implementation, // /// to make sure its prerequisites have already been registered. // /// // public ReadOnlyCollection DependsOn { get; private set; } #endregion #region "State of a Subsystem (public)" /// /// Indicate whether there is any implementation registered to the subsystem. /// public bool IsRegistered => _cachedImplInfos.Count > 0; /// /// Get the information about the registered implementations. /// public ReadOnlyCollection Implementations => _cachedImplInfos; #endregion #region "private/internal instance members" private protected readonly object _syncObj; private protected ReadOnlyCollection _cachedImplInfos; private protected SubsystemInfo(SubsystemKind kind, Type subsystemType) { _syncObj = new object(); _cachedImplInfos = Utils.EmptyReadOnlyCollection(); Kind = kind; SubsystemType = subsystemType; AllowUnregistration = false; AllowMultipleRegistration = false; RequiredCmdlets = Utils.EmptyReadOnlyCollection(); RequiredFunctions = Utils.EmptyReadOnlyCollection(); } private protected abstract void AddImplementation(ISubsystem rawImpl); private protected abstract ISubsystem RemoveImplementation(Guid id); internal void RegisterImplementation(ISubsystem impl) { AddImplementation(impl); ApplicationInsightsTelemetry.SendUseTelemetry(ApplicationInsightsTelemetry.s_subsystemRegistration, impl.Name); } internal ISubsystem UnregisterImplementation(Guid id) { return RemoveImplementation(id); } #endregion #region "Static factory overloads" internal static SubsystemInfo Create(SubsystemKind kind) where TConcreteSubsystem : class, ISubsystem { return new SubsystemInfoImpl(kind); } internal static SubsystemInfo Create( SubsystemKind kind, bool allowUnregistration, bool allowMultipleRegistration) where TConcreteSubsystem : class, ISubsystem { return new SubsystemInfoImpl(kind) { AllowUnregistration = allowUnregistration, AllowMultipleRegistration = allowMultipleRegistration, }; } internal static SubsystemInfo Create( SubsystemKind kind, bool allowUnregistration, bool allowMultipleRegistration, ReadOnlyCollection requiredCmdlets, ReadOnlyCollection requiredFunctions) where TConcreteSubsystem : class, ISubsystem { if (allowMultipleRegistration && (requiredCmdlets.Count > 0 || requiredFunctions.Count > 0)) { throw new ArgumentException( StringUtil.Format( SubsystemStrings.InvalidSubsystemInfo, kind.ToString())); } return new SubsystemInfoImpl(kind) { AllowUnregistration = allowUnregistration, AllowMultipleRegistration = allowMultipleRegistration, RequiredCmdlets = requiredCmdlets, RequiredFunctions = requiredFunctions, }; } #endregion #region "ImplementationInfo" /// /// Information about an implementation of a subsystem. /// public class ImplementationInfo { internal ImplementationInfo(SubsystemKind kind, ISubsystem implementation) { Id = implementation.Id; Kind = kind; Name = implementation.Name; Description = implementation.Description; ImplementationType = implementation.GetType(); } /// /// Gets the unique identifier for a subsystem implementation. /// public Guid Id { get; } /// /// Gets the kind of subsystem. /// public SubsystemKind Kind { get; } /// /// Gets the name of a subsystem implementation. /// public string Name { get; } /// /// Gets the description of a subsystem implementation. /// public string Description { get; } /// /// Gets the implementation type. /// public Type ImplementationType { get; } } #endregion } internal sealed class SubsystemInfoImpl : SubsystemInfo where TConcreteSubsystem : class, ISubsystem { private ReadOnlyCollection _registeredImpls; internal SubsystemInfoImpl(SubsystemKind kind) : base(kind, typeof(TConcreteSubsystem)) { _registeredImpls = Utils.EmptyReadOnlyCollection(); } /// /// The 'add' and 'remove' operations are implemented in a way to optimize the 'reading' operation, /// so that reading is lock-free and allocation-free, at the cost of O(n) copy in 'add' and 'remove' /// ('n' is the number of registered implementations). /// /// /// In the subsystem scenario, registration operations will be minimum, and in most cases, the registered /// implementation will never be unregistered, so optimization for reading is more important. /// /// The subsystem implementation to be added. private protected override void AddImplementation(ISubsystem rawImpl) { lock (_syncObj) { var impl = (TConcreteSubsystem)rawImpl; if (_registeredImpls.Count == 0) { _registeredImpls = new ReadOnlyCollection(new[] { impl }); _cachedImplInfos = new ReadOnlyCollection(new[] { new ImplementationInfo(Kind, impl) }); return; } if (!AllowMultipleRegistration) { throw new InvalidOperationException( StringUtil.Format( SubsystemStrings.MultipleRegistrationNotAllowed, Kind.ToString())); } foreach (TConcreteSubsystem item in _registeredImpls) { if (item.Id == impl.Id) { throw new InvalidOperationException( StringUtil.Format( SubsystemStrings.ImplementationAlreadyRegistered, impl.Id, Kind.ToString())); } } int newCapacity = _registeredImpls.Count + 1; var implList = new List(newCapacity); implList.AddRange(_registeredImpls); implList.Add(impl); var implInfo = new List(newCapacity); implInfo.AddRange(_cachedImplInfos); implInfo.Add(new ImplementationInfo(Kind, impl)); _registeredImpls = new ReadOnlyCollection(implList); _cachedImplInfos = new ReadOnlyCollection(implInfo); } } /// /// The 'add' and 'remove' operations are implemented in a way to optimize the 'reading' operation, /// so that reading is lock-free and allocation-free, at the cost of O(n) copy in 'add' and 'remove' /// ('n' is the number of registered implementations). /// /// /// In the subsystem scenario, registration operations will be minimum, and in most cases, the registered /// implementation will never be unregistered, so optimization for reading is more important. /// /// The id of the subsystem implementation to be removed. /// The subsystem implementation that was removed. private protected override ISubsystem RemoveImplementation(Guid id) { if (!AllowUnregistration) { throw new InvalidOperationException( StringUtil.Format( SubsystemStrings.UnregistrationNotAllowed, Kind.ToString())); } lock (_syncObj) { if (_registeredImpls.Count == 0) { throw new InvalidOperationException( StringUtil.Format( SubsystemStrings.NoImplementationRegistered, Kind.ToString())); } int index = -1; for (int i = 0; i < _registeredImpls.Count; i++) { if (_registeredImpls[i].Id == id) { index = i; break; } } if (index == -1) { throw new InvalidOperationException( StringUtil.Format( SubsystemStrings.ImplementationNotFound, id.ToString())); } ISubsystem target = _registeredImpls[index]; if (_registeredImpls.Count == 1) { _registeredImpls = Utils.EmptyReadOnlyCollection(); _cachedImplInfos = Utils.EmptyReadOnlyCollection(); } else { int newCapacity = _registeredImpls.Count - 1; var implList = new List(newCapacity); var implInfo = new List(newCapacity); for (int i = 0; i < _registeredImpls.Count; i++) { if (index == i) { continue; } implList.Add(_registeredImpls[i]); implInfo.Add(_cachedImplInfos[i]); } _registeredImpls = new ReadOnlyCollection(implList); _cachedImplInfos = new ReadOnlyCollection(implInfo); } return target; } } internal TConcreteSubsystem? GetImplementation() { var localRef = _registeredImpls; return localRef.Count > 0 ? localRef[localRef.Count - 1] : null; } internal ReadOnlyCollection GetAllImplementations() { return _registeredImpls; } } }