|
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.metadata.ant.types.sbs; |
|
19 |
|
20 import java.io.File; |
|
21 import java.io.IOException; |
|
22 import java.util.Hashtable; |
|
23 import java.util.Map; |
|
24 import java.util.Map.Entry; |
|
25 import java.util.regex.Matcher; |
|
26 import java.util.regex.Pattern; |
|
27 |
|
28 import javax.persistence.EntityManager; |
|
29 import javax.persistence.EntityManagerFactory; |
|
30 import javax.xml.parsers.ParserConfigurationException; |
|
31 import javax.xml.parsers.SAXParser; |
|
32 import javax.xml.parsers.SAXParserFactory; |
|
33 |
|
34 import org.xml.sax.SAXException; |
|
35 |
|
36 import com.nokia.helium.metadata.AutoCommitEntityManager; |
|
37 import com.nokia.helium.metadata.MetadataException; |
|
38 import com.nokia.helium.metadata.ant.types.AbstractComponentBaseMetadataInput; |
|
39 import com.nokia.helium.metadata.ant.types.SeverityEnum; |
|
40 import com.nokia.helium.metadata.model.metadata.Component; |
|
41 import com.nokia.helium.metadata.model.metadata.ComponentTime; |
|
42 import com.nokia.helium.metadata.model.metadata.ExecutionTime; |
|
43 import com.nokia.helium.metadata.model.metadata.LogFile; |
|
44 import com.nokia.helium.metadata.model.metadata.MetadataEntry; |
|
45 import com.nokia.helium.metadata.model.metadata.Severity; |
|
46 import com.nokia.helium.metadata.model.metadata.SeverityDAO; |
|
47 import com.nokia.helium.metadata.model.metadata.SysdefUnitDAO; |
|
48 import com.nokia.helium.metadata.model.metadata.WhatLogEntry; |
|
49 |
|
50 |
|
51 /** |
|
52 * This metadata input will parse SBSv2 log and collect |
|
53 * data into the database. |
|
54 * <pre> |
|
55 * |
|
56 * <hlm:sbsmetadatainput > |
|
57 * <fileset dir="${project.dir}/../data/"> |
|
58 * <include name="*_compile.log"/> |
|
59 * </fileset> |
|
60 * <hlm:metadatafilterset id="sbs.metadata.filter"> |
|
61 * <metadatafilterset filterfile="common.csv" /> |
|
62 * </hlm:metadatafilterset> |
|
63 * </hlm:sbsmetadatainput> |
|
64 * </pre> |
|
65 * |
|
66 * @ant.task name="sbsmetadatainput" category="Metadata" |
|
67 */ |
|
68 public class SBSLogMetaDataInput extends AbstractComponentBaseMetadataInput implements SBSLogEvents { |
|
69 private static final String DEFAULT_COMPONENT_NAME = "general"; |
|
70 private EntityManager entityManager; |
|
71 private AutoCommitEntityManager autoCommitEM; |
|
72 private LogFile logFile; |
|
73 private File epocroot; |
|
74 private SysdefUnitDAO unitDAO; |
|
75 private boolean checkMissing = true; |
|
76 |
|
77 private Map<String, Severity> priorities; |
|
78 private Pattern buildTimeMatcher = Pattern.compile("^Run time\\s+(\\d+)\\s+seconds$"); |
|
79 private boolean failOnInvalidXml = true; |
|
80 private Map<String, ComponentTime> componentTimes = new Hashtable<String, ComponentTime>(); |
|
81 |
|
82 /** |
|
83 * Defines if XML format error should be fatal, or treated as build error. |
|
84 * @param failOnXmlError |
|
85 */ |
|
86 public void setFailOnInvalidXml(boolean failOnInvalidXml) { |
|
87 this.failOnInvalidXml = failOnInvalidXml; |
|
88 } |
|
89 |
|
90 |
|
91 /** |
|
92 * Defines epocroot. |
|
93 * @param epocroot |
|
94 * @ant.not-required Default to EPOCROOT. |
|
95 */ |
|
96 public void setEpocroot(File epocroot) { |
|
97 this.epocroot = epocroot; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Get epocroot. |
|
102 * @return a File object representing epocroot, or throw a BuildException |
|
103 * if epocroot attribute and EPOCROOT environment variable |
|
104 * are not defined. |
|
105 */ |
|
106 public File getEpocroot() { |
|
107 return epocroot; |
|
108 } |
|
109 |
|
110 /** |
|
111 * {@inheritDoc} |
|
112 */ |
|
113 @Override |
|
114 public void extract(EntityManagerFactory factory, File file) |
|
115 throws MetadataException { |
|
116 SAXParserFactory saxFactory = SAXParserFactory.newInstance(); |
|
117 entityManager = factory.createEntityManager(); |
|
118 autoCommitEM = new AutoCommitEntityManager(factory); |
|
119 unitDAO = new SysdefUnitDAO(); |
|
120 unitDAO.setEntityManager(this.getEntityManager()); |
|
121 try { |
|
122 logFile = getLogFile(entityManager, file); |
|
123 |
|
124 SeverityDAO pdao = new SeverityDAO(); |
|
125 pdao.setEntityManager(entityManager); |
|
126 priorities = pdao.getSeverities(); |
|
127 |
|
128 // always create the default component and associated time. |
|
129 this.getDefaultComponent(logFile); |
|
130 this.addElapsedTime(this.getDefaultComponentName(), 0.0); |
|
131 |
|
132 SAXParser parser = saxFactory.newSAXParser(); |
|
133 parser.parse(file, new SBSLogHandler(this, file)); |
|
134 |
|
135 // Pushing component times into the database. |
|
136 entityManager.getTransaction().begin(); |
|
137 for (Entry<String, ComponentTime> entry : componentTimes.entrySet()) { |
|
138 ComponentTime ct = entry.getValue(); |
|
139 ct.setComponent(entityManager.merge(getComponent(entry.getKey(), logFile))); |
|
140 entityManager.persist(ct); |
|
141 } |
|
142 entityManager.getTransaction().commit(); |
|
143 } catch (SAXException e) { |
|
144 if (failOnInvalidXml) { |
|
145 throw new MetadataException(e.getMessage(), e); |
|
146 } |
|
147 } catch (IOException e) { |
|
148 throw new MetadataException(e.getMessage(), e); |
|
149 } catch (ParserConfigurationException e) { |
|
150 throw new MetadataException(e.getMessage(), e); |
|
151 } finally { |
|
152 logFile = null; |
|
153 autoCommitEM.close(); |
|
154 autoCommitEM = null; |
|
155 entityManager.close(); |
|
156 entityManager = null; |
|
157 unitDAO = null; |
|
158 priorities = null; |
|
159 clear(); |
|
160 componentTimes.clear(); |
|
161 } |
|
162 } |
|
163 |
|
164 /** |
|
165 * {@inheritDoc} |
|
166 */ |
|
167 @Override |
|
168 protected EntityManager getEntityManager() { |
|
169 return entityManager; |
|
170 } |
|
171 |
|
172 /** |
|
173 * {@inheritDoc} |
|
174 */ |
|
175 @Override |
|
176 public void add(SeverityEnum.Severity severity, |
|
177 String text, int lineNumber) { |
|
178 add(severity, this.getDefaultComponentName(), text, lineNumber); |
|
179 |
|
180 // Searching for build time. |
|
181 Matcher matcher = buildTimeMatcher.matcher(text); |
|
182 if (matcher.matches()) { |
|
183 ExecutionTime entry = new ExecutionTime(); |
|
184 entry.setLogFile(autoCommitEM.merge(logFile)); |
|
185 entry.setTime(new Integer(matcher.group(1)).intValue()); |
|
186 autoCommitEM.persist(entry); |
|
187 } |
|
188 } |
|
189 |
|
190 /** |
|
191 * {@inheritDoc} |
|
192 */ |
|
193 @Override |
|
194 public void add(SeverityEnum.Severity severity, |
|
195 String component, String text, int lineNumber) { |
|
196 if (severity != SeverityEnum.Severity.INFO) { |
|
197 MetadataEntry entry = new MetadataEntry(); |
|
198 entry.setComponent(autoCommitEM.merge(getComponent(component, logFile))); |
|
199 entry.setLineNumber(lineNumber); |
|
200 entry.setText(text); |
|
201 entry.setLogFile(logFile); |
|
202 entry.setSeverity(priorities.get(severity.toString())); |
|
203 autoCommitEM.persist(entry); |
|
204 } |
|
205 } |
|
206 |
|
207 /** |
|
208 * {@inheritDoc} |
|
209 */ |
|
210 @Override |
|
211 public void addWhatEntry(String component, String location, int lineNumber) { |
|
212 boolean fileExists = (new File(location)).exists(); |
|
213 WhatLogEntry entry = new WhatLogEntry(); |
|
214 entry.setComponent(autoCommitEM.merge(getComponent(component, logFile))); |
|
215 entry.setMember(location); |
|
216 entry.setMissing(!fileExists); |
|
217 autoCommitEM.persist(entry); |
|
218 if (checkMissing && !fileExists) { |
|
219 MetadataEntry mentry = new MetadataEntry(); |
|
220 mentry.setComponent(autoCommitEM.merge(getComponent(component, logFile))); |
|
221 mentry.setLineNumber(lineNumber); |
|
222 mentry.setText("Missing: " + location); |
|
223 mentry.setLogFile(logFile); |
|
224 mentry.setSeverity(priorities.get(SeverityEnum.Severity.ERROR.toString())); |
|
225 autoCommitEM.persist(mentry); |
|
226 } |
|
227 } |
|
228 |
|
229 /** |
|
230 * {@inheritDoc} |
|
231 */ |
|
232 @Override |
|
233 public SeverityEnum.Severity check(String text, int lineNumber) { |
|
234 check(getDefaultComponentName(), text, lineNumber); |
|
235 return null; |
|
236 } |
|
237 |
|
238 /** |
|
239 * {@inheritDoc} |
|
240 */ |
|
241 @Override |
|
242 public SeverityEnum.Severity check( |
|
243 String component, String text, int lineNumber) { |
|
244 SeverityEnum.Severity severity = this.getSeverity(text); |
|
245 if (severity != SeverityEnum.Severity.NONE) { |
|
246 MetadataEntry entry = new MetadataEntry(); |
|
247 entry.setComponent(autoCommitEM.merge(getComponent(component, logFile))); |
|
248 entry.setLineNumber(lineNumber); |
|
249 entry.setText(text); |
|
250 entry.setLogFile(logFile); |
|
251 entry.setSeverity(priorities.get(severity.toString())); |
|
252 autoCommitEM.persist(entry); |
|
253 } |
|
254 return severity; |
|
255 } |
|
256 |
|
257 /** |
|
258 * {@inheritDoc} |
|
259 */ |
|
260 @Override |
|
261 public void declareComponent(String component) { |
|
262 getComponent(component, logFile); |
|
263 } |
|
264 |
|
265 /** |
|
266 * Define if we need to treat add error entries |
|
267 * for missing files. |
|
268 * @param checkMissing |
|
269 */ |
|
270 public void setCheckMissing(boolean checkMissing) { |
|
271 this.checkMissing = checkMissing; |
|
272 } |
|
273 |
|
274 /** |
|
275 * {@inheritDoc} |
|
276 */ |
|
277 @Override |
|
278 public String getDefaultComponentName() { |
|
279 return DEFAULT_COMPONENT_NAME; |
|
280 } |
|
281 |
|
282 /** |
|
283 * {@inheritDoc} |
|
284 */ |
|
285 @Override |
|
286 protected Component createComponent(String name, LogFile logFile) { |
|
287 Component component = new Component(); |
|
288 component.setComponent(name); |
|
289 component.setLogFile(logFile); |
|
290 component.setSysdefUnit(unitDAO.getUnitByLocation(name)); |
|
291 getEntityManager().getTransaction().begin(); |
|
292 getEntityManager().persist(component); |
|
293 getEntityManager().getTransaction().commit(); |
|
294 return component; |
|
295 } |
|
296 |
|
297 |
|
298 @Override |
|
299 public void addElapsedTime(String name, double duration) { |
|
300 if (!componentTimes.containsKey(name)) { |
|
301 ComponentTime ct = new ComponentTime(); |
|
302 componentTimes.put(name, ct); |
|
303 } |
|
304 ComponentTime ct = componentTimes.get(name); |
|
305 ct.setDuration(ct.getDuration() + duration); |
|
306 } |
|
307 |
|
308 } |