Introduced OptionalJobProperty.

This commit is contained in:
Jesse Glick committed 2015-10-27 14:38:42 -04:00
1 parent b21c33fb0b
commit 53c86ec857
4 files changed
+99 -1

No files matched your search

@@ -38,6 +38,7 @@ import java.util.Collection;
import java.util.Collections;
import jenkins.model.Jenkins;
import jenkins.model.OptionalJobProperty;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.ExportedBean;
@@ -63,6 +64,8 @@ import org.kohsuke.stapler.export.ExportedBean;
* and {@link #prebuild(AbstractBuild, BuildListener)} are invoked after those
* of {@link Publisher}s.
*
* <p>Consider extending {@link OptionalJobProperty} instead.
*
* @param <J>
* When you restrict your job property to be only applicable to a certain
* subtype of {@link Job}, you can use this type parameter to improve
@@ -32,6 +32,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.lang.reflect.Type;
import java.lang.reflect.ParameterizedType;
import jenkins.model.OptionalJobProperty;
import net.sf.json.JSONObject;
@@ -60,7 +61,7 @@ public abstract class JobPropertyDescriptor extends Descriptor<JobProperty<?>> {
* {@inheritDoc}
*
* @return
* null to avoid setting an instance of {@link JobProperty} to the target project.
* null to avoid setting an instance of {@link JobProperty} to the target project (or just use {@link OptionalJobProperty})
*/
@Override
public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) throws FormException {
@@ -0,0 +1,61 @@
/*
* The MIT License
*
* Copyright 2015 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 jenkins.model;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
/**
* Job property which may or may not be present.
* Must define {@code config-details.jelly} or {@code config-details.groovy}.
* May define {@code help.html}.
* @since TODO
*/
public abstract class OptionalJobProperty<J extends Job<?,?>> extends JobProperty<J> {
@Override
public OptionalJobPropertyDescriptor getDescriptor() {
return (OptionalJobPropertyDescriptor) super.getDescriptor();
}
public static abstract class OptionalJobPropertyDescriptor extends JobPropertyDescriptor {
protected OptionalJobPropertyDescriptor(Class<? extends JobProperty<?>> clazz) {
super(clazz);
}
protected OptionalJobPropertyDescriptor() {}
@Override
public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) throws FormException {
return formData.optBoolean("specified") ? super.newInstance(req, formData) : null;
}
}
}
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2015 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.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:f="/lib/form">
<f:block>
<f:optionalBlock title="${descriptor.displayName}" name="specified" checked="${instance != null}" inline="true" help="${descriptor.helpFile}">
<st:include page="config-details" from="${descriptor}" class="${descriptor.clazz}"/>
</f:optionalBlock>
</f:block>
</j:jelly>