587
|
1 |
/*
|
628
|
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 |
*/
|
587
|
17 |
|
|
18 |
package com.nokia.helium.diamonds;
|
|
19 |
|
|
20 |
import org.apache.tools.ant.BuildEvent;
|
628
|
21 |
|
587
|
22 |
import org.apache.tools.ant.Project;
|
|
23 |
import java.util.Date;
|
|
24 |
import java.io.File;
|
628
|
25 |
import java.io.FileInputStream;
|
|
26 |
import java.io.FileOutputStream;
|
587
|
27 |
import java.io.IOException;
|
|
28 |
import java.text.SimpleDateFormat;
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import org.apache.log4j.Logger;
|
628
|
31 |
import com.nokia.helium.core.EmailSendException;
|
|
32 |
import com.nokia.helium.core.ant.Message;
|
|
33 |
import com.nokia.helium.core.MessageCreationException;
|
587
|
34 |
import com.nokia.helium.diamonds.XMLMerger.XMLMergerException;
|
628
|
35 |
import java.io.InputStream;
|
587
|
36 |
|
|
37 |
/**
|
628
|
38 |
* Base diamonds logger implementation. The common implementation like initialization done here and
|
|
39 |
* used by sub classes.
|
587
|
40 |
*/
|
|
41 |
public class DiamondsListenerImpl implements DiamondsListener {
|
|
42 |
|
628
|
43 |
private static ArrayList<InputStream> finalStreamList = new ArrayList<InputStream>();
|
587
|
44 |
|
|
45 |
private static DiamondsClient diamondsClient;
|
|
46 |
|
|
47 |
private static boolean isInitialized;
|
|
48 |
|
|
49 |
private static ArrayList<String> deferLogList = new ArrayList<String>();
|
|
50 |
|
628
|
51 |
private static Logger log = Logger.getLogger(DiamondsListenerImpl.class);
|
587
|
52 |
|
628
|
53 |
private static Project project;
|
|
54 |
|
|
55 |
private static Object mutexObject = new Object();;
|
587
|
56 |
|
628
|
57 |
private static SimpleDateFormat timeFormat;
|
587
|
58 |
|
|
59 |
/**
|
|
60 |
* Default constructor
|
|
61 |
*/
|
|
62 |
public DiamondsListenerImpl() {
|
628
|
63 |
}
|
|
64 |
|
|
65 |
public static void initialize(Project prj) {
|
|
66 |
project = prj;
|
|
67 |
log.debug("buildbegin:" + project);
|
|
68 |
Date date = new Date();
|
|
69 |
timeFormat = new SimpleDateFormat(DiamondsConfig.getTimeFormat());
|
|
70 |
log.debug("build.start.time:" + date);
|
|
71 |
project.setProperty("build.start.time", timeFormat.format(date));
|
587
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Function to process logging info during end of build
|
|
76 |
*
|
628
|
77 |
* @param event of target execution.
|
587
|
78 |
*/
|
628
|
79 |
public final void buildBegin(BuildEvent buildEvent) throws DiamondsException {
|
587
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Function to process logging info during end of build
|
|
84 |
*
|
628
|
85 |
* @param event of target execution.
|
587
|
86 |
*/
|
|
87 |
@SuppressWarnings("unchecked")
|
628
|
88 |
public void buildEnd(BuildEvent buildEvent) throws DiamondsException {
|
|
89 |
log.debug("build end: " + isInitialized());
|
587
|
90 |
if (isInitialized()) {
|
628
|
91 |
project.setProperty("build.end.time", timeFormat.format(new Date()));
|
|
92 |
sendMessage("final.message");
|
587
|
93 |
isInitialized = false;
|
628
|
94 |
InputStream first = finalStreamList.remove(0);
|
587
|
95 |
try {
|
628
|
96 |
//printStreamContent(first);
|
|
97 |
File fullResultsFile = File.createTempFile("diamonds-full-results", ".xml");
|
|
98 |
XMLMerger merger = new XMLMerger(first, fullResultsFile);
|
|
99 |
String smtpServer = DiamondsConfig.getSMTPServer();
|
|
100 |
String ldapServer = DiamondsConfig.getLDAPServer();
|
|
101 |
for (InputStream stream : finalStreamList) {
|
587
|
102 |
try {
|
628
|
103 |
merger.merge(stream);
|
|
104 |
}
|
|
105 |
catch (XMLMerger.XMLMergerException xe) {
|
587
|
106 |
log.debug("Error during the merge: ", xe);
|
|
107 |
}
|
|
108 |
}
|
628
|
109 |
diamondsClient.sendDataByMail(fullResultsFile.getAbsolutePath(), smtpServer, ldapServer);
|
|
110 |
} catch (EmailSendException ese) {
|
|
111 |
log.warn("Error occured while sending mail: " + ese.getMessage());
|
|
112 |
|
587
|
113 |
} catch (IOException e) {
|
|
114 |
log.error("Error sending diamonds final log: IOException", e);
|
628
|
115 |
}
|
|
116 |
catch (XMLMergerException e) {
|
587
|
117 |
log.error("Error sending diamonds final log: XMLMergerException ", e);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Function to process logging info during begining of target execution
|
|
124 |
*
|
628
|
125 |
* @param event of target execution.
|
587
|
126 |
*/
|
|
127 |
public void targetBegin(BuildEvent buildEvent) throws DiamondsException {
|
|
128 |
initDiamondsClient();
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Function to process logging info during end of target execution
|
|
133 |
*
|
628
|
134 |
* @param event of target execution.
|
587
|
135 |
*/
|
|
136 |
public void targetEnd(BuildEvent buildEvent) throws DiamondsException {
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
|
140 |
* returns true if diamonds is already initialized for the build.
|
|
141 |
*
|
|
142 |
* @param true diamonds initialized otherwise false.
|
|
143 |
*/
|
|
144 |
public static boolean isInitialized() {
|
|
145 |
return isInitialized;
|
|
146 |
}
|
|
147 |
|
628
|
148 |
public static void mergeToFullResults(InputStream stream) throws DiamondsException {
|
|
149 |
finalStreamList.add(stream);
|
587
|
150 |
}
|
|
151 |
|
628
|
152 |
|
|
153 |
/**
|
|
154 |
* Helper function to return the default project passed to messages.
|
|
155 |
*/
|
|
156 |
static Project getProject() {
|
|
157 |
return project;
|
587
|
158 |
}
|
628
|
159 |
|
|
160 |
|
|
161 |
|
587
|
162 |
protected DiamondsClient getDiamondsClient() {
|
|
163 |
return diamondsClient;
|
|
164 |
}
|
628
|
165 |
|
|
166 |
|
587
|
167 |
protected boolean getIsInitialized() {
|
|
168 |
return isInitialized;
|
|
169 |
}
|
628
|
170 |
|
|
171 |
protected static SimpleDateFormat getTimeFormat() {
|
587
|
172 |
return timeFormat;
|
|
173 |
}
|
628
|
174 |
|
587
|
175 |
protected ArrayList<String> getDeferLogList() {
|
|
176 |
return deferLogList;
|
|
177 |
}
|
628
|
178 |
|
|
179 |
protected static File streamToTempFile(InputStream stream) throws IOException {
|
|
180 |
File temp = File.createTempFile("diamonds", "xml");
|
|
181 |
FileOutputStream out = new FileOutputStream(temp);
|
|
182 |
int read = 0;
|
|
183 |
byte[] bytes = new byte[1024];
|
|
184 |
|
|
185 |
while ((read = stream.read(bytes)) != -1) {
|
|
186 |
out.write(bytes, 0, read);
|
|
187 |
}
|
|
188 |
out.flush();
|
|
189 |
out.close();
|
|
190 |
stream.close();
|
|
191 |
return temp;
|
|
192 |
}
|
587
|
193 |
|
628
|
194 |
private static void sendMessage(Message message, String buildID) throws DiamondsException {
|
|
195 |
try {
|
|
196 |
File tempFile = streamToTempFile(message.getInputStream());
|
|
197 |
tempFile.deleteOnExit();
|
|
198 |
if (buildID == null) {
|
|
199 |
buildID = diamondsClient.getBuildId(new FileInputStream(tempFile));
|
|
200 |
if (buildID != null) {
|
|
201 |
project.setProperty(DiamondsConfig.getBuildIdProperty(), buildID);
|
|
202 |
log.info("got Build ID from diamonds:" + buildID);
|
|
203 |
}
|
|
204 |
} else {
|
|
205 |
diamondsClient.sendData(new FileInputStream(tempFile), buildID);
|
|
206 |
}
|
|
207 |
mergeToFullResults(new FileInputStream(tempFile));
|
|
208 |
} catch (IOException iex) {
|
|
209 |
log.debug("IOException while retriving message:", iex);
|
|
210 |
throw new DiamondsException("IOException while retriving message");
|
|
211 |
}
|
|
212 |
catch (MessageCreationException mex) {
|
|
213 |
log.debug("IOException while retriving message:", mex);
|
|
214 |
throw new DiamondsException("error during message retrival");
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
/**
|
|
219 |
* Send message to diamonds.
|
|
220 |
* @param messageId - id to look from in the ant config and to send it to diamonds.
|
|
221 |
* id is pointing to an fmpp message.
|
|
222 |
*/
|
|
223 |
public static void sendMessage(String messageId) throws DiamondsException {
|
|
224 |
log.debug("send-message:" + messageId);
|
|
225 |
synchronized (mutexObject) {
|
|
226 |
String buildID = project.getProperty(DiamondsConfig.getBuildIdProperty());
|
|
227 |
Object obj = project.getReference(messageId);
|
|
228 |
if (obj != null) {
|
|
229 |
if (obj instanceof Message) {
|
|
230 |
sendMessage((Message)obj, buildID);
|
|
231 |
}
|
|
232 |
} else {
|
|
233 |
log.debug("Message not sent for message id: " + messageId);
|
|
234 |
}
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
587
|
238 |
/**
|
|
239 |
* Initializes the diamonds client and sends the initial data
|
|
240 |
*/
|
|
241 |
@SuppressWarnings("unchecked")
|
|
242 |
protected void initDiamondsClient() throws DiamondsException {
|
|
243 |
if (!isInitialized) {
|
628
|
244 |
diamondsClient = new DiamondsClient(DiamondsConfig.getHost(),
|
|
245 |
DiamondsConfig.getPort(),
|
|
246 |
DiamondsConfig.getPath(),
|
|
247 |
DiamondsConfig.getMailInfo());
|
|
248 |
String buildID = project.getProperty(DiamondsConfig.getBuildIdProperty());
|
|
249 |
if (buildID == null ) {
|
|
250 |
sendMessage("initial.message");
|
587
|
251 |
}
|
|
252 |
isInitialized = true;
|
|
253 |
}
|
|
254 |
}
|
|
255 |
} |