Skip to content

Commit

Permalink
[JENKINS-64816] prepare for support of node monitors by JCasC (#8629)
Browse files Browse the repository at this point in the history
* [JENKINS-64816] prepare for support of node monitors by JCasC

In order that configuration as code plugin can properly support node
monitors we need to add databound constructors.

* fix warnings

* add javadoc that DataBoundConstructor is a must

* remove duplicate extension

* delete obsolete install methods
  • Loading branch information
mawinter69 authored Nov 18, 2023
1 parent f344879 commit 442a42f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

/**
Expand All @@ -40,6 +41,11 @@
* @author Kohsuke Kawaguchi
*/
public class ArchitectureMonitor extends NodeMonitor {

@DataBoundConstructor
public ArchitectureMonitor() {
}

@Extension @Symbol("architecture")
public static final class DescriptorImpl extends AbstractAsyncNodeMonitorDescriptor<String> {
@Override
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/hudson/node_monitors/ClockMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

/**
Expand All @@ -46,6 +47,11 @@
* @since 1.123
*/
public class ClockMonitor extends NodeMonitor {

@DataBoundConstructor
public ClockMonitor() {
}

public ClockDifference getDifferenceFor(Computer c) {
return DESCRIPTOR.get(c);
}
Expand Down
19 changes: 13 additions & 6 deletions core/src/main/java/hudson/node_monitors/DiskSpaceMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package hudson.node_monitors;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Computer;
Expand All @@ -34,6 +35,7 @@
import java.io.IOException;
import java.text.ParseException;
import jenkins.model.Jenkins;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

/**
Expand Down Expand Up @@ -61,7 +63,17 @@ public String getColumnCaption() {
return Jenkins.get().hasPermission(Jenkins.ADMINISTER) ? super.getColumnCaption() : null;
}

public static final DiskSpaceMonitorDescriptor DESCRIPTOR = new DiskSpaceMonitorDescriptor() {
@SuppressFBWarnings(value = "MS_PKGPROTECT", justification = "for backward compatibility")
public static /*almost final*/ DiskSpaceMonitorDescriptor DESCRIPTOR;

@Extension @Symbol("diskSpace")
public static class DescriptorImpl extends DiskSpaceMonitorDescriptor {

@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "for backward compatibility")
public DescriptorImpl() {
DESCRIPTOR = this;
}

@NonNull
@Override
public String getDisplayName() {
Expand All @@ -78,10 +90,5 @@ protected Callable<DiskSpace, IOException> createCallable(Computer c) {

return p.asCallableWith(new GetUsableSpace());
}
};

@Extension
public static DiskSpaceMonitorDescriptor install() {
return DESCRIPTOR;
}
}
3 changes: 3 additions & 0 deletions core/src/main/java/hudson/node_monitors/NodeMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
* <p>
* {@link NodeMonitor}s are persisted via XStream.
*
* <h2>CasC</h2>
* To be able to configure {@link NodeMonitor}s via JCasC, they should have a {@link org.kohsuke.stapler.DataBoundConstructor}
*
* @author Kohsuke Kawaguchi
* @since 1.123
*/
Expand Down
21 changes: 19 additions & 2 deletions core/src/main/java/hudson/node_monitors/ResponseTimeMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package hudson.node_monitors;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.model.Computer;
import hudson.remoting.Callable;
Expand All @@ -34,6 +35,8 @@
import java.util.logging.Logger;
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
Expand All @@ -44,8 +47,22 @@
* @author Kohsuke Kawaguchi
*/
public class ResponseTimeMonitor extends NodeMonitor {

@DataBoundConstructor
public ResponseTimeMonitor() {
}

@SuppressFBWarnings(value = "MS_PKGPROTECT", justification = "for backward compatibility")
public static /*almost final*/ AbstractNodeMonitorDescriptor<Data> DESCRIPTOR;

@Extension
public static final AbstractNodeMonitorDescriptor<Data> DESCRIPTOR = new AbstractAsyncNodeMonitorDescriptor<>() {
@Symbol("responseTime")
public static class DescriptorImpl extends AbstractAsyncNodeMonitorDescriptor<Data> {

@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "for backward compatibility")
public DescriptorImpl() {
DESCRIPTOR = this;
}

@Override
protected Callable<Data, IOException> createCallable(Computer c) {
Expand Down Expand Up @@ -91,7 +108,7 @@ public String getDisplayName() {
public NodeMonitor newInstance(StaplerRequest req, JSONObject formData) throws FormException {
return new ResponseTimeMonitor();
}
};
}

private static final class Step1 extends MasterToSlaveCallable<Data, IOException> {
private Data cur;
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/hudson/node_monitors/SwapSpaceMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jenkinsci.Symbol;
import org.jvnet.hudson.MemoryMonitor;
import org.jvnet.hudson.MemoryUsage;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
Expand All @@ -48,6 +49,11 @@
* @since 1.233
*/
public class SwapSpaceMonitor extends NodeMonitor {

@DataBoundConstructor
public SwapSpaceMonitor() {
}

/**
* Returns the HTML representation of the space.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ protected Callable<DiskSpace, IOException> createCallable(Computer c) {
}
}

/**
* @deprecated as of 2.0
*/
@Deprecated
public static DiskSpaceMonitorDescriptor install() {
return (DiskSpaceMonitorDescriptor) Jenkins.get().getDescriptor(TemporarySpaceMonitor.class);
}

protected static final class GetTempSpace extends MasterToSlaveFileCallable<DiskSpace> {
@Override
public DiskSpace invoke(File f, VirtualChannel channel) throws IOException {
Expand Down

0 comments on commit 442a42f

Please sign in to comment.