Beispiel #1
0
        /// <summary>
        /// Creates a <see cref="System.Management.Automation.Job"/> object that invokes a static method
        /// (of the class named by <see cref="Microsoft.PowerShell.Cmdletization.CmdletAdapter&lt;TObjectInstance&gt;.ClassName"/>)
        /// in the wrapped object model.
        /// </summary>
        /// <param name="session">Remote session to invoke the method in.</param>
        /// <param name="methodInvocationInfo">Method invocation details.</param>
        internal override StartableJob CreateStaticMethodInvocationJob(CimSession session, MethodInvocationInfo methodInvocationInfo)
        {
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, isStaticCmdlet: true);

            if (tracker.IsSessionTerminated(session))
            {
                return(null);
            }

            if (!IsSupportedSession(session, tracker))
            {
                return(null);
            }

            CimJobContext jobContext = this.CreateJobContext(session, targetObject: null);

            StartableJob result;

            if (methodInvocationInfo.MethodName.Equals(CimCmdletAdapter.CreateInstance_MethodName, StringComparison.OrdinalIgnoreCase))
            {
                result = new CreateInstanceJob(
                    jobContext,
                    methodInvocationInfo);
            }
            else
            {
                result = new StaticMethodInvocationJob(
                    jobContext,
                    methodInvocationInfo);
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a <see cref="System.Management.Automation.Job"/> object that performs a query against the wrapped object model.
        /// </summary>
        /// <param name="session">Remote session to query</param>
        /// <param name="baseQuery">Query parameters</param>
        /// <returns><see cref="System.Management.Automation.Job"/> object that performs a query against the wrapped object model</returns>
        internal override StartableJob CreateQueryJob(CimSession session, QueryBuilder baseQuery)
        {
            CimQuery query = baseQuery as CimQuery;

            if (query == null)
            {
                throw new ArgumentNullException("baseQuery");
            }

            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, isStaticCmdlet: false);

            if (tracker.IsSessionTerminated(session))
            {
                return(null);
            }

            if (!IsSupportedSession(session, tracker))
            {
                return(null);
            }

            CimJobContext jobContext = this.CreateJobContext(session, targetObject: null);
            StartableJob  queryJob   = query.GetQueryJob(jobContext);

            return(queryJob);
        }
