From d8096f24e46bd55f657fde2b08e0c796a7e22bee Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Wed, 30 Mar 2011 17:22:33 +0100 Subject: [PATCH] refactor the save method so that it is available to all and null safe. also added a readResolve to handle when null properties are deserialized --- core/src/main/java/hudson/model/ListView.java | 7 ------- core/src/main/java/hudson/model/TreeView.java | 4 ---- core/src/main/java/hudson/model/View.java | 21 ++++++++++++++++--- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/hudson/model/ListView.java b/core/src/main/java/hudson/model/ListView.java index db792ecc9f..e06110b0e2 100644 --- a/core/src/main/java/hudson/model/ListView.java +++ b/core/src/main/java/hudson/model/ListView.java @@ -91,13 +91,6 @@ public class ListView extends View implements Saveable { this.owner = owner; } - public void save() throws IOException { - // persistence is a part of the owner. - // due to the initialization timing issue, it can be null when this method is called. - if (owner!=null) - owner.save(); - } - private Object readResolve() { if(includeRegex!=null) includePattern = Pattern.compile(includeRegex); diff --git a/core/src/main/java/hudson/model/TreeView.java b/core/src/main/java/hudson/model/TreeView.java index a68b84af0f..9ca1e3ff15 100644 --- a/core/src/main/java/hudson/model/TreeView.java +++ b/core/src/main/java/hudson/model/TreeView.java @@ -148,10 +148,6 @@ public class TreeView extends View implements ViewGroup { // noop } - public void save() throws IOException { - owner.save(); - } - public void doCreateView( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException, FormException { checkPermission(View.CREATE); views.add(View.create(req,rsp,this)); diff --git a/core/src/main/java/hudson/model/View.java b/core/src/main/java/hudson/model/View.java index 1103a523a7..8e12a08ee9 100644 --- a/core/src/main/java/hudson/model/View.java +++ b/core/src/main/java/hudson/model/View.java @@ -130,6 +130,13 @@ public abstract class View extends AbstractModelObject implements AccessControll this.owner = owner; } + private Object readResolve() { + if (properties == null) { + properties = new ArrayList(); + } + return this; + } + /** * Gets all the items in this collection in a read-only view. */ @@ -213,7 +220,15 @@ public abstract class View extends AbstractModelObject implements AccessControll ps.add(p); p.setView(this); properties = ps; - owner.save(); + save(); + } + + public void save() throws IOException { + // persistence is a part of the owner + // due to initialization timing issue, it can be null when this method is called + if (owner != null) { + owner.save(); + } } /** @@ -615,7 +630,7 @@ public abstract class View extends AbstractModelObject implements AccessControll checkPermission(CONFIGURE); description = req.getParameter("description"); - owner.save(); + save(); rsp.sendRedirect("."); // go to the top page } @@ -658,7 +673,7 @@ public abstract class View extends AbstractModelObject implements AccessControll } properties = props; - owner.save(); + save(); rsp.sendRedirect2("../"+name); }