File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\TaskThread.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Red Hat, Inc.
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
26 import hudson.console.AnnotatedLargeText;
27 import hudson.util.StreamTaskListener;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.lang.ref.WeakReference;
33 import java.nio.charset.Charset;
34 import org.kohsuke.stapler.framework.io.LargeText;
35 import org.kohsuke.stapler.framework.io.ByteBuffer;
36
37 /**
38 * {@link Thread} for performing one-off task.
39 *
40 * <p>
41 * Designed to be used inside {@link TaskAction}.
42 *
43 *
44 *
45 * @author Kohsuke Kawaguchi
46 * @since 1.191
47 * @see TaskAction
48 */
49 public abstract class TaskThread extends Thread {
50 /**
51 * @deprecated as of Hudson 1.350
52 * Use {@link #log}. It's the same object, in a better type.
53 */
54 private final LargeText text;
55
56 /**
57 * Represents the output from this task thread.
58 */
59 private final AnnotatedLargeText<TaskAction> log;
60
61 /**
62 * Represents the interface to produce output.
63 */
64 private TaskListener listener;
65
66 private final TaskAction owner;
67
68 private volatile boolean isRunning;
69
70 /**
71 *
72 * @param output
73 * Determines where the output from this task thread goes.
74 */
75 protected TaskThread(TaskAction owner, ListenerAndText output) {
76 //FIXME this failed to compile super(owner.getBuild().toString()+' '+owner.getDisplayName());
77 //Please implement more general way how to get information about action owner,
78 //if you want it in the thread's name.
79 super(owner.getDisplayName());
80 this.owner = owner;
81 this.text = this.log = output.text;
82 this.listener = output.listener;
83 this.isRunning = true;
84 }
85
86 public Reader readAll() throws IOException {
87 // this method can be invoked from another thread.
88 return text.readAll();
89 }
90
91 /**
92 * Registers that this {@link TaskThread} is run for the specified
93 * {@link TaskAction}. This can be explicitly called from subtypes
94 * to associate a single {@link TaskThread} across multiple tag actions.
95 */
96 protected final void associateWith(TaskAction action) {
97 action.workerThread = this;
98 action.log = new WeakReference<AnnotatedLargeText>(log);
99 }
100
101 /**
102 * Starts the task execution asynchronously.
103 */
104 @Override
105 public void start() {
106 associateWith(owner);
107 super.start();
108 }
109
110 public boolean isRunning() {
111 return isRunning;
112 }
113
114 /**
115 * Determines where the output of this {@link TaskThread} goes.
116 * <p>
117 * Subclass can override this to send the output to a file, for example.
118 */
119 protected ListenerAndText createListener() throws IOException {
120 return ListenerAndText.forMemory();
121 }
122
123 @Override
124 public final void run() {
125 isRunning = true;
126 try {
127 perform(listener);
128 listener.getLogger().println("Completed");
129 owner.workerThread = null;
130 } catch (InterruptedException e) {
131 listener.getLogger().println("Aborted");
132 } catch (Exception e) {
133 e.printStackTrace(listener.getLogger());
134 } finally {
135 listener = null;
136 isRunning =false;
137 }
138 log.markAsComplete();
139 }
140
141 /**
142 * Do the actual work.
143 *
144 * @throws Exception
145 * The exception is recorded and reported as a failure.
146 */
147 protected abstract void perform(TaskListener listener) throws Exception;
148
149 /**
150 * Tuple of {@link TaskListener} and {@link AnnotatedLargeText}, representing
151 * the interface for producing output and how to retrieve it later.
152 */
153 public static final class ListenerAndText {
154 final TaskListener listener;
155 final AnnotatedLargeText<TaskAction> text;
156
157 public ListenerAndText(TaskListener listener, AnnotatedLargeText<TaskAction> text) {
158 this.listener = listener;
159 this.text = text;
160 }
161
162 /**
163 * @deprecated as of Hudson 1.350
164 * Use {@link #forMemory(TaskThread)} and pass in the calling {@link TaskAction}
165 */
166 public static ListenerAndText forMemory() {
167 return forMemory(null);
168 }
169
170 /**
171 * @deprecated as of Hudson 1.350
172 * Use {@link #forFile(File, TaskThread)} and pass in the calling {@link TaskAction}
173 */
174 public static ListenerAndText forFile(File f) throws IOException {
175 return forFile(f,null);
176 }
177
178 /**
179 * Creates one that's backed by memory.
180 */
181 public static ListenerAndText forMemory(TaskAction context) {
182 // StringWriter is synchronized
183 ByteBuffer log = new ByteBuffer();
184
185 return new ListenerAndText(
186 new StreamTaskListener(log),
187 new AnnotatedLargeText<TaskAction>(log,Charset.defaultCharset(),false,context)
188 );
189 }
190
191 /**
192 * Creates one that's backed by a file.
193 */
194 public static ListenerAndText forFile(File f, TaskAction context) throws IOException {
195 return new ListenerAndText(
196 new StreamTaskListener(f),
197 new AnnotatedLargeText<TaskAction>(f,Charset.defaultCharset(),false,context)
198 );
199 }
200 }
201 }
202