提交 b59ac469 编写于 作者: K kohsuke

added a new extension point LabelAtomProperty to allow plugins to add semantics to labels

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@33849 71c3de6d-444a-0410-be80-ed276b4c234a
上级 831a6e8b
......@@ -62,13 +62,13 @@ import com.thoughtworks.xstream.io.HierarchicalStreamReader;
*/
@ExportedBean
public abstract class Label implements Comparable<Label>, ModelObject {
protected final String name;
private volatile Set<Node> nodes;
private volatile Set<Cloud> clouds;
protected transient final String name;
private transient volatile Set<Node> nodes;
private transient volatile Set<Cloud> clouds;
@Exported
public final LoadStatistics loadStatistics;
public final NodeProvisioner nodeProvisioner;
public transient final LoadStatistics loadStatistics;
public transient final NodeProvisioner nodeProvisioner;
public Label(String name) {
this.name = name;
......
......@@ -23,10 +23,26 @@
*/
package hudson.model.label;
import hudson.BulkChange;
import hudson.XmlFile;
import hudson.model.Descriptor.FormException;
import hudson.model.Failure;
import hudson.model.Hudson;
import hudson.model.Label;
import hudson.model.Saveable;
import hudson.model.listeners.SaveableListener;
import hudson.util.DescribableList;
import hudson.util.VariableResolver;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Atomic single token label, like "foo" or "bar".
......@@ -34,9 +50,21 @@ import hudson.util.VariableResolver;
* @author Kohsuke Kawaguchi
* @since 1.372
*/
public class LabelAtom extends Label {
public class LabelAtom extends Label implements Saveable {
private DescribableList<LabelAtomProperty,LabelAtomPropertyDescriptor> properties =
new DescribableList<LabelAtomProperty,LabelAtomPropertyDescriptor>(this);
public LabelAtom(String name) {
super(name);
load();
}
/**
* Properties associated with this label.
*/
@Exported
public DescribableList<LabelAtomProperty, LabelAtomPropertyDescriptor> getProperties() {
return properties;
}
@Override
......@@ -49,6 +77,55 @@ public class LabelAtom extends Label {
return LabelOperatorPrecedence.ATOM;
}
/*package*/ XmlFile getConfigFile() {
return new XmlFile(Hudson.XSTREAM,new File(Hudson.getInstance().root,"labels/"+name+".xml"));
}
public void save() throws IOException {
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);
}
}
public void load() {
XmlFile file = getConfigFile();
if(file.exists()) {
try {
file.unmarshal(this);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to load "+file, e);
}
}
properties.setOwner(this);
}
/**
* Returns all the {@link LabelAtomPropertyDescriptor}s that can be potentially configured
* on this label.
*/
public List<LabelAtomPropertyDescriptor> getApplicablePropertyDescriptors() {
return LabelAtomProperty.all();
}
/**
* Accepts the update to the node configuration.
*/
public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException, FormException {
final Hudson app = Hudson.getInstance();
app.checkPermission(Hudson.ADMINISTER);
properties.rebuildHetero(req, req.getSubmittedForm(), getApplicablePropertyDescriptors(), "properties");
save();
// take the user back to the label top page.
rsp.sendRedirect2(".");
}
/**
* Obtains an atom by its {@linkplain #getName() name}.
*/
......@@ -64,4 +141,6 @@ public class LabelAtom extends Label {
return false;
}
}
private static final Logger LOGGER = Logger.getLogger(LabelAtom.class.getName());
}
/*
* The MIT License
*
* Copyright (c) 2010, InfraDNA, 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.label;
import hudson.DescriptorExtensionList;
import hudson.ExtensionPoint;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Hudson;
import hudson.slaves.NodeDescriptor;
import hudson.slaves.NodePropertyDescriptor;
import org.kohsuke.stapler.export.ExportedBean;
/**
* Extensible property of {@link LabelAtom}.
*
* <p>
* Plugins can contribute this extension point to add additional data or UI actions to {@link LabelAtom}.
* {@link LabelAtomProperty}s show up in the configuration screen of a label, and they are persisted
* with the {@link LabelAtom} object.
*
* @author Kohsuke Kawaguchi
* @since 1.373
*/
@ExportedBean
public class LabelAtomProperty extends AbstractDescribableImpl<LabelAtomProperty> implements ExtensionPoint {
/**
* Lists up all the registered {@link LabelAtomPropertyDescriptor}s in the system.
*/
public static DescriptorExtensionList<LabelAtomProperty,LabelAtomPropertyDescriptor> all() {
return Hudson.getInstance().getDescriptorList(LabelAtomProperty.class);
}
}
/*
* The MIT License
*
* Copyright (c) 2010, InfraDNA, 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.label;
import hudson.Extension;
import hudson.model.Descriptor;
/**
* {@link Descriptor} for {@link LabelAtom}.
*
* <p>
* Put {@link Extension} on your descriptor implementation to have it auto-registered.
*
* @author Kohsuke Kawaguchi
* @since 1.373
*/
public abstract class LabelAtomPropertyDescriptor extends Descriptor<LabelAtomProperty> {
}
<!--
The MIT License
Copyright (c) 2010, InfraDNA, 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.
-->
<!--
Config page
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<l:layout norefresh="true" permission="${it.CONFIGURE}" title="${%title(it.displayName)}">
<st:include page="sidepanel.jelly"/>
<l:main-panel>
<f:form method="post" action="configSubmit" name="config">
<f:entry title="${%Name}">
<f:textbox value="${it.name}" readonly="true" />
</f:entry>
<f:hetero-list name="properties" hasHeader="true"
descriptors="${it.getApplicablePropertyDescriptors()}"
items="${it.properties}"
addCaption="${%Add label property}"/>
<f:block>
<f:submit value="${%Save}"/>
</f:block>
</f:form>
</l:main-panel>
</l:layout>
</j:jelly>
/*
* The MIT License
*
* Copyright (c) 2010, InfraDNA, 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.label;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* @author Kohsuke Kawaguchi
*/
public class LabelAtomPropertyTest extends HudsonTestCase {
public static class LabelAtomPropertyImpl extends LabelAtomProperty {
public final String abc;
@DataBoundConstructor
public LabelAtomPropertyImpl(String abc) {
this.abc = abc;
}
@TestExtension
public static class DescriptorImpl extends LabelAtomPropertyDescriptor {
@Override
public String getDisplayName() {
return "Test label atom property";
}
}
}
/**
* Tests the configuration persistence between disk, memory, and UI.
*/
public void testConfigRoundtrip() throws Exception {
LabelAtom foo = hudson.getLabelAtom("foo");
LabelAtomPropertyImpl old = new LabelAtomPropertyImpl("value");
foo.getProperties().add(old);
assertTrue(foo.getConfigFile().exists());
foo.load(); // make sure load works
// it should survive the configuration roundtrip
submit(createWebClient().goTo("label/foo/configure").getFormByName("config"));
assertEquals(1,foo.getProperties().size());
assertEqualDataBoundBeans(old, foo.getProperties().get(LabelAtomPropertyImpl.class));
}
}
<!--
The MIT License
Copyright (c) 2010, InfraDNA, 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.
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry field="abc">
<f:textbox />
</f:entry>
</j:jelly>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册