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.metadata.ant.conditions;
|
|
19 |
|
|
20 |
import java.io.File;
|
|
21 |
import com.nokia.helium.jpa.ORMReader;
|
|
22 |
import com.nokia.helium.jpa.entity.metadata.Metadata;
|
|
23 |
import org.apache.tools.ant.BuildException;
|
|
24 |
import org.apache.tools.ant.Project;
|
|
25 |
import org.apache.tools.ant.taskdefs.condition.Condition;
|
|
26 |
import org.apache.tools.ant.types.DataType;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* This class implements a Ant Condition which report true if it finds any
|
|
30 |
* matching severity inside a database for an XML log.
|
|
31 |
*
|
|
32 |
* Example:
|
|
33 |
* <pre>
|
|
34 |
* <target name="fail-on-build-error">
|
|
35 |
* <fail message="The build contains errors">
|
|
36 |
* <hlm:metadataHasSeverity log="my.log" db="my.db" severity="error"/>
|
|
37 |
* </fail>
|
|
38 |
* </target>
|
|
39 |
* </pre>
|
|
40 |
*
|
|
41 |
* The condition will eval as true if the my.db contains error stored for my.log file.
|
|
42 |
*
|
|
43 |
* @ant.type name="metadataHasSeverity" category="Metadata"
|
|
44 |
*/
|
|
45 |
public class MetaDataLogCondition extends DataType implements Condition {
|
|
46 |
|
|
47 |
// The severity to count
|
|
48 |
private String severity;
|
|
49 |
private String logFile;
|
|
50 |
private File fileName;
|
|
51 |
private boolean countMissing = true;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* Sets which severity will be counted.
|
|
55 |
*
|
|
56 |
* @param severity
|
|
57 |
* @ant.required
|
|
58 |
*/
|
|
59 |
public void setSeverity(String severity) {
|
|
60 |
this.severity = severity;
|
|
61 |
}
|
|
62 |
|
|
63 |
public void setDb(File file) {
|
|
64 |
fileName = file;
|
|
65 |
}
|
|
66 |
|
|
67 |
public void setLog(String log) {
|
|
68 |
logFile = log;
|
|
69 |
}
|
|
70 |
|
|
71 |
public void setCountMissing(boolean countMissing) {
|
|
72 |
this.countMissing = countMissing;
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Get the number of a particular severity.
|
|
77 |
*
|
|
78 |
* @return the number of a particular severity.
|
|
79 |
*/
|
|
80 |
public int getSeverity() {
|
|
81 |
if (fileName == null || !fileName.exists() || logFile == null) {
|
|
82 |
//this.log("Error: Log file does not exist " + fileName);
|
|
83 |
return -1;
|
|
84 |
}
|
|
85 |
if (severity == null)
|
|
86 |
throw new BuildException("'severity' attribute is not defined");
|
|
87 |
|
|
88 |
log("Looking for severity '" + severity + "' under '" + fileName.getAbsolutePath() + "'", Project.MSG_DEBUG);
|
|
89 |
|
|
90 |
Metadata.PriorityEnum prty = null;
|
|
91 |
if (severity.equalsIgnoreCase("ERROR")) {
|
|
92 |
prty = Metadata.PriorityEnum.ERROR;
|
|
93 |
} else if (severity.equalsIgnoreCase("WARNING")) {
|
|
94 |
prty = Metadata.PriorityEnum.WARNING;
|
|
95 |
} else if (severity.equalsIgnoreCase("FATAL")) {
|
|
96 |
prty = Metadata.PriorityEnum.FATAL;
|
|
97 |
} else if (severity.equalsIgnoreCase("INFO")) {
|
|
98 |
prty = Metadata.PriorityEnum.INFO;
|
|
99 |
} else if (severity.equalsIgnoreCase("REMARK")) {
|
|
100 |
prty = Metadata.PriorityEnum.REMARK;
|
|
101 |
}
|
|
102 |
else
|
|
103 |
throw new BuildException("'severity' attribute is not valid");
|
|
104 |
|
|
105 |
// log file under the DB is always represented with / and not \.
|
|
106 |
String logname = logFile.replace('\\', '/');
|
|
107 |
String query = "select Count(m.id) from MetadataEntry m JOIN m.logFile as l JOIN m.priority as p where l.path like '%" + logname + "' and UPPER(p.priority)='" + severity.toUpperCase() + "'";
|
618
|
108 |
ORMReader reader = new ORMReader(fileName.getAbsolutePath());
|
|
109 |
Number number = (Number)reader.executeSingleResult(query, null);
|
|
110 |
int retValue = number.intValue();
|
587
|
111 |
// Looking for missing file as error
|
|
112 |
if (countMissing && prty == Metadata.PriorityEnum.ERROR) {
|
|
113 |
String queryMissing = "select Count(m.id) from WhatLogEntry m JOIN m.component as c JOIN c.logFile as l where l.path like '%" + logname + "' and m.missing=1";
|
618
|
114 |
Number numberMissing = (Number) reader.executeSingleResult(queryMissing, null);
|
|
115 |
retValue = number.intValue() + numberMissing.intValue();
|
587
|
116 |
}
|
618
|
117 |
reader.close();
|
|
118 |
return retValue;
|
587
|
119 |
}
|
|
120 |
|
|
121 |
/**
|
|
122 |
* This method open the defined file and count the number of message tags
|
|
123 |
* with their severity attribute matching the configured one.
|
|
124 |
*
|
|
125 |
* @return if true if message with the defined severity have been found.
|
|
126 |
*/
|
|
127 |
public boolean eval() {
|
|
128 |
int severity = getSeverity();
|
|
129 |
if (severity < 0) {
|
|
130 |
return false;
|
|
131 |
}
|
|
132 |
return severity > 0;
|
|
133 |
}
|
|
134 |
} |