2016-03-30 14:20:52 -07:00
|
|
|
/********************************************************************++
|
|
|
|
|
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
--********************************************************************/
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
namespace System.Management.Automation
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Command factory provides a generic interface to create different types of commands.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class CommandFactory
|
|
|
|
|
{
|
|
|
|
|
#region private_members
|
|
|
|
|
|
2016-07-29 13:02:49 -07:00
|
|
|
private ExecutionContext _context;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
#endregion private_members
|
|
|
|
|
|
|
|
|
|
#region public_properties
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execution context under which the command should be created.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal ExecutionContext Context
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
get { return _context; }
|
|
|
|
|
set { _context = value; }
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion public_properties
|
|
|
|
|
|
|
|
|
|
#region ctor
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the new instance of CommandFactory class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal CommandFactory()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the new instance of CommandFactory class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">Execution context.</param>
|
|
|
|
|
internal CommandFactory(ExecutionContext context)
|
|
|
|
|
{
|
|
|
|
|
this.Context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion ctor
|
|
|
|
|
|
|
|
|
|
#region public_methods
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-08-28 05:44:55 +00:00
|
|
|
/// Creates a command object corresponding to specified name. The command processor will use global scope.
|
2016-03-30 14:20:52 -07:00
|
|
|
/// </summary>
|
2016-08-28 05:44:55 +00:00
|
|
|
/// <param name="commandName">Creates a command object corresponding to specified name.</param>
|
2016-03-30 14:20:52 -07:00
|
|
|
/// <param name="commandOrigin"> Location where the command was dispatched from. </param>
|
|
|
|
|
/// <returns>Created command processor object.</returns>
|
|
|
|
|
/// <exception cref="InvalidOperationException">
|
|
|
|
|
/// Thrown if session state does not contain the CommandDiscovery instance.
|
|
|
|
|
/// </exception>
|
|
|
|
|
internal CommandProcessorBase CreateCommand(string commandName, CommandOrigin commandOrigin)
|
|
|
|
|
{
|
|
|
|
|
return _CreateCommand(commandName, commandOrigin, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-08-28 05:44:55 +00:00
|
|
|
/// Creates a command object corresponding to specified name.
|
2016-03-30 14:20:52 -07:00
|
|
|
/// </summary>
|
2016-08-28 05:44:55 +00:00
|
|
|
/// <param name="commandName">Creates a command object corresponding to specified name.</param>
|
2016-03-30 14:20:52 -07:00
|
|
|
/// <param name="commandOrigin"> Location where the command was dispatched from. </param>
|
2017-01-16 13:31:14 -08:00
|
|
|
/// <param name="useLocalScope">
|
2016-03-30 14:20:52 -07:00
|
|
|
/// True if command processor should use local scope to execute the command,
|
|
|
|
|
/// False otherwise.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>Created command processor object.</returns>
|
|
|
|
|
/// <exception cref="InvalidOperationException">
|
|
|
|
|
/// Thrown if session state does not contain the CommandDiscovery instance.
|
|
|
|
|
/// </exception>
|
|
|
|
|
internal CommandProcessorBase CreateCommand(string commandName,
|
|
|
|
|
CommandOrigin commandOrigin, bool? useLocalScope)
|
|
|
|
|
{
|
|
|
|
|
return _CreateCommand(commandName, commandOrigin, useLocalScope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-08-28 05:44:55 +00:00
|
|
|
/// Creates a command object corresponding to specified name.
|
2016-03-30 14:20:52 -07:00
|
|
|
/// </summary>
|
2016-08-28 05:44:55 +00:00
|
|
|
/// <param name="commandName">Creates a command object corresponding to specified name.</param>
|
2016-03-30 14:20:52 -07:00
|
|
|
/// <param name="executionContext">Execution Context.</param>
|
|
|
|
|
/// <param name="commandOrigin"> Location where the command was dispatched from. </param>
|
|
|
|
|
/// <returns>Created command processor object.</returns>
|
|
|
|
|
/// <exception cref="InvalidOperationException">
|
|
|
|
|
/// Thrown if session state does not contain the CommandDiscovery instance.
|
|
|
|
|
/// </exception>
|
|
|
|
|
internal CommandProcessorBase CreateCommand(string commandName, ExecutionContext executionContext, CommandOrigin commandOrigin)
|
|
|
|
|
{
|
|
|
|
|
this.Context = executionContext;
|
|
|
|
|
return _CreateCommand(commandName, commandOrigin, false);
|
|
|
|
|
}
|
|
|
|
|
#endregion public_methods
|
|
|
|
|
|
|
|
|
|
#region helper_methods
|
|
|
|
|
|
|
|
|
|
private CommandProcessorBase _CreateCommand(string commandName,
|
|
|
|
|
CommandOrigin commandOrigin, bool? useLocalScope)
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
if (_context == null)
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
|
|
|
|
throw PSTraceSource.NewInvalidOperationException(DiscoveryExceptions.ExecutionContextNotSet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Look for a cmdlet...
|
2016-07-29 13:02:49 -07:00
|
|
|
CommandDiscovery discovery = _context.CommandDiscovery;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
if (discovery == null)
|
|
|
|
|
{
|
|
|
|
|
throw PSTraceSource.NewInvalidOperationException(DiscoveryExceptions.CommandDiscoveryMissing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Look for the command using command discovery mechanisms. This will resolve
|
|
|
|
|
// aliases, functions, filters, cmdlets, scripts, and applications.
|
|
|
|
|
|
|
|
|
|
return discovery.LookupCommandProcessor(commandName, commandOrigin, useLocalScope);
|
|
|
|
|
}
|
|
|
|
|
#endregion helper_methods
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|