/hudson-core/src/main/java/hudson/tasks/Publisher.java

http://github.com/hudson/hudson · Java · 188 lines · 66 code · 15 blank · 107 comment · 5 complexity · 8861d47172088b8bfa6f1d10c2ffad25 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2011, Oracle Corporation, Kohsuke Kawaguchi, Anton Kozak
  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.tasks;
  25. import hudson.DescriptorExtensionList;
  26. import hudson.Extension;
  27. import hudson.ExtensionComponent;
  28. import hudson.model.Action;
  29. import hudson.model.Build;
  30. import hudson.model.BuildListener;
  31. import hudson.model.Describable;
  32. import hudson.model.Project;
  33. import hudson.model.Descriptor;
  34. import hudson.model.Hudson;
  35. import hudson.model.Result;
  36. import java.util.List;
  37. import java.util.ArrayList;
  38. import java.util.Collections;
  39. import java.util.Comparator;
  40. /**
  41. * {@link BuildStep}s that run after the build is completed.
  42. *
  43. * <p>
  44. * To register a custom {@link Publisher} from a plugin,
  45. * put {@link Extension} on your descriptor implementation.
  46. *
  47. * <p>
  48. * Starting 1.178, publishers are exposed to all kinds of different
  49. * project type, not just the freestyle project type (in particular,
  50. * the native maven2 job type.) This is convenient default for
  51. * {@link Publisher}s in particular initially, but we encourage advanced
  52. * plugins to consider writing MavenReporter, as it offers the
  53. * potential of reducing the amount of configuration needed to run the plugin.
  54. *
  55. * For those plugins that don't want {@link Publisher} to show up in
  56. * different job type, use {@link BuildStepDescriptor} for the base type
  57. * of your descriptor to control which job type it supports.
  58. *
  59. * @author Kohsuke Kawaguchi, Anton Kozak
  60. */
  61. public abstract class Publisher extends BuildStepCompatibilityLayer implements BuildStep, Describable<Publisher> {
  62. /**
  63. * @deprecated
  64. * Don't extend from {@link Publisher} directly. Instead, choose {@link Recorder} or {@link Notifier}
  65. * as your base class.
  66. */
  67. @Deprecated
  68. protected Publisher() {
  69. }
  70. //
  71. // these two methods need to remain to keep binary compatibility with plugins built with Hudson < 1.150
  72. //
  73. /**
  74. * Default implementation that does nothing.
  75. * @deprecated since 1.150
  76. */
  77. @Deprecated @Override
  78. public boolean prebuild(Build build, BuildListener listener) {
  79. return true;
  80. }
  81. /**
  82. * Default implementation that does nothing.
  83. * @deprecated since 1.150
  84. */
  85. @Deprecated @Override
  86. public Action getProjectAction(Project project) {
  87. return null;
  88. }
  89. /**
  90. * Return true if this {@link Publisher} needs to run after the build result is
  91. * fully finalized.
  92. *
  93. * <p>
  94. * The execution of normal {@link Publisher}s are considered within a part
  95. * of the build. This allows publishers to mark the build as a failure, or
  96. * to include their execution time in the total build time.
  97. *
  98. * <p>
  99. * So normally, that is the preferrable behavior, but in a few cases
  100. * this is problematic. One of such cases is when a publisher needs to
  101. * trigger other builds, which in turn need to see this build as a
  102. * completed build. Those plugins that need to do this can return true
  103. * from this method, so that the {@link #perform(AbstractBuild, Launcher, BuildListener)}
  104. * method is called after the build is marked as completed.
  105. *
  106. * <p>
  107. * When {@link Publisher} behaves this way, note that they can no longer
  108. * change the build status anymore.
  109. *
  110. * @author Kohsuke Kawaguchi
  111. * @since 1.153
  112. */
  113. public boolean needsToRunAfterFinalized() {
  114. return false;
  115. }
  116. /**
  117. * Returns true if this {@link Publisher} needs to run depends on Build {@link Result}.
  118. * <p/>
  119. * Can be used if execution of {@link Publisher} is not required for some Build {@link Result},
  120. * i.e. ABORTED, FAILED, etc.
  121. * <p/>
  122. *
  123. * @since 2.1.1
  124. */
  125. public boolean needsToRun(Result buildResult) {
  126. return true;
  127. }
  128. public Descriptor<Publisher> getDescriptor() {
  129. return Hudson.getInstance().getDescriptorOrDie(getClass());
  130. }
  131. /**
  132. * {@link Publisher} has a special sort semantics that requires a subtype.
  133. *
  134. * @see DescriptorExtensionList#createDescriptorList(Hudson, Class)
  135. */
  136. public static final class DescriptorExtensionListImpl extends DescriptorExtensionList<Publisher,Descriptor<Publisher>>
  137. implements Comparator<ExtensionComponent<Descriptor<Publisher>>> {
  138. public DescriptorExtensionListImpl(Hudson hudson) {
  139. super(hudson,Publisher.class);
  140. }
  141. @Override
  142. protected List<ExtensionComponent<Descriptor<Publisher>>> sort(List<ExtensionComponent<Descriptor<Publisher>>> r) {
  143. List<ExtensionComponent<Descriptor<Publisher>>> copy = new ArrayList<ExtensionComponent<Descriptor<Publisher>>>(r);
  144. Collections.sort(copy,this);
  145. return copy;
  146. }
  147. public int compare(ExtensionComponent<Descriptor<Publisher>> lhs, ExtensionComponent<Descriptor<Publisher>> rhs) {
  148. int r = classify(lhs.getInstance())-classify(rhs.getInstance());
  149. if (r!=0) return r;
  150. return lhs.compareTo(rhs);
  151. }
  152. /**
  153. * If recorder, return 0, if unknown return 1, if notifier returns 2.
  154. * This is used as a sort key.
  155. */
  156. private int classify(Descriptor<Publisher> d) {
  157. if(d.isSubTypeOf(Recorder.class)) return 0;
  158. if(d.isSubTypeOf(Notifier.class)) return 2;
  159. // for compatibility, if the descriptor is manually registered in a specific way, detect that.
  160. Class<? extends Publisher> kind = PublisherList.KIND.get(d);
  161. if(kind==Recorder.class) return 0;
  162. if(kind==Notifier.class) return 2;
  163. return 1;
  164. }
  165. }
  166. /**
  167. * Returns all the registered {@link Publisher} descriptors.
  168. */
  169. // for backward compatibility, the signature is not BuildStepDescriptor
  170. public static DescriptorExtensionList<Publisher,Descriptor<Publisher>> all() {
  171. return Hudson.getInstance().<Publisher,Descriptor<Publisher>>getDescriptorList(Publisher.class);
  172. }
  173. }