1 /* |
1 /* |
2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). |
2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). |
3 * All rights reserved. |
3 * All rights reserved. |
4 * This component and the accompanying materials are made available |
4 * This component and the accompanying materials are made available |
5 * under the terms of the License "Eclipse Public License v1.0" |
5 * under the terms of the License "Eclipse Public License v1.0" |
6 * which accompanies this distribution, and is available |
6 * which accompanies this distribution, and is available |
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
8 * |
8 * |
9 * Initial Contributors: |
9 * Initial Contributors: |
10 * Nokia Corporation - initial contribution. |
10 * Nokia Corporation - initial contribution. |
11 * |
11 * |
12 * Contributors: |
12 * Contributors: |
13 * |
13 * |
14 * Description: |
14 * Description: |
15 * |
15 * |
16 */ |
16 */ |
17 |
|
18 |
17 |
19 package com.nokia.helium.diamonds; |
18 package com.nokia.helium.diamonds; |
20 |
19 |
21 import org.apache.tools.ant.BuildEvent; |
20 import org.apache.tools.ant.BuildEvent; |
|
21 |
22 import org.apache.tools.ant.Project; |
22 import org.apache.tools.ant.Project; |
23 import org.apache.tools.ant.util.FileUtils; |
|
24 import java.util.Date; |
23 import java.util.Date; |
25 import java.util.List; |
|
26 import java.util.HashSet; |
|
27 import java.io.File; |
24 import java.io.File; |
|
25 import java.io.FileInputStream; |
|
26 import java.io.FileOutputStream; |
28 import java.io.IOException; |
27 import java.io.IOException; |
29 import java.text.SimpleDateFormat; |
28 import java.text.SimpleDateFormat; |
30 import java.util.ArrayList; |
29 import java.util.ArrayList; |
31 import java.util.ListIterator; |
|
32 import org.apache.log4j.Logger; |
30 import org.apache.log4j.Logger; |
33 import java.util.Properties; |
31 import com.nokia.helium.core.EmailSendException; |
34 import com.nokia.helium.core.PropertiesSource; |
32 import com.nokia.helium.core.ant.Message; |
35 import com.nokia.helium.core.TemplateInputSource; |
33 import com.nokia.helium.core.MessageCreationException; |
36 import com.nokia.helium.core.TemplateProcessor; |
|
37 import com.nokia.helium.diamonds.XMLMerger.XMLMergerException; |
34 import com.nokia.helium.diamonds.XMLMerger.XMLMergerException; |
|
35 import java.io.InputStream; |
38 |
36 |
39 /** |
37 /** |
40 * Base diamonds logger implementation. The common implementation like |
38 * Base diamonds logger implementation. The common implementation like initialization done here and |
41 * initialization done here and used by sub classes. |
39 * used by sub classes. |
42 */ |
40 */ |
43 public class DiamondsListenerImpl implements DiamondsListener { |
41 public class DiamondsListenerImpl implements DiamondsListener { |
44 |
42 |
45 private static ArrayList<File> finalLogList = new ArrayList<File>(); |
43 private static ArrayList<InputStream> finalStreamList = new ArrayList<InputStream>(); |
46 |
44 |
47 private static DiamondsClient diamondsClient; |
45 private static DiamondsClient diamondsClient; |
48 |
46 |
49 private static boolean isInitialized; |
47 private static boolean isInitialized; |
50 |
48 |
51 private static ArrayList<String> deferLogList = new ArrayList<String>(); |
49 private static ArrayList<String> deferLogList = new ArrayList<String>(); |
52 |
50 |
53 private TemplateProcessor templateProcessor; |
51 private static Logger log = Logger.getLogger(DiamondsListenerImpl.class); |
54 |
52 |
55 private Project project; |
53 private static Project project; |
56 |
54 |
57 private SimpleDateFormat timeFormat; |
55 private static Object mutexObject = new Object();; |
58 |
56 |
59 private Date buildStartTime; |
57 private static SimpleDateFormat timeFormat; |
60 |
|
61 private Logger log = Logger.getLogger(DiamondsListenerImpl.class); |
|
62 |
58 |
63 /** |
59 /** |
64 * Default constructor |
60 * Default constructor |
65 */ |
61 */ |
66 public DiamondsListenerImpl() { |
62 public DiamondsListenerImpl() { |
67 templateProcessor = new TemplateProcessor(); |
63 } |
68 timeFormat = new SimpleDateFormat(DiamondsConfig |
64 |
69 .getDiamondsProperties().getProperty("tstampformat")); |
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)); |
70 } |
72 } |
71 |
73 |
72 /** |
74 /** |
73 * Function to process logging info during end of build |
75 * Function to process logging info during end of build |
74 * |
76 * |
75 * @param event |
77 * @param event of target execution. |
76 * of target execution. |
78 */ |
77 */ |
79 public final void buildBegin(BuildEvent buildEvent) throws DiamondsException { |
78 public final void buildBegin(BuildEvent buildEvent) |
|
79 throws DiamondsException { |
|
80 project = buildEvent.getProject(); |
|
81 buildStartTime = new Date(); |
|
82 } |
80 } |
83 |
81 |
84 /** |
82 /** |
85 * Function to process logging info during end of build |
83 * Function to process logging info during end of build |
86 * |
84 * |
87 * @param event |
85 * @param event of target execution. |
88 * of target execution. |
|
89 */ |
86 */ |
90 @SuppressWarnings("unchecked") |
87 @SuppressWarnings("unchecked") |
91 public final void buildEnd(BuildEvent buildEvent) throws DiamondsException { |
88 public void buildEnd(BuildEvent buildEvent) throws DiamondsException { |
|
89 log.debug("build end: " + isInitialized()); |
92 if (isInitialized()) { |
90 if (isInitialized()) { |
93 log.debug("diamonds:DiamondsListenerImpl:sending final data to diamonds."); |
91 project.setProperty("build.end.time", timeFormat.format(new Date())); |
94 String output = DiamondsConfig.getOutputDir() + File.separator |
92 sendMessage("final.message"); |
95 + "diamonds-finish.xml"; |
|
96 File outputFile = new File(output); |
|
97 String finishTemplateFile = "diamonds_finish.xml.ftl"; |
|
98 Properties tempProperties = new Properties(); |
|
99 tempProperties.put("build.end.time", timeFormat |
|
100 .format(new Date())); |
|
101 List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>(); |
|
102 sourceList.add(new PropertiesSource("ant", project |
|
103 .getProperties())); |
|
104 sourceList |
|
105 .add(new PropertiesSource("diamonds", tempProperties)); |
|
106 templateProcessor.convertTemplate(DiamondsConfig |
|
107 .getTemplateDir(), finishTemplateFile, output, |
|
108 sourceList); |
|
109 |
|
110 log.info("Sending final data to diamonds."); |
|
111 // String mergedFile = mergeFiles(output); |
|
112 diamondsClient.sendData(output, DiamondsConfig |
|
113 .getDiamondsProperties().getDiamondsBuildID()); |
|
114 mergeToFullResults(outputFile); |
|
115 isInitialized = false; |
93 isInitialized = false; |
116 DiamondsProperties props = DiamondsConfig.getDiamondsProperties(); |
94 InputStream first = finalStreamList.remove(0); |
117 String smtpServer = project.getProperty(props |
|
118 .getProperty("smtpserver")); |
|
119 String ldapServer = project.getProperty(props |
|
120 .getProperty("ldapserver")); |
|
121 |
|
122 try { |
95 try { |
123 File first = finalLogList.remove(0); |
96 //printStreamContent(first); |
124 String outputDir = DiamondsConfig.getOutputDir(); |
97 File fullResultsFile = File.createTempFile("diamonds-full-results", ".xml"); |
125 File fullResultsFile = new File(outputDir + File.separator |
98 XMLMerger merger = new XMLMerger(first, fullResultsFile); |
126 + "diamonds-full-results.xml"); |
99 String smtpServer = DiamondsConfig.getSMTPServer(); |
127 FileUtils.getFileUtils().copyFile(first, fullResultsFile); |
100 String ldapServer = DiamondsConfig.getLDAPServer(); |
128 XMLMerger merger = new XMLMerger(fullResultsFile); |
101 for (InputStream stream : finalStreamList) { |
129 HashSet<File> fileHash = new HashSet<File>(finalLogList); |
|
130 for (File file : fileHash) { |
|
131 try { |
102 try { |
132 merger.merge(file); |
103 merger.merge(stream); |
133 } catch (XMLMerger.XMLMergerException xe) { |
104 } |
|
105 catch (XMLMerger.XMLMergerException xe) { |
134 log.debug("Error during the merge: ", xe); |
106 log.debug("Error during the merge: ", xe); |
135 } |
107 } |
136 } |
108 } |
137 // diamondsClient.sendData(fullResultsFile.getAbsolutePath(), DiamondsConfig.getDiamondsProperties().getDiamondsBuildID()); |
109 diamondsClient.sendDataByMail(fullResultsFile.getAbsolutePath(), smtpServer, ldapServer); |
138 diamondsClient.sendDataByMail( |
110 } catch (EmailSendException ese) { |
139 fullResultsFile.getAbsolutePath(), smtpServer, |
111 log.warn("Error occured while sending mail: " + ese.getMessage()); |
140 ldapServer); |
112 |
141 } catch (IOException e) { |
113 } catch (IOException e) { |
142 log.error("Error sending diamonds final log: IOException", e); |
114 log.error("Error sending diamonds final log: IOException", e); |
143 } catch (XMLMergerException e) { |
115 } |
|
116 catch (XMLMergerException e) { |
144 log.error("Error sending diamonds final log: XMLMergerException ", e); |
117 log.error("Error sending diamonds final log: XMLMergerException ", e); |
145 } |
118 } |
146 } |
119 } |
147 } |
120 } |
148 |
121 |
149 /** |
122 /** |
150 * Function to process logging info during begining of target execution |
123 * Function to process logging info during begining of target execution |
151 * |
124 * |
152 * @param event |
125 * @param event of target execution. |
153 * of target execution. |
|
154 */ |
126 */ |
155 public void targetBegin(BuildEvent buildEvent) throws DiamondsException { |
127 public void targetBegin(BuildEvent buildEvent) throws DiamondsException { |
156 initDiamondsClient(); |
128 initDiamondsClient(); |
157 } |
129 } |
158 |
130 |
159 /** |
131 /** |
160 * Function to process logging info during end of target execution |
132 * Function to process logging info during end of target execution |
161 * |
133 * |
162 * @param event |
134 * @param event of target execution. |
163 * of target execution. |
|
164 */ |
135 */ |
165 public void targetEnd(BuildEvent buildEvent) throws DiamondsException { |
136 public void targetEnd(BuildEvent buildEvent) throws DiamondsException { |
166 } |
137 } |
167 |
138 |
168 /** |
139 /** |
172 */ |
143 */ |
173 public static boolean isInitialized() { |
144 public static boolean isInitialized() { |
174 return isInitialized; |
145 return isInitialized; |
175 } |
146 } |
176 |
147 |
177 public static void mergeToFullResults(File xmlFile) throws DiamondsException { |
148 public static void mergeToFullResults(InputStream stream) throws DiamondsException { |
178 finalLogList.add(xmlFile); |
149 finalStreamList.add(stream); |
179 } |
150 } |
180 |
151 |
181 protected String getSourceFile(String inputName) { |
152 |
182 return DiamondsConfig.getOutputDir() + File.separator + inputName |
153 /** |
183 + ".xml"; |
154 * Helper function to return the default project passed to messages. |
184 } |
155 */ |
185 |
156 static Project getProject() { |
|
157 return project; |
|
158 } |
|
159 |
|
160 |
|
161 |
186 protected DiamondsClient getDiamondsClient() { |
162 protected DiamondsClient getDiamondsClient() { |
187 return diamondsClient; |
163 return diamondsClient; |
188 } |
164 } |
189 |
165 |
190 protected TemplateProcessor getTemplateProcessor() { |
166 |
191 return templateProcessor; |
|
192 } |
|
193 |
|
194 protected boolean getIsInitialized() { |
167 protected boolean getIsInitialized() { |
195 return isInitialized; |
168 return isInitialized; |
196 } |
169 } |
197 |
170 |
198 protected SimpleDateFormat getTimeFormat() { |
171 protected static SimpleDateFormat getTimeFormat() { |
199 return timeFormat; |
172 return timeFormat; |
200 } |
173 } |
201 |
174 |
202 protected ArrayList<String> getDeferLogList() { |
175 protected ArrayList<String> getDeferLogList() { |
203 return deferLogList; |
176 return deferLogList; |
204 } |
177 } |
205 |
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 } |
|
193 |
|
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 |
206 /** |
238 /** |
207 * Initializes the diamonds client and sends the initial data |
239 * Initializes the diamonds client and sends the initial data |
208 */ |
240 */ |
209 @SuppressWarnings("unchecked") |
241 @SuppressWarnings("unchecked") |
210 protected void initDiamondsClient() throws DiamondsException { |
242 protected void initDiamondsClient() throws DiamondsException { |
211 String outputDir = DiamondsConfig.getOutputDir(); |
|
212 if (!isInitialized) { |
243 if (!isInitialized) { |
213 String startTemplateFile = "diamonds_start.xml.ftl"; |
244 diamondsClient = new DiamondsClient(DiamondsConfig.getHost(), |
214 String output = outputDir + File.separator |
245 DiamondsConfig.getPort(), |
215 + "diamonds-start.log.xml"; |
246 DiamondsConfig.getPath(), |
216 new File(outputDir).mkdirs(); |
247 DiamondsConfig.getMailInfo()); |
217 File outputFile = new File(output); |
248 String buildID = project.getProperty(DiamondsConfig.getBuildIdProperty()); |
218 Properties tempProperties = new Properties(); |
249 if (buildID == null ) { |
219 tempProperties.put("build.start.time", timeFormat |
250 sendMessage("initial.message"); |
220 .format(buildStartTime)); |
|
221 List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>(); |
|
222 sourceList.add(new PropertiesSource("ant", project |
|
223 .getProperties())); |
|
224 sourceList |
|
225 .add(new PropertiesSource("diamonds", tempProperties)); |
|
226 DiamondsProperties diamondsProperties = DiamondsConfig |
|
227 .getDiamondsProperties(); |
|
228 templateProcessor.convertTemplate(DiamondsConfig |
|
229 .getTemplateDir(), startTemplateFile, output, |
|
230 sourceList); |
|
231 mergeToFullResults(outputFile); |
|
232 |
|
233 // String mergedFile = mergeFiles(output); |
|
234 log.info("Initializing diamonds client"); |
|
235 diamondsClient = new DiamondsClient(project |
|
236 .getProperty(diamondsProperties.getProperty("host")), |
|
237 project.getProperty(diamondsProperties |
|
238 .getProperty("port")), project |
|
239 .getProperty(diamondsProperties |
|
240 .getProperty("path")), project |
|
241 .getProperty(diamondsProperties |
|
242 .getProperty("mail"))); |
|
243 String buildID = diamondsClient |
|
244 .getBuildId(outputFile.getAbsolutePath()); |
|
245 if (buildID != null) { |
|
246 diamondsProperties.setDiamondsBuildID(buildID); |
|
247 project.setProperty(diamondsProperties.getProperty("buildid-property"), |
|
248 diamondsProperties.getDiamondsBuildID()); |
|
249 log.info("Got build id from diamonds: " + buildID); |
|
250 } else { |
|
251 diamondsProperties.setDiamondsBuildID(buildID); |
|
252 project.setProperty(diamondsProperties.getProperty("buildid-property"), |
|
253 "default"); |
|
254 log.info("diamonds build id set to default and in record only mode"); |
|
255 } |
|
256 if (deferLogList.size() > 0) { |
|
257 log |
|
258 .debug("diamonds:DiamondsListenerImpl: sending DefferList"); |
|
259 ListIterator<String> defferList = deferLogList |
|
260 .listIterator(); |
|
261 while (defferList.hasNext()) { |
|
262 String mergedDeferFile = defferList.next(); |
|
263 mergeToFullResults(new File(mergedDeferFile)); |
|
264 diamondsClient.sendData(mergedDeferFile, DiamondsConfig |
|
265 .getDiamondsProperties().getDiamondsBuildID()); |
|
266 } |
|
267 deferLogList.clear(); |
|
268 } |
251 } |
269 isInitialized = true; |
252 isInitialized = true; |
270 } |
253 } |
271 } |
254 } |
272 } |
255 } |