File size: 11,130 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Management.Automation;
using System.Management.Automation.Internal;
namespace Microsoft.PowerShell.Commands.Internal.Format
{
/// <summary>
/// Inner command class used to manage the sub pipelines
/// it determines which command should process the incoming objects
/// based on the object type
///
/// This class is the implementation class for out-console and out-file.
/// </summary>
internal sealed class OutputManagerInner : ImplementationCommandBase
{
#region tracer
[TraceSource("format_out_OutputManagerInner", "OutputManagerInner")]
internal static readonly PSTraceSource tracer = PSTraceSource.GetTracer("format_out_OutputManagerInner", "OutputManagerInner");
#endregion tracer
#region LineOutput
internal LineOutput LineOutput
{
set
{
lock (_syncRoot)
{
_lo = value;
if (_isStopped)
{
_lo.StopProcessing();
}
}
}
}
private LineOutput _lo = null;
#endregion
/// <summary>
/// Handler for processing each object coming through the pipeline
/// it forwards the call to the pipeline manager object.
/// </summary>
internal override void ProcessRecord()
{
PSObject so = this.ReadObject();
if (so == null || so == AutomationNull.Value)
{
return;
}
// on demand initialization when the first pipeline
// object is initialized
if (_mgr == null)
{
_mgr = new SubPipelineManager();
_mgr.Initialize(_lo, this.OuterCmdlet().Context);
}
#if false
// if the object supports IEnumerable,
// unpack the object and process each member separately
IEnumerable e = PSObjectHelper.GetEnumerable (so);
if (e == null)
{
this.mgr.Process (so);
}
else
{
foreach (object obj in e)
{
this.mgr.Process (PSObjectHelper.AsPSObject (obj));
}
}
#else
_mgr.Process(so);
#endif
}
/// <summary>
/// Handler for processing shut down. It forwards the call to the
/// pipeline manager object.
/// </summary>
internal override void EndProcessing()
{
// shut down only if we ever processed a pipeline object
_mgr?.ShutDown();
}
internal override void StopProcessing()
{
lock (_syncRoot)
{
_lo?.StopProcessing();
_isStopped = true;
}
}
/// <summary>
/// Make sure we dispose of the sub pipeline manager.
/// </summary>
protected override void InternalDispose()
{
base.InternalDispose();
if (_mgr != null)
{
_mgr.Dispose();
_mgr = null;
}
}
/// <summary>
/// Instance of the pipeline manager object.
/// </summary>
private SubPipelineManager _mgr = null;
/// <summary>
/// True if the cmdlet has been stopped.
/// </summary>
private bool _isStopped = false;
/// <summary>
/// Lock object.
/// </summary>
private readonly object _syncRoot = new object();
}
/// <summary>
/// Object managing the sub-pipelines that execute
/// different output commands (or different instances of the
/// default one)
/// </summary>
internal sealed class SubPipelineManager : IDisposable
{
/// <summary>
/// Entry defining a command to be run in a separate pipeline.
/// </summary>
private sealed class CommandEntry : IDisposable
{
/// <summary>
/// Instance of pipeline wrapper object.
/// </summary>
internal CommandWrapper command = new CommandWrapper();
/// <summary>
/// </summary>
/// <param name="typeName">ETS type name of the object to process.</param>
/// <returns>True if there is a match.</returns>
internal bool AppliesToType(string typeName)
{
foreach (string s in _applicableTypes)
{
if (string.Equals(s, typeName, StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
/// <summary>
/// Just dispose of the inner command wrapper.
/// </summary>
public void Dispose()
{
if (this.command == null)
return;
this.command.Dispose();
this.command = null;
}
/// <summary>
/// Ordered list of ETS type names this object is handling.
/// </summary>
private readonly StringCollection _applicableTypes = new StringCollection();
}
/// <summary>
/// Initialize the pipeline manager before any object is processed.
/// </summary>
/// <param name="lineOutput">LineOutput to pass to the child pipelines.</param>
/// <param name="context">ExecutionContext to pass to the child pipelines.</param>
internal void Initialize(LineOutput lineOutput, ExecutionContext context)
{
_lo = lineOutput;
InitializeCommandsHardWired(context);
}
/// <summary>
/// Hard wired registration helper for specialized types.
/// </summary>
/// <param name="context">ExecutionContext to pass to the child pipeline.</param>
private void InitializeCommandsHardWired(ExecutionContext context)
{
// set the default handler
RegisterCommandDefault(context, "out-lineoutput", typeof(OutLineOutputCommand));
/*
NOTE:
This is the spot where we could add new specialized handlers for
additional types. Adding a handler here would cause a new sub-pipeline
to be created.
For example, the following line would add a new handler named "out-example"
to be invoked when the incoming object type is "MyNamespace.Whatever.Example"
RegisterCommandForTypes (context, "out-example", new string[] { "MyNamespace.Whatever.Example" });
And the method can be like this:
private void RegisterCommandForTypes (ExecutionContext context, string commandName, Type commandType, string[] types)
{
CommandEntry ce = new CommandEntry ();
ce.command.Initialize (context, commandName, commandType);
ce.command.AddNamedParameter ("LineOutput", this.lo);
for (int k = 0; k < types.Length; k++)
{
ce.AddApplicableType (types[k]);
}
this.commandEntryList.Add (ce);
}
*/
}
/// <summary>
/// Register the default output command.
/// </summary>
/// <param name="context">ExecutionContext to pass to the child pipeline.</param>
/// <param name="commandName">Name of the command to execute.</param>
/// <param name="commandType">Type of the command to execute.</param>
private void RegisterCommandDefault(ExecutionContext context, string commandName, Type commandType)
{
CommandEntry ce = new CommandEntry();
ce.command.Initialize(context, commandName, commandType);
ce.command.AddNamedParameter("LineOutput", _lo);
_defaultCommandEntry = ce;
}
/// <summary>
/// Process an incoming parent pipeline object.
/// </summary>
/// <param name="so">Pipeline object to process.</param>
internal void Process(PSObject so)
{
// select which pipeline should handle the object
CommandEntry ce = this.GetActiveCommandEntry(so);
Diagnostics.Assert(ce != null, "CommandEntry ce must not be null");
// delegate the processing
ce.command.Process(so);
}
/// <summary>
/// Shut down the child pipelines.
/// </summary>
internal void ShutDown()
{
// we assume that command entries are never null
foreach (CommandEntry ce in _commandEntryList)
{
Diagnostics.Assert(ce != null, "ce != null");
ce.command.ShutDown();
ce.command = null;
}
// we assume we always have a default command entry
Diagnostics.Assert(_defaultCommandEntry != null, "defaultCommandEntry != null");
_defaultCommandEntry.command.ShutDown();
_defaultCommandEntry.command = null;
}
public void Dispose()
{
// we assume that command entries are never null
foreach (CommandEntry ce in _commandEntryList)
{
Diagnostics.Assert(ce != null, "ce != null");
ce.Dispose();
}
// we assume we always have a default command entry
Diagnostics.Assert(_defaultCommandEntry != null, "defaultCommandEntry != null");
_defaultCommandEntry.Dispose();
}
/// <summary>
/// It selects the applicable out command (it can be the default one)
/// to process the current pipeline object.
/// </summary>
/// <param name="so">Pipeline object to be processed.</param>
/// <returns>Applicable command entry.</returns>
private CommandEntry GetActiveCommandEntry(PSObject so)
{
string typeName = PSObjectHelper.PSObjectIsOfExactType(so.InternalTypeNames);
foreach (CommandEntry ce in _commandEntryList)
{
if (ce.AppliesToType(typeName))
return ce;
}
// failed any match: return the default handler
return _defaultCommandEntry;
}
private LineOutput _lo = null;
/// <summary>
/// List of command entries, each with a set of applicable types.
/// </summary>
private readonly List<CommandEntry> _commandEntryList = new List<CommandEntry>();
/// <summary>
/// Default command entry to be executed when all type matches fail.
/// </summary>
private CommandEntry _defaultCommandEntry = new CommandEntry();
}
}
|