File size: 13,676 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | // 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
{
/// <summary>
/// Class used to represent the metadata and state of a subsystem.
/// </summary>
public abstract class SubsystemInfo
{
#region "Metadata of a Subsystem (public)"
/// <summary>
/// Gets the kind of a concrete subsystem.
/// </summary>
public SubsystemKind Kind { get; }
/// <summary>
/// Gets the type of a concrete subsystem.
/// </summary>
public Type SubsystemType { get; }
/// <summary>
/// Gets a value indicating whether the subsystem allows to unregister an implementation.
/// </summary>
public bool AllowUnregistration { get; private set; }
/// <summary>
/// Gets a value indicating whether the subsystem allows to have multiple implementations registered.
/// </summary>
public bool AllowMultipleRegistration { get; private set; }
/// <summary>
/// Gets the names of the required cmdlets that have to be implemented by the subsystem implementation.
/// </summary>
public ReadOnlyCollection<string> RequiredCmdlets { get; private set; }
/// <summary>
/// Gets the names of the required functions that have to be implemented by the subsystem implementation.
/// </summary>
public ReadOnlyCollection<string> RequiredFunctions { get; private set; }
// /// <summary>
// /// 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.
// /// </summary>
// public ReadOnlyCollection<SubsystemKind> DependsOn { get; private set; }
#endregion
#region "State of a Subsystem (public)"
/// <summary>
/// Indicate whether there is any implementation registered to the subsystem.
/// </summary>
public bool IsRegistered => _cachedImplInfos.Count > 0;
/// <summary>
/// Get the information about the registered implementations.
/// </summary>
public ReadOnlyCollection<ImplementationInfo> Implementations => _cachedImplInfos;
#endregion
#region "private/internal instance members"
private protected readonly object _syncObj;
private protected ReadOnlyCollection<ImplementationInfo> _cachedImplInfos;
private protected SubsystemInfo(SubsystemKind kind, Type subsystemType)
{
_syncObj = new object();
_cachedImplInfos = Utils.EmptyReadOnlyCollection<ImplementationInfo>();
Kind = kind;
SubsystemType = subsystemType;
AllowUnregistration = false;
AllowMultipleRegistration = false;
RequiredCmdlets = Utils.EmptyReadOnlyCollection<string>();
RequiredFunctions = Utils.EmptyReadOnlyCollection<string>();
}
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<TConcreteSubsystem>(SubsystemKind kind)
where TConcreteSubsystem : class, ISubsystem
{
return new SubsystemInfoImpl<TConcreteSubsystem>(kind);
}
internal static SubsystemInfo Create<TConcreteSubsystem>(
SubsystemKind kind,
bool allowUnregistration,
bool allowMultipleRegistration) where TConcreteSubsystem : class, ISubsystem
{
return new SubsystemInfoImpl<TConcreteSubsystem>(kind)
{
AllowUnregistration = allowUnregistration,
AllowMultipleRegistration = allowMultipleRegistration,
};
}
internal static SubsystemInfo Create<TConcreteSubsystem>(
SubsystemKind kind,
bool allowUnregistration,
bool allowMultipleRegistration,
ReadOnlyCollection<string> requiredCmdlets,
ReadOnlyCollection<string> 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<TConcreteSubsystem>(kind)
{
AllowUnregistration = allowUnregistration,
AllowMultipleRegistration = allowMultipleRegistration,
RequiredCmdlets = requiredCmdlets,
RequiredFunctions = requiredFunctions,
};
}
#endregion
#region "ImplementationInfo"
/// <summary>
/// Information about an implementation of a subsystem.
/// </summary>
public class ImplementationInfo
{
internal ImplementationInfo(SubsystemKind kind, ISubsystem implementation)
{
Id = implementation.Id;
Kind = kind;
Name = implementation.Name;
Description = implementation.Description;
ImplementationType = implementation.GetType();
}
/// <summary>
/// Gets the unique identifier for a subsystem implementation.
/// </summary>
public Guid Id { get; }
/// <summary>
/// Gets the kind of subsystem.
/// </summary>
public SubsystemKind Kind { get; }
/// <summary>
/// Gets the name of a subsystem implementation.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the description of a subsystem implementation.
/// </summary>
public string Description { get; }
/// <summary>
/// Gets the implementation type.
/// </summary>
public Type ImplementationType { get; }
}
#endregion
}
internal sealed class SubsystemInfoImpl<TConcreteSubsystem> : SubsystemInfo
where TConcreteSubsystem : class, ISubsystem
{
private ReadOnlyCollection<TConcreteSubsystem> _registeredImpls;
internal SubsystemInfoImpl(SubsystemKind kind)
: base(kind, typeof(TConcreteSubsystem))
{
_registeredImpls = Utils.EmptyReadOnlyCollection<TConcreteSubsystem>();
}
/// <summary>
/// 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).
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="rawImpl">The subsystem implementation to be added.</param>
private protected override void AddImplementation(ISubsystem rawImpl)
{
lock (_syncObj)
{
var impl = (TConcreteSubsystem)rawImpl;
if (_registeredImpls.Count == 0)
{
_registeredImpls = new ReadOnlyCollection<TConcreteSubsystem>(new[] { impl });
_cachedImplInfos = new ReadOnlyCollection<ImplementationInfo>(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<TConcreteSubsystem>(newCapacity);
implList.AddRange(_registeredImpls);
implList.Add(impl);
var implInfo = new List<ImplementationInfo>(newCapacity);
implInfo.AddRange(_cachedImplInfos);
implInfo.Add(new ImplementationInfo(Kind, impl));
_registeredImpls = new ReadOnlyCollection<TConcreteSubsystem>(implList);
_cachedImplInfos = new ReadOnlyCollection<ImplementationInfo>(implInfo);
}
}
/// <summary>
/// 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).
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="id">The id of the subsystem implementation to be removed.</param>
/// <returns>The subsystem implementation that was removed.</returns>
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<TConcreteSubsystem>();
_cachedImplInfos = Utils.EmptyReadOnlyCollection<ImplementationInfo>();
}
else
{
int newCapacity = _registeredImpls.Count - 1;
var implList = new List<TConcreteSubsystem>(newCapacity);
var implInfo = new List<ImplementationInfo>(newCapacity);
for (int i = 0; i < _registeredImpls.Count; i++)
{
if (index == i)
{
continue;
}
implList.Add(_registeredImpls[i]);
implInfo.Add(_cachedImplInfos[i]);
}
_registeredImpls = new ReadOnlyCollection<TConcreteSubsystem>(implList);
_cachedImplInfos = new ReadOnlyCollection<ImplementationInfo>(implInfo);
}
return target;
}
}
internal TConcreteSubsystem? GetImplementation()
{
var localRef = _registeredImpls;
return localRef.Count > 0 ? localRef[localRef.Count - 1] : null;
}
internal ReadOnlyCollection<TConcreteSubsystem> GetAllImplementations()
{
return _registeredImpls;
}
}
}
|