// // Copyright (C) Microsoft. All rights reserved. // using System; using System.Management.Automation; namespace Microsoft.PowerShell.Commands { /// /// Base class for object-based event registration. /// public abstract class ObjectEventRegistrationBase : PSCmdlet { #region parameters /// /// Parameter for an identifier for this event subscription /// [Parameter(Position = 100)] public string SourceIdentifier { get { return sourceIdentifier; } set { sourceIdentifier = value; } } private string sourceIdentifier = Guid.NewGuid().ToString(); /// /// Parameter for any action to be invoked when the event arrives /// [Parameter(Position = 101)] public ScriptBlock Action { get { return action; } set { action = value; } } private ScriptBlock action = null; /// /// Parameter for additional data to be associated with this event subscription /// [Parameter] public PSObject MessageData { get { return messageData; } set { messageData = value; } } private PSObject messageData = null; /// /// Parameter for the flag that determines if this subscription is used to support /// other subscriptions /// [Parameter] public SwitchParameter SupportEvent { get { return supportEvent; } set { supportEvent = value; } } private SwitchParameter supportEvent = new SwitchParameter(); /// /// Parameter for the flag that determines whether this /// subscription will forward its events to the PowerShell client during remote executions /// [Parameter] public SwitchParameter Forward { get { return forward; } set { forward = value; } } private SwitchParameter forward = new SwitchParameter(); /// /// Parameter to indicate that the subscriber should be auto-unregistered after being triggered for specified times. /// If the value is equal or less than zero, there is no limit on the number of times the event can be triggered without being unregistered /// [Parameter] public int MaxTriggerCount { get { return _maxTriggerCount; } set { _maxTriggerCount = value <= 0 ? 0 : value; } } private int _maxTriggerCount = 0; #endregion parameters /// /// Returns the object that generates events to be monitored /// protected abstract Object GetSourceObject(); /// /// Returns the event name to be monitored on the input object /// protected abstract String GetSourceObjectEventName(); /// /// Gets the subscriber generated by this command /// protected PSEventSubscriber NewSubscriber { get { return newSubscriber; } } private PSEventSubscriber newSubscriber; /// /// Check arguments /// protected override void BeginProcessing() { if (((bool)forward) && (action != null)) { ThrowTerminatingError( new ErrorRecord( new ArgumentException(EventingResources.ActionAndForwardNotSupported), "ACTION_AND_FORWARD_NOT_SUPPORTED", ErrorCategory.InvalidOperation, null)); } } /// /// Subscribe to the event on the object /// protected override void EndProcessing() { Object inputObject = PSObject.Base(GetSourceObject()); string eventName = GetSourceObjectEventName(); try { if ( ((inputObject != null) || (eventName != null)) && (Events.GetEventSubscribers(sourceIdentifier).GetEnumerator().MoveNext()) ) { // Detect if the event identifier already exists ErrorRecord errorRecord = new ErrorRecord( new ArgumentException( String.Format( System.Globalization.CultureInfo.CurrentCulture, EventingResources.SubscriberExists, sourceIdentifier)), "SUBSCRIBER_EXISTS", ErrorCategory.InvalidArgument, inputObject); WriteError(errorRecord); } else { newSubscriber = Events.SubscribeEvent( inputObject, eventName, sourceIdentifier, messageData, action, (bool) supportEvent, (bool) forward, _maxTriggerCount); if((action != null) && (! (bool) supportEvent)) WriteObject(newSubscriber.Action); } } catch(ArgumentException e) { ErrorRecord errorRecord = new ErrorRecord( e, "INVALID_REGISTRATION", ErrorCategory.InvalidArgument, inputObject); WriteError(errorRecord); } catch (InvalidOperationException e) { ErrorRecord errorRecord = new ErrorRecord( e, "INVALID_REGISTRATION", ErrorCategory.InvalidOperation, inputObject); WriteError(errorRecord); } } } }