public CommandExecutorEventArgs(CommandExecutorContext context, object result)
		{
			if(context == null)
				throw new ArgumentNullException("context");

			_context = context;
			_result = result;
		}
Esempio n. 2
0
        public static CommandCompletionContext Create(CommandExecutorContext context, object result, Exception exception = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(new CommandCompletionContext(context.Executor, context.Expression, context.Parameter, result, exception));
        }
Esempio n. 3
0
        public CommandExecutorEventArgs(CommandExecutorContext context, object result)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            _context = context;
            _result  = result;
        }
Esempio n. 4
0
        public CommandContext(CommandExecutorContext session, CommandExpression expression, ICommand command, object parameter, IDictionary <string, object> extendedProperties = null)
        {
            _session    = session;
            _command    = command ?? throw new ArgumentNullException(nameof(command));
            _parameter  = parameter;
            _expression = expression;

            if (extendedProperties != null && extendedProperties.Count > 0)
            {
                _states = new Dictionary <string, object>(extendedProperties, StringComparer.OrdinalIgnoreCase);
            }
        }
Esempio n. 5
0
        protected CommandContext(CommandContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _session     = context._session;
            _expression  = context._expression;
            _command     = context._command;
            _commandNode = context._commandNode;
            _parameter   = context._parameter;
            _states      = context._states;
        }
Esempio n. 6
0
        protected virtual object OnExecute(CommandExecutorContext context)
        {
            var queue      = new Queue <Tuple <CommandExpression, CommandTreeNode> >();
            var expression = context.Expression;

            while (expression != null)
            {
                //查找指定路径的命令节点
                var node = this.Find(expression.FullPath);

                //如果指定的路径在命令树中是不存在的则抛出异常
                if (node == null)
                {
                    throw new CommandNotFoundException(expression.FullPath);
                }

                //将命令表达式的选项集绑定到当前命令上
                if (node.Command != null)
                {
                    expression.Options.Bind(node.Command);
                }

                //将找到的命令表达式和对应的节点加入队列中
                queue.Enqueue(new Tuple <CommandExpression, CommandTreeNode>(expression, node));

                //设置下一个待搜索的命令表达式
                expression = expression.Next;
            }

            //如果队列为空则返回空
            if (queue.Count < 1)
            {
                return(null);
            }

            //初始化第一个输入参数
            var parameter = context.Parameter;

            while (queue.Count > 0)
            {
                var entry = queue.Dequeue();

                //执行队列中的命令
                parameter = this.ExecuteCommand(context, entry.Item1, entry.Item2, parameter);
            }

            //返回最后一个命令的执行结果
            return(parameter);
        }
Esempio n. 7
0
        protected bool OnFailed(CommandExecutorContext context, Exception ex)
        {
            var args = new CommandExecutorFailureEventArgs(context, ex);

            //激发“Failed”事件
            this.OnFailed(args);

            //输出异常信息
            if (!args.ExceptionHandled && args.Exception != null)
            {
                this.Error.WriteLine(args.Exception);
            }

            return(args.ExceptionHandled);
        }
Esempio n. 8
0
        private void OnCompleted(CommandExecutorContext context, IEnumerable <CommandCompleterDescriptor> descriptors, Exception exception = null)
        {
            if (descriptors == null)
            {
                return;
            }

            foreach (var descriptor in descriptors)
            {
                try
                {
                    descriptor.Command.OnCompleted(CommandCompletionContext.Create(context, descriptor.Result, exception));
                }
                catch (Exception ex)
                {
                    Zongsoft.Diagnostics.Logger.Error(ex);
                }
            }
        }
Esempio n. 9
0
        protected virtual object ExecuteCommand(CommandExecutorContext context, CommandExpression expression, CommandTreeNode node, object parameter)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (node.Command == null)
            {
                return(null);
            }

            return(node.Command.Execute(this.CreateCommandContext(expression, node, parameter)));
        }
Esempio n. 10
0
        public CommandContext(CommandExecutorContext session, CommandExpression expression, CommandTreeNode commandNode, object parameter, IDictionary <string, object> extendedProperties = null)
        {
            if (commandNode == null)
            {
                throw new ArgumentNullException(nameof(commandNode));
            }

            if (commandNode.Command == null)
            {
                throw new ArgumentException($"The Command property of '{commandNode.FullPath}' command-node is null.");
            }

            _session     = session;
            _commandNode = commandNode;
            _command     = commandNode.Command;
            _parameter   = parameter;
            _expression  = expression;

            if (extendedProperties != null && extendedProperties.Count > 0)
            {
                _states = new Dictionary <string, object>(extendedProperties, StringComparer.OrdinalIgnoreCase);
            }
        }
		protected virtual object OnExecute(CommandExecutorContext context)
		{
			var queue = new Queue<Tuple<CommandExpression, CommandTreeNode>>();
			var expression = context.Expression;

			while(expression != null)
			{
				//查找指定路径的命令节点
				var node = this.Find(expression.FullPath);

				//如果指定的路径在命令树中是不存在的则抛出异常
				if(node == null)
					throw new CommandNotFoundException(expression.FullPath);

				//将命令表达式的选项集绑定到当前命令上
				if(node.Command != null)
					expression.Options.Bind(node.Command);

				//将找到的命令表达式和对应的节点加入队列中
				queue.Enqueue(new Tuple<CommandExpression, CommandTreeNode>(expression, node));

				//设置下一个待搜索的命令表达式
				expression = expression.Next;
			}

			//如果队列为空则返回空
			if(queue.Count < 1)
				return null;

			//初始化第一个输入参数
			var parameter = context.Parameter;

			while(queue.Count > 0)
			{
				var entry = queue.Dequeue();

				//执行队列中的命令
				parameter = this.ExecuteCommand(context, entry.Item1, entry.Item2, parameter);
			}

			//返回最后一个命令的执行结果
			return parameter;
		}
		protected override void OnExecute(CommandExecutorContext context)
		{
			var command = context.Command;

			if(command != null)
				context.Result = command.Execute(new TerminalCommandContext(this, context.CommandLine, context.CommandNode, context.Parameter));
		}
