15 * |
15 * |
16 */ |
16 */ |
17 |
17 |
18 package com.nokia.helium.metadata.ant.types; |
18 package com.nokia.helium.metadata.ant.types; |
19 |
19 |
20 import java.io.*; |
20 import java.io.BufferedReader; |
21 import org.apache.tools.ant.BuildException; |
21 import java.io.File; |
22 import java.util.*; |
22 import java.io.FileNotFoundException; |
23 import org.apache.log4j.Logger; |
23 import java.io.FileReader; |
|
24 import java.io.IOException; |
|
25 import java.util.Map; |
|
26 import java.util.regex.Matcher; |
24 import java.util.regex.Pattern; |
27 import java.util.regex.Pattern; |
25 import java.util.regex.Matcher; |
28 |
|
29 import javax.persistence.EntityManager; |
|
30 import javax.persistence.EntityManagerFactory; |
|
31 |
|
32 import com.nokia.helium.metadata.AutoCommitEntityManager; |
|
33 import com.nokia.helium.metadata.MetadataException; |
|
34 import com.nokia.helium.metadata.model.metadata.LogFile; |
|
35 import com.nokia.helium.metadata.model.metadata.MetadataEntry; |
|
36 import com.nokia.helium.metadata.model.metadata.Severity; |
|
37 import com.nokia.helium.metadata.model.metadata.SeverityDAO; |
26 |
38 |
27 /** |
39 /** |
28 * This Type is to specify and use the abld logparser type to parse and store the data. |
40 * This Type is to specify and use the abld logparser type to parse and store |
29 * |
41 * the data. |
|
42 * |
30 * <pre> |
43 * <pre> |
31 * <hlm:metadatafilterset id="abld.metadata.filter"> |
44 * <hlm:metadatafilterset id="abld.metadata.filter"> |
32 * <metadatafilterset filterfile="common.csv" /> |
45 * <metadatafilterset filterfile="common.csv" /> |
33 * </hlm:metadatafilterset> |
46 * </hlm:metadatafilterset> |
34 * |
47 * |
35 * <hlm:abldmetadatainput> |
48 * <hlm:abldmetadatainput> |
36 * <fileset dir="${project.dir}/../data/"> |
49 * <fileset dir="${project.dir}/../data/"> |
37 * <include name="*_compile*.log"/> |
50 * <include name="*_compile*.log"/> |
38 * </fileset> |
51 * </fileset> |
39 * <metadatafilterset refid="abld.metadata.filter" /> |
52 * <metadatafilterset refid="abld.metadata.filter" /> |
40 * </hlm:antmetadatainput> |
53 * </hlm:antmetadatainput> |
41 * </pre> |
54 * </pre> |
42 * |
55 * |
43 * @ant.task name="abldmetadatainput" category="Metadata" |
56 * @ant.task name="abldmetadatainput" category="Metadata" |
44 */ |
57 */ |
45 public class AbldLogMetaDataInput extends TextLogMetaDataInput { |
58 public class AbldLogMetaDataInput extends AbstractComponentBaseMetadataInput { |
46 |
59 |
47 private Logger log = Logger.getLogger(AbldLogMetaDataInput.class); |
60 public static final String DEFAULT_COMPONENT = "General"; |
|
61 private Pattern abldFinishedPattern = Pattern |
|
62 .compile("^===\\s+.+\\s+finished.*"); |
|
63 private Pattern abldStartedPattern = Pattern |
|
64 .compile("^===\\s+(.+)\\s+started.*"); |
|
65 private Pattern abldComponentPattern = Pattern |
|
66 .compile("^===\\s+(.+?)\\s+==\\s+(.+)"); |
|
67 |
|
68 private String currentComponent = DEFAULT_COMPONENT; |
|
69 private EntityManager entityManager; |
48 |
70 |
49 private Pattern abldFinishedPattern = Pattern.compile("^===\\s+.+\\s+finished.*"); |
71 /** |
50 private Pattern abldStartedPattern = Pattern.compile("^===\\s+(.+)\\s+started.*"); |
72 * {@inheritDoc} |
51 private Pattern abldComponentPattern = Pattern.compile("^===\\s+(.+?)\\s+==\\s+(.+)"); |
73 */ |
52 |
74 @Override |
|
75 public void extract(EntityManagerFactory factory, File file) |
|
76 throws MetadataException { |
|
77 entityManager = factory.createEntityManager(); |
|
78 AutoCommitEntityManager autoCommitEM = new AutoCommitEntityManager(factory); |
|
79 try { |
|
80 // Creating the filename |
|
81 LogFile logFile = getLogFile(entityManager, file); |
53 |
82 |
54 private String currentComponent; |
83 // Always defines the default component |
55 |
84 this.getDefaultComponent(logFile); |
56 private boolean entryCreated; |
|
57 |
85 |
58 private boolean recordText; |
86 // Loading the available priorities |
59 |
87 SeverityDAO severityDao = new SeverityDAO(); |
60 public AbldLogMetaDataInput() { |
88 severityDao.setEntityManager(entityManager); |
61 } |
89 Map<String, Severity> priorities = severityDao.getSeverities(); |
62 |
90 |
63 /** |
91 |
64 * Function to check from the input stream if is there any entries available. |
92 // Parsing the log file |
65 * @return true if there are any entry available otherwise false. |
93 BufferedReader reader = new BufferedReader(new FileReader(file)); |
66 */ |
|
67 public boolean isEntryCreated(File currentFile) { |
|
68 String exceptions = ""; |
|
69 int lineNumber = getLineNumber(); |
|
70 BufferedReader currentReader = getCurrentReader(); |
|
71 log.debug("Getting next set of log entries for Abld Input"); |
|
72 try { |
|
73 if (currentReader == null) { |
|
74 lineNumber = 0; |
|
75 setLineNumber(lineNumber); |
|
76 log.debug("Current abld log file name:" + currentFile); |
|
77 log.debug("Processing file: " + currentFile); |
|
78 currentReader = new BufferedReader(new FileReader(currentFile)); |
|
79 setCurrentReader(currentReader); |
|
80 } |
|
81 String logText = null; |
94 String logText = null; |
82 while ((logText = currentReader.readLine()) != null) { |
95 int lineNumber = 0; |
83 lineNumber ++; |
96 while ((logText = reader.readLine()) != null) { |
84 setLineNumber(lineNumber); |
97 lineNumber++; |
85 logText = logText.replaceFirst("'^\\s*\\[.+?\\]\\s*", ""); |
98 String line = logText.replaceFirst("^[ ]*\\[.+?\\][ ]*", ""); |
86 if (logText.startsWith("++ Finished at")) { |
99 |
87 if (currentComponent != null && !entryCreated) { |
100 if (line.startsWith("=== ")) { |
88 addEntry("DEFAULT", currentComponent, currentFile.toString(), |
101 Matcher matcher = abldComponentPattern.matcher(line); |
89 0, "" ); |
102 if (matcher.matches()) { |
90 entryCreated = true; |
103 currentComponent = matcher.group(2); |
91 recordText = false; |
104 getComponent(currentComponent, logFile); // declare the component... |
92 return true; |
|
93 } |
|
94 entryCreated = false; |
|
95 } else if (logText.startsWith("=== ")) { |
|
96 Matcher finishMatch = abldFinishedPattern.matcher(logText); |
|
97 if (finishMatch.matches()) { |
|
98 if (currentComponent != null && !entryCreated) { |
|
99 addEntry("DEFAULT", currentComponent, currentFile.toString(), |
|
100 0, "" ); |
|
101 entryCreated = true; |
|
102 recordText = false; |
|
103 return true; |
|
104 } |
|
105 entryCreated = false; |
|
106 } else { |
105 } else { |
107 Matcher componentMatch = abldComponentPattern.matcher(logText); |
106 matcher = abldStartedPattern.matcher(line); |
108 if (componentMatch.matches()) { |
107 if (matcher.matches()) { |
109 currentComponent = componentMatch.group(2); |
108 currentComponent = DEFAULT_COMPONENT; |
110 recordText = true; |
109 } else { |
111 } |
110 matcher = abldFinishedPattern.matcher(line); |
112 |
111 if (matcher.matches()) { |
113 Matcher startMatch = abldStartedPattern.matcher(logText); |
112 currentComponent = DEFAULT_COMPONENT; |
114 if (startMatch.matches()) { |
113 } |
115 currentComponent = startMatch.group(1); |
|
116 recordText = true; |
|
117 } |
114 } |
118 } |
115 } |
119 } else { |
116 } else { |
120 if (recordText) { |
117 SeverityEnum.Severity severity = getSeverity(line); |
121 String severity = getSeverity(logText); |
118 if (severity != SeverityEnum.Severity.NONE) { |
122 if (severity != null) { |
119 MetadataEntry entry = new MetadataEntry(); |
123 entryCreated = true; |
120 entry.setLogFile(autoCommitEM.merge(logFile)); |
124 addEntry(severity, currentComponent, currentFile.toString(), |
121 entry.setLineNumber(lineNumber); |
125 lineNumber, logText ); |
122 entry.setSeverity(autoCommitEM.merge(priorities.get(severity.toString()))); |
126 return true; |
123 entry.setText(line); |
127 } |
124 entry.setComponent(autoCommitEM.merge(getComponent(currentComponent, logFile))); |
|
125 autoCommitEM.persist(entry); |
128 } |
126 } |
129 } |
127 } |
130 } |
128 } |
131 currentReader.close(); |
129 reader.close(); |
132 currentReader = null; |
|
133 setCurrentReader(currentReader); |
|
134 } catch (FileNotFoundException ex) { |
130 } catch (FileNotFoundException ex) { |
135 log.debug("FileNotFoundException in AbldLogMetadata", ex); |
131 throw new MetadataException(ex.getMessage(), ex); |
136 try { |
132 } catch (IOException ex) { |
137 currentReader.close(); |
133 throw new MetadataException(ex.getMessage(), ex); |
138 } catch ( IOException iex) { |
134 } finally { |
139 // We are Ignoring the errors as no need to fail the build. |
135 if (autoCommitEM != null) { |
140 log.debug("Exception in closing reader", iex); |
136 autoCommitEM.close(); |
141 } |
137 } |
142 currentReader = null; |
138 if (entityManager != null) { |
143 setCurrentReader(null); |
139 if (entityManager.getTransaction().isActive()) { |
144 exceptions = exceptions + ex.getMessage() + "\n"; |
140 entityManager.getTransaction().rollback(); |
145 return false; |
141 } |
146 } catch (IOException ex) { |
142 entityManager.close(); |
147 log.debug("IOException in AbldLogMetadata", ex); |
143 entityManager = null; |
148 try { |
|
149 currentReader.close(); |
|
150 } catch ( IOException iex) { |
|
151 // We are Ignoring the errors as no need to fail the build. |
|
152 log.debug("IOException in closing reader", iex); |
|
153 } |
144 } |
154 currentReader = null; |
|
155 setCurrentReader(null); |
|
156 exceptions = exceptions + ex.getMessage() + "\n"; |
|
157 return false; |
|
158 } |
145 } |
159 if (!exceptions.equals("")) { |
146 clear(); |
160 throw new BuildException(exceptions); |
|
161 } |
|
162 return false; |
|
163 } |
147 } |
|
148 |
|
149 @Override |
|
150 protected EntityManager getEntityManager() { |
|
151 return entityManager; |
|
152 } |
|
153 |
|
154 @Override |
|
155 protected String getDefaultComponentName() { |
|
156 return DEFAULT_COMPONENT; |
|
157 } |
|
158 |
164 } |
159 } |