Class JenkinsUtils
- java.lang.Object
-
- com.nirima.jenkins.plugins.docker.utils.JenkinsUtils
-
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public class JenkinsUtils extends Object
Utilities to fetch things out of jenkins environment.
-
-
Constructor Summary
Constructors Constructor Description JenkinsUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
bldToString(StringBuilder sb, String attributeName, int value)
Used to help with toString methods: Appends to a toString result.static void
bldToString(StringBuilder sb, String attributeName, Object value)
Used to help with toString methods: Appends to a toString result.static <T> void
bldToString(StringBuilder sb, String attributeName, T[] value)
Used to help with toString methods: Appends to a toString result.static void
endToString(StringBuilder sb)
Used to help with toString methods: Ends a toString result.static String[]
filterStringArray(String[] arr)
Clones a String array but stripping all entries and omitting any that are null or empty after stripping.static <C extends Collection>
CfixEmpty(C collection)
Turns empty collections into nulls.static <T> T[]
fixEmpty(T[] array)
Turns empty arrays into nulls.static DockerCloud
getCloudByNameOrThrow(String serverName)
Finds theDockerCloud
with theCloud.getDisplayName()
matching the specified name.static Optional<DockerCloud>
getCloudForBuild(AbstractBuild build)
If the build was on a docker cloud, get the cloud.static Optional<DockerCloud>
getCloudForChannel(hudson.remoting.VirtualChannel channel)
If the build was workflow, get the ID of that channel.static Optional<DockerCloud>
getCloudThatWeBuiltOn(Run<?,?> build, Launcher launcher)
static String
getInstanceId()
static List<String>
getServerNames()
static boolean
getSystemPropertyBoolean(String key, boolean defaultValue)
returns the Java system property specified bykey
.static Long
getSystemPropertyLong(String key, Long defaultValue)
returns the Java system property specified bykey
.static String
getSystemPropertyString(String key, String defaultValue)
returns the Java system property specified bykey
.static <T> T
makeCopy(T original)
Makes a copy of a Jenkins object.static <T> List<T>
makeCopyOfList(List<? extends T> listOrNull)
Makes a copy of aList
of Jenkins objects.static void
setTestInstanceId(String id)
static String[]
splitAndFilterEmpty(String s, String separator)
Splits a (potentially null/empty) text string into an array of non-empty strings.static List<String>
splitAndFilterEmptyList(String s, String separator)
Splits a (potentially null/empty) text string into aList
of non-empty strings.static Map<String,String>
splitAndFilterEmptyMap(String s, String separator)
Splits a (potentially null/empty) text string of the form "name=value<separator>foo=bar" into aMap
, ignoring empty sections and sections that do not include an "=".static List<String>
splitAndTrimFilterEmptyList(String s, String separator)
Splits a (potentially null/empty) text string into aList
of non-empty strings, trimming each entry too.static StringBuilder
startToString(Object subjectOfToString)
Used to help with toString methods: Starts a new toString result.
-
-
-
Method Detail
-
getCloudForBuild
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static Optional<DockerCloud> getCloudForBuild(AbstractBuild build)
If the build was on a docker cloud, get the cloud.- Parameters:
build
- The build under inspection.- Returns:
Optional
containing theDockerCloud
or otherwiseOptional.empty()
.
-
getCloudForChannel
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static Optional<DockerCloud> getCloudForChannel(hudson.remoting.VirtualChannel channel)
If the build was workflow, get the ID of that channel.- Parameters:
channel
- From the build under inspection.- Returns:
Optional
containing theDockerCloud
or otherwiseOptional.empty()
.
-
getCloudThatWeBuiltOn
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static Optional<DockerCloud> getCloudThatWeBuiltOn(Run<?,?> build, Launcher launcher)
-
getCloudByNameOrThrow
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @NonNull public static DockerCloud getCloudByNameOrThrow(String serverName)
Finds theDockerCloud
with theCloud.getDisplayName()
matching the specified name.- Parameters:
serverName
- The name to look for.- Returns:
DockerCloud
with theCloud.getDisplayName()
matching the specified name.- Throws:
IllegalArgumentException
- if noDockerCloud
exists with that name.
-
getServerNames
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @NonNull public static List<String> getServerNames()
-
getInstanceId
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @NonNull public static String getInstanceId()
-
setTestInstanceId
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static void setTestInstanceId(String id)
-
getSystemPropertyString
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static String getSystemPropertyString(String key, String defaultValue)
returns the Java system property specified bykey
. If that fails, a default value is returned instead.To be replaced with jenkins.util.SystemProperties.getString() once they lift their @Restricted(NoExternalUse.class)
- Parameters:
key
- the key of the system property to read.defaultValue
- the default value which shall be returned in case the property is not defined.- Returns:
- the system property of
key
, ordefaultValue
in case the property is not defined.
-
getSystemPropertyLong
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static Long getSystemPropertyLong(String key, Long defaultValue)
returns the Java system property specified bykey
. If that fails, a default value is returned instead.In case the value of the system property cannot be parsed properly (e.g. a character was passed, causing a parsing error to occur), the default value is returned.
To be replaced with jenkins.util.SystemProperties.getLong() once they lift their @Restricted(NoExternalUse.class)
- Parameters:
key
- the key of the system property to read.defaultValue
- the default value which shall be returned in case the property is not defined.- Returns:
- the system property of
key
, ordefaultValue
in case the property is not defined.
-
getSystemPropertyBoolean
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static boolean getSystemPropertyBoolean(String key, boolean defaultValue)
returns the Java system property specified bykey
. If that fails, a default value is returned instead.In case the value of the system property cannot be parsed properly (e.g. an invalid identifier was passed), the value
false
is returned.To be replaced with jenkins.util.SystemProperties.getBoolean() once they lift their @Restricted(NoExternalUse.class)
- Parameters:
key
- the key of the system property to read.defaultValue
- the default value which shall be returned in case the property is not defined.- Returns:
- the system property of
key
, ordefaultValue
in case the property is not defined.
-
fixEmpty
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @CheckForNull public static <T> T[] fixEmpty(@Nullable T[] array)
Turns empty arrays into nulls.- Type Parameters:
T
- Any kind ofObject
.- Parameters:
array
- The array (or null)- Returns:
- null or the non-empty array.
-
fixEmpty
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @CheckForNull public static <C extends Collection> C fixEmpty(@Nullable C collection)
Turns empty collections into nulls.- Type Parameters:
C
- Any kind ofCollection
.- Parameters:
collection
- The collection (or null)- Returns:
- null or the non-empty collection.
-
fixEmpty
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @CheckForNull public static <C extends Map<?,?>> C fixEmpty(@Nullable C map)
Turns empty maps into nulls.- Type Parameters:
C
- Any kind ofMap
.- Parameters:
map
- The map (or null)- Returns:
- null or the non-empty map.
-
startToString
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static StringBuilder startToString(Object subjectOfToString)
Used to help with toString methods: Starts a new toString result.- Parameters:
subjectOfToString
- The class being turned into a string.- Returns:
- A
StringBuilder
containing the class's name.
-
endToString
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static void endToString(StringBuilder sb)
Used to help with toString methods: Ends a toString result.- Parameters:
sb
- TheStringBuilder
fromstartToString(Object)
.
-
bldToString
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static <T> void bldToString(StringBuilder sb, String attributeName, @Nullable T[] value)
Used to help with toString methods: Appends to a toString result.- Type Parameters:
T
- The type being appended.- Parameters:
sb
- TheStringBuilder
fromstartToString(Object)
.attributeName
- The field namevalue
- The field value
-
bldToString
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static void bldToString(StringBuilder sb, String attributeName, int value)
Used to help with toString methods: Appends to a toString result.- Parameters:
sb
- TheStringBuilder
fromstartToString(Object)
.attributeName
- The field namevalue
- The field value
-
bldToString
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static void bldToString(StringBuilder sb, String attributeName, @Nullable Object value)
Used to help with toString methods: Appends to a toString result.- Parameters:
sb
- TheStringBuilder
fromstartToString(Object)
.attributeName
- The field namevalue
- The field value
-
splitAndFilterEmpty
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @NonNull public static String[] splitAndFilterEmpty(@Nullable String s, String separator)
Splits a (potentially null/empty) text string into an array of non-empty strings.- Parameters:
s
- The string to be split.separator
- The separator regex, e.g. "\n".- Returns:
- An array (possibly empty, never null).
-
splitAndFilterEmptyList
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @NonNull public static List<String> splitAndFilterEmptyList(@Nullable String s, String separator)
Splits a (potentially null/empty) text string into aList
of non-empty strings.- Parameters:
s
- The string to be split.separator
- The separator regex, e.g. "\n".- Returns:
- A
List
(possibly empty, never null).
-
splitAndTrimFilterEmptyList
public static List<String> splitAndTrimFilterEmptyList(String s, String separator)
Splits a (potentially null/empty) text string into aList
of non-empty strings, trimming each entry too.- Parameters:
s
- The string to be split.separator
- The separator regex, e.g. "\n".- Returns:
- A
List
(possibly empty, never null).
-
splitAndFilterEmptyMap
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @NonNull public static Map<String,String> splitAndFilterEmptyMap(@Nullable String s, String separator)
Splits a (potentially null/empty) text string of the form "name=value<separator>foo=bar" into aMap
, ignoring empty sections and sections that do not include an "=".- Parameters:
s
- The string to be split.separator
- The separator regex, e.g. "\n".- Returns:
- A
Map
(possibly empty, never null).
-
filterStringArray
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @NonNull public static String[] filterStringArray(@Nullable String[] arr)
Clones a String array but stripping all entries and omitting any that are null or empty after stripping.- Parameters:
arr
- The starting array; this will not be modified.- Returns:
- A new array no longer than the one given, but which may be empty.
-
makeCopyOfList
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static <T> List<T> makeCopyOfList(@Nullable List<? extends T> listOrNull)
Makes a copy of aList
of Jenkins objects. This is effectively a deep clone. Typically used to copy something that's been configured in a template before it's used in something generated from that template.- Type Parameters:
T
- The type of thing to be copied.- Parameters:
listOrNull
- The list of things to be copied.- Returns:
- A deep clone of the list.
-
makeCopy
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static <T> T makeCopy(@Nullable T original)
Makes a copy of a Jenkins object. This is effectively a deep clone. Typically used to copy something that's been configured in a template before it's used in something generated from that template.- Type Parameters:
T
- The type of thing to be copied.- Parameters:
original
- The thing to be copied.- Returns:
- A deep clone of the thing.
-
-