From fd7b5b919e439d9ae1a0336520b0aa5823bdce0b Mon Sep 17 00:00:00 2001 From: kohsuke Date: Fri, 5 Jan 2007 00:35:55 +0000 Subject: [PATCH] adding a new kind of listeners for Trac integration, CVS-NEWS integration, java.net issue tracker integration, etc, which all work with commit messages. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@1639 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/model/Hudson.java | 48 ++++++++----- .../hudson/model/listeners/SCMListener.java | 71 +++++++++++++++++++ 2 files changed, 103 insertions(+), 16 deletions(-) create mode 100644 core/src/main/java/hudson/model/listeners/SCMListener.java diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index 2bd99933a9..e42cc56633 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -12,6 +12,7 @@ import hudson.Util; import hudson.XmlFile; import hudson.model.Descriptor.FormException; import hudson.model.listeners.JobListener; +import hudson.model.listeners.SCMListener; import hudson.remoting.LocalChannel; import hudson.remoting.VirtualChannel; import hudson.scm.CVSSCM; @@ -25,6 +26,7 @@ import hudson.tasks.Mailer; import hudson.tasks.Publisher; import hudson.triggers.Trigger; import hudson.triggers.Triggers; +import hudson.util.CopyOnWriteList; import hudson.util.FormFieldValidator; import hudson.util.XStream2; import org.apache.commons.fileupload.FileItem; @@ -148,7 +150,12 @@ public final class Hudson extends JobCollection implements Node { /** * List of registered {@link JobListener}s. */ - private transient final List jobListeners = new Vector(); + private transient final CopyOnWriteList jobListeners = new CopyOnWriteList(); + + /** + * List of registered {@link SCMListener}s. + */ + private transient final CopyOnWriteList scmListeners = new CopyOnWriteList(); public static Hudson getInstance() { return theInstance; @@ -174,10 +181,8 @@ public final class Hudson extends JobCollection implements Node { getQueue().load(); - synchronized(jobListeners) { - for (JobListener l : jobListeners) - l.onLoaded(); - } + for (JobListener l : jobListeners) + l.onLoaded(); } /** @@ -239,7 +244,8 @@ public final class Hudson extends JobCollection implements Node { /** * Adds a new {@link JobListener}. * - * @see #removeListener(JobListener) + * @deprecated + * Use {@code getJobListners().add(l)} instead. */ public void addListener(JobListener l) { jobListeners.add(l); @@ -248,13 +254,27 @@ public final class Hudson extends JobCollection implements Node { /** * Deletes an existing {@link JobListener}. * - * @return - * true if the listener was indeed registered. + * @deprecated + * Use {@code getJobListners().remove(l)} instead. */ public boolean removeListener(JobListener l ) { return jobListeners.remove(l); } + /** + * Gets all the installed {@link JobListener}s. + */ + public CopyOnWriteList getJobListeners() { + return jobListeners; + } + + /** + * Gets all the installed {@link SCMListener}s. + */ + public CopyOnWriteList getSCMListeners() { + return scmListeners; + } + /** * Gets the plugin object from its short name. * @@ -607,10 +627,8 @@ public final class Hudson extends JobCollection implements Node { * Called in response to {@link Job#doDoDelete(StaplerRequest, StaplerResponse)} */ /*package*/ void deleteJob(Job job) throws IOException { - synchronized(jobListeners) { - for (JobListener l : jobListeners) - l.onDeleted(job); - } + for (JobListener l : jobListeners) + l.onDeleted(job); jobs.remove(job.getName()); if(views!=null) { @@ -892,10 +910,8 @@ public final class Hudson extends JobCollection implements Node { jobs.put(name,result); } - synchronized(jobListeners) { - for (JobListener l : jobListeners) - l.onCreated(result); - } + for (JobListener l : jobListeners) + l.onCreated(result); rsp.sendRedirect2(req.getContextPath()+'/'+result.getUrl()+"configure"); return result; diff --git a/core/src/main/java/hudson/model/listeners/SCMListener.java b/core/src/main/java/hudson/model/listeners/SCMListener.java new file mode 100644 index 0000000000..af46a6be72 --- /dev/null +++ b/core/src/main/java/hudson/model/listeners/SCMListener.java @@ -0,0 +1,71 @@ +package hudson.model.listeners; + +import hudson.model.AbstractBuild; +import hudson.model.Hudson; +import hudson.scm.SCM; +import hudson.scm.ChangeLogSet; + +/** + * Receives notifications about SCM activities in Hudson. + * + *

+ * This is an abstract class so that methods added in the future won't break existing listeners. + * + *

+ * Once instanciated, use the {@link #register()} method to start receiving events. + * + * @author Kohsuke Kawaguchi + * @see Hudson#getSCMListeners() + * @since 1.70 + */ +public abstract class SCMListener { + /** + * Called once the changelog is determined. + * + *

+ * During a build, Hudson fetches the update of the workspace from SCM, + * and determines the changelog (see {@link SCM#checkout}). Immediately + * after that, a build will invoke this method on all registered + * {@link SCMListener}s. + * + *

+ * This is an opportunity for SCM-related plugins to act on changelog. + * A typical usage includes parsing commit messages and do cross-referencing + * between other systems. + * + *

+ * If a build failed before we successfully determine changelog, this method + * will not be invoked (for example, if "cvs update" failed.) OTOH, this method + * is invoked before the actual build (like ant invocation) happens. + * + *

+ * TODO: once we have cvsnews plugin, refer to its usage. + * + * @param build + * The build in progress, which just finished determining changelog. + * At this point this build is still in progress. Never null. + * @param changelog + * Set of changes detected in this build. This is the same value + * returned from {@link AbstractBuild#getChangeSet()} but passed + * separately for convenience. + */ + public void onChangeLogParsed(AbstractBuild build, ChangeLogSet changelog) { + } + + /** + * Registers this {@link SCMListener} so that it will start receiving events. + */ + public final void register() { + Hudson.getInstance().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. + */ + public final boolean unregister() { + return Hudson.getInstance().getSCMListeners().remove(this); + } +}