// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. namespace System.Management.Automation { /// /// Define all the output streams and one input stream for a workflow. /// public sealed class PowerShellStreams : IDisposable { /// /// Input stream for incoming objects. /// private PSDataCollection _inputStream; /// /// Output stream for returned objects. /// private PSDataCollection _outputStream; /// /// Error stream for error messages. /// private PSDataCollection _errorStream; /// /// Warning stream for warning messages. /// private PSDataCollection _warningStream; /// /// Progress stream for progress messages. /// private PSDataCollection _progressStream; /// /// Verbose stream for verbose messages. /// private PSDataCollection _verboseStream; /// /// Debug stream for debug messages. /// private PSDataCollection _debugStream; /// /// Information stream for Information messages. /// private PSDataCollection _informationStream; /// /// If the object is already disposed or not. /// private bool _disposed; /// /// Private object for thread-safe execution. /// private readonly object _syncLock = new object(); /// /// Default constructor. /// public PowerShellStreams() { _inputStream = null; _outputStream = null; _errorStream = null; _warningStream = null; _progressStream = null; _verboseStream = null; _debugStream = null; _informationStream = null; _disposed = false; } /// /// Default constructor. /// public PowerShellStreams(PSDataCollection pipelineInput) { // Populate the input collection if there is any... _inputStream = pipelineInput ?? new PSDataCollection(); _inputStream.Complete(); _outputStream = new PSDataCollection(); _errorStream = new PSDataCollection(); _warningStream = new PSDataCollection(); _progressStream = new PSDataCollection(); _verboseStream = new PSDataCollection(); _debugStream = new PSDataCollection(); _informationStream = new PSDataCollection(); _disposed = false; } /// /// Dispose implementation. /// public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } /// /// Protected virtual implementation of Dispose. /// /// private void Dispose(bool disposing) { if (_disposed) return; lock (_syncLock) { if (!_disposed) { if (disposing) { _inputStream.Dispose(); _outputStream.Dispose(); _errorStream.Dispose(); _warningStream.Dispose(); _progressStream.Dispose(); _verboseStream.Dispose(); _debugStream.Dispose(); _informationStream.Dispose(); _inputStream = null; _outputStream = null; _errorStream = null; _warningStream = null; _progressStream = null; _verboseStream = null; _debugStream = null; _informationStream = null; } _disposed = true; } } } /// /// Gets input stream. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public PSDataCollection InputStream { get { return _inputStream; } set { _inputStream = value; } } /// /// Gets output stream. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public PSDataCollection OutputStream { get { return _outputStream; } set { _outputStream = value; } } /// /// Gets error stream. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public PSDataCollection ErrorStream { get { return _errorStream; } set { _errorStream = value; } } /// /// Gets warning stream. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public PSDataCollection WarningStream { get { return _warningStream; } set { _warningStream = value; } } /// /// Gets progress stream. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public PSDataCollection ProgressStream { get { return _progressStream; } set { _progressStream = value; } } /// /// Gets verbose stream. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public PSDataCollection VerboseStream { get { return _verboseStream; } set { _verboseStream = value; } } /// /// Get debug stream. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public PSDataCollection DebugStream { get { return _debugStream; } set { _debugStream = value; } } /// /// Gets Information stream. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public PSDataCollection InformationStream { get { return _informationStream; } set { _informationStream = value; } } /// /// Marking all the streams as completed so that no further data can be added and /// jobs will know that there is no more data coming in. /// public void CloseAll() { if (!_disposed) { lock (_syncLock) { if (!_disposed) { _outputStream.Complete(); _errorStream.Complete(); _warningStream.Complete(); _progressStream.Complete(); _verboseStream.Complete(); _debugStream.Complete(); _informationStream.Complete(); } } } } } }