Class JenkinsUtils


  • @Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class)
    public class JenkinsUtils
    extends Object
    Utilities to fetch things out of jenkins environment.
    • Constructor Detail

      • JenkinsUtils

        public JenkinsUtils()
    • 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 the DockerCloud or otherwise Optional.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 the DockerCloud or otherwise Optional.empty().
      • getCloudThatWeBuiltOn

        @Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class)
        public static Optional<DockerCloud> getCloudThatWeBuiltOn​(Run<?,​?> build,
                                                                  Launcher launcher)
      • 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 by key. 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, or defaultValue 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 by key. 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, or defaultValue 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 by key. 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, or defaultValue 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 of Object.
        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 of Collection.
        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 of Map.
        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 - The StringBuilder from startToString(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 - The StringBuilder from startToString(Object).
        attributeName - The field name
        value - 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 - The StringBuilder from startToString(Object).
        attributeName - The field name
        value - 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 - The StringBuilder from startToString(Object).
        attributeName - The field name
        value - 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 a List 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 a List 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 a Map, 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 a List 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.