提交 91bffe3b 编写于 作者: A abayer

Adding SaveableListener, with calls to SaveableListener.fireOnChange in...

Adding SaveableListener, with calls to SaveableListener.fireOnChange in Saveable.save implementations

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@23502 71c3de6d-444a-0410-be80-ed276b4c234a
上级 26547976
......@@ -27,6 +27,7 @@ import hudson.model.Hudson;
import hudson.model.Descriptor;
import hudson.model.Saveable;
import hudson.model.listeners.ItemListener;
import hudson.model.listeners.SaveableListener;
import hudson.model.Descriptor.FormException;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
......@@ -227,6 +228,7 @@ public abstract class Plugin implements Saveable {
public void save() throws IOException {
if(BulkChange.contains(this)) return;
getConfigXml().write(this);
SaveableListener.fireOnChange(this, getConfigXml());
}
/**
......
......@@ -25,6 +25,7 @@ package hudson;
import hudson.model.Hudson;
import hudson.model.Saveable;
import hudson.model.listeners.SaveableListener;
import hudson.util.Scrambler;
import hudson.util.XStream2;
......@@ -89,6 +90,7 @@ public final class ProxyConfiguration implements Saveable {
public void save() throws IOException {
if(BulkChange.contains(this)) return;
getXmlFile().write(this);
SaveableListener.fireOnChange(this, getXmlFile());
}
public static XmlFile getXmlFile() {
......
......@@ -31,6 +31,7 @@ import hudson.model.AbstractModelObject;
import hudson.model.Failure;
import hudson.model.Hudson;
import hudson.model.Saveable;
import hudson.model.listeners.SaveableListener;
import hudson.util.CopyOnWriteList;
import hudson.util.RingBufferLogHandler;
import hudson.util.XStream2;
......@@ -199,6 +200,7 @@ public class LogRecorder extends AbstractModelObject implements Saveable {
public synchronized void save() throws IOException {
if(BulkChange.contains(this)) return;
getConfigFile().write(this);
SaveableListener.fireOnChange(this, getConfigFile());
}
/**
......
......@@ -29,6 +29,7 @@ import hudson.Functions;
import hudson.BulkChange;
import hudson.cli.declarative.CLIMethod;
import hudson.cli.declarative.CLIResolver;
import hudson.model.listeners.SaveableListener;
import hudson.security.AccessControlled;
import hudson.security.Permission;
import hudson.security.ACL;
......@@ -216,6 +217,7 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet
public synchronized void save() throws IOException {
if(BulkChange.contains(this)) return;
getConfigFile().write(this);
SaveableListener.fireOnChange(this, getConfigFile());
}
public final XmlFile getConfigFile() {
......
......@@ -28,6 +28,7 @@ import hudson.DescriptorExtensionList;
import hudson.Util;
import hudson.XmlFile;
import hudson.model.Descriptor.FormException;
import hudson.model.listeners.SaveableListener;
import hudson.node_monitors.NodeMonitor;
import hudson.slaves.NodeDescriptor;
import hudson.util.DescribableList;
......@@ -65,6 +66,7 @@ public final class ComputerSet extends AbstractModelObject {
private static final Saveable MONITORS_OWNER = new Saveable() {
public void save() throws IOException {
getConfigFile().write(monitors);
SaveableListener.fireOnChange(this, getConfigFile());
}
};
......
......@@ -27,6 +27,7 @@ import hudson.XmlFile;
import hudson.BulkChange;
import hudson.Util;
import static hudson.Util.singleQuote;
import hudson.model.listeners.SaveableListener;
import hudson.scm.CVSSCM;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
......@@ -479,6 +480,7 @@ public abstract class Descriptor<T extends Describable<T>> implements Saveable {
if(BulkChange.contains(this)) return;
try {
getConfigFile().write(this);
SaveableListener.fireOnChange(this, getConfigFile());
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to save "+getConfigFile(),e);
}
......
......@@ -33,6 +33,7 @@ import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import hudson.Util;
import hudson.XmlFile;
import hudson.BulkChange;
import hudson.model.listeners.SaveableListener;
import hudson.util.HexBinaryConverter;
import hudson.util.Iterators;
import hudson.util.XStream2;
......@@ -701,6 +702,7 @@ public class Fingerprint implements ModelObject, Saveable {
File file = getFingerprintFile(md5sum);
getConfigFile(file).write(this);
SaveableListener.fireOnChange(this, getConfigFile(file));
if(logger.isLoggable(Level.FINE))
logger.fine("Saving fingerprint "+file+" took "+(System.currentTimeMillis()-start)+"ms");
......
......@@ -58,6 +58,7 @@ import hudson.model.Descriptor.FormException;
import hudson.model.listeners.ItemListener;
import hudson.model.listeners.JobListener;
import hudson.model.listeners.JobListener.JobListenerAdapter;
import hudson.model.listeners.SaveableListener;
import hudson.model.listeners.SCMListener;
import hudson.remoting.LocalChannel;
import hudson.remoting.VirtualChannel;
......@@ -2109,6 +2110,7 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
public synchronized void save() throws IOException {
if(BulkChange.contains(this)) return;
getConfigFile().write(this);
SaveableListener.fireOnChange(this, getConfigFile());
}
......
......@@ -32,6 +32,7 @@ import hudson.cli.declarative.CLIMethod;
import hudson.cli.declarative.CLIResolver;
import hudson.remoting.AsyncFutureImpl;
import hudson.model.Node.Mode;
import hudson.model.listeners.SaveableListener;
import hudson.model.queue.CauseOfBlockage;
import hudson.model.queue.FoldableAction;
import hudson.model.queue.CauseOfBlockage.BecauseLabelIsBusy;
......@@ -295,7 +296,9 @@ public class Queue extends ResourceController implements Saveable {
}
try {
new XmlFile(XSTREAM, getXMLQueueFile()).write(items);
XmlFile queueFile = new XmlFile(XSTREAM, getXMLQueueFile());
queueFile.write(items);
SaveableListener.fireOnChange(this, queueFile);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to write out the queue file " + getXMLQueueFile(), e);
}
......
......@@ -36,6 +36,7 @@ import hudson.cli.declarative.CLIMethod;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixRun;
import hudson.model.listeners.RunListener;
import hudson.model.listeners.SaveableListener;
import hudson.search.SearchIndexBuilder;
import hudson.security.ACL;
import hudson.security.AccessControlled;
......@@ -1302,6 +1303,7 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
public synchronized void save() throws IOException {
if(BulkChange.contains(this)) return;
getDataFile().write(this);
SaveableListener.fireOnChange(this, getDataFile());
}
private XmlFile getDataFile() {
......
......@@ -24,7 +24,7 @@
package hudson.model;
import hudson.BulkChange;
import hudson.model.listeners.SaveableListener;
import java.io.IOException;
/**
......@@ -41,6 +41,8 @@ public interface Saveable {
* <p>
* For making a bulk change efficiently, see {@link BulkChange}.
*
* <p>
* To support listeners monitoring changes to this object, call {@link SaveableListener.fireOnChange}
* @throws IOException
* if the persistence failed.
*/
......
......@@ -35,6 +35,7 @@ import hudson.XmlFile;
import hudson.lifecycle.Lifecycle;
import hudson.model.UpdateSite.Data;
import hudson.model.UpdateSite.Plugin;
import hudson.model.listeners.SaveableListener;
import hudson.util.DaemonThreadFactory;
import hudson.util.IOException2;
import hudson.util.PersistedList;
......@@ -263,6 +264,7 @@ public class UpdateCenter extends AbstractModelObject implements Saveable {
if(BulkChange.contains(this)) return;
try {
getConfigFile().write(sites);
SaveableListener.fireOnChange(this, getConfigFile());
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to save "+getConfigFile(),e);
}
......
......@@ -31,6 +31,7 @@ import hudson.XmlFile;
import hudson.BulkChange;
import hudson.tasks.Mailer;
import hudson.model.Descriptor.FormException;
import hudson.model.listeners.SaveableListener;
import hudson.security.ACL;
import hudson.security.AccessControlled;
import hudson.security.Permission;
......@@ -395,6 +396,7 @@ public class User extends AbstractModelObject implements AccessControlled, Savea
public synchronized void save() throws IOException {
if(BulkChange.contains(this)) return;
getConfigFile().write(this);
SaveableListener.fireOnChange(this, getConfigFile());
}
/**
......
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts,
* Andrew Bayer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.model.listeners;
import hudson.ExtensionPoint;
import hudson.ExtensionListView;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.XmlFile;
import hudson.model.Hudson;
import hudson.model.Saveable;
import hudson.util.CopyOnWriteList;
/**
* Receives notifications about save actions on {@link Saveable} objects in Hudson.
*
* <p>
* This is an abstract class so that methods added in the future won't break existing listeners.
*
* @author Andrew Bayer
* @since 1.334
*/
public abstract class SaveableListener implements ExtensionPoint {
/**
* Called when a change is made to a {@link Saveable} object.
*
* @param o
* The saveable object.
* @param file
* The {@link XmlFile} for this saveable object.
*/
public void onChange(Saveable o, XmlFile file) {}
/**
* Registers this object as an active listener so that it can start getting
* callbacks invoked.
*
* @deprecated as of 1.281
* Put {@link Extension} on your class to get it auto-registered.
*/
public void register() {
all().add(this);
}
/**
* Reverse operation of {@link #register()}.
*/
public void unregister() {
all().remove(this);
}
/**
* Fires the {@link #onChange} event.
*/
public static void fireOnChange(Saveable o, XmlFile file) {
for (SaveableListener l : all()) {
l.onChange(o,file);
}
}
/**
* Returns all the registered {@link SaveableListener} descriptors.
*/
public static ExtensionList<SaveableListener> all() {
return Hudson.getInstance().getExtensionList(SaveableListener.class);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册