Esempio n. 13
0
        public object Execute(string commandText, object parameter = null)
        {
            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentNullException(nameof(commandText));
            }

            CommandExecutorContext context = null;

            try
            {
                //创建命令执行器上下文对象
                context = this.CreateExecutorContext(commandText, parameter);

                if (context == null)
                {
                    throw new InvalidOperationException("Create executor context failed.");
                }
            }
            catch (Exception ex)
            {
                //激发“Error”事件
                if (!this.OnFailed(context, ex))
                {
                    throw;
                }

                return(null);
            }

            //创建事件参数对象
            var executingArgs = new CommandExecutorExecutingEventArgs(context);

            //激发“Executing”事件
            this.OnExecuting(executingArgs);

            if (executingArgs.Cancel)
            {
                return(executingArgs.Result);
            }

            object result = null;

            try
            {
                //调用执行请求
                result = this.OnExecute(context);
            }
            catch (Exception ex)
            {
                //激发“Error”事件
                if (!this.OnFailed(context, ex))
                {
                    throw;
                }
            }

            //创建事件参数对象
            var executedArgs = new CommandExecutorExecutedEventArgs(context, result);

            //激发“Executed”事件
            this.OnExecuted(executedArgs);

            //返回最终的执行结果
            return(executedArgs.Result);
        }
Esempio n. 14
0
 public CommandExecutorExecutingEventArgs(CommandExecutorContext context, bool cancel = false) : base(context)
 {
     _cancel = cancel;
 }
 public CommandExecutorFailureEventArgs(CommandExecutorContext context, Exception exception, bool handled) : base(exception, handled)
 {
     _context = context;
 }
		protected bool OnFailed(CommandExecutorContext context, Exception ex)
		{
			var args = new CommandExecutorFailureEventArgs(context, ex);

			//激发“Failed”事件
			this.OnFailed(args);

			//输出异常信息
			if(!args.ExceptionHandled && args.Exception != null)
				this.Error.WriteLine(args.Exception);

			return args.ExceptionHandled;
		}
Esempio n. 17
0
 public CommandExecutorEventArgs(CommandExecutorContext context)
 {
     _context = context ?? throw new ArgumentNullException(nameof(context));
 }
Esempio n. 18
0
        protected virtual object OnExecute(CommandExecutorContext context, out IEnumerable <CommandCompleterDescriptor> descriptors)
        {
            var queue      = new Queue <Tuple <CommandExpression, CommandTreeNode> >();
            var expression = context.Expression;

            //设置输出参数默认值
            descriptors = System.Linq.Enumerable.Empty <CommandCompleterDescriptor>();

            while (expression != null)
            {
                //查找指定路径的命令节点
                var node = this.Find(expression.FullPath);

                //如果指定的路径在命令树中是不存在的则抛出异常
                if (node == null)
                {
                    throw new CommandNotFoundException(expression.FullPath);
                }

                //将命令表达式的选项集绑定到当前命令上
                if (node.Command != null)
                {
                    expression.Options.Bind(node.Command);
                }

                //将找到的命令表达式和对应的节点加入队列中
                queue.Enqueue(new Tuple <CommandExpression, CommandTreeNode>(expression, node));

                //设置下一个待搜索的命令表达式
                expression = expression.Next;
            }

            //如果队列为空则返回空
            if (queue.Count < 1)
            {
                return(null);
            }

            //创建输出参数的列表对象
            descriptors = new List <CommandCompleterDescriptor>();

            //初始化第一个输入参数
            var parameter = context.Parameter;

            while (queue.Count > 0)
            {
                var entry = queue.Dequeue();

                //执行队列中的命令
                parameter = this.ExecuteCommand(context, entry.Item1, entry.Item2, parameter);

                //判断命令是否需要清理,如果是则加入到清理列表中
                if (entry.Item2 != null && entry.Item2.Command is ICommandCompletion)
                {
                    ((IList <CommandCompleterDescriptor>)descriptors).Add(new CommandCompleterDescriptor((ICommandCompletion)entry.Item2.Command, parameter));
                }
            }

            //返回最后一个命令的执行结果
            return(parameter);
        }
 public CommandExecutorExecutedEventArgs(CommandExecutorContext context) : base(context)
 {
 }
