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 |
|
|
20 |
import java.io.File;
|
|
21 |
import java.util.ArrayList;
|
628
|
22 |
import java.util.Iterator;
|
587
|
23 |
import java.util.List;
|
|
24 |
import java.util.regex.Pattern;
|
628
|
25 |
|
|
26 |
import javax.persistence.EntityManager;
|
|
27 |
import javax.persistence.EntityManagerFactory;
|
|
28 |
|
|
29 |
import org.apache.tools.ant.Project;
|
|
30 |
import org.apache.tools.ant.Task;
|
587
|
31 |
import org.apache.tools.ant.types.DataType;
|
628
|
32 |
import org.apache.tools.ant.types.Resource;
|
|
33 |
import org.apache.tools.ant.types.ResourceCollection;
|
|
34 |
|
|
35 |
import com.nokia.helium.metadata.MetaDataInput;
|
|
36 |
import com.nokia.helium.metadata.MetadataException;
|
|
37 |
import com.nokia.helium.metadata.model.metadata.LogFile;
|
|
38 |
import com.nokia.helium.metadata.model.metadata.LogFileDAO;
|
587
|
39 |
|
|
40 |
/**
|
|
41 |
* Abstract base class to provide common functionality for the log parsing.
|
|
42 |
*/
|
628
|
43 |
public abstract class LogMetaDataInput extends DataType implements MetaDataInput {
|
587
|
44 |
|
628
|
45 |
private List<ResourceCollection> resourceCollections = new ArrayList<ResourceCollection>();
|
|
46 |
private Task task;
|
|
47 |
private List<MetaDataFilterSet> metadataFilterSets = new ArrayList<MetaDataFilterSet>();
|
|
48 |
private List<MetaDataFilter> completeFilterList;
|
587
|
49 |
|
|
50 |
/**
|
|
51 |
* Adds the fileset (list of input log files to be processed).
|
|
52 |
* @param fileSet fileset to be added
|
|
53 |
*
|
|
54 |
*/
|
628
|
55 |
public void add(ResourceCollection resourceCollection) {
|
|
56 |
resourceCollections.add(resourceCollection);
|
587
|
57 |
}
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Adds the fileset (list of input log files to be processed).
|
|
61 |
* @param fileSet fileset to be added
|
|
62 |
*
|
|
63 |
*/
|
|
64 |
public void add(MetaDataFilterSet metadataFilterSet) {
|
|
65 |
metadataFilterSets.add(metadataFilterSet);
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Helper function called by ant to create the new filter
|
|
70 |
*/
|
|
71 |
public MetaDataFilterSet createMetaDataFilterSet() {
|
|
72 |
MetaDataFilterSet filterSet = new MetaDataFilterSet();
|
|
73 |
add(filterSet);
|
|
74 |
return filterSet;
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
|
78 |
* Helper function to return all the filters associated with this metadata input
|
|
79 |
* @return all the filters merged based on the order of definition.
|
|
80 |
*/
|
628
|
81 |
private synchronized List<MetaDataFilter> getMetaDataFilters() {
|
|
82 |
if (completeFilterList == null) {
|
|
83 |
completeFilterList = new ArrayList<MetaDataFilter>();
|
|
84 |
for (MetaDataFilterSet filterSet : metadataFilterSets) {
|
|
85 |
completeFilterList.addAll(filterSet.getAllFilters());
|
587
|
86 |
}
|
|
87 |
}
|
628
|
88 |
return completeFilterList;
|
587
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Returns the severity matches for the log text
|
|
93 |
* @param log text for which the severity needs to be identified.
|
|
94 |
* @return the severity of the input text
|
|
95 |
*/
|
628
|
96 |
protected SeverityEnum.Severity getSeverity(String logText) {
|
|
97 |
for (MetaDataFilter filter : getMetaDataFilters()) {
|
587
|
98 |
Pattern pattern = filter.getPattern();
|
|
99 |
if ((pattern.matcher(logText)).matches()) {
|
628
|
100 |
return filter.getSeverity();
|
587
|
101 |
}
|
|
102 |
}
|
628
|
103 |
return SeverityEnum.Severity.NONE;
|
587
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
628
|
107 |
* Logging through the Ant task
|
587
|
108 |
*/
|
628
|
109 |
public void log(String message) {
|
|
110 |
log(message, Project.MSG_INFO);
|
587
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
628
|
114 |
* Logging through the Ant task
|
587
|
115 |
*/
|
628
|
116 |
public void log(String message, int level) {
|
|
117 |
if (task != null) {
|
|
118 |
task.log(message, level);
|
|
119 |
} else {
|
|
120 |
getProject().log(message, level);
|
|
121 |
}
|
587
|
122 |
}
|
|
123 |
/**
|
628
|
124 |
* Get the LogFile instance for the file log.
|
|
125 |
* @param entityManager the entityManager to do the query.
|
|
126 |
* @param file the log file locations
|
|
127 |
* @return the LogFile entry for the file log.
|
587
|
128 |
*/
|
628
|
129 |
protected LogFile getLogFile(EntityManager entityManager, File file) {
|
|
130 |
// Creating the filename
|
|
131 |
LogFileDAO lfdao = new LogFileDAO();
|
|
132 |
lfdao.setEntityManager(entityManager);
|
|
133 |
LogFile logFile = lfdao.findByLogName(file);
|
|
134 |
if (logFile == null) {
|
|
135 |
log("Creating a logfile entry.", Project.MSG_DEBUG);
|
|
136 |
logFile = new LogFile();
|
|
137 |
logFile.setPath(file.getAbsolutePath());
|
|
138 |
entityManager.getTransaction().begin();
|
|
139 |
entityManager.persist(logFile);
|
|
140 |
entityManager.getTransaction().commit();
|
|
141 |
}
|
|
142 |
return logFile;
|
|
143 |
}
|
|
144 |
|
|
145 |
/**
|
|
146 |
* {@inheritDoc}
|
|
147 |
* Implements default behavior, for each log file the reference inside
|
|
148 |
* the database will first be removed, and then data will be collected.
|
|
149 |
*/
|
|
150 |
@SuppressWarnings("unchecked")
|
|
151 |
public void extract(Task task, EntityManagerFactory factory)
|
|
152 |
throws MetadataException {
|
|
153 |
this.task = task;
|
|
154 |
try {
|
|
155 |
for (ResourceCollection resourceCollection : resourceCollections) {
|
|
156 |
Iterator<Resource> ri = (Iterator<Resource>)resourceCollection.iterator();
|
|
157 |
while (ri.hasNext()) {
|
|
158 |
File file = new File(ri.next().toString());
|
|
159 |
remove(factory, file);
|
|
160 |
log("Extracting data from " + file);
|
|
161 |
extract(factory, file);
|
587
|
162 |
}
|
|
163 |
}
|
628
|
164 |
} finally {
|
|
165 |
this.task = null;
|
587
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
628
|
170 |
* Removing a file log data from the database (if any).
|
|
171 |
* @param factory
|
|
172 |
* @param file
|
|
173 |
* @throws MetadataException
|
587
|
174 |
*/
|
628
|
175 |
public void remove(EntityManagerFactory factory, File file) throws MetadataException {
|
|
176 |
EntityManager entityManager = factory.createEntityManager();
|
|
177 |
try {
|
|
178 |
LogFileDAO logFileDAO = new LogFileDAO();
|
|
179 |
logFileDAO.setEntityManager(entityManager);
|
|
180 |
LogFile logFile = logFileDAO.findByLogName(file);
|
|
181 |
if (logFile != null) {
|
|
182 |
log("Removing log from database: " + file.getAbsolutePath());
|
|
183 |
entityManager.getTransaction().begin();
|
|
184 |
logFileDAO.remove(logFile);
|
|
185 |
entityManager.getTransaction().commit();
|
587
|
186 |
}
|
628
|
187 |
} finally {
|
|
188 |
entityManager.close();
|
587
|
189 |
}
|
|
190 |
}
|
628
|
191 |
|
|
192 |
/**
|
|
193 |
* Extracting the data from
|
|
194 |
* @param factory
|
|
195 |
* @param file
|
|
196 |
* @throws MetadataException
|
|
197 |
*/
|
|
198 |
public abstract void extract(EntityManagerFactory factory, File file) throws MetadataException;
|
587
|
199 |
} |