buildframework/helium/sf/java/diamonds/src/com/nokia/helium/diamonds/ant/HeliumListener.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    17 
    17 
    18 
    18 
    19 package com.nokia.helium.diamonds.ant;
    19 package com.nokia.helium.diamonds.ant;
    20 
    20 
    21 import java.io.File;
    21 import java.io.File;
    22 import java.util.Hashtable;
    22 import java.util.ArrayList;
    23 import java.util.List;
    23 import java.util.List;
    24 import java.util.ArrayList;
    24 
    25 import java.io.IOException;
    25 import org.apache.log4j.Logger;
       
    26 import org.apache.tools.ant.BuildEvent;
    26 import org.apache.tools.ant.BuildListener;
    27 import org.apache.tools.ant.BuildListener;
    27 import org.apache.tools.ant.BuildException;
       
    28 import com.nokia.helium.diamonds.*;
       
    29 import com.nokia.helium.core.PropertiesSource;
       
    30 import com.nokia.helium.core.TemplateProcessor;
       
    31 
       
    32 import org.apache.tools.ant.BuildEvent;
       
    33 import org.apache.tools.ant.Project;
    28 import org.apache.tools.ant.Project;
    34 import org.apache.log4j.Logger;
    29 
       
    30 import com.nokia.helium.diamonds.AllTargetDiamondsListener;
       
    31 import com.nokia.helium.diamonds.DiamondsConfig;
       
    32 import com.nokia.helium.diamonds.DiamondsException;
       
    33 import com.nokia.helium.diamonds.DiamondsListener;
       
    34 import com.nokia.helium.diamonds.DiamondsListenerImpl;
       
    35 import com.nokia.helium.diamonds.StageDiamondsListener;
       
    36 import com.nokia.helium.diamonds.TargetDiamondsListener;
    35 
    37 
    36 /**
    38 /**
    37  * Listener class that can connect to Ant and log information regarding to build
    39  * Listener class that can connect to Ant and log information regarding to build
    38  * times, number of errors and such. Data is sent to Diamonds server, where it
    40  * times, number of errors and such. Data is sent to Diamonds server, where it
    39  * is processed further.
    41  * is processed further.
    55 
    57 
    56     private Project project;
    58     private Project project;
    57 
    59 
    58     private boolean isInitialized;
    60     private boolean isInitialized;
    59 
    61 
    60     private boolean skipDiamonds;
    62     private boolean skipDiamonds ;
    61     private boolean skipDiamondsSet;
    63     private boolean skipDiamondsSet;
    62 
    64 
    63     /**
    65     /**
    64      * Default constructor.
    66      * Default constructor.
    65      */
    67      */
    77     /**
    79     /**
    78      * Triggered when a target starts.
    80      * Triggered when a target starts.
    79      */
    81      */
    80     @SuppressWarnings("unchecked") 
    82     @SuppressWarnings("unchecked") 
    81     public void targetStarted(BuildEvent event) {
    83     public void targetStarted(BuildEvent event) {
       
    84         Project prj = event.getProject();
    82         String targetName = event.getTarget().getName();
    85         String targetName = event.getTarget().getName();
    83         project = event.getProject();
    86 
    84 
    87         String diamondsEnabled = prj.getProperty("diamonds.enabled");
    85         String skip = project.getProperty("skip.diamonds");
    88         String skip = prj.getProperty("skip.diamonds");
    86         if (skip != null && skip.equals("true")) {
    89         log.debug("diamondsenabled: " + diamondsEnabled);
    87             skipDiamonds = true;
    90         log.debug("skip: " + skip);
    88         }
    91         if (!isInitialized) {
       
    92             if (diamondsEnabled != null && !Project.toBoolean(diamondsEnabled)) {
       
    93                 log.info("'diamonds.enabled' is not true, to use diamonds set 'diamonds.enabled' to 'true'.");
       
    94                 skipDiamonds = true;
       
    95                 isInitialized = true;
       
    96             } else if (skip != null && Project.toBoolean(skip)) {
       
    97                 log.info("'skip.diamonds' is deprecated. Please consider using 'diamonds.enabled'.");
       
    98                 skipDiamonds = true;
       
    99                 isInitialized = true;
       
   100             }
       
   101         }
       
   102         
    89         try {
   103         try {
    90             if (!skipDiamonds) {
   104             if (!skipDiamonds) {
    91                 if (!isInitialized) {
   105                 if (!isInitialized) {
    92                     String configFile = project
   106                     /**
    93                             .getProperty("diamonds.listener.configuration.file");
   107                      * Initialize Diamonds if and only if initializer-target-name has been called
    94                     parseConfig(configFile, project.getProperties());
   108                      */
    95                     isInitialized = true;
   109                     String buildID = prj.getProperty(DiamondsConfig.getBuildIdProperty());
    96                 }
   110                     log.debug("targetStarted:buildid:" + buildID);
    97                 DiamondsProperties diamondProperties = DiamondsConfig
   111                     String initializerTargetName = prj.getProperty(DiamondsConfig.getInitializerTargetProperty());
    98                         .getDiamondsProperties();
   112                     log.debug("initializerTargetName:" + initializerTargetName);
    99 
   113                     log.debug("targetName:" + targetName);
   100                 /**
   114                     if ( buildID != null || (initializerTargetName != null && targetName.equals(initializerTargetName))) {
   101                  * Initialize Diamonds if and only if initializer-target-name has been called
   115                         isInitialized = true;
   102                  */
   116                         project = prj;
   103                 if (targetName.equals(DiamondsConfig.getInitialiserTargetName())) {                                        
   117                         log.debug("trying to initialize diamonds config");
   104                     String categoryName = diamondProperties.getProperty("category-property");
   118                         DiamondsConfig.initialize(project);
   105                     String category = project.getProperty(categoryName);
   119                         DiamondsListenerImpl.initialize(project);
   106                     log.debug("category:" + category);
   120                         String category = DiamondsConfig.getCategory();
   107                     if (category != null && diamondsListeners.isEmpty()) {
   121                         log.debug("category:" + category);
   108                         addListeners(event);
   122                         if (category != null && diamondsListeners.isEmpty()) {
   109                         log.info("Diamonds enabled");
   123                             addListeners(event);
       
   124                             log.info("Diamonds enabled");
       
   125                         }
   110                     }
   126                     }
   111                 }
   127                 }
   112             } else {
   128             } else {
   113                 if (!skipDiamondsSet && skipDiamonds)
   129                 if (!skipDiamondsSet && skipDiamonds)
   114                 {
   130                 {
   115                     log.info("skip.diamonds set, to use diamonds don't set skip.diamonds.");
       
   116                     skipDiamondsSet = true;
   131                     skipDiamondsSet = true;
   117                 }
   132                 }
   118             }
   133             }
   119         } catch (DiamondsException ex) {
   134         } catch (DiamondsException ex) {
   120             log.debug("Diamonds error: ", ex);
   135             log.debug("Diamonds error: ", ex);
   141                 }
   156                 }
   142             }
   157             }
   143         }
   158         }
   144     }
   159     }
   145 
   160 
   146     @SuppressWarnings("unchecked") 
       
   147     private void parseConfig(String configFile, Hashtable<String, String> antProperties) {
       
   148         TemplateProcessor templateProcessor = new TemplateProcessor();
       
   149         File outputFile = null;
       
   150         try {
       
   151             outputFile = File.createTempFile("diamonds", "-config.xml");
       
   152             outputFile.deleteOnExit();
       
   153             log
       
   154                     .debug("Preprocessing the diamonds configuration: "
       
   155                             + configFile);
       
   156             List sourceList = new ArrayList();
       
   157             sourceList.add(new PropertiesSource("ant", antProperties));
       
   158             templateProcessor.convertTemplate(configFile,
       
   159                     outputFile.toString(), sourceList);
       
   160         } catch (IOException e) {
       
   161             throw new BuildException(
       
   162                     "Diamonds configuration pre-parsing error: "
       
   163                             + e.getMessage());
       
   164         }
       
   165         try {
       
   166             DiamondsConfig.parseConfiguration(outputFile.toString());
       
   167         } catch (DiamondsException e) {
       
   168             throw new BuildException("Diamonds configuration parsing error: "
       
   169                     + e.getMessage());
       
   170         }
       
   171     }
       
   172 
       
   173     private void addListeners(BuildEvent event) throws DiamondsException {
   161     private void addListeners(BuildEvent event) throws DiamondsException {
   174         if (DiamondsConfig.isStagesInConfig()) {
   162         if (DiamondsConfig.isStagesInConfig()) {
   175             StageDiamondsListener stageListener = new StageDiamondsListener();
   163             StageDiamondsListener stageListener = new StageDiamondsListener();
   176             diamondsListeners.add(stageListener);
   164             diamondsListeners.add(stageListener);
   177             stageListener.buildBegin(event);
   165             stageListener.buildBegin(event);
   179         if (DiamondsConfig.isTargetsInConfig()) {
   167         if (DiamondsConfig.isTargetsInConfig()) {
   180             TargetDiamondsListener targetListener = new TargetDiamondsListener();
   168             TargetDiamondsListener targetListener = new TargetDiamondsListener();
   181             diamondsListeners.add(targetListener);
   169             diamondsListeners.add(targetListener);
   182             targetListener.buildBegin(event);
   170             targetListener.buildBegin(event);
   183         }
   171         }
       
   172         
       
   173         AllTargetDiamondsListener allTargetListener = new AllTargetDiamondsListener();
       
   174         diamondsListeners.add(allTargetListener);
       
   175         allTargetListener.buildBegin(event);
   184     }
   176     }
   185 
   177 
   186     /**
   178     /**
   187      * Triggered when a target finishes.
   179      * Triggered when a target finishes.
   188      */
   180      */
   228      */
   220      */
   229     private void cleanup() {
   221     private void cleanup() {
   230         String loggingoutputfile = project.getProperty("logging.output.file");
   222         String loggingoutputfile = project.getProperty("logging.output.file");
   231         if (loggingoutputfile != null) {
   223         if (loggingoutputfile != null) {
   232             File file = new File(loggingoutputfile);
   224             File file = new File(loggingoutputfile);
   233             if (file.exists())
   225             if (file.exists()) {
   234                 file.delete();
   226                 file.delete();
       
   227             }
   235         }
   228         }
   236     }
   229     }
   237 
   230 
   238     /**
   231     /**
   239      * Triggered when a task starts.
   232      * Triggered when a task starts.