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.types;
|
|
19 |
|
628
|
20 |
|
|
21 |
import java.io.BufferedReader;
|
|
22 |
import java.io.File;
|
|
23 |
import java.io.FileNotFoundException;
|
|
24 |
import java.io.FileReader;
|
|
25 |
import java.io.IOException;
|
|
26 |
import java.util.Map;
|
|
27 |
import java.util.regex.Matcher;
|
587
|
28 |
import java.util.regex.Pattern;
|
628
|
29 |
|
|
30 |
import javax.persistence.EntityManager;
|
|
31 |
import javax.persistence.EntityManagerFactory;
|
|
32 |
|
|
33 |
import com.nokia.helium.metadata.AutoCommitEntityManager;
|
|
34 |
import com.nokia.helium.metadata.MetadataException;
|
|
35 |
import com.nokia.helium.metadata.model.metadata.Component;
|
|
36 |
import com.nokia.helium.metadata.model.metadata.LogFile;
|
|
37 |
import com.nokia.helium.metadata.model.metadata.MetadataEntry;
|
|
38 |
import com.nokia.helium.metadata.model.metadata.Severity;
|
|
39 |
import com.nokia.helium.metadata.model.metadata.SeverityDAO;
|
587
|
40 |
|
|
41 |
/**
|
|
42 |
* This Type is to specify and use the ant logparser type to parse and store the data.
|
|
43 |
*
|
|
44 |
* <pre>
|
|
45 |
* <hlm:metadatafilterset id="ant.metadata.filter">
|
|
46 |
* <metadatafilterset filterfile="${project.dir}/../data/common.csv" />
|
|
47 |
* </hlm:metadatafilterset>
|
|
48 |
*
|
|
49 |
* <hlm:antmetadatainput>
|
|
50 |
* <fileset dir="${project.dir}/../data/">
|
|
51 |
* <include name="*zip*.log"/>
|
|
52 |
* </fileset>
|
|
53 |
* <metadatafilterset refid="ant.metadata.filter" />
|
|
54 |
* </hlm:antmetadatainput>
|
|
55 |
* </pre>
|
|
56 |
*
|
|
57 |
* @ant.task name="antmetadatainput" category="Metadata"
|
|
58 |
*/
|
628
|
59 |
public class AntLogMetaDataInput extends AbstractComponentBaseMetadataInput {
|
587
|
60 |
|
628
|
61 |
public static final String DEFAULT_COMPONENT = "Ant";
|
587
|
62 |
private Pattern antTargetPattern = Pattern.compile("^([^\\s=\\[\\]]+):$");
|
628
|
63 |
private EntityManager entityManager;
|
587
|
64 |
|
|
65 |
/**
|
628
|
66 |
* {@inheritDoc}
|
587
|
67 |
*/
|
628
|
68 |
@Override
|
|
69 |
public void extract(EntityManagerFactory factory, File file)
|
|
70 |
throws MetadataException {
|
|
71 |
Component currentComponent = null;
|
|
72 |
entityManager = factory.createEntityManager();
|
|
73 |
AutoCommitEntityManager autoCommitEM = new AutoCommitEntityManager(factory);
|
|
74 |
try {
|
|
75 |
// Loading the available priorities
|
|
76 |
SeverityDAO severityDao = new SeverityDAO();
|
|
77 |
severityDao.setEntityManager(entityManager);
|
|
78 |
Map<String, Severity> priorities = severityDao.getSeverities();
|
|
79 |
|
|
80 |
// Creating the filename
|
|
81 |
LogFile logFile = getLogFile(entityManager, file);
|
|
82 |
|
|
83 |
// Parsing the log file
|
|
84 |
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
85 |
String logText = null;
|
|
86 |
int lineNumber = 0;
|
|
87 |
while ((logText = reader.readLine()) != null) {
|
|
88 |
lineNumber++;
|
|
89 |
String line = logText.replaceFirst("^[ ]*\\[.+?\\][ ]*", "");
|
|
90 |
|
|
91 |
Matcher matcher = antTargetPattern.matcher(line);
|
|
92 |
if (matcher.matches()) {
|
|
93 |
currentComponent = getComponent(matcher.group(1), logFile);
|
|
94 |
} else {
|
|
95 |
if (currentComponent == null) {
|
|
96 |
currentComponent = getDefaultComponent(logFile);
|
|
97 |
}
|
|
98 |
SeverityEnum.Severity severity = getSeverity(line);
|
|
99 |
if (severity != SeverityEnum.Severity.NONE) {
|
|
100 |
MetadataEntry entry = new MetadataEntry();
|
|
101 |
entry.setLogFile(logFile);
|
|
102 |
entry.setLineNumber(lineNumber);
|
|
103 |
entry.setSeverity(autoCommitEM.merge(priorities.get(severity.toString())));
|
|
104 |
entry.setText(line);
|
|
105 |
entry.setComponent(autoCommitEM.merge(currentComponent));
|
|
106 |
autoCommitEM.persist(entry);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
|
110 |
reader.close();
|
|
111 |
} catch (FileNotFoundException ex) {
|
|
112 |
throw new MetadataException(ex.getMessage(), ex);
|
|
113 |
} catch (IOException ex) {
|
|
114 |
throw new MetadataException(ex.getMessage(), ex);
|
|
115 |
} finally {
|
|
116 |
if (autoCommitEM != null) {
|
|
117 |
autoCommitEM.close();
|
|
118 |
}
|
|
119 |
if (entityManager != null) {
|
|
120 |
entityManager.close();
|
|
121 |
}
|
|
122 |
clear();
|
|
123 |
}
|
587
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
628
|
127 |
* {@inheritDoc}
|
587
|
128 |
*/
|
628
|
129 |
@Override
|
|
130 |
protected EntityManager getEntityManager() {
|
|
131 |
return entityManager;
|
587
|
132 |
}
|
|
133 |
} |