protected internal virtual void HandleInvocationInContext(DelegateInvocation invocation)
        {
            var commandContext   = Context.CommandContext;
            var oldValue         = commandContext.AuthorizationCheckEnabled;
            var contextExecution = invocation.ContextExecution;

            ProcessEngineConfigurationImpl configuration = Context.ProcessEngineConfiguration;

            var popExecutionContext = false;

            try
            {
                if (!configuration.AuthorizationEnabledForCustomCode)
                {
                    // the custom code should be executed without authorization
                    commandContext.DisableAuthorizationCheck();
                }

                try
                {
                    commandContext.DisableUserOperationLog();

                    try
                    {
                        if ((contextExecution != null) && !IsCurrentContextExecution(contextExecution))
                        {
                            popExecutionContext = SetExecutionContext(contextExecution);
                        }

                        invocation.Proceed();
                    }
                    //TODO Debug
                    catch (System.Exception e)
                    {
                        throw e;
                    }
                    finally
                    {
                        if (popExecutionContext)
                        {
                            Context.RemoveExecutionContext();
                        }
                    }
                }
                finally
                {
                    commandContext.EnableUserOperationLog();
                }
            }
            finally
            {
                if (oldValue)
                {
                    commandContext.EnableAuthorizationCheck();
                }
            }
        }
        protected internal virtual IProcessApplicationReference GetProcessApplicationForInvocation(
            DelegateInvocation invocation)
        {
            var contextExecution = invocation.ContextExecution;
            var contextResource  = invocation.ContextResource;

            if (contextExecution != null)
            {
                return(ProcessApplicationContextUtil.GetTargetProcessApplication(contextExecution as CoreExecution));
            }
            if (contextResource != null)
            {
                return(ProcessApplicationContextUtil.GetTargetProcessApplication(contextResource));
            }
            return(null);
        }
        public virtual void HandleInvocation(DelegateInvocation invocation)
        {
            var processApplication = GetProcessApplicationForInvocation(invocation);

            if ((processApplication != null) && ProcessApplicationContextUtil.RequiresContextSwitch(processApplication))
            {
                Context.ExecuteWithinProcessApplication <object>(() =>
                {
                    HandleInvocation(invocation);
                    return(null);
                },
                                                                 processApplication, new InvocationContext(invocation.ContextExecution));
            }
            else
            {
                HandleInvocationInContext(invocation);
            }
        }