public final class ExplicitProcessParameters extends Object
It is strongly recommended for any feature that performs execution of a process to support ExplicitProcessParameters
,
from a contextual Lookup
, or at worst from Lookup.getDefault()
. It will allow for future customizations and
automation of the feature, enhancing the process launch for various environments, technologies etc.
Note: please refer also to StartupExtender
API in the extexecution
module, which contributes globally
to launcher arguments.
Two groups of parameters are recognized: ExplicitProcessParameters.getLauncherArguments()
, which should be passed
first to the process (i.e. launcher parameters) and ExplicitProcessParameters.getArguments()
that represent the ordinary
process arguments.
java
executable is used to launch the application, or even Maven project (see below), the launcherArgs should correspond to VM
arguments, and args correspond to the main class' arguments (passed to the main class). Additional environment variables can be specified.
If the object is marked as ExplicitProcessParameters.isArgReplacement()
, the launcher implementor SHOULD replace all
default or configured parameters with contents of this instruction. Both arguments and launcherArguments can have value null
, which means "undefined":
in that case, the relevant group of configured parameters should not be affected.
Since these parameters are passed externally, there's an utility method, ExplicitProcessParameters.buildExplicitParameters(org.openide.util.Lookup)
that builds the explicit parameter instruction based on Lookup
contents. The parameters are
merged in the order of the configured rank
and appearance (in the sort ascending order).
The default rank is 0
, which allows both append or prepend parameters. If an item's
ExplicitProcessParameters.isArgReplacement()
is true, all arguments collected so far are discarded.
// collect all instructions from the Lookup and build decorating parametersExplicitProcessParameters
decorator =ExplicitProcessParameters
.buildExplicitParameters(runContext);ExplicitProcessParameters
params =ExplicitProcessParameters
.builder(). // include project's (or pre-configured) application parameters args(getProjectAppParams()). // include project's (or pre-configured) VM parameters as launcher ones launcherArgs(getProjectVMParams()). // now combine with the decorating instructions: will append or reset args or launcher args combine(decorator). build(); // build a commandline for e.g. launcher or VM: include launcher args first // then the fixed "middle part" (e.g. the main class name), then application arguments.List
<String
> commandLine = params.getAllArguments("theMainClass");
runContext
Lookup.
Supposing that a Maven project module supports ExplicitProcessParameters
(it does from version 2/2.144), the caller may influence or override the
parameters passed to the maven exec:exec task (for Run action) this way:
ActionProvider ap = ... ; // obtain ActionProvider from the project.
ExplicitProcessParameters explicit = ExplicitProcessParameters.builder().
launcherArg("-DvmArg2=2").
arg("paramY").
build();
ap.invokeAction(ActionProvider.COMMAND_RUN, Lookups.fixed(explicit));
By default, args instruction(s) will discard the default parameters, so the above example will also ignore all application
parameters provided in maven action mapping. The caller may, for example, want to just append parameters (i.e. list of files ?) and
completely replace (default) VM parameters which may be unsuitable for the operation:
ExplicitProcessParameters
override =ExplicitProcessParameters
.builder(). // override the default: do not append to the base ones, but discard them replaceLauncherArgs(true). // ... and insist on empty launcher args launcherArgs(). // ... or some other specific one(s) launcherArgs("-Xmx1000m"). // override the default: keep base application arguments replaceArgs(false). // and add file list args(files). build();
Note that multiple ExplicitProcessParameters
instances may be added to the Lookup, acting as append or replacement
for the parameters collected so far.
Modifier and Type | Class and Description |
---|---|
static class |
ExplicitProcessParameters.Builder
Builds the
ExplicitProcessParameters instance. |
Modifier and Type | Method and Description |
---|---|
static ExplicitProcessParameters.Builder |
builder() |
static ExplicitProcessParameters |
buildExplicitParameters(Collection<? extends ExplicitProcessParameters> items)
Merges individual instruction.
|
static ExplicitProcessParameters |
buildExplicitParameters(Lookup context)
Merges ExplicitProcessParameters instructions found in the Lookup.
|
static ExplicitProcessParameters |
empty()
Returns an empty instance of parameters that has no effect.
|
List<String> |
getAllArguments(List<String> middle)
Returns the argument lists merged.
|
List<String> |
getAllArguments(String... middle)
Returns the argument lists merged.
|
List<String> |
getArguments()
Returns the arguments to be passed.
|
Map<String,String> |
getEnvironmentVariables()
Returns a map of additional environment variables to be set for the process.
|
List<String> |
getLauncherArguments()
Returns the launcher arguments to be passed.
|
File |
getWorkingDirectory()
Returns working directory to be set for the process.
|
boolean |
isArgReplacement()
Instructs to replace arguments collected so far.
|
boolean |
isEmpty()
Returns true, if the instance has no effect when
ExplicitProcessParameters.Builder.combine(org.netbeans.api.extexecution.base.ExplicitProcessParameters) d onto base parameters. |
boolean |
isLauncherArgReplacement()
Instructs to replace launcher arguments collected so far.
|
public static ExplicitProcessParameters empty()
ExplicitProcessParameters.isEmpty()
.public boolean isEmpty()
ExplicitProcessParameters.Builder.combine(org.netbeans.api.extexecution.base.ExplicitProcessParameters)
d onto base parameters.public List<String> getArguments()
null
if the object does not
want to alter the argument list.null
if the argument list should not be altered.public List<String> getLauncherArguments()
null
if the object does not
want to alter the argument list.null
if the launcher argument list should not be altered.public boolean isArgReplacement()
public boolean isLauncherArgReplacement()
@NonNull public List<String> getAllArguments(List<String> middle)
middle
(if any), then (normal) arguments. The method is a convenience to build
a complete command line for the launcher + command + command arguments.@NonNull public List<String> getAllArguments(@NullAllowed String... middle)
middle
(if any), then (normal) arguments. The method is a convenience to build
a complete command line for the launcher + command + command arguments.@CheckForNull public File getWorkingDirectory()
nul
@NonNull public Map<String,String> getEnvironmentVariables()
null
value of a variable should be interpreted as a removal
of that variable from the environment.@NonNull public static ExplicitProcessParameters buildExplicitParameters(Lookup context)
ExplicitProcessParameters.buildExplicitParameters(java.util.Collection)
for more details.context
- context for the executionpublic static ExplicitProcessParameters buildExplicitParameters(Collection<? extends ExplicitProcessParameters> items)
ExplicitProcessParameters
. It is strongly recommended to support explicit parameters in order to allow for
customizations and automation.
Processes instructions in the order of ExplicitProcessParameters.Builder.position(int)
and appearance. Whenever an item is flagged as
a replacement, all arguments (launcher arguments) collected to that point are discarded. Item's arguments (launcher arguments)
will become the only ones listed.
Note: if a replacement instruction and all the following (if any) have ExplicitProcessParameters.getArguments()
null
(= no change),
the result will report no change. It is therefore possible to discard all contributions by appending a no-change replacement
last.
Environment variables are overridden by newly set variables.
items
- individual instructions.public static ExplicitProcessParameters.Builder builder()