summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakozak2011-11-22 03:50:28 (EST)
committer Winston Prakash2011-12-01 20:46:55 (EST)
commit29169f0c536e93587dc884eddd6a89daf7530df7 (patch)
tree2c67810cc81c7c129b7f7ae77c664d4c06414c38
parentecfaa4313f12dac7376ebc3bd22e4644d5351440 (diff)
downloadorg.eclipse.hudson.core-29169f0c536e93587dc884eddd6a89daf7530df7.zip
org.eclipse.hudson.core-29169f0c536e93587dc884eddd6a89daf7530df7.tar.gz
org.eclipse.hudson.core-29169f0c536e93587dc884eddd6a89daf7530df7.tar.bz2
Replace property_name enum with simple constants. Add javadocs. Introduce BaseProperty class which contains common methods for IProperty handling
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
-rw-r--r--hudson-core/src/main/java/hudson/model/BaseProperty.java120
-rw-r--r--hudson-core/src/main/java/hudson/model/FreeStyleProject.java19
-rw-r--r--hudson-core/src/main/java/hudson/model/Job.java27
-rw-r--r--hudson-core/src/main/java/hudson/model/StringProperty.java68
-rw-r--r--hudson-core/src/main/java/org/eclipse/hudson/api/model/IJob.java2
-rw-r--r--hudson-core/src/main/java/org/eclipse/hudson/api/model/IProperty.java2
-rw-r--r--hudson-core/src/main/resources/lib/hudson/project/config-customWorkspace.jelly2
7 files changed, 157 insertions, 83 deletions
diff --git a/hudson-core/src/main/java/hudson/model/BaseProperty.java b/hudson-core/src/main/java/hudson/model/BaseProperty.java
new file mode 100644
index 0000000..a1b7b77
--- /dev/null
+++ b/hudson-core/src/main/java/hudson/model/BaseProperty.java
@@ -0,0 +1,120 @@
+/*******************************************************************************
+ *
+ * Copyright (c) 2011 Oracle Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *
+ * Nikita Levyankov
+ *
+ *******************************************************************************/
+
+package hudson.model;
+
+import java.io.IOException;
+import org.eclipse.hudson.api.model.IJob;
+import org.eclipse.hudson.api.model.IProperty;
+
+/**
+ * Base property implementation. Contains common methods for setting and getting cascading and overridden properties.
+ * <p/>
+ * Date: 9/22/11
+ *
+ * @author Nikita Levyankov
+ */
+public abstract class BaseProperty<T> implements IProperty<T> {
+
+ private String propertyKey;
+ private transient IJob job;
+ private T originalValue;
+ private boolean propertyOverridden;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setKey(String propertyKey) {
+ this.propertyKey = propertyKey;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setJob(IJob job) {
+ assert job != null;
+ this.job = job;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("unchecked")
+ public T getCascadingValue() throws IOException {
+ return job.hasCascadingProject() ?
+ (T) job.getCascadingProject().getProperty(propertyKey, this.getClass()).getValue() : null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isPropertyOverridden() {
+ return propertyOverridden;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public T getValue() throws IOException {
+ if (isPropertyOverridden() || null != getOriginalValue()) {
+ return getOriginalValue();
+ }
+ return getCascadingValue();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("unchecked")
+ public void setValue(T value) throws IOException {
+ value = prepareValue(value);
+ if (!job.hasCascadingProject()) {
+ originalValue = value;
+ } else if (allowOverrideValue(
+ (T) job.getCascadingProject().getProperty(propertyKey, this.getClass()).getValue(), value)) {
+ originalValue = value;
+ propertyOverridden = true;
+ } else {
+ this.originalValue = null;
+ propertyOverridden = false;
+ }
+ }
+
+ /**
+ * Returns true, if cascading value should be overridden by candidate value.
+ *
+ * @param cascadingValue value from cascading project if any.
+ * @param candidateValue candidate value.
+ * @return true if cascading value should be replaced by candidate value.
+ */
+ protected abstract boolean allowOverrideValue(T cascadingValue, T candidateValue);
+
+ /**
+ * Pre-process candidate value.
+ *
+ * @param candidateValue candidateValue.
+ * @return candidateValue by default.
+ */
+ protected T prepareValue(T candidateValue) {
+ return candidateValue;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public T getOriginalValue() {
+ return originalValue;
+ }
+}
diff --git a/hudson-core/src/main/java/hudson/model/FreeStyleProject.java b/hudson-core/src/main/java/hudson/model/FreeStyleProject.java
index 97f3a49..2adf096 100644
--- a/hudson-core/src/main/java/hudson/model/FreeStyleProject.java
+++ b/hudson-core/src/main/java/hudson/model/FreeStyleProject.java
@@ -36,6 +36,8 @@ import javax.servlet.ServletException;
public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> implements TopLevelItem,
IFreeStyleProject {
+ public static final String CUSTOM_WORKSPACE_PROPERTY_KEY = "customWorkspace";
+
/**
* See {@link #setCustomWorkspace(String)}.
*
@@ -59,18 +61,14 @@ public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> i
return FreeStyleBuild.class;
}
- public String getCustomWorkspace(boolean useParentValue) throws IOException{
- StringProperty jobProperty = getCustomWorkspaceProperty();
+ public String getCustomWorkspace(boolean useParentValue) throws IOException {
+ StringProperty jobProperty = getStringProperty(CUSTOM_WORKSPACE_PROPERTY_KEY);
if (!useParentValue) {
- return jobProperty.getOriginalValue();
+ return jobProperty.getOriginalValue();
}
return jobProperty.getValue();
}
- public StringProperty getCustomWorkspaceProperty() throws IOException {
- return (StringProperty)getProperty(PROPERTY_NAME.CUSTOM_WORKSPACE, StringProperty.class);
- }
-
public String getCustomWorkspace() throws IOException {
return getCustomWorkspace(true);
}
@@ -95,15 +93,16 @@ public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> i
* @throws IOException if any.
*/
public void setCustomWorkspace(String customWorkspace) throws IOException {
- StringProperty jobProperty = getCustomWorkspaceProperty();
+ StringProperty jobProperty = getStringProperty(CUSTOM_WORKSPACE_PROPERTY_KEY);
jobProperty.setValue(customWorkspace);
save();
}
@Override
- protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException {
+ protected void submit(StaplerRequest req, StaplerResponse rsp)
+ throws IOException, ServletException, Descriptor.FormException {
setCustomWorkspace(
- req.hasParameter("customWorkspace")? req.getParameter("customWorkspace.directory") : null);
+ req.hasParameter("customWorkspace") ? req.getParameter("customWorkspace.directory") : null);
super.submit(req, rsp);
}
diff --git a/hudson-core/src/main/java/hudson/model/Job.java b/hudson-core/src/main/java/hudson/model/Job.java
index 0696b71..2e67b5b 100644
--- a/hudson-core/src/main/java/hudson/model/Job.java
+++ b/hudson-core/src/main/java/hudson/model/Job.java
@@ -131,11 +131,7 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
private transient volatile boolean holdOffBuildUntilSave;
private volatile LogRotator logRotator;
- private ConcurrentMap<Enum, IProperty> jobProperties = new ConcurrentHashMap<Enum, IProperty>();
-
- public enum PROPERTY_NAME {
- CUSTOM_WORKSPACE
- }
+ private ConcurrentMap<String, IProperty> jobProperties = new ConcurrentHashMap<String, IProperty>();
/**
* Not all plugins are good at calculating their health report quickly.
@@ -211,35 +207,38 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
/**
* Put job property to properties map.
+ *
* @param key key.
* @param property property instance.
*/
- protected void putJobProperty(Enum key, IProperty property) {
+ protected void putJobProperty(String key, IProperty property) {
jobProperties.put(key, property);
}
/**
* Returns job property by specified key.
+ *
* @param key key.
- * @return {@link IProperty} instance or null.
+ * @return {@link org.hudsonci.api.model.IProperty} instance or null.
* @throws IOException if any.
*/
- public IProperty getProperty(Enum key) throws IOException {
+ public IProperty getProperty(String key) throws IOException {
return getProperty(key, null);
}
/**
* Returns null safe job property by specified key. if property is not present, try instantiate it.
+ *
* @param key key.
* @param clazz type of property..
- * @return {@link IProperty} instance or null.
+ * @return {@link org.hudsonci.api.model.IProperty} instance or null.
* @throws IOException if any.
*/
- public IProperty getProperty(Enum key, Class clazz) throws IOException {
+ public IProperty getProperty(String key, Class clazz) throws IOException {
IProperty t = jobProperties.get(key);
if (null == t && null != clazz) {
try {
- t = (IProperty)clazz.newInstance();
+ t = (IProperty) clazz.newInstance();
t.setJob(this);
t.setKey(key);
putJobProperty(key, t);
@@ -252,6 +251,10 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
return t;
}
+ public StringProperty getStringProperty(String key) throws IOException {
+ return (StringProperty) getProperty(key, StringProperty.class);
+ }
+
@Override
public synchronized void save() throws IOException {
if (allowSave.get()) {
@@ -307,7 +310,7 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
}
if (null == jobProperties) {
- jobProperties = new ConcurrentHashMap<Enum, IProperty>();
+ jobProperties = new ConcurrentHashMap<String, IProperty>();
}
for (IProperty property : jobProperties.values()) {
property.setJob(this);
diff --git a/hudson-core/src/main/java/hudson/model/StringProperty.java b/hudson-core/src/main/java/hudson/model/StringProperty.java
index aa2824d..ef86fe2 100644
--- a/hudson-core/src/main/java/hudson/model/StringProperty.java
+++ b/hudson-core/src/main/java/hudson/model/StringProperty.java
@@ -15,67 +15,19 @@
package hudson.model;
-import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
-import org.eclipse.hudson.api.model.IJob;
-import org.eclipse.hudson.api.model.IProperty;
-/**
- * String property.
- * <p/>
- * Date: 9/22/11
- *
- * @author Nikita Levyankov
- */
-public class StringProperty implements IProperty<String> {
-
- private Enum propertyKey;
- private transient IJob job;
- private String originalValue;
- private boolean propertyOverridden;
-
- public void setKey(Enum propertyKey) {
- this.propertyKey = propertyKey;
- }
-
- public void setJob(IJob job) {
- this.job = job;
- }
-
- public StringProperty() {
- }
-
- public void setValue(String value) throws IOException {
- value = StringUtils.trimToNull(value);
- if (!job.hasCascadingProject()) {
- originalValue = value;
- } else if (!StringUtils.equalsIgnoreCase(
- (String) job.getCascadingProject().getProperty(propertyKey, this.getClass()).getValue(), value)) {
- originalValue = value;
- propertyOverridden = true;
- } else {
- this.originalValue = null;
- propertyOverridden = false;
- }
- }
-
- public String getOriginalValue() {
- return originalValue;
- }
-
- public String getCascadingValue() throws IOException {
- return job.hasCascadingProject() ?
- (String) job.getCascadingProject().getProperty(propertyKey, this.getClass()).getValue() : null;
- }
+public class StringProperty extends BaseProperty<String> {
- public boolean isPropertyOverridden() {
- return propertyOverridden;
+ @Override
+ protected String prepareValue(String candidateValue) {
+ return StringUtils.trimToNull(candidateValue);
}
- public String getValue() throws IOException {
- if (isPropertyOverridden() || null != getOriginalValue()) {
- return getOriginalValue();
- }
- return getCascadingValue();
+ /**
+ * {@inheritDoc}
+ */
+ protected boolean allowOverrideValue(String cascadingValue, String candidateValue) {
+ return !StringUtils.equalsIgnoreCase(cascadingValue, candidateValue);
}
-}
+} \ No newline at end of file
diff --git a/hudson-core/src/main/java/org/eclipse/hudson/api/model/IJob.java b/hudson-core/src/main/java/org/eclipse/hudson/api/model/IJob.java
index c18c92f..ec4e560 100644
--- a/hudson-core/src/main/java/org/eclipse/hudson/api/model/IJob.java
+++ b/hudson-core/src/main/java/org/eclipse/hudson/api/model/IJob.java
@@ -52,7 +52,7 @@ public interface IJob<T extends IJob> {
* @return {@link IProperty} instance or null.
* @throws java.io.IOException if any.
*/
- IProperty getProperty(Enum key, Class<? extends IProperty> clazz) throws IOException;
+ IProperty getProperty(String key, Class<? extends IProperty> clazz) throws IOException;
/**
* Checks whether current job is inherited from other project.
diff --git a/hudson-core/src/main/java/org/eclipse/hudson/api/model/IProperty.java b/hudson-core/src/main/java/org/eclipse/hudson/api/model/IProperty.java
index 46dc8ab..9516460 100644
--- a/hudson-core/src/main/java/org/eclipse/hudson/api/model/IProperty.java
+++ b/hudson-core/src/main/java/org/eclipse/hudson/api/model/IProperty.java
@@ -32,7 +32,7 @@ public interface IProperty<T> extends Serializable {
*
* @param key key.
*/
- void setKey(Enum key);
+ void setKey(String key);
/**
* Sets the job, which is owner of current property.
diff --git a/hudson-core/src/main/resources/lib/hudson/project/config-customWorkspace.jelly b/hudson-core/src/main/resources/lib/hudson/project/config-customWorkspace.jelly
index 7beebfb..5fc8939 100644
--- a/hudson-core/src/main/resources/lib/hudson/project/config-customWorkspace.jelly
+++ b/hudson-core/src/main/resources/lib/hudson/project/config-customWorkspace.jelly
@@ -17,7 +17,7 @@
<!-- custom workspace -->
<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">
- <j:set var="customWorkspaceProperty" value="${it.getCustomWorkspaceProperty()}"/>
+ <j:set var="customWorkspaceProperty" value="${it.getStringProperty(it.CUSTOM_WORKSPACE_PROPERTY_KEY)}"/>
<j:set var="customWorkspace" value="${customWorkspaceProperty.getValue()}"/>
<f:optionalBlock name="customWorkspace" title="${%Use custom workspace}" checked="${customWorkspace!=null}"
isCascadingValue="${customWorkspaceProperty.isPropertyOverridden()}"