Skip to content

Commit 0673bc6

Browse files
Vlatombejglick
andauthored
Introduce Named, FullyNamed, FullyNamedModelObject interfaces (#10827)
* Introduce `Nameable`, `FullyNameable`, `FullyNameableModelObject` interfaces These correspond to methods `getName`, `getFullName`, `getFullDisplayName` that did not have any common interface to call them, causing some inconvenient usages of switch statements or even reflection in CloudBees CI HA. * Nameable -> Named * Improve javadoc Co-authored-by: Jesse Glick <jglick@cloudbees.com> * Make ResourceActivity extend ModelObject * Use full name for affinity key if available. --------- Co-authored-by: Jesse Glick <jglick@cloudbees.com>
1 parent 761fa21 commit 0673bc6

File tree

17 files changed

+112
-69
lines changed

17 files changed

+112
-69
lines changed

core/src/main/java/hudson/model/AbstractItem.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ protected AbstractItem(ItemGroup parent, String name) {
129129
doSetName(name);
130130
}
131131

132+
@NonNull
132133
@Override
133134
@Exported(visibility = 999)
134135
public String getName() {
@@ -470,6 +471,7 @@ public void movedTo(DirectlyModifiableTopLevelItemGroup destination, AbstractIte
470471
@Override
471472
public abstract Collection<? extends Job> getAllJobs();
472473

474+
@NonNull
473475
@Override
474476
@Exported
475477
public final String getFullName() {

core/src/main/java/hudson/model/Item.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
import hudson.util.Secret;
3939
import java.io.IOException;
4040
import java.util.Collection;
41+
import jenkins.model.FullyNamed;
42+
import jenkins.model.FullyNamedModelObject;
4143
import jenkins.model.Jenkins;
44+
import jenkins.model.Named;
4245
import jenkins.search.SearchGroup;
4346
import jenkins.util.SystemProperties;
4447
import jenkins.util.io.OnMaster;
@@ -73,7 +76,7 @@
7376
* @see Items
7477
* @see ItemVisitor
7578
*/
76-
public interface Item extends PersistenceRoot, SearchableModelObject, AccessControlled, OnMaster {
79+
public interface Item extends PersistenceRoot, FullyNamedModelObject, SearchableModelObject, FullyNamed, Named, AccessControlled, OnMaster {
7780
/**
7881
* Gets the parent that contains this item.
7982
*/
@@ -97,6 +100,8 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont
97100
*
98101
* @see #getFullName()
99102
*/
103+
@NonNull
104+
@Override
100105
String getName();
101106

102107
/**
@@ -110,6 +115,8 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont
110115
*
111116
* @see jenkins.model.Jenkins#getItemByFullName(String,Class)
112117
*/
118+
@NonNull
119+
@Override
113120
String getFullName();
114121

115122
/**
@@ -127,13 +134,6 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont
127134
@Override
128135
String getDisplayName();
129136

130-
/**
131-
* Works like {@link #getDisplayName()} but return
132-
* the full path that includes all the display names
133-
* of the ancestors.
134-
*/
135-
String getFullDisplayName();
136-
137137
/**
138138
* Gets the relative name to this item from the specified group.
139139
*

core/src/main/java/hudson/model/ItemGroup.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.util.function.Predicate;
3434
import java.util.stream.Collectors;
3535
import java.util.stream.Stream;
36+
import jenkins.model.FullyNamed;
37+
import jenkins.model.FullyNamedModelObject;
3638
import org.springframework.security.access.AccessDeniedException;
3739

3840
/**
@@ -41,19 +43,7 @@
4143
* @author Kohsuke Kawaguchi
4244
* @see ItemGroupMixIn
4345
*/
44-
public interface ItemGroup<T extends Item> extends PersistenceRoot, ModelObject {
45-
/**
46-
* Gets the full name of this {@link ItemGroup}.
47-
*
48-
* @see Item#getFullName()
49-
*/
50-
String getFullName();
51-
52-
/**
53-
* @see Item#getFullDisplayName()
54-
*/
55-
String getFullDisplayName();
56-
46+
public interface ItemGroup<T extends Item> extends FullyNamed, FullyNamedModelObject, PersistenceRoot {
5747
/**
5848
* Gets all the items in this collection in a read-only view.
5949
*/

core/src/main/java/hudson/model/Queue.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
import java.util.logging.Logger;
110110
import java.util.stream.Collectors;
111111
import jenkins.console.WithConsoleUrl;
112+
import jenkins.model.FullyNamed;
113+
import jenkins.model.FullyNamedModelObject;
112114
import jenkins.model.Jenkins;
113115
import jenkins.model.queue.AsynchronousExecution;
114116
import jenkins.model.queue.CompositeCauseOfBlockage;
@@ -1882,7 +1884,7 @@ public interface NonBlockingTask extends Task {}
18821884
* design, a {@link Task} must have at least one sub-task.)
18831885
* Most of the time, the primary subtask is the only sub task.
18841886
*/
1885-
public interface Task extends ModelObject, SubTask {
1887+
public interface Task extends FullyNamedModelObject, SubTask {
18861888
/**
18871889
* Returns true if the execution should be blocked
18881890
* for temporary reasons.
@@ -1928,21 +1930,22 @@ default CauseOfBlockage getCauseOfBlockage() {
19281930
*/
19291931
String getName();
19301932

1931-
/**
1932-
* @see hudson.model.Item#getFullDisplayName()
1933-
*/
1934-
String getFullDisplayName();
1935-
19361933
/**
19371934
* Returns task-specific key which is used by the {@link LoadBalancer} to choose one particular executor
19381935
* amongst all the free executors on all possibly suitable nodes.
19391936
* NOTE: To be able to re-use the same node during the next run this key should not change from one run to
19401937
* another. You probably want to compute that key based on the job's name.
19411938
*
1942-
* @return by default: {@link #getFullDisplayName()}
1939+
* @return by default: {@link FullyNamed#getFullName()} if implements {@link FullyNamed} or {@link #getFullDisplayName()} otherwise.
19431940
* @see hudson.model.LoadBalancer
19441941
*/
1945-
default String getAffinityKey() { return getFullDisplayName(); }
1942+
default String getAffinityKey() {
1943+
if (this instanceof FullyNamed fullyNamed) {
1944+
return fullyNamed.getFullName();
1945+
} else {
1946+
return getFullDisplayName();
1947+
}
1948+
}
19461949

19471950
/**
19481951
* Checks the permission to see if the current user can abort this executable.

core/src/main/java/hudson/model/ResourceActivity.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* @author Kohsuke Kawaguchi
3131
*/
32-
public interface ResourceActivity {
32+
public interface ResourceActivity extends ModelObject {
3333
/**
3434
* Gets the list of {@link Resource}s that this task requires.
3535
* Used to make sure no two conflicting tasks run concurrently.
@@ -47,9 +47,4 @@ public interface ResourceActivity {
4747
default ResourceList getResourceList() {
4848
return ResourceList.EMPTY;
4949
}
50-
51-
/**
52-
* Used for rendering HTML.
53-
*/
54-
String getDisplayName();
5550
}

core/src/main/java/hudson/model/queue/MappingWorksheet.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static java.lang.Math.max;
2828

2929
import com.google.common.collect.Iterables;
30+
import edu.umd.cs.findbugs.annotations.NonNull;
3031
import hudson.model.Computer;
3132
import hudson.model.Executor;
3233
import hudson.model.Label;
@@ -48,6 +49,7 @@
4849
import java.util.List;
4950
import java.util.Map;
5051
import java.util.logging.Logger;
52+
import jenkins.model.Named;
5153

5254
/**
5355
* Defines a mapping problem for answering "where do we execute this task?"
@@ -114,7 +116,7 @@ public int size() {
114116
}
115117
}
116118

117-
public final class ExecutorChunk extends ReadOnlyList<ExecutorSlot> {
119+
public final class ExecutorChunk extends ReadOnlyList<ExecutorSlot> implements Named {
118120
public final int index;
119121
public final Computer computer;
120122
public final Node node;
@@ -150,6 +152,8 @@ public boolean canAccept(WorkChunk c) {
150152
/**
151153
* Node name.
152154
*/
155+
@NonNull
156+
@Override
153157
public String getName() {
154158
return node.getNodeName();
155159
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package jenkins.model;
2+
3+
import edu.umd.cs.findbugs.annotations.NonNull;
4+
5+
/**
6+
* An interface for objects that have a name and a parent, so exposing a full name.
7+
*/
8+
public interface FullyNamed {
9+
/**
10+
* Returns the full name of this object, which is a qualified name
11+
* that includes the names of all its ancestors, separated by '/'.
12+
*
13+
* @return the full name of this object.
14+
*/
15+
@NonNull
16+
String getFullName();
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package jenkins.model;
2+
3+
import hudson.model.ModelObject;
4+
import jenkins.security.stapler.StaplerAccessibleType;
5+
6+
/**
7+
* A model object that has a human-readable full name. This is usually valid when nested as part of an object hierarchy.
8+
*
9+
* <p>
10+
* This interface is used to mark objects that can be qualified in the context of a Jenkins instance.
11+
* It is typically used for objects that are part of the Jenkins model and can be referenced by their names.
12+
*
13+
* @see ModelObject
14+
*/
15+
@StaplerAccessibleType
16+
public interface FullyNamedModelObject extends ModelObject {
17+
/**
18+
* Works like {@link #getDisplayName()} but return
19+
* the full path that includes all the display names
20+
* of the ancestors in an unspecified format.
21+
*/
22+
String getFullDisplayName();
23+
}

core/src/main/java/jenkins/model/HistoricalBuild.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import hudson.markup.MarkupFormatter;
3030
import hudson.model.BallColor;
3131
import hudson.model.BuildBadgeAction;
32-
import hudson.model.ModelObject;
3332
import hudson.model.ParameterValue;
3433
import hudson.model.ParametersAction;
3534
import hudson.model.Queue;
@@ -48,7 +47,7 @@
4847
* @since 2.477
4948
*/
5049
@Restricted(Beta.class)
51-
public interface HistoricalBuild extends ModelObject {
50+
public interface HistoricalBuild extends FullyNamedModelObject {
5251

5352
/**
5453
* @return A build number
@@ -74,12 +73,6 @@ public interface HistoricalBuild extends ModelObject {
7473
@CheckForNull
7574
String getDescription();
7675

77-
/**
78-
* @return a human-readable full display name of this build.
79-
*/
80-
@NonNull
81-
String getFullDisplayName();
82-
8376
/**
8477
* Get the {@link Queue.Item#getId()} of the original queue item from where this {@link HistoricalBuild} instance
8578
* originated.

core/src/main/java/jenkins/model/IComputer.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import edu.umd.cs.findbugs.annotations.NonNull;
2828
import hudson.Util;
2929
import hudson.model.Computer;
30-
import hudson.model.Node;
30+
import hudson.model.ModelObject;
3131
import hudson.security.ACL;
3232
import hudson.security.AccessControlled;
3333
import java.util.List;
@@ -45,13 +45,7 @@
4545
* @since 2.480
4646
*/
4747
@Restricted(Beta.class)
48-
public interface IComputer extends AccessControlled, IconSpec {
49-
/**
50-
* Returns {@link Node#getNodeName() the name of the node}.
51-
*/
52-
@NonNull
53-
String getName();
54-
48+
public interface IComputer extends AccessControlled, IconSpec, ModelObject, Named {
5549
/**
5650
* Used to render the list of executors.
5751
* @return a snapshot of the executor display information
@@ -64,12 +58,6 @@ public interface IComputer extends AccessControlled, IconSpec {
6458
*/
6559
boolean isOffline();
6660

67-
/**
68-
* @return the node name for UI purposes.
69-
*/
70-
@NonNull
71-
String getDisplayName();
72-
7361
/**
7462
* Returns {@code true} if the computer is accepting tasks. Needed to allow agents programmatic suspension of task
7563
* scheduling that does not overlap with being offline.

0 commit comments

Comments
 (0)