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.helium.internaldata.ant.listener;
|
|
19 |
|
628
|
20 |
import java.lang.management.ManagementFactory;
|
|
21 |
import java.lang.management.MemoryMXBean;
|
|
22 |
import java.lang.management.MemoryUsage;
|
587
|
23 |
import java.util.Date;
|
628
|
24 |
import java.util.Hashtable;
|
|
25 |
|
|
26 |
import org.apache.log4j.Logger;
|
587
|
27 |
import org.apache.tools.ant.BuildEvent;
|
|
28 |
import org.apache.tools.ant.BuildListener;
|
|
29 |
import org.apache.tools.ant.Project;
|
|
30 |
import org.apache.tools.ant.SubBuildListener;
|
|
31 |
import org.dom4j.Document;
|
628
|
32 |
|
|
33 |
import com.nokia.helium.internaldata.ant.taskdefs.HlmAssertMessageTask;
|
587
|
34 |
|
|
35 |
/**
|
|
36 |
* Listener class for the Logger.
|
|
37 |
*
|
|
38 |
*/
|
|
39 |
public class Listener implements BuildListener, SubBuildListener {
|
|
40 |
|
|
41 |
// Logger for listener
|
|
42 |
private Logger log;
|
|
43 |
|
|
44 |
// Root node.
|
|
45 |
private BuildNode buildNode;
|
|
46 |
|
|
47 |
// Ant build Stack. Useful to associate with current parent.
|
|
48 |
private EndLessStack<DataNode> buildEventStack = new EndLessStack<DataNode>();
|
|
49 |
|
|
50 |
// default list of properties to extract.
|
628
|
51 |
private String[] propList = {"os.name", "user.name", "build.name",
|
|
52 |
"build.number", "build.id", "build.family", "build.system",
|
|
53 |
"env.NUMBER_OF_PROCESSORS", "helium.version",
|
|
54 |
"env.SYMSEE_VERSION", "diamonds.build.id"};
|
587
|
55 |
|
|
56 |
// Memory bean
|
|
57 |
private MemoryMXBean mbean;
|
|
58 |
|
|
59 |
public Listener() {
|
|
60 |
log = Logger.getLogger(Listener.class);
|
|
61 |
mbean = ManagementFactory.getMemoryMXBean();
|
|
62 |
}
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Method to call to trigger the data sending.
|
|
66 |
*/
|
|
67 |
public void sendData(String smtpServer, BuildEvent event) {
|
|
68 |
if (buildNode != null) {
|
|
69 |
Document database = null;
|
|
70 |
log.debug("Creating the XML log.");
|
|
71 |
XMLRenderer writer = new XMLRenderer(buildNode, database, this.extractProperties(), event);
|
|
72 |
EmailDataSender sender = new EmailDataSender();
|
|
73 |
// Setting the server address.
|
|
74 |
sender.setSMTPServer(smtpServer);
|
|
75 |
log.debug("Sending the data.");
|
|
76 |
String xml = writer.toString();
|
|
77 |
log.debug(xml);
|
|
78 |
sender.sendData(xml);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Extracting properties from the build.
|
|
85 |
* @return a hashtable containing relevant properties and their value.
|
|
86 |
*/
|
|
87 |
@SuppressWarnings("unchecked")
|
|
88 |
private Hashtable<String, String> extractProperties() {
|
|
89 |
Hashtable<String, String> properties = new Hashtable<String, String>();
|
|
90 |
if (buildNode != null) {
|
|
91 |
Project project = (Project)buildNode.getReference();
|
|
92 |
Hashtable<String, String> projProps = project.getProperties();
|
|
93 |
for (int i = 0; i < propList.length; i++) {
|
|
94 |
if (projProps.containsKey(propList[i])) {
|
|
95 |
properties.put(propList[i], projProps.get(propList[i]));
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
return properties;
|
|
100 |
}
|
|
101 |
|
|
102 |
//-------------------------------------------------------------
|
|
103 |
//
|
|
104 |
// Implementing BuildListener and SubBuildListener interface
|
|
105 |
//
|
|
106 |
//-------------------------------------------------------------
|
|
107 |
public synchronized void buildFinished(BuildEvent event) {
|
|
108 |
log.debug("buildFinished");
|
|
109 |
if (buildNode != null) {
|
|
110 |
BuildNode node = (BuildNode)buildNode.find(event.getProject());
|
|
111 |
if (node != null) {
|
|
112 |
node.setEndTime(new Date());
|
|
113 |
node.setSuccessful(event.getException() == null);
|
|
114 |
}
|
|
115 |
}
|
|
116 |
String smtpServer = event.getProject().getProperty("email.smtp.server");
|
|
117 |
this.sendData(smtpServer, event);
|
|
118 |
}
|
|
119 |
|
|
120 |
public synchronized void buildStarted(BuildEvent event) {
|
|
121 |
if (buildNode == null) {
|
|
122 |
// Create data node for a build
|
|
123 |
buildNode = new BuildNode(null, event.getProject());
|
|
124 |
// Garbage collector for execution.
|
|
125 |
buildEventStack.setDefaultElement(buildNode);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
public synchronized void subBuildFinished(BuildEvent event) {
|
|
130 |
if (buildNode != null) {
|
|
131 |
BuildNode node = (BuildNode)buildNode.find(event.getProject());
|
|
132 |
if (node != null) {
|
|
133 |
node.setEndTime(new Date());
|
|
134 |
node.setReference(null);
|
|
135 |
node.setSuccessful(event.getException() == null);
|
|
136 |
} else {
|
|
137 |
log.debug("subBuildFinished - could not find subbuild.");
|
|
138 |
}
|
|
139 |
buildEventStack.pop();
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
public synchronized void subBuildStarted(BuildEvent event) {
|
|
144 |
DataNode parentNode = buildEventStack.peek();
|
|
145 |
if (parentNode != null) {
|
|
146 |
BuildNode node = new BuildNode(parentNode, event.getProject());
|
|
147 |
buildEventStack.push(node);
|
|
148 |
}
|
|
149 |
}
|
|
150 |
|
|
151 |
public void messageLogged(BuildEvent event) {
|
|
152 |
// Ignoring message logging.
|
|
153 |
}
|
|
154 |
|
|
155 |
public synchronized void targetFinished(BuildEvent event) {
|
|
156 |
if (buildNode != null) {
|
|
157 |
DataNode node = buildNode.find(event.getTarget());
|
|
158 |
if (node != null) {
|
|
159 |
node.setEndTime(new Date());
|
|
160 |
MemoryUsage mem = mbean.getHeapMemoryUsage();
|
|
161 |
TargetNode tnode = (TargetNode)node;
|
|
162 |
tnode.setEndUsedHeap(mem.getUsed());
|
|
163 |
tnode.setEndCommittedHeap(mem.getCommitted());
|
|
164 |
node.setReference(null);
|
|
165 |
} else {
|
|
166 |
log.debug("targetFinished - could not find target.");
|
|
167 |
}
|
|
168 |
buildEventStack.pop();
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
public synchronized void targetStarted(BuildEvent event) {
|
|
173 |
DataNode parentNode = buildEventStack.peek();
|
|
174 |
if (parentNode != null) {
|
|
175 |
TargetNode node = new TargetNode(parentNode, event.getTarget());
|
|
176 |
MemoryUsage mem = mbean.getHeapMemoryUsage();
|
|
177 |
node.setStartUsedHeap(mem.getUsed());
|
|
178 |
node.setStartCommittedHeap(mem.getCommitted());
|
|
179 |
buildEventStack.push(node);
|
|
180 |
} else {
|
|
181 |
log.debug("targetStarted - could not find parent.");
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
public synchronized void taskFinished(BuildEvent event) {
|
|
186 |
// Ignoring task information
|
|
187 |
}
|
|
188 |
|
|
189 |
public synchronized void taskStarted(BuildEvent event) {
|
|
190 |
// Ignoring task information
|
|
191 |
}
|
|
192 |
|
588
|
193 |
public void addAssertTask(HlmAssertMessageTask assertTask) {
|
587
|
194 |
if (buildNode != null) {
|
|
195 |
DataNode parentNode = buildNode.find(assertTask.getOwningTarget());
|
|
196 |
if (parentNode != null) {
|
|
197 |
new AssertNode(parentNode, assertTask);
|
|
198 |
} else {
|
|
199 |
new AssertNode(buildEventStack.peek(), assertTask);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
}
|
|
203 |
} |