@NotThreadSafe public abstract class AbstractCommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>> extends Object implements com.atlassian.bitbucket.scm.CommandBuilder<B>
CommandBuilder
functionality, to simplify creating builders.
All CommandBuilder
and CommandBuilderSupport
methods are implemented except for the actual
build
method.Modifier and Type | Field and Description |
---|---|
protected LinkedList<String> |
arguments
Maintains an ordered list of arguments.
|
protected String |
binary
The binary to execute when the command is run.
|
protected Map<String,String> |
environment
Maintains a map of environment variables and their values.
|
protected com.atlassian.bitbucket.scm.CommandErrorHandler |
errorHandler
The
CommandErrorHandler to receive and process the InputStream for reading data from the
command's error stream. |
protected com.atlassian.bitbucket.scm.CommandExitHandler |
exitHandler
The
CommandExitHandler to be invoked when the command runs. |
protected com.atlassian.bitbucket.i18n.I18nService |
i18nService
The
I18nService , used to internationalise error messages for exceptions thrown during processing. |
protected com.atlassian.bitbucket.scm.CommandInputHandler |
inputHandler
The
CommandInputHandler to receive and process the OutputStream for feeding data into the
command. |
protected Path |
workDir
The working directory in which the command should be run.
|
protected File |
workingDirectory
Deprecated.
in 6.4 for removal in 8.0. Use
workDir instead. |
Modifier | Constructor and Description |
---|---|
protected |
AbstractCommandBuilder(com.atlassian.bitbucket.i18n.I18nService i18nService,
String binary)
Constructs a new
AbstractCommandBuilder and sets the I18nService to use for internationalising
error messages and the binary to execute. |
protected |
AbstractCommandBuilder(com.atlassian.bitbucket.i18n.I18nService i18nService,
String binary,
File workingDirectory)
Deprecated.
in 6.4 for removal in 8.0. Use
AbstractCommandBuilder(I18nService, String, Path) instead. |
protected |
AbstractCommandBuilder(com.atlassian.bitbucket.i18n.I18nService i18nService,
String binary,
Path workDir)
Constructs a new
AbstractCommandBuilder , sets the I18nService to use for internationalising
error messages and the binary to execute, and optionally sets the workingDirectory where it
should be executed. |
Modifier and Type | Method and Description |
---|---|
B |
argument(String argument)
|
B |
argumentAfter(String anchor,
String argument)
Finds the index of the specified
anchor and inserts the provided argument immediately after it
in the arguments . |
B |
argumentAt(int index,
String argument)
|
B |
argumentBefore(String anchor,
String argument)
Finds the index of the specified
anchor and inserts the provided argument directly before it in
the arguments . |
B |
clearArguments()
Clears the
arguments , but does not null or replace the list instance. |
B |
clearEnvironment()
Clears the
environment , but does not null or replace the map instance. |
B |
clearInputHandler()
Clears the
inputHandler , setting it to null . |
B |
defaultErrorHandler()
Indicates the default
CommandErrorHandler should be applied when CommandBuilderSupport.build(CommandOutputHandler) is
called by setting the errorHandler to null . |
B |
defaultExitHandler()
Indicates the default
CommandExitHandler should be applied when CommandBuilderSupport.build(CommandOutputHandler) is
called by setting the exitHandler to null . |
B |
defaultWorkDir()
Indicates the default working directory should be used when
CommandBuilderSupport.build(CommandOutputHandler) is called by
setting the workDir to null . |
B |
defaultWorkingDirectory()
Deprecated.
in 6.4 for removal in 8.0. Use
defaultWorkDir() instead. |
protected File |
directoryExists(File value)
Deprecated.
in 6.4 for removal in 8.0. Use
directoryExists(Path) instead |
protected Path |
directoryExists(Path value)
Ensures the provided
File is not null , exists and is a directory. |
protected File |
directoryExistsOrIsNull(File value)
Deprecated.
in 6.4 for removal in 8.0. Use
directoryExistsOrIsNull(Path) instead |
protected Path |
directoryExistsOrIsNull(Path value)
Ensures the provided
File is null , or that it exists and is a directory. |
B |
errorHandler(com.atlassian.bitbucket.scm.CommandErrorHandler value)
Sets the provided
value as the errorHandler to use for processing the standard error stream
when the built command is executed. |
B |
exitHandler(com.atlassian.bitbucket.scm.CommandExitHandler value)
Sets the provided
value as the exitHandler to be called when the built command exits or is
canceled. |
B |
inputHandler(com.atlassian.bitbucket.scm.CommandInputHandler value)
Sets the provided
value as the inputHandler to use for providing data on the standard input
stream when the built command is executed. |
protected String |
notBlank(String argument)
Ensures the provided argument is not
null , empty or blank. |
protected String |
notBlank(String value,
String name)
Ensures the provided value is not
null , empty or blank. |
B |
rawArgument(String argument)
|
B |
rawArgumentAfter(String anchor,
String argument)
Finds the index of the specified
anchor and inserts the provided argument immediately after it
in the arguments . |
B |
rawArgumentAt(int index,
String argument)
|
B |
rawArgumentBefore(String anchor,
String argument)
Finds the index of the specified
anchor and inserts the provided argument directly before it in
the arguments . |
B |
removeEnvironment(String name)
Clears the environment variable of the provided
name if it has been set |
protected abstract B |
self()
Returns a reference to the current builder, typed correctly to appease Java generics.
|
B |
withEnvironment(String name,
String value)
Puts the provided
value in the environment map with the specified name , after ensuring
both the name and value are not blank . |
B |
workDir(Path value)
Sets the provided
value as the workDir to use when the built command is executed. |
B |
workDir(String value)
Constructs a
Path from the provided value and sets it as the
working directory to use when the built command is executed. |
B |
workingDirectory(File value)
Deprecated.
in 6.4 for removal in 8.0. Use
workDir(Path) instead. |
B |
workingDirectory(Path value)
Deprecated.
in 6.4 for removal in 8.0. Use
workDir(Path) instead. |
B |
workingDirectory(String value)
Deprecated.
in 6.4 for removal in 8.0. Use
workDir(String) instead. |
protected final LinkedList<String> arguments
LinkedList
is used to optimise insertion, since the argument
list is modified more than it is read.
Derived classes may add and remove arguments, but they may not replace the list reference.
protected final String binary
null
and may not be updated.
Note: When constructing the Command
in CommandBuilderSupport.build(CommandOutputHandler)
, the derived class may opt
to use an alternative, equivalent binary. For example, when using git, give a binary of "git" and a first
argument of "http-backend", the builder may opt to call git-http-backend
(from libexec/git-core
)
rather than git http-backend
.
protected final Map<String,String> environment
Derived classes may add, replace and remove keys and their values, but they may not replace the map reference.
Note: Java's ProcessBuilder.environment
does not support null
keys or values. Derived
classes should validate entries before they are put into the map to ensure they do not violate that contract.
protected final com.atlassian.bitbucket.i18n.I18nService i18nService
I18nService
, used to internationalise error messages for exceptions thrown during processing.protected com.atlassian.bitbucket.scm.CommandExitHandler exitHandler
CommandExitHandler
to be invoked when the command runs. This field may be null
, but when it
is implementations should apply a default when building the Command
.DefaultCommandExitHandler
protected com.atlassian.bitbucket.scm.CommandInputHandler inputHandler
CommandInputHandler
to receive and process the OutputStream
for feeding data into the
command. Not all commands will support input, so this field may be null
.protected com.atlassian.bitbucket.scm.CommandErrorHandler errorHandler
CommandErrorHandler
to receive and process the InputStream
for reading data from the
command's error stream. This field may be null
, but when it is implementations should apply a default
when building the Command
. The default should be accumulate error stream output to provide to the
CommandExitHandler
.StringOutputHandler
protected Path workDir
ProcessBuilder.directory
,
this field may be null
to use the Java process's working directory.@Deprecated protected File workingDirectory
workDir
instead.ProcessBuilder.directory
,
this field may be null
to use the Java process's working directory.protected AbstractCommandBuilder(@Nonnull com.atlassian.bitbucket.i18n.I18nService i18nService, @Nonnull String binary)
AbstractCommandBuilder
and sets the I18nService
to use for internationalising
error messages and the binary
to execute.i18nService
- the I18nService
for internationalising error messagesbinary
- the binary to executeIllegalArgumentException
- if the provided binary
is empty or blankNullPointerException
- if the provided i18nService
or binary
is null
@Deprecated protected AbstractCommandBuilder(@Nonnull com.atlassian.bitbucket.i18n.I18nService i18nService, @Nonnull String binary, @Nullable File workingDirectory)
AbstractCommandBuilder(I18nService, String, Path)
instead.AbstractCommandBuilder
, sets the I18nService
to use for internationalising
error messages and the binary
to execute, and optionally sets the workingDirectory
where it
should be executed.i18nService
- the I18nService
for internationalising error messagesbinary
- the binary to executeworkingDirectory
- the working directory for the command, or null
for the defaultIllegalArgumentException
- if the provided binary
is empty or blank, or if a working directory
is provided which does not exist or is not a directoryNullPointerException
- if the provided i18nService
or binary
is null
protected AbstractCommandBuilder(@Nonnull com.atlassian.bitbucket.i18n.I18nService i18nService, @Nonnull String binary, @Nullable Path workDir)
AbstractCommandBuilder
, sets the I18nService
to use for internationalising
error messages and the binary
to execute, and optionally sets the workingDirectory
where it
should be executed.i18nService
- the I18nService
for internationalising error messagesbinary
- the binary to executeworkDir
- the working directory for the command, or null
for the defaultIllegalArgumentException
- if the provided binary
is empty or blank, or if a working directory
is provided which does not exist or is not a directoryNullPointerException
- if the provided i18nService
or binary
is null
@Nonnull public B argument(@Nonnull String argument)
argument
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
argument
- the argument to append to the commandself()
IllegalArgumentException
- if the provided argument
is empty or blankNullPointerException
- if the provided argument
is null
@Nonnull public B argumentAfter(@Nonnull String anchor, @Nonnull String argument)
anchor
and inserts the provided argument
immediately after it
in the arguments
. If the anchor
is not found, the provided argument
is appended at the
end of the argument list.argumentAfter
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
anchor
- the argument serving as the anchor for the new argumentargument
- the argument to add immediately after the specified anchorself()
IllegalArgumentException
- if the provided anchor
or argument
is empty or blankNullPointerException
- if the provided anchor
or argument
is null
@Nonnull public B argumentAt(int index, @Nonnull String argument)
argument
at the specified index
in the arguments
, after ensuring
it is not blank
.argumentAt
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
index
- the index at which the argument should be insertedargument
- the argument to insert at the specified indexself()
IllegalArgumentException
- if the provided argument
is empty or blankIndexOutOfBoundsException
- if the specified index
is out of range for the argument listNullPointerException
- if the provided argument
is null
@Nonnull public B argumentBefore(@Nonnull String anchor, @Nonnull String argument)
anchor
and inserts the provided argument
directly before it in
the arguments
. If the anchor
is not found, the provided argument
is appended at the end
of the argument list.argumentBefore
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
anchor
- the argument serving as the anchor for the new argumentargument
- the argument to add immediately before the specified anchorself()
IllegalArgumentException
- if the provided anchor
or argument
is empty or blankNullPointerException
- if the provided anchor
or argument
is null
@Nonnull public B clearArguments()
arguments
, but does not null
or replace the list instance.@Nonnull public B clearEnvironment()
environment
, but does not null
or replace the map instance.@Nonnull public B removeEnvironment(@Nonnull String name)
name
if it has been set@Nonnull public B clearInputHandler()
inputHandler
, setting it to null
.@Nonnull public B defaultErrorHandler()
CommandErrorHandler
should be applied when CommandBuilderSupport.build(CommandOutputHandler)
is
called by setting the errorHandler
to null
.
Derived classes may override this to preemptively set the default error handler, or they may apply the default
in CommandBuilderSupport.build(CommandOutputHandler)
when the errorHandler
field is null
. The latter is the
preferred approach, as it does not require code using the builder to call this method to trigger applying the
default handler.
@Nonnull public B defaultExitHandler()
CommandExitHandler
should be applied when CommandBuilderSupport.build(CommandOutputHandler)
is
called by setting the exitHandler
to null
.
Derived classes may override this to preemptively set the default exit handler, or they may apply the default
in CommandBuilderSupport.build(CommandOutputHandler)
when the exitHandler
field is null
. The latter is the
preferred approach, as it does not require code using the builder to call this method to trigger applying the
default handler.
@Nonnull public B defaultWorkDir()
CommandBuilderSupport.build(CommandOutputHandler)
is called by
setting the workDir
to null
. This is consistent with ProcessBuilder
.@Deprecated @Nonnull public B defaultWorkingDirectory()
defaultWorkDir()
instead.CommandBuilderSupport.build(CommandOutputHandler)
is called by
setting the workDir
to null
. This is consistent with ProcessBuilder
.@Nonnull public B errorHandler(@Nonnull com.atlassian.bitbucket.scm.CommandErrorHandler value)
value
as the errorHandler
to use for processing the standard error stream
when the built command is executed.
Note: As described on the CommandErrorHandler
and CommandExitHandler
interfaces, providing an
error handler will prevent the exit handler
from receiving any output
written to the standard error stream.
errorHandler
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
value
- the handler to receive and process the command's error streamself()
NullPointerException
- if the provided value
is null
@Nonnull public B exitHandler(@Nonnull com.atlassian.bitbucket.scm.CommandExitHandler value)
value
as the exitHandler
to be called when the built command exits or is
canceled.
Note: As described on the CommandErrorHandler
and CommandExitHandler
interfaces, providing an
error handler
will prevent the exit handler from receiving any output
written to the standard error stream.
exitHandler
in interface com.atlassian.bitbucket.scm.CommandBuilderSupport<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
value
- the handler which should be invoked when the command exitsself()
NullPointerException
- if the provided value
is null
@Nonnull public B inputHandler(@Nonnull com.atlassian.bitbucket.scm.CommandInputHandler value)
value
as the inputHandler
to use for providing data on the standard input
stream when the built command is executed.inputHandler
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
value
- the handler to receive and process the command's input streamself()
NullPointerException
- if the provided value
is null
@Nonnull public B rawArgument(@Nonnull String argument)
argument
to the arguments
, after ensuring it is not null
.
Whitespace: Leading and trailing whitespace on the argument
is ignored, and the value is
used verbatim.
rawArgument
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
argument
- the argument to append to the commandself()
NullPointerException
- if the provided argument
is null
@Nonnull public B rawArgumentAfter(@Nonnull String anchor, @Nonnull String argument)
anchor
and inserts the provided argument
immediately after it
in the arguments
. If the anchor
is not found, the provided argument
is appended at the
end of the argument list.
Whitespace: Leading and trailing whitespace on the anchor
or argument
is ignored,
and the values are used verbatim.
rawArgumentAfter
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
anchor
- the argument serving as the anchor for the new argumentargument
- the argument to add immediately after the specified anchorself()
NullPointerException
- if the provided anchor
or argument
is null
@Nonnull public B rawArgumentAt(int index, @Nonnull String argument)
argument
at the specified index
in the arguments
, after ensuring
it is not blank
.
Whitespace: Leading and trailing whitespace on the argument
is ignored, and the value is
used verbatim.
rawArgumentAt
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
index
- the index at which the argument should be insertedargument
- the argument to insert at the specified indexself()
IndexOutOfBoundsException
- if the specified index
is out of range for the argument listNullPointerException
- if the provided argument
is null
@Nonnull public B rawArgumentBefore(@Nonnull String anchor, @Nonnull String argument)
anchor
and inserts the provided argument
directly before it in
the arguments
. If the anchor
is not found, the provided argument
is appended at the end
of the argument list.
Whitespace: Leading and trailing whitespace on the anchor
or argument
is ignored,
and the values are used verbatim.
rawArgumentBefore
in interface com.atlassian.bitbucket.scm.CommandBuilder<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
anchor
- the argument serving as the anchor for the new argumentargument
- the argument to add immediately before the specified anchorself()
NullPointerException
- if the provided anchor
or argument
is null
@Nonnull public B withEnvironment(@Nonnull String name, @Nonnull String value)
value
in the environment
map with the specified name
, after ensuring
both the name
and value
are not blank
.withEnvironment
in interface com.atlassian.bitbucket.scm.CommandBuilderSupport<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
name
- the name of the environment variable to setvalue
- the value to set for the environment variableself()
IllegalArgumentException
- if the provided name
or value
is empty or blankNullPointerException
- if the provided name
or value
is null
@Nonnull public B workDir(@Nonnull Path value)
value
as the workDir
to use when the built command is executed.workDir
in interface com.atlassian.bitbucket.scm.CommandBuilderSupport<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
value
- the desired working directoryself()
IllegalArgumentException
- if the provided value
does not exist, or does not reference a directoryNullPointerException
- if the provided value
is null
@Nonnull public B workDir(@Nonnull String value)
Path
from the provided value
and sets it
as the
working directory to use when the built command is executed.workDir
in interface com.atlassian.bitbucket.scm.CommandBuilderSupport<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
value
- the absolute path to the desired working directoryself()
IllegalArgumentException
- if the provided value
does not exist, or does not reference a directoryNullPointerException
- if the provided value
is null
@Deprecated @Nonnull public B workingDirectory(@Nonnull File value)
workDir(Path)
instead.value
as the workDir
to use when the built command is executed.workingDirectory
in interface com.atlassian.bitbucket.scm.CommandBuilderSupport<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
value
- the desired working directoryself()
IllegalArgumentException
- if the provided value
does not exist, or does not reference a directoryNullPointerException
- if the provided value
is null
@Deprecated @Nonnull public B workingDirectory(@Nonnull Path value)
workDir(Path)
instead.value
as the workDir
to use when the built command is executed.workingDirectory
in interface com.atlassian.bitbucket.scm.CommandBuilderSupport<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
value
- the desired working directoryself()
IllegalArgumentException
- if the provided value
does not exist, or does not reference a directoryNullPointerException
- if the provided value
is null
@Deprecated @Nonnull public B workingDirectory(@Nonnull String value)
workDir(String)
instead.Path
from the provided value
and sets it
as the
working directory to use when the built command is executed.workingDirectory
in interface com.atlassian.bitbucket.scm.CommandBuilderSupport<B extends com.atlassian.bitbucket.scm.CommandBuilder<B>>
value
- the absolute path to the desired working directoryself()
IllegalArgumentException
- if the provided value
does not exist, or does not reference a directoryNullPointerException
- if the provided value
is null
@Deprecated @Nonnull protected File directoryExists(@Nonnull File value)
directoryExists(Path)
insteadFile
is not null
, exists and is a directory.value
- the File
to validatevalue
IllegalArgumentException
- if the provided value
does not exist, or does not reference a directoryNullPointerException
- if the provided value
is null
@Nonnull protected Path directoryExists(@Nonnull Path value)
File
is not null
, exists and is a directory.value
- the File
to validatevalue
IllegalArgumentException
- if the provided value
does not exist, or does not reference a directoryNullPointerException
- if the provided value
is null
@Deprecated @Nullable protected File directoryExistsOrIsNull(@Nullable File value)
directoryExistsOrIsNull(Path)
insteadFile
is null
, or that it exists and is a directory.value
- the File
to validatevalue
, which may be null
IllegalArgumentException
- if the provided value
is not null
but does not exist, or does
not reference a directory@Nullable protected Path directoryExistsOrIsNull(@Nullable Path value)
File
is null
, or that it exists and is a directory.value
- the File
to validatevalue
, which may be null
IllegalArgumentException
- if the provided value
is not null
but does not exist, or does
not reference a directory@Nonnull protected String notBlank(@Nullable String argument)
null
, empty or blank. The returned argument is trimmed to remove
both leading and trailing whitespace.argument
- the argument to validateargument
, trimmedIllegalArgumentException
- if the provided argument
is empty or blankNullPointerException
- if the provided argument
is null
protected String notBlank(@Nullable String value, @Nullable String name)
null
, empty or blank. The returned value is trimmed to remove
both leading and trailing whitespace.value
- the value to validatename
- the value's name, used in exception messagesvalue
, trimmedIllegalArgumentException
- if the provided value
is empty or blankNullPointerException
- if the provided value
is null
protected abstract B self()
Implementations of this method on derived builders are encouraged to use a covariant return type to return the actual implementation class. For example:
public interface MyBuilder extends CommandBuilder<MyBuilder> {
//Implementation omitted
}
public class DefaultMyBuilder extends AbstractCommandBuilder<MyBuilder> implements MyBuilder {
//Implementation omitted
@Override
protected DefaultMyBuilder self() {
return this;
}
}
this
, in a derived classCopyright © 2020 Atlassian. All rights reserved.