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;
|
628
|
21 |
import java.util.ArrayList;
|
|
22 |
import java.util.Date;
|
|
23 |
import java.util.Iterator;
|
|
24 |
import java.util.List;
|
|
25 |
|
|
26 |
import javax.persistence.EntityManager;
|
|
27 |
import javax.persistence.EntityManagerFactory;
|
|
28 |
import javax.persistence.Query;
|
|
29 |
|
587
|
30 |
import org.apache.tools.ant.BuildException;
|
|
31 |
import org.apache.tools.ant.Project;
|
|
32 |
import org.apache.tools.ant.taskdefs.condition.Condition;
|
|
33 |
import org.apache.tools.ant.types.DataType;
|
628
|
34 |
import org.apache.tools.ant.types.Resource;
|
|
35 |
import org.apache.tools.ant.types.ResourceCollection;
|
|
36 |
|
|
37 |
import com.nokia.helium.metadata.FactoryManager;
|
|
38 |
import com.nokia.helium.metadata.MetadataException;
|
|
39 |
import com.nokia.helium.metadata.ant.types.SeverityEnum;
|
587
|
40 |
|
|
41 |
/**
|
|
42 |
* This class implements a Ant Condition which report true if it finds any
|
|
43 |
* matching severity inside a database for an XML log.
|
|
44 |
*
|
|
45 |
* Example:
|
|
46 |
* <pre>
|
|
47 |
* <target name="fail-on-build-error">
|
|
48 |
* <fail message="The build contains errors">
|
628
|
49 |
* <hlm:metadataHasSeverity log="my.log" database="my.db" severity="error"/>
|
587
|
50 |
* </fail>
|
|
51 |
* </target>
|
|
52 |
* </pre>
|
|
53 |
*
|
|
54 |
* The condition will eval as true if the my.db contains error stored for my.log file.
|
|
55 |
*
|
|
56 |
* @ant.type name="metadataHasSeverity" category="Metadata"
|
|
57 |
*/
|
|
58 |
public class MetaDataLogCondition extends DataType implements Condition {
|
|
59 |
|
|
60 |
// The severity to count
|
628
|
61 |
private SeverityEnum severity;
|
|
62 |
private File log;
|
|
63 |
private File database;
|
|
64 |
private List<ResourceCollection> resourceCollections = new ArrayList<ResourceCollection>();
|
587
|
65 |
|
|
66 |
/**
|
628
|
67 |
* Defines which severity will be counted.
|
587
|
68 |
*
|
|
69 |
* @param severity
|
|
70 |
* @ant.required
|
|
71 |
*/
|
628
|
72 |
public void setSeverity(SeverityEnum severity) {
|
587
|
73 |
this.severity = severity;
|
|
74 |
}
|
|
75 |
|
628
|
76 |
/**
|
|
77 |
* Defines the database to use.
|
|
78 |
* @param database
|
|
79 |
*/
|
|
80 |
@Deprecated
|
|
81 |
public void setDb(File database) {
|
|
82 |
log("The usage of the 'db' attribute is deprecated, please use the database attribute instead.", Project.MSG_WARN);
|
|
83 |
setDatabase(database);
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Defines the database to use.
|
|
88 |
* @param database
|
|
89 |
*/
|
|
90 |
public void setDatabase(File database) {
|
|
91 |
this.database = database;
|
587
|
92 |
}
|
|
93 |
|
628
|
94 |
/**
|
|
95 |
* The log file to look severity for in the metadata.
|
|
96 |
* @param log the actual real log file.
|
|
97 |
*/
|
|
98 |
public void setLog(File log) {
|
|
99 |
this.log = log;
|
587
|
100 |
}
|
|
101 |
|
628
|
102 |
/**
|
|
103 |
* Defines if missing file shall be counted (Deprecated attribute is ignored).
|
|
104 |
* @param countMissing
|
|
105 |
*/
|
|
106 |
@Deprecated
|
587
|
107 |
public void setCountMissing(boolean countMissing) {
|
628
|
108 |
log("The usage of the 'countMissing' attribute is deprecated.", Project.MSG_WARN);
|
|
109 |
//this.countMissing = countMissing;
|
|
110 |
}
|
|
111 |
|
|
112 |
/**
|
|
113 |
*
|
|
114 |
* @param resourceCollection
|
|
115 |
*/
|
|
116 |
public void add(ResourceCollection resourceCollection) {
|
|
117 |
resourceCollections.add(resourceCollection);
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Get the severity for a specific log file.
|
|
122 |
* @param file
|
|
123 |
* @return
|
|
124 |
* @throws MetadataException
|
|
125 |
*/
|
|
126 |
public int getSeverity(EntityManager em, File file) throws MetadataException {
|
|
127 |
// log file under the DB is always represented with / and not \.
|
|
128 |
String queryString = "select Count(m.id) from MetadataEntry m JOIN m.logFile as l " +
|
|
129 |
"JOIN m.severity as p where l.path='" +
|
|
130 |
file.getAbsolutePath().replace('\\', '/') +
|
|
131 |
"' and p.severity='" + severity.getSeverity() + "'";
|
|
132 |
log("Query: " + queryString, Project.MSG_DEBUG);
|
|
133 |
Query query = em.createQuery(queryString);
|
|
134 |
Number number = (Number)query.getSingleResult();
|
|
135 |
log("Result: " + number, Project.MSG_DEBUG);
|
|
136 |
return number.intValue();
|
587
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Get the number of a particular severity.
|
|
141 |
*
|
|
142 |
* @return the number of a particular severity.
|
|
143 |
*/
|
628
|
144 |
@SuppressWarnings("unchecked")
|
|
145 |
public int getSeverity() throws MetadataException {
|
|
146 |
if (log == null && resourceCollections.isEmpty()) {
|
|
147 |
throw new BuildException("'log' attribute not defined.");
|
587
|
148 |
}
|
628
|
149 |
if (database == null) {
|
|
150 |
throw new BuildException("'database' attribute not defined.");
|
|
151 |
}
|
|
152 |
if (log != null && !log.exists()) {
|
|
153 |
log("Could not find " + log + ".", Project.MSG_WARN);
|
|
154 |
}
|
|
155 |
if (severity == null) {
|
|
156 |
throw new BuildException("'severity' attribute is not defined.");
|
|
157 |
}
|
587
|
158 |
|
628
|
159 |
EntityManagerFactory factory = null;
|
|
160 |
EntityManager em = null;
|
|
161 |
int result = 0;
|
|
162 |
try {
|
|
163 |
factory = FactoryManager.getFactoryManager().getEntityManagerFactory(database);
|
|
164 |
em = factory.createEntityManager();
|
|
165 |
Date before = new Date();
|
|
166 |
if (!resourceCollections.isEmpty()) {
|
|
167 |
for (ResourceCollection rc : resourceCollections) {
|
|
168 |
Iterator<Resource> ri = rc.iterator();
|
|
169 |
while (ri.hasNext()) {
|
|
170 |
Resource resource = ri.next();
|
|
171 |
log("Looking for severity '" + severity.getValue() + "' under '" + resource + "'");
|
|
172 |
result += getSeverity(em, new File(resource.toString()));
|
|
173 |
}
|
|
174 |
}
|
|
175 |
} else {
|
|
176 |
log("Looking for severity '" + severity.getValue() + "' under '" + log.getAbsolutePath() + "'");
|
|
177 |
result = getSeverity(em, log);
|
|
178 |
}
|
|
179 |
Date after = new Date();
|
|
180 |
log("Elapsed time: " + (after.getTime() - before.getTime()) + " ms");
|
|
181 |
} finally {
|
|
182 |
if (em != null) {
|
|
183 |
em.close();
|
|
184 |
}
|
|
185 |
if (factory != null) {
|
|
186 |
factory.close();
|
|
187 |
}
|
587
|
188 |
}
|
628
|
189 |
return result;
|
587
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* This method open the defined file and count the number of message tags
|
|
194 |
* with their severity attribute matching the configured one.
|
|
195 |
*
|
|
196 |
* @return if true if message with the defined severity have been found.
|
|
197 |
*/
|
|
198 |
public boolean eval() {
|
628
|
199 |
try {
|
|
200 |
int severity = getSeverity();
|
|
201 |
if (severity < 0) {
|
|
202 |
return false;
|
|
203 |
}
|
|
204 |
return severity > 0;
|
|
205 |
} catch (MetadataException ex) {
|
|
206 |
throw new BuildException(ex.getMessage(), ex);
|
587
|
207 |
}
|
|
208 |
}
|
|
209 |
} |