From 7672f00b11141eb91235104ab7ba83c7ed6c678c Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Fri, 30 May 2014 17:05:22 -0400 Subject: [PATCH] Modernizing SCMListener registration. --- .../main/java/hudson/model/AbstractBuild.java | 4 +- .../hudson/model/listeners/SCMListener.java | 43 ++++++++++++++----- core/src/main/java/jenkins/model/Jenkins.java | 5 +-- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java index c195d9855f..aec68e72ef 100644 --- a/core/src/main/java/hudson/model/AbstractBuild.java +++ b/core/src/main/java/hudson/model/AbstractBuild.java @@ -605,7 +605,7 @@ public abstract class AbstractBuild

,R extends Abs if (project.checkout(build, launcher,listener, changeLogFile)) { // check out succeeded SCM scm = project.getScm(); - for (SCMListener l : Jenkins.getInstance().getSCMListeners()) { + for (SCMListener l : SCMListener.all()) { try { l.onCheckout(build, scm, build.getWorkspace(), listener, changeLogFile, project.pollingBaseline); } catch (Exception e) { @@ -616,7 +616,7 @@ public abstract class AbstractBuild

,R extends Abs build.scm = scm.createChangeLogParser(); build.changeSet = new WeakReference>(build.calcChangeSet()); - for (SCMListener l : Jenkins.getInstance().getSCMListeners()) + for (SCMListener l : SCMListener.all()) try { l.onChangeLogParsed(build,listener,build.getChangeSet()); } catch (Exception e) { diff --git a/core/src/main/java/hudson/model/listeners/SCMListener.java b/core/src/main/java/hudson/model/listeners/SCMListener.java index ee66d354b7..b2f56a8f41 100644 --- a/core/src/main/java/hudson/model/listeners/SCMListener.java +++ b/core/src/main/java/hudson/model/listeners/SCMListener.java @@ -23,6 +23,7 @@ */ package hudson.model.listeners; +import hudson.Extension; import hudson.ExtensionPoint; import hudson.FilePath; import hudson.Launcher; @@ -36,6 +37,10 @@ import hudson.scm.ChangeLogSet; import hudson.scm.SCM; import hudson.scm.SCMRevisionState; import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; import javax.annotation.CheckForNull; import jenkins.model.Jenkins; @@ -113,20 +118,36 @@ public abstract class SCMListener implements ExtensionPoint { onChangeLogParsed((Run) build, build.getProject().getScm(), listener, changelog); } - /** - * Registers this {@link SCMListener} so that it will start receiving events. - */ + @SuppressWarnings("deprecation") + public static Collection all() { + Jenkins j = Jenkins.getInstance(); + if (j == null) { + return Collections.emptySet(); + } + List r = new ArrayList(j.getExtensionList(SCMListener.class)); + for (SCMListener l : j.getSCMListeners()) { + r.add(l); + } + return r; + } + + /** @deprecated Use {@link Extension} instead. */ + @Deprecated public final void register() { - Jenkins.getInstance().getSCMListeners().add(this); + Jenkins j = Jenkins.getInstance(); + if (j != null) { + j.getSCMListeners().add(this); + } } - /** - * Unregisters this {@link SCMListener} so that it will never receive further events. - * - *

- * Unless {@link SCMListener} is unregistered, it will never be a subject of GC. - */ + /** @deprecated Use {@link Extension} instead. */ + @Deprecated public final boolean unregister() { - return Jenkins.getInstance().getSCMListeners().remove(this); + Jenkins j = Jenkins.getInstance(); + if (j != null) { + return j.getSCMListeners().remove(this); + } else { + return false; + } } } diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 556660c495..139dc5501c 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -1220,9 +1220,8 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve updateComputerList(AUTOMATIC_SLAVE_LAUNCH); } - /** - * Gets all the installed {@link SCMListener}s. - */ + /** @deprecated Use {@link SCMListener#all} instead. */ + @Deprecated public CopyOnWriteList getSCMListeners() { return scmListeners; } -- GitLab