628
|
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.logger.ant.taskdefs;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.FileOutputStream;
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.io.PrintStream;
|
|
23 |
import java.util.ArrayList;
|
|
24 |
import java.util.Hashtable;
|
|
25 |
import java.util.List;
|
|
26 |
|
|
27 |
import org.apache.tools.ant.BuildException;
|
|
28 |
import org.apache.tools.ant.DefaultLogger;
|
|
29 |
import org.apache.tools.ant.MagicNames;
|
|
30 |
import org.apache.tools.ant.Project;
|
|
31 |
import org.apache.tools.ant.ProjectHelper;
|
|
32 |
import org.apache.tools.ant.RuntimeConfigurable;
|
|
33 |
import org.apache.tools.ant.Task;
|
|
34 |
import org.apache.tools.ant.TaskContainer;
|
|
35 |
import org.apache.tools.ant.UnknownElement;
|
|
36 |
import org.apache.tools.ant.taskdefs.Recorder.VerbosityLevelChoices;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* The taskRecorder task allows you to record the output of the execution
|
|
40 |
* of a set of task into a log file. The output is not redirected to any
|
|
41 |
* other recorders, so nothing will appear on the console.
|
|
42 |
*
|
|
43 |
* Example of usage:
|
|
44 |
* <pre>
|
|
45 |
* <hlm:taskRecorder output="output.log" logLevel="verbose" >
|
|
46 |
* <echo>This output will be recorded under output.log</echo>
|
|
47 |
* <property name="new.property" value="value" />
|
|
48 |
* </hlm:taskRecorder>
|
|
49 |
* </pre>
|
|
50 |
*
|
|
51 |
* In the previous example the output of echo task is redirected to the output log.
|
|
52 |
*
|
|
53 |
* @ant.task name="taskRecorder" category="Logging"
|
|
54 |
*/
|
|
55 |
public class TaskRecorder extends Task implements TaskContainer {
|
|
56 |
private List<Task> tasks = new ArrayList<Task>();
|
|
57 |
private File output;
|
|
58 |
private int logLevel = Project.MSG_INFO;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* {@inheritDoc}
|
|
62 |
*/
|
|
63 |
@Override
|
|
64 |
public void addTask(Task task) {
|
|
65 |
tasks.add(task);
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
* {@inheritDoc}
|
|
70 |
*/
|
|
71 |
public void execute() {
|
|
72 |
if (output == null) {
|
|
73 |
throw new BuildException("The output attribute has not been defined.");
|
|
74 |
}
|
|
75 |
|
|
76 |
// creating a project delegator, which will propagate the properties into
|
|
77 |
// the parent project
|
|
78 |
ProjectDelegator subProject = new ProjectDelegator(getProject());
|
|
79 |
getProject().initSubProject(subProject);
|
|
80 |
// set user-defined properties
|
|
81 |
getProject().copyUserProperties(subProject);
|
|
82 |
subProject.initProperties();
|
|
83 |
ProjectHelper.configureProject(subProject, new File(getProject().getProperty(MagicNames.ANT_FILE)));
|
|
84 |
|
|
85 |
// The delegator enables the property propagation.
|
|
86 |
subProject.setUseDelegate(true);
|
|
87 |
// Let's replicate all our childs into a sequential task which is
|
|
88 |
// going to use the delegate project.
|
|
89 |
UnknownElement subTask = new UnknownElement("sequential");
|
|
90 |
subTask.setTaskName("sequential");
|
|
91 |
subTask.setNamespace("");
|
|
92 |
subTask.setQName("sequential");
|
|
93 |
subTask.setProject(subProject);
|
|
94 |
new RuntimeConfigurable(subTask, "sequential");
|
|
95 |
for (Task task : this.tasks) {
|
|
96 |
if (task instanceof UnknownElement) {
|
|
97 |
UnknownElement ue = ((UnknownElement)task).copy(subProject);
|
|
98 |
ue.setProject(subProject);
|
|
99 |
subTask.addChild(ue);
|
|
100 |
subTask.getWrapper().addChild(ue.getWrapper());
|
|
101 |
} else {
|
|
102 |
log("Task " + task.getTaskName() + " is not a UnknownElement. Element will be ignored.", Project.MSG_WARN);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
PrintStream out = null;
|
|
107 |
DefaultLogger logger = null;
|
|
108 |
try {
|
|
109 |
// Creating the a logger to record the execution
|
|
110 |
out = new PrintStream(new FileOutputStream(output));
|
|
111 |
logger = new DefaultLogger();
|
|
112 |
logger.setMessageOutputLevel(this.logLevel);
|
|
113 |
logger.setOutputPrintStream(out);
|
|
114 |
logger.setErrorPrintStream(out);
|
|
115 |
subProject.addBuildListener(logger);
|
|
116 |
log("Recording output to " + output.getAbsolutePath());
|
|
117 |
subTask.perform();
|
|
118 |
} catch (IOException ex) {
|
|
119 |
log("Can't set output to " + output + ": " + ex.getMessage(), Project.MSG_ERR);
|
|
120 |
throw new BuildException("Can't set output to " + output + ": " + ex.getMessage());
|
|
121 |
} finally {
|
|
122 |
if (logger != null) {
|
|
123 |
subProject.removeBuildListener(logger);
|
|
124 |
}
|
|
125 |
if (out != null) {
|
|
126 |
out.close();
|
|
127 |
}
|
|
128 |
subProject = null;
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Defines the output log.
|
|
134 |
* @param output
|
|
135 |
* @ant.required
|
|
136 |
*/
|
|
137 |
public void setOutput(File output) {
|
|
138 |
this.output = output;
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Defines the logging level (e.g error, warning, info, verbose, debug).
|
|
143 |
* @param logLevel
|
|
144 |
* @ant.not-required Default is info.
|
|
145 |
*/
|
|
146 |
public void setLogLevel(VerbosityLevelChoices logLevel) {
|
|
147 |
this.logLevel = logLevel.getLevel();
|
|
148 |
}
|
|
149 |
|
|
150 |
|
|
151 |
/**
|
|
152 |
* Project used to delegate property manipulation
|
|
153 |
* calls to a delegate project.
|
|
154 |
*
|
|
155 |
*/
|
|
156 |
class ProjectDelegator extends Project {
|
|
157 |
private Project delegate;
|
|
158 |
private boolean useDelegate;
|
|
159 |
|
|
160 |
public ProjectDelegator(Project delegate) {
|
|
161 |
this.delegate = delegate;
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* @return the useDelegate
|
|
166 |
*/
|
|
167 |
public boolean isUseDelegate() {
|
|
168 |
return useDelegate;
|
|
169 |
}
|
|
170 |
|
|
171 |
/**
|
|
172 |
* @param useDelegate the useDelegate to set
|
|
173 |
*/
|
|
174 |
public void setUseDelegate(boolean useDelegate) {
|
|
175 |
this.useDelegate = useDelegate;
|
|
176 |
}
|
|
177 |
|
|
178 |
/**
|
|
179 |
* @return
|
|
180 |
* @see org.apache.tools.ant.Project#getProperties()
|
|
181 |
*/
|
|
182 |
public Hashtable getProperties() {
|
|
183 |
if (useDelegate) {
|
|
184 |
return delegate.getProperties();
|
|
185 |
} else {
|
|
186 |
return super.getProperties();
|
|
187 |
}
|
|
188 |
}
|
|
189 |
/**
|
|
190 |
* @param propertyName
|
|
191 |
* @return
|
|
192 |
* @see org.apache.tools.ant.Project#getProperty(java.lang.String)
|
|
193 |
*/
|
|
194 |
public String getProperty(String propertyName) {
|
|
195 |
if (useDelegate) {
|
|
196 |
return delegate.getProperty(propertyName);
|
|
197 |
} else {
|
|
198 |
return super.getProperty(propertyName);
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
/**
|
|
203 |
* @return
|
|
204 |
* @see org.apache.tools.ant.Project#getUserProperties()
|
|
205 |
*/
|
|
206 |
public Hashtable getUserProperties() {
|
|
207 |
if (useDelegate) {
|
|
208 |
return delegate.getUserProperties();
|
|
209 |
} else {
|
|
210 |
return super.getUserProperties();
|
|
211 |
}
|
|
212 |
}
|
|
213 |
/**
|
|
214 |
* @param propertyName
|
|
215 |
* @return
|
|
216 |
* @see org.apache.tools.ant.Project#getUserProperty(java.lang.String)
|
|
217 |
*/
|
|
218 |
public String getUserProperty(String propertyName) {
|
|
219 |
if (useDelegate) {
|
|
220 |
return delegate.getUserProperty(propertyName);
|
|
221 |
} else {
|
|
222 |
return super.getUserProperty(propertyName);
|
|
223 |
}
|
|
224 |
}
|
|
225 |
/**
|
|
226 |
* @param value
|
|
227 |
* @return
|
|
228 |
* @throws BuildException
|
|
229 |
* @see org.apache.tools.ant.Project#replaceProperties(java.lang.String)
|
|
230 |
*/
|
|
231 |
public String replaceProperties(String value) {
|
|
232 |
if (useDelegate) {
|
|
233 |
return delegate.replaceProperties(value);
|
|
234 |
} else {
|
|
235 |
return super.replaceProperties(value);
|
|
236 |
}
|
|
237 |
}
|
|
238 |
|
|
239 |
/**
|
|
240 |
* @param name
|
|
241 |
* @param value
|
|
242 |
* @see org.apache.tools.ant.Project#setNewProperty(java.lang.String, java.lang.String)
|
|
243 |
*/
|
|
244 |
public void setNewProperty(String name, String value) {
|
|
245 |
if (useDelegate) {
|
|
246 |
delegate.setNewProperty(name, value);
|
|
247 |
} else {
|
|
248 |
super.setNewProperty(name, value);
|
|
249 |
}
|
|
250 |
}
|
|
251 |
|
|
252 |
/**
|
|
253 |
* @param name
|
|
254 |
* @param value
|
|
255 |
* @see org.apache.tools.ant.Project#setProperty(java.lang.String, java.lang.String)
|
|
256 |
*/
|
|
257 |
public void setProperty(String name, String value) {
|
|
258 |
if (useDelegate) {
|
|
259 |
delegate.setProperty(name, value);
|
|
260 |
} else {
|
|
261 |
super.setProperty(name, value);
|
|
262 |
}
|
|
263 |
}
|
|
264 |
|
|
265 |
/**
|
|
266 |
* @param name
|
|
267 |
* @param value
|
|
268 |
* @see org.apache.tools.ant.Project#setUserProperty(java.lang.String, java.lang.String)
|
|
269 |
*/
|
|
270 |
public void setUserProperty(String name, String value) {
|
|
271 |
if (useDelegate) {
|
|
272 |
delegate.setUserProperty(name, value);
|
|
273 |
} else {
|
|
274 |
super.setUserProperty(name, value);
|
|
275 |
}
|
|
276 |
}
|
|
277 |
}
|
|
278 |
}
|