From 365665f37eed4a9fbd8504a465399581903d555a Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 7 Aug 2008 05:41:56 +0000 Subject: [PATCH] defining methods for persisting data. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11315 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/Plugin.java | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/core/src/main/java/hudson/Plugin.java b/core/src/main/java/hudson/Plugin.java index 32d51c93e8..3c0f6abd6e 100644 --- a/core/src/main/java/hudson/Plugin.java +++ b/core/src/main/java/hudson/Plugin.java @@ -14,9 +14,11 @@ import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.io.File; import java.net.URL; import net.sf.json.JSONObject; +import com.thoughtworks.xstream.XStream; /** * Base class of Hudson plugin. @@ -143,4 +145,38 @@ public abstract class Plugin { // use serveLocalizedFile to support automatic locale selection rsp.serveLocalizedFile(req, new URL(wrapper.baseResourceURL,'.'+path)); } + +// +// Convenience methods for those plugins that persist configuration +// + /** + * Loads serializable fields of this instance from the persisted storage. + * + *

+ * If there was no previously persisted state, this method is no-op. + */ + protected void load() throws IOException { + XmlFile xml = getConfigXml(); + if(xml.exists()) + xml.unmarshal(this); + } + + /** + * Saves serializable fields of this instance to the persisted storage. + */ + protected void save() throws IOException { + getConfigXml().write(this); + } + + /** + * Controls the file where {@link #load()} and {@link #save()} + * persists data. + * + * This method can be also overriden if the plugin wants to + * use a custom {@link XStream} instance to persist data. + */ + protected XmlFile getConfigXml() { + return new XmlFile(Hudson.XSTREAM, + new File(Hudson.getInstance().getRootDir(),wrapper.getShortName()+".xml")); + } } -- GitLab