Esempio n. 20
0
 protected virtual CommandContext CreateCommandContext(CommandExecutorContext session, CommandExpression expression, CommandTreeNode node, object parameter)
 {
     return(node == null || node.Command == null ? null : new CommandContext(session, expression, node, parameter));
 }
Esempio n. 21
0
        protected virtual object OnExecute(CommandExecutorContext session, out IEnumerable <CommandCompletionContext> completes)
        {
            var queue      = new Queue <Tuple <CommandExpression, CommandTreeNode> >();
            var expression = session.Expression;

            //设置输出参数默认值
            completes = System.Linq.Enumerable.Empty <CommandCompletionContext>();

            while (expression != null)
            {
                //查找指定路径的命令节点
                var node = this.Find(expression.FullPath);

                //如果指定的路径在命令树中是不存在的则抛出异常
                if (node == null)
                {
                    throw new CommandNotFoundException(expression.FullPath);
                }

                //将命令表达式的选项集绑定到当前命令上
                if (node.Command != null)
                {
                    expression.Options.Bind(node.Command);
                }

                //将找到的命令表达式和对应的节点加入队列中
                queue.Enqueue(new Tuple <CommandExpression, CommandTreeNode>(expression, node));

                //设置下一个待搜索的命令表达式
                expression = expression.Next;
            }

            //如果队列为空则返回空
            if (queue.Count < 1)
            {
                return(null);
            }

            //创建输出参数的列表对象
            completes = new List <CommandCompletionContext>();

            //初始化第一个输入参数
            var parameter = session.Parameter;

            while (queue.Count > 0)
            {
                var entry = queue.Dequeue();

                //创建命令执行上下文
                var context = this.CreateCommandContext(session, entry.Item1, entry.Item2, parameter);

                //如果命令上下文为空或命令空则直接返回
                if (context == null || context.Command == null)
                {
                    return(null);
                }

                //执行当前命令
                parameter = this.OnExecuteCommand(context);

                //判断命令是否需要完成通知,如果是则加入到清理列表中
                if (context.Command is ICommandCompletion completion)
                {
                    ((ICollection <CommandCompletionContext>)completes).Add(new CommandCompletionContext(context, parameter));
                }
            }

            //返回最后一个命令的执行结果
            return(parameter);
        }
Esempio n. 22
0
        public object Execute(string commandText, object parameter = null)
        {
            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentNullException(nameof(commandText));
            }

            CommandExecutorContext context = null;

            try
            {
                //创建命令执行器上下文对象
                context = this.CreateExecutorContext(commandText, parameter);

                if (context == null)
                {
                    throw new InvalidOperationException("Create executor context failed.");
                }
            }
            catch (Exception ex)
            {
                //激发“Error”事件
                if (!this.OnFailed(context, ex))
                {
                    throw;
                }

                return(null);
            }

            //创建事件参数对象
            var executingArgs = new CommandExecutorExecutingEventArgs(context);

            //激发“Executing”事件
            this.OnExecuting(executingArgs);

            if (executingArgs.Cancel)
            {
                return(executingArgs.Result);
            }

            var complateInvoked = false;
            IEnumerable <CommandCompletionContext> completes = null;

            try
            {
                //调用执行请求
                context.Result = this.OnExecute(context, out completes);
            }
            catch (Exception ex)
            {
                complateInvoked = true;

                //执行命令完成通知
                this.OnCompletes(completes, ex);

                //激发“Error”事件
                if (!this.OnFailed(context, ex))
                {
                    throw;
                }
            }

            //执行命令完成通知
            if (!complateInvoked)
            {
                this.OnCompletes(completes, null);
            }

            //创建事件参数对象
            var executedArgs = new CommandExecutorExecutedEventArgs(context);

            //激发“Executed”事件
            this.OnExecuted(executedArgs);

            //返回最终的执行结果
            return(executedArgs.Result);
        }
 public CommandExecutorExecutedEventArgs(CommandExecutorContext context, object result) : base(context, result)
 {
 }
Esempio n. 24
0
			protected override void OnExecute(CommandExecutorContext context)
			{
				var args = context.Parameter as ReceivedEventArgs;

				if(args == null)
					throw new InvalidOperationException("Invalid execution parameter.");

				context.Result = context.Command.Execute(
					new FtpCommandContext(
						this,
						context.Command,
						(FtpServerChannel)args.Channel,
						(FtpStatement)args.ReceivedObject));
			}
 public CommandExecutorFailureEventArgs(CommandExecutorContext context, Exception exception) : base(exception, false)
 {
     _context = context;
 }
		protected virtual object ExecuteCommand(CommandExecutorContext context, CommandExpression expression, CommandTreeNode node, object parameter)
		{
			if(context == null)
				throw new ArgumentNullException(nameof(context));

			if(node == null)
				throw new ArgumentNullException(nameof(node));

			if(node.Command == null)
				return null;

			return node.Command.Execute(this.CreateCommandContext(expression, node, parameter));
		}