buildframework/helium/sf/java/diamonds/src/com/nokia/helium/diamonds/AllTargetDiamondsListener.java
changeset 628 7c4a911dc066
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
       
     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.diamonds;
       
    19 
       
    20 import java.io.BufferedWriter;
       
    21 import java.io.FileInputStream;
       
    22 import java.io.File;
       
    23 import java.io.FileWriter;
       
    24 import java.io.IOException;
       
    25 import java.util.Calendar;
       
    26 import java.util.Date;
       
    27 import java.util.Vector;
       
    28 import org.apache.log4j.Logger;
       
    29 import org.apache.tools.ant.BuildEvent;
       
    30 import org.apache.tools.ant.Target;
       
    31 
       
    32 /**
       
    33  * Generate target times
       
    34  */
       
    35 public class AllTargetDiamondsListener extends DiamondsListenerImpl {
       
    36 
       
    37     private static Logger log = Logger.getLogger(DiamondsListenerImpl.class);
       
    38 
       
    39     private Vector<AntTarget> antTargets = new Vector<AntTarget>();
       
    40 
       
    41 
       
    42     
       
    43 
       
    44     /**
       
    45      * Function to process logging info during begining of target execution
       
    46      * 
       
    47      * @param event of target execution.
       
    48      */
       
    49     public void targetBegin(BuildEvent buildEvent) {
       
    50         antTargets.add(new AntTarget(buildEvent.getTarget()));
       
    51     }
       
    52 
       
    53     /**
       
    54      * Function to process logging info during end of target execution
       
    55      * 
       
    56      * @param event of target execution.
       
    57      */
       
    58     public void targetEnd(BuildEvent buildEvent) {
       
    59         for (AntTarget at : antTargets)
       
    60         {
       
    61             if (at.equals(buildEvent.getTarget())) {
       
    62                 at.setEndTime(new Date());
       
    63             }
       
    64         }
       
    65     }
       
    66 
       
    67     /**
       
    68      * Function to process logging info during end of build
       
    69      * 
       
    70      * @param event of target execution.
       
    71      */
       
    72     public void buildEnd(BuildEvent buildEvent) throws DiamondsException {
       
    73         try {
       
    74             if (isInitialized()) {
       
    75                 File tempFile = File.createTempFile("diamonds-targets", ".xml");
       
    76                 FileWriter fstream = new FileWriter(tempFile);
       
    77                 BufferedWriter out = new BufferedWriter(fstream);
       
    78                 out.write("<targets>\n");
       
    79                 
       
    80                 for (AntTarget at : antTargets)
       
    81                 {
       
    82                     Calendar startcalendar = Calendar.getInstance();
       
    83                     Calendar endcalendar = Calendar.getInstance();
       
    84                     startcalendar.setTime(at.getStartTime());
       
    85                     if (at.getEndTime() != null)
       
    86                     {
       
    87                         endcalendar.setTime(at.getEndTime());
       
    88                         endcalendar.add(Calendar.SECOND, -5);
       
    89                         if (endcalendar.after(startcalendar))
       
    90                         {
       
    91                             out.write("<target>\n");
       
    92                             out.write("<name>" + at.getName() + "</name>\n");
       
    93                             out.write("<started>" + getTimeFormat().format(at.getStartTime()) + "</started>\n");
       
    94                             out.write("<finished>" + getTimeFormat().format(at.getEndTime()) + "</finished>\n");
       
    95                             out.write("</target>\n");
       
    96                         }
       
    97                     }
       
    98                 }
       
    99                 
       
   100                 out.write("</targets>\n");
       
   101                 out.close();
       
   102                 FileInputStream stream = new FileInputStream(tempFile);
       
   103                 log.debug("alltargetdiamondslistener file: " + tempFile);
       
   104                 mergeToFullResults(stream);
       
   105                 stream.close();
       
   106                 stream = new FileInputStream(tempFile); 
       
   107                 log.debug("diamondsclient: " + getDiamondsClient());
       
   108                 log.debug("diamondsclient: " + DiamondsConfig.getBuildId());
       
   109                 getDiamondsClient().sendData(stream, DiamondsConfig.getBuildId());
       
   110             }
       
   111         }
       
   112         catch (IOException e)
       
   113         {
       
   114             e.printStackTrace();
       
   115         }
       
   116     }
       
   117     
       
   118     class AntTarget {
       
   119         private String targetName;
       
   120         private Date startTime;
       
   121         private Date endTime;
       
   122         private int hashCode;
       
   123         
       
   124         public AntTarget(Target target)
       
   125         {
       
   126             targetName = target.getName();
       
   127             hashCode = target.hashCode();
       
   128             startTime = new Date();
       
   129         }
       
   130         public String getName() { return targetName; }
       
   131         public Date getStartTime() { return startTime; }
       
   132         public Date getEndTime() { return endTime; }
       
   133         public void setEndTime(Date e) { endTime = e; }
       
   134         public boolean equals(Object obj) { return obj.hashCode() == hashCode; }
       
   135         public int hashCode() { return hashCode; }
       
   136     }
       
   137 }