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