587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.ant.taskdefs;
|
|
19 |
|
628
|
20 |
import java.util.concurrent.ExecutorService;
|
|
21 |
import java.util.concurrent.Executors;
|
|
22 |
import java.util.concurrent.ThreadPoolExecutor;
|
|
23 |
|
587
|
24 |
import org.apache.tools.ant.BuildException;
|
628
|
25 |
import org.apache.tools.ant.taskdefs.ExecTask;
|
587
|
26 |
|
|
27 |
/**
|
|
28 |
* Exec task using shared thread pool
|
|
29 |
* @ant.task name="exec"
|
|
30 |
*/
|
|
31 |
public class HlmExecTask extends ExecTask
|
|
32 |
{
|
|
33 |
private static int poolSize = Runtime.getRuntime().availableProcessors();
|
|
34 |
private static int maxPoolSize = poolSize * 2;
|
|
35 |
private static ExecutorService threadPool = Executors.newFixedThreadPool(maxPoolSize);
|
|
36 |
private final Object semaphore = new Object();
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Submit exec into pool and throw exceptions
|
|
40 |
*/
|
|
41 |
public void execute()
|
|
42 |
{
|
588
|
43 |
String property = getProject().getProperty("number.of.threads");
|
628
|
44 |
if (property != null) {
|
588
|
45 |
((ThreadPoolExecutor)threadPool).setCorePoolSize(Integer.parseInt(property));
|
|
46 |
((ThreadPoolExecutor)threadPool).setMaximumPoolSize(Integer.parseInt(property));
|
587
|
47 |
}
|
588
|
48 |
TaskRunnable taskRunnable = new TaskRunnable();
|
|
49 |
threadPool.submit(taskRunnable);
|
587
|
50 |
try {
|
|
51 |
synchronized (semaphore) {
|
628
|
52 |
while (!taskRunnable.isFinished()) {
|
587
|
53 |
semaphore.wait();
|
628
|
54 |
}
|
587
|
55 |
}
|
|
56 |
} catch (InterruptedException e) { e.printStackTrace(); }
|
|
57 |
|
588
|
58 |
Throwable exception = taskRunnable.getException();
|
|
59 |
if (exception != null)
|
587
|
60 |
{
|
628
|
61 |
if (exception instanceof BuildException) {
|
588
|
62 |
throw (BuildException)exception;
|
628
|
63 |
} else {
|
588
|
64 |
exception.printStackTrace();
|
628
|
65 |
}
|
587
|
66 |
}
|
|
67 |
}
|
|
68 |
|
|
69 |
private class TaskRunnable implements Runnable {
|
|
70 |
private boolean finished;
|
|
71 |
private volatile Thread thread;
|
|
72 |
private Throwable exception;
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Executes the task within a thread and takes care about
|
|
76 |
* Exceptions raised within the task.
|
|
77 |
*/
|
|
78 |
public void run() {
|
|
79 |
try {
|
|
80 |
thread = Thread.currentThread();
|
|
81 |
HlmExecTask.super.execute();
|
|
82 |
} // CheckStyle:IllegalCatch OFF
|
|
83 |
catch (Throwable t) {
|
|
84 |
exception = t;
|
|
85 |
} // CheckStyle:IllegalCatch ON
|
|
86 |
finally {
|
|
87 |
synchronized (semaphore) {
|
|
88 |
finished = true;
|
|
89 |
semaphore.notifyAll();
|
|
90 |
}
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* get any exception that got thrown during execution;
|
|
96 |
* @return an exception or null for no exception/not yet finished
|
|
97 |
*/
|
|
98 |
public Throwable getException() {
|
|
99 |
return exception;
|
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Provides the indicator that the task has been finished.
|
|
104 |
* @return Returns true when the task is finished.
|
|
105 |
*/
|
|
106 |
boolean isFinished() {
|
|
107 |
return finished;
|
|
108 |
}
|
|
109 |
|
|
110 |
void interrupt() {
|
|
111 |
thread.interrupt();
|
|
112 |
}
|
|
113 |
}
|
|
114 |
} |