提交 c1b58970 编写于 作者: K Kohsuke Kawaguchi

generalized the reconfigure concept.

上级 f9d83a48
/*
* The MIT License
*
* Copyright (c) 2011, CloudBees, Inc.
*
* 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;
import hudson.model.Descriptor.FormException;
import hudson.slaves.NodeProperty;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
/**
* Marks modern {@link Describable}s that allow the current instances to pass information down to the next
* instance when the configuration is submitted.
*
* <p>
* As the generics signature implies, it isn't up to individual {@link Describable} implementation to
* implement this. Rather, it is up to the designer of an extension point to mark the extension point
* as {@link ReconfigurableDescribable}, as it requires coordination at the owner of the extension point.
*
* <h1>Use Cases</h1>
* <h2>Invisible Property</h2>
* <p>
* This mechanism can be used to create an entirely invisible {@link Describable}, which is handy
* for {@link NodeProperty}, {@link JobProperty}, etc. To do so, define a descriptor with null
* {@linkplain Descriptor#getDisplayName() display name} and empty config.jelly to prevent it from
* showing up in the config UI, then implement {@link #reconfigure(StaplerRequest, JSONObject)}
* and simply return {@code this}.
*
* <h2>Passing some values without going through clients</h2>
* <p>
* Sometimes your {@link Describable} object may have some expensive objects that you might want to
* hand over to the next instance. This hook lets you do that.
*
* @author Kohsuke Kawaguchi
* @since 1.406
*/
public interface ReconfigurableDescribable<T extends ReconfigurableDescribable<T>> extends Describable<T> {
/**
* When a parent/owner object of a Describable gets a config form submission and instances are
* recreated, this method is invoked on the existing instance (meaning the 'this' reference
* points to the existing instance) to create a new instance to be added to the parent/owner object.
*
* <p>
* The default implementation of this should be the following:
* <pre>
* return form==null ? null : getDescriptor().newInstance(req, form);
* </pre>
*
* @param req
* The current HTTP request being processed.
* @param form
* JSON fragment that corresponds to this describable object.
* If the newly submitted form doesn't include a fragment for this describable
* (meaning the user has de-selected your descriptor), then this argument is null.
*
* @return
* The new instance. To not to create an instance of a describable, return null.
*/
T reconfigure(StaplerRequest req, JSONObject form) throws FormException;
}
......@@ -50,7 +50,7 @@ import java.util.List;
* @author Stephen Connolly
* @since 1.406
*/
public class ViewProperty implements Describable<ViewProperty>, ExtensionPoint {
public class ViewProperty implements ReconfigurableDescribable<ViewProperty>, ExtensionPoint {
/**
* The view object that owns this property.
* This value will be set by the core code.
......@@ -71,6 +71,6 @@ public class ViewProperty implements Describable<ViewProperty>, ExtensionPoint {
}
public ViewProperty reconfigure(StaplerRequest req, JSONObject form) throws Descriptor.FormException {
return getDescriptor().newInstance(req, form);
return form==null ? null : getDescriptor().newInstance(req, form);
}
}
......@@ -36,8 +36,10 @@ import hudson.model.DependencyGraph;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Descriptor.FormException;
import hudson.model.ReconfigurableDescribable;
import hudson.model.Saveable;
import net.sf.json.JSONObject;
import org.apache.avalon.framework.configuration.Reconfigurable;
import org.kohsuke.stapler.StaplerRequest;
import java.io.IOException;
......@@ -141,11 +143,23 @@ public class DescribableList<T extends Describable<T>, D extends Descriptor<T>>
List<T> newList = new ArrayList<T>();
for (Descriptor<T> d : descriptors) {
T existing = get((D)d);
String name = d.getJsonSafeClassName();
if (json.has(name)) {
T instance = d.newInstance(req, json.getJSONObject(name));
newList.add(instance);
JSONObject o = json.optJSONObject(name);
T instance = null;
if (o!=null) {
if (existing instanceof ReconfigurableDescribable)
instance = (T)((ReconfigurableDescribable)existing).reconfigure(req,o);
else
instance = d.newInstance(req, o);
} else {
if (existing instanceof ReconfigurableDescribable)
instance = (T)((ReconfigurableDescribable)existing).reconfigure(req,null);
}
if (instance!=null)
newList.add(instance);
}
replaceBy(newList);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册