Backend Development 15 min read

Analyzing Jenkins doCheckJobName Method and Related Java Concurrency Structures

This article examines the Jenkins doCheckJobName implementation, explains how it validates job names and permissions, and explores the underlying Java technologies such as ViewGroup, CopyOnWriteArrayList, CopyOnWriteMap, TreeMap, and concurrency mechanisms used in Jenkins' source code.

Cloud Native Technology Community
Cloud Native Technology Community
Cloud Native Technology Community
Analyzing Jenkins doCheckJobName Method and Related Java Concurrency Structures

Jenkins is a Java‑based continuous‑integration server whose origins trace back to Hudson; the article begins with a brief history and states that the analysis will focus on source code rather than the Stapler framework.

The doCheckJobName method is invoked when a user types a job name in the UI. Its definition is:

public FormValidation doCheckJobName(@QueryParameter String value) {
    // this method can be used to check if a file exists anywhere in the file system,
    // so it should be protected.
    getOwner().checkPermission(Item.CREATE);
    if (Util.fixEmpty(value) == null) {
        return FormValidation.ok();
    }
    try {
        Jenkins.checkGoodName(value);
        value = value.trim(); // why trim *after* checkGoodName? not sure, but ItemGroupMixIn.createTopLevelItem does the same
        Jenkins.get().getProjectNamingStrategy().checkName(value);
    } catch (Failure e) {
        return FormValidation.error(e.getMessage());
    }
    if (getOwner().getItemGroup().getItem(value) != null) {
        return FormValidation.error(Messages.Hudson_JobAlreadyExists(value));
    }
    // looks good
    return FormValidation.ok();
}

Key lines perform permission checking ( getOwner().checkPermission(Item.CREATE) ) and name validation ( Jenkins.checkGoodName(value) ), then verify that the name does not already exist.

The method getOwner() returns a ViewGroup implementation. ViewGroup extends Saveable , ModelObject and AccessControlled , providing methods such as checkPermission and default getItemGroup() which ultimately returns the Jenkins instance itself.

Jenkins manages its views via a ViewGroupMixIn field. The mix‑in’s getViews() method copies the internal list of views, filters them by permission, sorts them, and returns the result:

public Collection
getViews() {
    List
orig = views();
    List
copy = new ArrayList<>(orig.size());
    for (View v : orig) {
        if (v.hasPermission(View.READ))
            copy.add(v);
    }
    copy.sort(View.SORTER);
    return copy;
}

The underlying views collection is a CopyOnWriteArrayList<View> , a concurrent list that provides lock‑free reads and copy‑on‑write writes:

private transient final CopyOnWriteArrayList
views = new CopyOnWriteArrayList<>();

CopyOnWriteArrayList’s read method simply returns the element from the current array, while its add method acquires a ReentrantLock , copies the backing array, appends the new element, and replaces the reference.

public boolean add(E e) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Object[] elements = getArray();
        int len = elements.length;
        Object[] newElements = Arrays.copyOf(elements, len + 1);
        newElements[len] = e;
        setArray(newElements);
        return true;
    } finally {
        lock.unlock();
    }
}

Jenkins also uses a CopyOnWriteMap (specifically CopyOnWriteMap.Tree ) to store items. The map holds a volatile core map and an unmodifiable view map. Writes are performed by copying the core map, applying the modification, and then updating the volatile reference:

public synchronized V put(K key, V value) {
    Map
m = copy();
    V r = m.put(key, value);
    update(m);
    return r;
}

The underlying TreeMap provides a sorted, navigable map based on a red‑black tree. Insertion follows the classic red‑black algorithm, rotating and recoloring nodes to maintain balance.

Overall, the article demonstrates how Jenkins leverages Java 8 default methods, concurrency utilities (CopyOnWriteArrayList, ReentrantLock), and classic data structures (TreeMap, red‑black tree) to implement safe, permission‑aware operations such as job‑name validation.

backendJavaConcurrencyCIJenkinsCopyOnWrite
Cloud Native Technology Community
Written by

Cloud Native Technology Community

The Cloud Native Technology Community, part of the CNBPA Cloud Native Technology Practice Alliance, focuses on evangelizing cutting‑edge cloud‑native technologies and practical implementations. It shares in‑depth content, case studies, and event/meetup information on containers, Kubernetes, DevOps, Service Mesh, and other cloud‑native tech, along with updates from the CNBPA alliance.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.