buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/AntLogMetaDataInput.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 587 85df38eb4012
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
/*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
 * All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
 * Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
 * Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
 * Description:  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
package com.nokia.helium.metadata.ant.types;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    20
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    21
import java.io.BufferedReader;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    22
import java.io.File;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    23
import java.io.FileNotFoundException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    24
import java.io.FileReader;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    25
import java.io.IOException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    26
import java.util.Map;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    27
import java.util.regex.Matcher;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
import java.util.regex.Pattern;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    29
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    30
import javax.persistence.EntityManager;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    31
import javax.persistence.EntityManagerFactory;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    32
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    33
import com.nokia.helium.metadata.AutoCommitEntityManager;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    34
import com.nokia.helium.metadata.MetadataException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    35
import com.nokia.helium.metadata.model.metadata.Component;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    36
import com.nokia.helium.metadata.model.metadata.LogFile;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    37
import com.nokia.helium.metadata.model.metadata.MetadataEntry;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    38
import com.nokia.helium.metadata.model.metadata.Severity;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    39
import com.nokia.helium.metadata.model.metadata.SeverityDAO;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
 * This Type is to specify and use the ant logparser type to parse and store the data.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
 * <pre>
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
 * &lt;hlm:metadatafilterset id="ant.metadata.filter"&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
 *    &lt;metadatafilterset filterfile="${project.dir}/../data/common.csv" /&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
 * &lt;/hlm:metadatafilterset&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
 * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
 * &lt;hlm:antmetadatainput&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
 *    &lt;fileset dir="${project.dir}/../data/"&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
 *        &lt;include name="*zip*.log"/&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
 *    &lt;/fileset&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
 *    &lt;metadatafilterset refid="ant.metadata.filter" /&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
 * &lt;/hlm:antmetadatainput&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
 * </pre>
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
 * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
 * @ant.task name="antmetadatainput" category="Metadata"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
 */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    59
public class AntLogMetaDataInput extends AbstractComponentBaseMetadataInput {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    61
    public static final String DEFAULT_COMPONENT = "Ant";
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
    private Pattern antTargetPattern = Pattern.compile("^([^\\s=\\[\\]]+):$");
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    63
    private EntityManager entityManager;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    66
     * {@inheritDoc}
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    68
    @Override
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    69
    public void extract(EntityManagerFactory factory, File file)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    70
        throws MetadataException {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    71
        Component currentComponent = null;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    72
        entityManager = factory.createEntityManager();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    73
        AutoCommitEntityManager autoCommitEM = new AutoCommitEntityManager(factory);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    74
        try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    75
            // Loading the available priorities
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    76
            SeverityDAO severityDao = new SeverityDAO();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    77
            severityDao.setEntityManager(entityManager);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    78
            Map<String, Severity> priorities = severityDao.getSeverities();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    79
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    80
            // Creating the filename
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    81
            LogFile logFile = getLogFile(entityManager, file);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    82
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    83
            // Parsing the log file
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    84
            BufferedReader reader = new BufferedReader(new FileReader(file));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    85
            String logText = null;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    86
            int lineNumber = 0;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    87
            while ((logText = reader.readLine()) != null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    88
                lineNumber++;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    89
                String line = logText.replaceFirst("^[ ]*\\[.+?\\][ ]*", "");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    90
                
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    91
                Matcher matcher = antTargetPattern.matcher(line);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    92
                if (matcher.matches()) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    93
                    currentComponent = getComponent(matcher.group(1), logFile);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    94
                } else {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    95
                    if (currentComponent == null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    96
                        currentComponent = getDefaultComponent(logFile);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    97
                    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    98
                    SeverityEnum.Severity severity = getSeverity(line);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    99
                    if (severity != SeverityEnum.Severity.NONE) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   100
                        MetadataEntry entry = new MetadataEntry();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   101
                        entry.setLogFile(logFile);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   102
                        entry.setLineNumber(lineNumber);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   103
                        entry.setSeverity(autoCommitEM.merge(priorities.get(severity.toString())));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   104
                        entry.setText(line);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   105
                        entry.setComponent(autoCommitEM.merge(currentComponent));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   106
                        autoCommitEM.persist(entry);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   107
                    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   108
                }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   109
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   110
            reader.close();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   111
        } catch (FileNotFoundException ex) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   112
            throw new MetadataException(ex.getMessage(), ex);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   113
        } catch (IOException ex) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   114
            throw new MetadataException(ex.getMessage(), ex);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   115
        } finally {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   116
            if (autoCommitEM != null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   117
                autoCommitEM.close();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   118
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   119
            if (entityManager != null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   120
                entityManager.close();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   121
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   122
            clear();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   123
        }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   124
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   125
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   127
     * {@inheritDoc}
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   128
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   129
    @Override
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   130
    protected EntityManager getEntityManager() {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   131
        return entityManager;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   132
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   133
}