Beispiel #3
0
        internal override StartableJob CreateStaticMethodInvocationJob(CimSession session, MethodInvocationInfo methodInvocationInfo)
        {
            StartableJob            staticMethodInvocationJob;
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, true);

            if (!tracker.IsSessionTerminated(session))
            {
                if (this.IsSupportedSession(session, tracker))
                {
                    CimJobContext cimJobContext = this.CreateJobContext(session, null);
                    if (!methodInvocationInfo.MethodName.Equals("cim:CreateInstance", StringComparison.OrdinalIgnoreCase))
                    {
                        staticMethodInvocationJob = new StaticMethodInvocationJob(cimJobContext, methodInvocationInfo);
                    }
                    else
                    {
                        staticMethodInvocationJob = new CreateInstanceJob(cimJobContext, methodInvocationInfo);
                    }
                    return(staticMethodInvocationJob);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #4
0
        internal override StartableJob CreateQueryJob(CimSession session, QueryBuilder baseQuery)
        {
            CimQuery cimQuery = baseQuery as CimQuery;

            if (cimQuery != null)
            {
                TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, false);
                if (!tracker.IsSessionTerminated(session))
                {
                    if (this.IsSupportedSession(session, tracker))
                    {
                        CimJobContext cimJobContext = this.CreateJobContext(session, null);
                        StartableJob  queryJob      = cimQuery.GetQueryJob(cimJobContext);
                        return(queryJob);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                throw new ArgumentNullException("baseQuery");
            }
        }
Beispiel #5
0
        /// <summary>
        /// Creates a <see cref="System.Management.Automation.Job"/> object that invokes an instance method in the wrapped object model.
        /// </summary>
        /// <param name="session">Remote session to invoke the method in.</param>
        /// <param name="objectInstance">The object on which to invoke the method.</param>
        /// <param name="methodInvocationInfo">Method invocation details.</param>
        /// <param name="passThru"><see langword="true"/> if successful method invocations should emit downstream the <paramref name="objectInstance"/> being operated on.</param>
        /// <returns></returns>
        internal override StartableJob CreateInstanceMethodInvocationJob(CimSession session, CimInstance objectInstance, MethodInvocationInfo methodInvocationInfo, bool passThru)
        {
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, isStaticCmdlet: false);

            if (tracker.IsSessionTerminated(session))
            {
                return(null);
            }

            if (!IsSupportedSession(session, tracker))
            {
                return(null);
            }

            CimJobContext jobContext = this.CreateJobContext(session, objectInstance);

            Dbg.Assert(objectInstance != null, "Caller should verify objectInstance != null");

            StartableJob result;

            if (methodInvocationInfo.MethodName.Equals(CimCmdletAdapter.DeleteInstance_MethodName, StringComparison.OrdinalIgnoreCase))
            {
                result = new DeleteInstanceJob(
                    jobContext,
                    passThru,
                    objectInstance,
                    methodInvocationInfo);
            }
            else if (methodInvocationInfo.MethodName.Equals(CimCmdletAdapter.ModifyInstance_MethodName, StringComparison.OrdinalIgnoreCase))
            {
                result = new ModifyInstanceJob(
                    jobContext,
                    passThru,
                    objectInstance,
                    methodInvocationInfo);
            }
            else
            {
                result = new InstanceMethodInvocationJob(
                    jobContext,
                    passThru,
                    objectInstance,
                    methodInvocationInfo);
            }

            return(result);
        }
Beispiel #6
0
        internal override void StartJob()
        {
            lock (_jobStateLock)
            {
                if (_jobWasStopped)
                {
                    this.SetCompletedJobState(JobState.Stopped, null);
                    return;
                }

                Dbg.Assert(!_alreadyReachedCompletedState, "Job shouldn't reach completed state, before ThrottlingJob has a chance to register for job-completed/failed events");

                TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.JobContext.CmdletInvocationInfo);
                if (tracker.IsSessionTerminated(this.JobContext.Session))
                {
                    this.SetCompletedJobState(JobState.Failed, new OperationCanceledException());
                    return;
                }

                if (!_jobWasStarted)
                {
                    _jobWasStarted = true;
                    this.SetJobState(JobState.Running);
                }
            }

            // This invocation can block (i.e. by calling Job.ShouldProcess) and wait for pipeline thread to unblock it
            // Therefore we have to do the invocation outside of the pipeline thread.
            ThreadPool.QueueUserWorkItem(delegate
            {
                this.ExceptionSafeWrapper(delegate
                {
                    IObservable <T> observable = this.GetCimOperation();
                    if (observable != null)
                    {
                        observable.Subscribe(this);
                    }
                });
            });
        }
Beispiel #7
0
        internal override StartableJob CreateInstanceMethodInvocationJob(CimSession session, CimInstance objectInstance, MethodInvocationInfo methodInvocationInfo, bool passThru)
        {
            StartableJob            instanceMethodInvocationJob;
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.CmdletInvocationInfo, false);

            if (!tracker.IsSessionTerminated(session))
            {
                if (this.IsSupportedSession(session, tracker))
                {
                    CimJobContext cimJobContext = this.CreateJobContext(session, objectInstance);
                    if (!methodInvocationInfo.MethodName.Equals("cim:DeleteInstance", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!methodInvocationInfo.MethodName.Equals("cim:ModifyInstance", StringComparison.OrdinalIgnoreCase))
                        {
                            instanceMethodInvocationJob = new InstanceMethodInvocationJob(cimJobContext, passThru, objectInstance, methodInvocationInfo);
                        }
                        else
                        {
                            instanceMethodInvocationJob = new ModifyInstanceJob(cimJobContext, passThru, objectInstance, methodInvocationInfo);
                        }
                    }
                    else
                    {
                        instanceMethodInvocationJob = new DeleteInstanceJob(cimJobContext, passThru, objectInstance, methodInvocationInfo);
                    }
                    return(instanceMethodInvocationJob);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #8
0
 internal override void StartJob()
 {
     lock (this._jobStateLock)
     {
         if (!this._jobWasStopped)
         {
             TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.JobContext.CmdletInvocationInfo);
             if (!tracker.IsSessionTerminated(this.JobContext.Session))
             {
                 if (!this._jobWasStarted)
                 {
                     this._jobWasStarted = true;
                     base.SetJobState(JobState.Running);
                 }
             }
             else
             {
                 this.SetCompletedJobState(JobState.Failed, new OperationCanceledException());
                 return;
             }
         }
         else
         {
             this.SetCompletedJobState(JobState.Stopped, null);
             return;
         }
     }
     ThreadPool.QueueUserWorkItem(param0 => this.ExceptionSafeWrapper(delegate
     {
         IObservable <T> cimOperation = this.GetCimOperation();
         if (cimOperation != null)
         {
             cimOperation.Subscribe(this);
         }
     }));
 }
Beispiel #9
0
        internal void ReportJobFailure(IContainsErrorRecord exception)
        {
            TerminatingErrorTracker terminatingErrorTracker = TerminatingErrorTracker.GetTracker(this.JobContext.CmdletInvocationInfo);

            bool      sessionWasAlreadyTerminated = false;
            bool      isThisTerminatingError      = false;
            Exception brokenSessionException      = null;

            lock (_jobStateLock)
            {
                if (!_jobWasStopped)
                {
                    brokenSessionException = terminatingErrorTracker.GetExceptionIfBrokenSession(
                        this.JobContext.Session,
                        this.JobContext.CmdletInvocationContext.CmdletDefinitionContext.SkipTestConnection,
                        out sessionWasAlreadyTerminated);
                }
            }

            if (brokenSessionException != null)
            {
                string brokenSessionMessage = string.Format(
                    CultureInfo.InvariantCulture,
                    CmdletizationResources.CimJob_BrokenSession,
                    brokenSessionException.Message);
                exception = CimJobException.CreateWithFullControl(
                    this.JobContext,
                    brokenSessionMessage,
                    "CimJob_BrokenCimSession",
                    ErrorCategory.ResourceUnavailable,
                    brokenSessionException);
                isThisTerminatingError = true;
            }
            else
            {
                CimJobException cje = exception as CimJobException;
                if ((cje != null) && (cje.IsTerminatingError))
                {
                    terminatingErrorTracker.MarkSessionAsTerminated(this.JobContext.Session, out sessionWasAlreadyTerminated);
                    isThisTerminatingError = true;
                }
            }

            bool writeError = !sessionWasAlreadyTerminated;

            if (writeError)
            {
                lock (_jobStateLock)
                {
                    if (_jobWasStopped)
                    {
                        writeError = false;
                    }
                }
            }

            ErrorRecord errorRecord = exception.ErrorRecord;

            errorRecord.SetInvocationInfo(this.JobContext.CmdletInvocationInfo);
            errorRecord.PreserveInvocationInfoOnce = true;

            if (writeError)
            {
                lock (_jobStateLock)
                {
                    if (!_alreadyReachedCompletedState)
                    {
                        if (isThisTerminatingError)
                        {
                            this.Error.Add(errorRecord);
                            CmdletMethodInvoker <bool> methodInvoker = terminatingErrorTracker.GetErrorReportingDelegate(errorRecord);
                            this.Results.Add(new PSStreamObject(PSStreamObjectType.ShouldMethod, methodInvoker));
                        }
                        else
                        {
                            this.WriteError(errorRecord);
                        }
                    }
                }
            }

            this.SetCompletedJobState(JobState.Failed, errorRecord.Exception);
        }
Beispiel #10
0
        internal void ReportJobFailure(IContainsErrorRecord exception)
        {
            TerminatingErrorTracker tracker = TerminatingErrorTracker.GetTracker(this.JobContext.CmdletInvocationInfo);
            bool      flag  = false;
            bool      flag1 = false;
            Exception exceptionIfBrokenSession = null;

            lock (this._jobStateLock)
            {
                if (!this._jobWasStopped)
                {
                    exceptionIfBrokenSession = tracker.GetExceptionIfBrokenSession(this.JobContext.Session, this.JobContext.CmdletInvocationContext.CmdletDefinitionContext.SkipTestConnection, out flag);
                }
            }
            if (exceptionIfBrokenSession == null)
            {
                CimJobException cimJobException = exception as CimJobException;
                if (cimJobException != null && cimJobException.IsTerminatingError)
                {
                    tracker.MarkSessionAsTerminated(this.JobContext.Session, out flag);
                    flag1 = true;
                }
            }
            else
            {
                object[] message = new object[1];
                message[0] = exceptionIfBrokenSession.Message;
                string str = string.Format(CultureInfo.InvariantCulture, CmdletizationResources.CimJob_BrokenSession, message);
                exception = CimJobException.CreateWithFullControl(this.JobContext, str, "CimJob_BrokenCimSession", ErrorCategory.ResourceUnavailable, exceptionIfBrokenSession);
                flag1     = true;
            }
            bool flag2 = !flag;

            if (flag2)
            {
                lock (this._jobStateLock)
                {
                    if (this._jobWasStopped)
                    {
                        flag2 = false;
                    }
                }
            }
            ErrorRecord errorRecord = exception.ErrorRecord;

            errorRecord.SetInvocationInfo(this.JobContext.CmdletInvocationInfo);
            errorRecord.PreserveInvocationInfoOnce = true;
            if (flag2)
            {
                lock (this._jobStateLock)
                {
                    if (!this._alreadyReachedCompletedState)
                    {
                        if (!flag1)
                        {
                            this.WriteError(errorRecord);
                        }
                        else
                        {
                            base.Error.Add(errorRecord);
                            CmdletMethodInvoker <bool> errorReportingDelegate = tracker.GetErrorReportingDelegate(errorRecord);
                            base.Results.Add(new PSStreamObject(PSStreamObjectType.ShouldMethod, errorReportingDelegate));
                        }
                    }
                }
            }
            this.SetCompletedJobState(JobState.Failed, errorRecord.Exception);
        }