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

http://github.com/hudson/hudson · Java · 140 lines · 53 code · 16 blank · 71 comment · 5 complexity · 272754b1c3f0d47816871c2405fd7f92 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jorg Heymans
  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.console.AnnotatedLargeText;
  26. import org.kohsuke.stapler.framework.io.LargeText;
  27. import org.kohsuke.stapler.StaplerRequest;
  28. import org.kohsuke.stapler.StaplerResponse;
  29. import javax.servlet.ServletException;
  30. import javax.servlet.http.HttpServletResponse;
  31. import java.lang.ref.WeakReference;
  32. import java.io.IOException;
  33. import hudson.security.Permission;
  34. import hudson.security.ACL;
  35. /**
  36. * Partial {@link Action} implementation for those who kick some
  37. * processing asynchronously (such as SCM tagging.)
  38. *
  39. * <p>
  40. * The class offers the basic set of functionality to do it.
  41. *
  42. * @author Kohsuke Kawaguchi
  43. * @since 1.191
  44. * @see TaskThread
  45. */
  46. public abstract class TaskAction extends AbstractModelObject implements Action {
  47. /**
  48. * If non-null, that means either the activitiy is in progress
  49. * asynchronously, or it failed unexpectedly and the thread is dead.
  50. */
  51. protected transient volatile TaskThread workerThread;
  52. /**
  53. * Hold the log of the tagging operation.
  54. */
  55. protected transient WeakReference<AnnotatedLargeText> log;
  56. /**
  57. * Gets the permission object that represents the permission to perform this task.
  58. */
  59. protected abstract Permission getPermission();
  60. /**
  61. * Gets the {@link ACL} against which the permissions are checked.
  62. */
  63. protected abstract ACL getACL();
  64. /**
  65. * @deprecated as of 1.350
  66. * Use {@link #obtainLog()}, which returns the same object in a more type-safe signature.
  67. */
  68. public LargeText getLog() {
  69. return obtainLog();
  70. }
  71. /**
  72. * Obtains the log file.
  73. *
  74. * <p>
  75. * The default implementation get this from {@link #workerThread},
  76. * so when it's complete, the log could be gone any time.
  77. *
  78. * <p>
  79. * Derived classes that persist the text should override this
  80. * method so that it fetches the file from disk.
  81. */
  82. public AnnotatedLargeText obtainLog() {
  83. WeakReference<AnnotatedLargeText> l = log;
  84. if(l==null) return null;
  85. return l.get();
  86. }
  87. public String getSearchUrl() {
  88. return getUrlName();
  89. }
  90. public TaskThread getWorkerThread() {
  91. return workerThread;
  92. }
  93. /**
  94. * Handles incremental log output.
  95. */
  96. public void doProgressiveLog( StaplerRequest req, StaplerResponse rsp) throws IOException {
  97. AnnotatedLargeText text = obtainLog();
  98. if(text!=null) {
  99. text.doProgressText(req,rsp);
  100. return;
  101. }
  102. rsp.setStatus(HttpServletResponse.SC_OK);
  103. }
  104. /**
  105. * Handles incremental log output.
  106. */
  107. public void doProgressiveHtml( StaplerRequest req, StaplerResponse rsp) throws IOException {
  108. AnnotatedLargeText text = obtainLog();
  109. if(text!=null) {
  110. text.doProgressiveHtml(req,rsp);
  111. return;
  112. }
  113. rsp.setStatus(HttpServletResponse.SC_OK);
  114. }
  115. /**
  116. * Clears the error status.
  117. */
  118. public synchronized void doClearError(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
  119. getACL().checkPermission(getPermission());
  120. if(workerThread!=null && !workerThread.isRunning())
  121. workerThread = null;
  122. rsp.sendRedirect(".");
  123. }
  124. }