/hudson-core/src/main/java/hudson/model/JobProperty.java

http://github.com/hudson/hudson · Java · 180 lines · 52 code · 16 blank · 112 comment · 1 complexity · 79697e48c414b90a8cb626569e53f614 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.model;
  25. import hudson.ExtensionPoint;
  26. import hudson.Launcher;
  27. import hudson.Plugin;
  28. import hudson.model.queue.SubTask;
  29. import hudson.tasks.BuildStep;
  30. import hudson.tasks.Builder;
  31. import hudson.tasks.Publisher;
  32. import hudson.tasks.BuildStepMonitor;
  33. import java.io.IOException;
  34. import java.util.Collection;
  35. import java.util.Collections;
  36. import org.kohsuke.stapler.export.ExportedBean;
  37. /**
  38. * Extensible property of {@link Job}.
  39. *
  40. * <p>
  41. * {@link Plugin}s can extend this to define custom properties
  42. * for {@link Job}s. {@link JobProperty}s show up in the user
  43. * configuration screen, and they are persisted with the job object.
  44. *
  45. * <p>
  46. * Configuration screen should be defined in <tt>config.jelly</tt>.
  47. * Within this page, the {@link JobProperty} instance is available
  48. * as <tt>instance</tt> variable (while <tt>it</tt> refers to {@link Job}.
  49. *
  50. * <p>
  51. * Starting 1.150, {@link JobProperty} implements {@link BuildStep},
  52. * meaning it gets the same hook as {@link Publisher} and {@link Builder}.
  53. * The primary intention of this mechanism is so that {@link JobProperty}s
  54. * can add actions to the new build. The {@link #perform(AbstractBuild, Launcher, BuildListener)}
  55. * and {@link #prebuild(AbstractBuild, BuildListener)} are invoked after those
  56. * of {@link Publisher}s.
  57. *
  58. * @param <J>
  59. * When you restrict your job property to be only applicable to a certain
  60. * subtype of {@link Job}, you can use this type parameter to improve
  61. * the type signature of this class. See {@link JobPropertyDescriptor#isApplicable(Class)}.
  62. *
  63. * @author Kohsuke Kawaguchi
  64. * @see JobPropertyDescriptor
  65. * @since 1.72
  66. */
  67. @ExportedBean
  68. public abstract class JobProperty<J extends Job<?,?>> implements Describable<JobProperty<?>>, BuildStep, ExtensionPoint {
  69. /**
  70. * The {@link Job} object that owns this property.
  71. * This value will be set by the Hudson code.
  72. * Derived classes can expect this value to be always set.
  73. */
  74. protected transient J owner;
  75. /**
  76. * Hook for performing post-initialization action.
  77. *
  78. * <p>
  79. * This method is invoked in two cases. One is when the {@link Job} that owns
  80. * this property is loaded from disk, and the other is when a job is re-configured
  81. * and all the {@link JobProperty} instances got re-created.
  82. */
  83. protected void setOwner(J owner) {
  84. this.owner = owner;
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public JobPropertyDescriptor getDescriptor() {
  90. return (JobPropertyDescriptor)Hudson.getInstance().getDescriptorOrDie(getClass());
  91. }
  92. /**
  93. * @deprecated
  94. * as of 1.341. Override {@link #getJobActions(Job)} instead.
  95. */
  96. public Action getJobAction(J job) {
  97. return null;
  98. }
  99. /**
  100. * {@link Action}s to be displayed in the job page.
  101. *
  102. * <p>
  103. * Returning actions from this method allows a job property to add them
  104. * to the left navigation bar in the job page.
  105. *
  106. * <p>
  107. * {@link Action} can implement additional marker interface to integrate
  108. * with the UI in different ways.
  109. *
  110. * @param job
  111. * Always the same as {@link #owner} but passed in anyway for backward compatibility (I guess.)
  112. * You really need not use this value at all.
  113. * @return
  114. * can be empty but never null.
  115. * @since 1.341
  116. * @see ProminentProjectAction
  117. * @see PermalinkProjectAction
  118. */
  119. public Collection<? extends Action> getJobActions(J job) {
  120. // delegate to getJobAction (singular) for backward compatible behavior
  121. Action a = getJobAction(job);
  122. if (a==null) return Collections.emptyList();
  123. return Collections.singletonList(a);
  124. }
  125. //
  126. // default no-op implementation
  127. //
  128. public boolean prebuild(AbstractBuild<?,?> build, BuildListener listener) {
  129. return true;
  130. }
  131. /**
  132. * {@inheritDoc}
  133. *
  134. * <p>
  135. * Invoked after {@link Publisher}s have run.
  136. */
  137. public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
  138. return true;
  139. }
  140. /**
  141. * Returns {@link BuildStepMonitor#NONE} by default, as {@link JobProperty}s normally don't depend
  142. * on its previous result.
  143. */
  144. public BuildStepMonitor getRequiredMonitorService() {
  145. return BuildStepMonitor.NONE;
  146. }
  147. public final Action getProjectAction(AbstractProject<?,?> project) {
  148. return getJobAction((J)project);
  149. }
  150. public final Collection<? extends Action> getProjectActions(AbstractProject<?,?> project) {
  151. return getJobActions((J)project);
  152. }
  153. public Collection<?> getJobOverrides() {
  154. return Collections.emptyList();
  155. }
  156. /**
  157. * Contributes {@link SubTask}s to {@link AbstractProject#getSubTasks()}
  158. *
  159. * @since 1.377
  160. */
  161. public Collection<? extends SubTask> getSubTasks() {
  162. return Collections.emptyList();
  163. }
  164. }