buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/LogMetaDataInput.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
--- a/buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/LogMetaDataInput.java	Wed Jun 16 16:51:40 2010 +0300
+++ b/buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/LogMetaDataInput.java	Fri Aug 13 14:59:05 2010 +0300
@@ -17,54 +17,43 @@
 
 package com.nokia.helium.metadata.ant.types;
 
-import com.nokia.helium.metadata.MetaDataInput;
-
-import org.apache.tools.ant.BuildException;
-import com.nokia.helium.jpa.entity.metadata.Metadata;
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
-import java.util.Vector;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Hashtable;
 import java.util.regex.Pattern;
-import org.apache.tools.ant.types.FileSet;
-import org.apache.tools.ant.DirectoryScanner;
-import org.apache.log4j.Logger;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
 import org.apache.tools.ant.types.DataType;
-import java.io.IOException;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.ResourceCollection;
+
+import com.nokia.helium.metadata.MetaDataInput;
+import com.nokia.helium.metadata.MetadataException;
+import com.nokia.helium.metadata.model.metadata.LogFile;
+import com.nokia.helium.metadata.model.metadata.LogFileDAO;
 
 /**
  * Abstract base class to provide common functionality for the log parsing.
  */
-public abstract class LogMetaDataInput extends DataType implements
-    MetaDataInput {
-
-    private static Logger log = Logger.getLogger(LogMetaDataInput.class);
-    
-    private Vector<FileSet> fileSetList = new Vector<FileSet>();
-
-    private int currentFileIndex;
+public abstract class LogMetaDataInput extends DataType implements MetaDataInput {
     
-    private boolean entryAddedForLog;
-
-    private List<File> fileList;
-    private Vector<MetaDataFilterSet> metadataFilterSets = new Vector<MetaDataFilterSet>();
-    private Vector<MetaDataFilter> completeFilterList;
-
-    private Iterator<Metadata.LogEntry> metadataInputIterator = new MetaDataInputIterator();
-
-    private List<Metadata.LogEntry> logEntries = new ArrayList<Metadata.LogEntry>();
-    
+    private List<ResourceCollection> resourceCollections = new ArrayList<ResourceCollection>();
+    private Task task;
+    private List<MetaDataFilterSet> metadataFilterSets = new ArrayList<MetaDataFilterSet>();
+    private List<MetaDataFilter> completeFilterList;
     
     /**
      * Adds the fileset (list of input log files to be processed).
      *  @param fileSet fileset to be added
      * 
      */
-    public void add(FileSet fileSet) {
-        fileSetList.add(fileSet);
+    public void add(ResourceCollection resourceCollection) {
+        resourceCollections.add(resourceCollection);
     }   
     
     /**
@@ -89,104 +78,14 @@
      * Helper function to return all the filters associated with this metadata input
      * @return all the filters merged based on the order of definition.
      */
-    private Vector<MetaDataFilter> getCompleteFilters() {
-        Vector<MetaDataFilter> allFilter = new Vector<MetaDataFilter>();
-        for (MetaDataFilterSet filterSet : metadataFilterSets) {
-            allFilter.addAll(filterSet.getAllFilters());
-        }
-        return allFilter;
-    }
-
-    /**
-     * Updates the list of filelist from the input fileset.
-     *  @param fileSetList input fileset list
-     *  @return the matched files including the base dir. 
-     */
-    private List<File> getFileListFromFileSet() {
-        fileList = new ArrayList<File>();
-        for (FileSet fs : fileSetList) {
-            DirectoryScanner ds = fs.getDirectoryScanner(getProject());
-            String[] includedFiles = ds.getIncludedFiles();
-            for ( String file : includedFiles ) {
-                fileList.add(new File(ds.getBasedir(), file));
-                log.debug("includedfiles: " + file);
+    private synchronized List<MetaDataFilter> getMetaDataFilters() {
+        if (completeFilterList == null) {
+            completeFilterList = new ArrayList<MetaDataFilter>();
+            for (MetaDataFilterSet filterSet : metadataFilterSets) {
+                completeFilterList.addAll(filterSet.getAllFilters());
             }
         }
-        log.debug("fileList.size" + fileList.size());
-        return fileList;
-    }
-
-    /**
-     * Internal function to get the entry
-     * @return the top most entry in the list.
-     */
-    private Metadata.LogEntry getEntry() throws IOException {
-        if (logEntries != null && logEntries.size() > 0) {
-            return logEntries.remove(0);
-        } else {
-            throw new IOException("No entries found");
-        }
-    }
-
-    /**
-     * Helper function to return the file list of the metadata input
-     * @return file list of this metadata input.
-     */
-    protected List<File> getFileList() {
-        return fileList;
-    }
-
-    protected File getCurrentFile() {
-        List<File> fileList = getFileList();
-        return fileList.get(currentFileIndex); 
-    }
-
-    /**
-     * Function to check from the input stream if is there any entries available. Implemented by the sub classes.
-     * @return true if there are any entry available otherwise false.
-     */
-    
-    boolean isEntryAvailable() {
-        int fileListSize = getFileList().size();
-        while (currentFileIndex < fileListSize) {
-            boolean entryCreated = false;
-            File currentFile = getCurrentFile();
-            entryCreated = isEntryCreated(currentFile);
-            if (entryCreated) {
-                if (!entryAddedForLog) {
-                    entryAddedForLog = true;
-                }
-                return entryCreated;
-            }
-            if (!entryAddedForLog) {
-                // If no entry, then logfile is added to the database.
-                addEntry("default", "general",
-                        getCurrentFile().toString(), -1, "", -1, null);
-                entryAddedForLog = true;
-                return true;
-            }
-            if (isAdditionalEntry()) {
-                return true;
-            }
-            currentFileIndex ++;
-        }
-        return false;
-    }
-
-    /**
-     * Function to check from the input stream if is there any entries available.
-     * @param file for which the contents needs to be parsed for errors
-     * @return true if there are any entry available otherwise false.
-     */
-    abstract boolean isEntryCreated(File currentFile);
-
-    /**
-     * Function to check if is there any additional entry. This is being used for example during streaming
-     * recorded and at the end of streaming use the recorded data to add any additional entry. Used by
-     * @return true if there are any additional entries which are to be recorded in the database.
-     */
-    protected boolean isAdditionalEntry() {
-        return false;
+        return completeFilterList;
     }
 
     /**
@@ -194,177 +93,107 @@
      * @param log text for which the severity needs to be identified.
      * @return the severity of the input text
      */
-    protected String getSeverity(String logText) {
-        if (completeFilterList == null) {
-            completeFilterList = getCompleteFilters();
-        }
-        for ( MetaDataFilter filter : completeFilterList) {
+    protected SeverityEnum.Severity getSeverity(String logText) {
+        for (MetaDataFilter filter : getMetaDataFilters()) {
             Pattern pattern = filter.getPattern();
             if ((pattern.matcher(logText)).matches()) {
-                return filter.getPriority();
+                return filter.getSeverity();
             }
         }
-        return null;
-    }
-
-
-    /**
-     * Helper function to store the entry which will be added to the database
-     * @param priority for the entry
-     * @param component of the entry
-     * @param logpath of the entry
-     * @param lineNo of the entry
-     * @param log text message of the entry
-     */
-    protected void addEntry(String priority, String component, String logPath, int lineNo, 
-            String logText) {
-        addEntry(priority, component, logPath, lineNo, logText, -1, null);
-    }
-    
-
-    /**
-     * Helper function to store the entry which will be added to the database
-     * @param priority for the entry
-     * @param component of the entry
-     * @param logpath of the entry
-     * @param lineNo of the entry
-     * @param log text message of the entry
-     * @param elapsedTime of the component
-     */
-    protected void addEntry(String priority, String component, String logPath, int lineNo, 
-            String logText, float elapsedTime, Metadata.WhatEntry whatEntry) {
-        //log.debug("adding entry to the list");
-        File logPathFile = new File(logPath.trim());
-        String baseDir = logPathFile.getParent();
-        //Note: Always the query should be in "/" format only, compatible for both linux / windows
-        String uniqueLogPath = baseDir + "/" +  logPathFile.getName();
-        Metadata.LogEntry entry = new Metadata.LogEntry(
-                logText, priority, 
-                component, uniqueLogPath, lineNo, elapsedTime, whatEntry);
-        logEntries.add(entry);
-    }
-    
-    /**
-     * Looks for the text which matches the filter regular expression and adds the entries to the database.
-     * @param logTextInfo text message to be searched with filter regular expressions
-     * @param priority for the entry
-     * @param currentComponent of the logtextInfo
-     * @param logpath fo;e fpr wjocj tje text info has to be looked for with filter expression
-     * @param lineNumber of the text message
-     */
-    protected boolean findAndAddEntries(String logTextInfo, String currentComponent, 
-            String logPath, int lineNumber) {
-        return findAndAddEntries(logTextInfo, currentComponent, logPath, lineNumber, null);
+        return SeverityEnum.Severity.NONE;
     }
 
     /**
-     * Looks for the text which matches the filter regular expression and adds the entries to the database.
-     * @param logTextInfo text message to be searched with filter regular expressions
-     * @param priority for the entry
-     * @param currentComponent of the logtextInfo
-     * @param logpath fo;e fpr wjocj tje text info has to be looked for with filter expression
-     * @param lineNumber of the text message
-     * @param stat object to capture statistics about the parsing.
+     * Logging through the Ant task
      */
-    protected boolean findAndAddEntries(String logTextInfo, String currentComponent, 
-            String logPath, int lineNumber, Statistics stat) {
-        boolean entryAdded = false; 
-        String[] logText = logTextInfo.split("\n");
-        String severity = null;
-        for (int i = 0; i < logText.length; i++) {
-            severity = getSeverity(logText[i]);
-            if (severity != null) {
-                addEntry(severity, currentComponent, logPath, 
-                        i + lineNumber, logText[i]);
-                if (stat != null) {
-                    stat.incrementSeverity(severity);
-                }
-                entryAdded = true;
-            }
-        }
-        return entryAdded;
+    public void log(String message) {
+        log(message, Project.MSG_INFO);    
     }
     
     /**
-     * Log text are processed based on iterator. When ever the entry is found the entry is returned
-     * and the function is called again for further entries.
-     * @return the iterator object for the metadata input.
+     * Logging through the Ant task
      */
-    public Iterator<Metadata.LogEntry> iterator() {
-        return metadataInputIterator;
+    public void log(String message, int level) {
+        if (task != null) {
+            task.log(message, level);
+        } else {
+            getProject().log(message, level);
+        }
     }
-
     /**
-     * Class to process the files as stream and add the entries todb
+     * Get the LogFile instance for the file log.
+     * @param entityManager the entityManager to do the query.
+     * @param file the log file locations
+     * @return the LogFile entry for the file log. 
      */
-    public class MetaDataInputIterator implements Iterator<Metadata.LogEntry> {
-        public boolean hasNext() {
-            if (fileList == null) {
-                fileList = getFileListFromFileSet();
-                if (fileList.isEmpty()) {
-                    throw new BuildException(" No input found.");
+    protected LogFile getLogFile(EntityManager entityManager, File file) {
+        // Creating the filename
+        LogFileDAO lfdao = new LogFileDAO();
+        lfdao.setEntityManager(entityManager);
+        LogFile logFile = lfdao.findByLogName(file);
+        if (logFile == null) {
+            log("Creating a logfile entry.", Project.MSG_DEBUG);
+            logFile = new LogFile();
+            logFile.setPath(file.getAbsolutePath());
+            entityManager.getTransaction().begin();
+            entityManager.persist(logFile);
+            entityManager.getTransaction().commit();
+        }
+        return logFile;
+    }
+    
+    /**
+     * {@inheritDoc}
+     * Implements default behavior, for each log file the reference inside
+     * the database will first be removed, and then data will be collected. 
+     */
+    @SuppressWarnings("unchecked")
+    public void extract(Task task, EntityManagerFactory factory)
+        throws MetadataException {
+        this.task = task;
+        try {
+            for (ResourceCollection resourceCollection : resourceCollections) {
+                Iterator<Resource> ri = (Iterator<Resource>)resourceCollection.iterator();
+                while (ri.hasNext()) {
+                    File file = new File(ri.next().toString());
+                    remove(factory, file);
+                    log("Extracting data from " + file);
+                    extract(factory, file);
                 }
             }
-            if (logEntries.size() > 0) {
-                return true;
-            }
-            boolean retValue = false;
-            retValue = isEntryAvailable();
-            return retValue;
-        }
-
-        /**
-         * Helper function to remove  entries if any
-         */
-        public void remove() {
-        }
-        
-
-        /**
-         * Gets the next entry, which has been identified
-         * @return log entry to be added to the database.
-         */
-        public Metadata.LogEntry next() {
-            Metadata.LogEntry entry = null;
-            try {
-                entry = getEntry();
-            } catch (IOException ex) {
-                log.debug("Exception while getting entry: No entry available", ex);
-            }
-            return entry;
+        } finally {
+            this.task = null;
         }
     }
     
     /**
-     * This class capture statistics about the number of severity counted when 
-     * parsing, the log.
+     * Removing a file log data from the database (if any).
+     * @param factory
+     * @param file
+     * @throws MetadataException
      */
-    public class Statistics {
-        private Map<String, Integer> statistics = new Hashtable<String, Integer>();
-        
-        /**
-         * Increment the severity counter by 1.
-         */
-        public void incrementSeverity(String severity) {
-            severity = severity.toLowerCase();
-            if (statistics.get(severity) == null) {
-                statistics.put(severity, new Integer(1));
-            } else {
-                statistics.put(severity, new Integer(statistics.get(severity).intValue() + 1));
+    public void remove(EntityManagerFactory factory, File file) throws MetadataException {
+        EntityManager entityManager = factory.createEntityManager();
+        try {
+            LogFileDAO logFileDAO = new LogFileDAO();
+            logFileDAO.setEntityManager(entityManager);
+            LogFile logFile = logFileDAO.findByLogName(file);
+            if (logFile != null) {
+                log("Removing log from database: " + file.getAbsolutePath());
+                entityManager.getTransaction().begin();
+                logFileDAO.remove(logFile);
+                entityManager.getTransaction().commit();
             }
-        }
-        
-        /**
-         * Get the severity counter.
-         * @return the number of message with the mentioned severity.
-         */
-        public int getSeveriry(String severity) {
-            severity = severity.toLowerCase();
-            if (statistics.get(severity) == null) {
-                return 0;
-            } else {
-                return statistics.get(severity).intValue();
-            }
+        } finally {
+            entityManager.close();
         }
     }
+    
+    /**
+     * Extracting the data from 
+     * @param factory
+     * @param file
+     * @throws MetadataException
+     */
+    public abstract void extract(EntityManagerFactory factory, File file) throws MetadataException;
 }
\ No newline at end of file