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 |
package com.nokia.helium.imaker.ant.engines;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.FileNotFoundException;
|
|
21 |
import java.io.FileOutputStream;
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.io.OutputStreamWriter;
|
|
24 |
import java.util.ArrayList;
|
|
25 |
import java.util.Date;
|
|
26 |
import java.util.List;
|
|
27 |
import java.util.Map.Entry;
|
|
28 |
import java.util.concurrent.ArrayBlockingQueue;
|
|
29 |
import java.util.concurrent.ThreadPoolExecutor;
|
|
30 |
import java.util.concurrent.TimeUnit;
|
|
31 |
|
628
|
32 |
import org.apache.tools.ant.BuildException;
|
587
|
33 |
import org.apache.tools.ant.Project;
|
|
34 |
import org.apache.tools.ant.types.DataType;
|
|
35 |
|
|
36 |
import com.nokia.helium.core.plexus.StreamRecorder;
|
|
37 |
import com.nokia.helium.imaker.IMaker;
|
|
38 |
import com.nokia.helium.imaker.IMakerException;
|
|
39 |
import com.nokia.helium.imaker.ant.Command;
|
|
40 |
import com.nokia.helium.imaker.ant.Engine;
|
|
41 |
import com.nokia.helium.imaker.ant.taskdefs.IMakerTask;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Engine purely based on Java. Parallelisation is
|
|
45 |
* implemented using multithreading.
|
|
46 |
*
|
|
47 |
* <pre>
|
|
48 |
* <defaultEngine id="imaker.default" threads="4" />
|
|
49 |
* </pre>
|
|
50 |
*
|
|
51 |
* @ant.type name=defaultEngine category="imaker"
|
|
52 |
*/
|
|
53 |
public class JavaEngine extends DataType implements Engine {
|
|
54 |
|
|
55 |
private IMakerTask task;
|
|
56 |
private OutputStreamWriter output;
|
|
57 |
private int threads = 1;
|
|
58 |
|
|
59 |
/**
|
|
60 |
* {@inheritDoc}
|
|
61 |
*/
|
|
62 |
@Override
|
|
63 |
public void setTask(IMakerTask task) {
|
|
64 |
this.task = task;
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Defines the number of iMaker jobs running in
|
|
69 |
* parallel.
|
|
70 |
* @ant.not-required Default value is 1.
|
|
71 |
*/
|
|
72 |
public void setThreads(int threads) {
|
|
73 |
this.threads = threads;
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* {@inheritDoc}
|
|
78 |
*/
|
|
79 |
@Override
|
|
80 |
public void build(List<List<Command>> cmds) throws IMakerException {
|
|
81 |
task.log("Building with Ant engine.");
|
|
82 |
if (threads <= 0) {
|
|
83 |
throw new BuildException("'threads' must be >= 0. (current value: " + threads + ")");
|
|
84 |
}
|
|
85 |
openLog();
|
|
86 |
// Do something with the configurations
|
|
87 |
for (List<Command> cmdlist : cmds) {
|
|
88 |
task.log("Building command list in parallel.");
|
|
89 |
if (cmdlist.size() > 0) {
|
|
90 |
ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(cmdlist.size());
|
|
91 |
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(threads, threads, 10, TimeUnit.MILLISECONDS, queue);
|
|
92 |
task.log("Adding " + cmdlist.size() + " to queue.");
|
|
93 |
for (final Command cmd : cmdlist) {
|
|
94 |
// Create a Runnable to wrap the image
|
|
95 |
// building.
|
|
96 |
threadPool.execute(new Runnable() {
|
|
97 |
public void run() {
|
|
98 |
try {
|
|
99 |
buildCommand(cmd);
|
|
100 |
} catch (IMakerException e) {
|
|
101 |
task.log(e.getMessage(), Project.MSG_ERR);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
});
|
|
105 |
}
|
|
106 |
threadPool.shutdown();
|
|
107 |
try {
|
|
108 |
while (!threadPool.isTerminated()) {
|
|
109 |
threadPool.awaitTermination(100, TimeUnit.MILLISECONDS);
|
|
110 |
}
|
|
111 |
} catch (InterruptedException e) {
|
|
112 |
e.printStackTrace();
|
|
113 |
}
|
|
114 |
}
|
|
115 |
}
|
|
116 |
closeLog();
|
|
117 |
}
|
|
118 |
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Build a Command.
|
|
122 |
* @param cmd
|
|
123 |
* @throws IMakerException
|
|
124 |
*/
|
|
125 |
protected void buildCommand(Command cmd) throws IMakerException {
|
|
126 |
IMaker imaker = task.getIMaker();
|
|
127 |
StreamRecorder rec = new StreamRecorder();
|
|
128 |
imaker.addOutputLineHandler(rec);
|
|
129 |
imaker.addErrorLineHandler(rec);
|
|
130 |
|
|
131 |
rec.consumeLine("-- " + cmd.getCmdLine());
|
|
132 |
rec.consumeLine("++ Started at " + new Date());
|
|
133 |
rec.consumeLine("+++ HiRes Start " + new Date().getTime());
|
|
134 |
|
|
135 |
List<String> args = new ArrayList<String>();
|
|
136 |
args.addAll(cmd.getArguments());
|
|
137 |
// Setting the working dir for the image creation.
|
|
138 |
File tempDir = imaker.createWorkDir();
|
|
139 |
args.add("WORKDIR=" + tempDir.getAbsolutePath());
|
|
140 |
// Pushing custom variables
|
588
|
141 |
for (Entry<String, String> cmdEnum : cmd.getVariables().entrySet()) {
|
|
142 |
if (cmdEnum.getKey().equals("WORKDIR")) {
|
587
|
143 |
task.log("WORKDIR cannot be defined by the user, the value will be ignored.", Project.MSG_WARN);
|
|
144 |
} else {
|
588
|
145 |
args.add(cmdEnum.getKey() + "=" + cmdEnum.getValue());
|
587
|
146 |
}
|
|
147 |
}
|
|
148 |
// Setting the target
|
|
149 |
args.add(cmd.getTarget());
|
|
150 |
try {
|
|
151 |
imaker.execute(args.toArray(new String[args.size()]));
|
|
152 |
} catch (IMakerException e) {
|
|
153 |
// logging iMaker execution error to the
|
|
154 |
// task and the output log.
|
|
155 |
task.log(e.getMessage(), Project.MSG_ERR);
|
|
156 |
rec.consumeLine(e.getMessage());
|
|
157 |
} finally {
|
|
158 |
rec.consumeLine("+++ HiRes End " + new Date().getTime());
|
|
159 |
rec.consumeLine("++ Finished at " + new Date());
|
|
160 |
}
|
|
161 |
// writing data
|
|
162 |
writeLog(rec.getBuffer().toString());
|
|
163 |
}
|
|
164 |
|
|
165 |
private void openLog() throws IMakerException {
|
|
166 |
if (task.getOutput() != null) {
|
|
167 |
try {
|
|
168 |
output = new OutputStreamWriter(new FileOutputStream(task.getOutput()));
|
|
169 |
} catch (FileNotFoundException e) {
|
|
170 |
throw new IMakerException(e.getMessage(), e);
|
|
171 |
}
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
private synchronized void writeLog(String str) throws IMakerException {
|
|
176 |
if (output != null) {
|
|
177 |
try {
|
|
178 |
output.write(str);
|
|
179 |
} catch (IOException e) {
|
|
180 |
throw new IMakerException(e.getMessage(), e);
|
|
181 |
}
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
private void closeLog() throws IMakerException {
|
|
186 |
if (output != null) {
|
|
187 |
try {
|
|
188 |
output.close();
|
|
189 |
} catch (IOException e) {
|
|
190 |
throw new IMakerException(e.getMessage(), e);
|
|
191 |
}
|
|
192 |
}
|
|
193 |
}
|
|
194 |
}
|