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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, 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.queue;
25
26 import hudson.model.Executor;
27 import jenkins.model.Jenkins;
28 import hudson.model.Queue;
29 import hudson.model.Queue.Executable;
30 import hudson.model.Queue.Task;
31 import hudson.remoting.AsyncFutureImpl;
32
33 import java.util.HashSet;
34 import java.util.Set;
35 import java.util.concurrent.ExecutionException;
36 import java.util.concurrent.Future;
37
38 /**
39 * Created when {@link Queue.Item} is created so that the caller can track the progress of the task.
40 *
41 * @author Kohsuke Kawaguchi
42 */
43 public final class FutureImpl extends AsyncFutureImpl<Executable> implements QueueTaskFuture<Executable> {
44 private final Task task;
45
46 /**
47 * If the computation has started, set to {@link Executor}s that are running the build.
48 */
49 private final Set<Executor> executors = new HashSet<Executor>();
50
51 /**
52 * {@link Future} that completes when the task started running.
53 *
54 * In contrast, {@link FutureImpl} will complete when the task finishes.
55 */
56 /*package*/ final AsyncFutureImpl<Executable> start = new AsyncFutureImpl<Executable>();
57
58 public FutureImpl(Task task) {
59 this.task = task;
60 }
61
62 public Future<Executable> getStartCondition() {
63 return start;
64 }
65
66 public final Executable waitForStart() throws InterruptedException, ExecutionException {
67 return getStartCondition().get();
68 }
69
70 @Override
71 public boolean cancel(boolean mayInterruptIfRunning) {
72 Queue q = Jenkins.getInstance().getQueue();
73 synchronized (q) {
74 synchronized (this) {
75 if(!executors.isEmpty()) {
76 if(mayInterruptIfRunning)
77 for (Executor e : executors)
78 e.interrupt();
79 return mayInterruptIfRunning;
80 }
81 return q.cancel(task);
82 }
83 }
84 }
85
86 synchronized void addExecutor(Executor executor) {
87 this.executors.add(executor);
88 }
89 }
90