buildframework/helium/sf/java/diamonds/src/com/nokia/helium/diamonds/TargetDiamondsListener.java
changeset 628 7c4a911dc066
parent 587 85df38eb4012
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
     1 /*
     1 /*
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     5  * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description:  
    14  * Description:  
    15 *
    15  *
    16 */
    16  */
    17 
    17 
    18 package com.nokia.helium.diamonds;
    18 package com.nokia.helium.diamonds;
    19 
    19 
       
    20 import java.io.File;
       
    21 import java.io.FileInputStream;
       
    22 import java.io.IOException;
       
    23 import java.io.InputStream;
       
    24 import java.util.List;
       
    25 import java.util.Map;
       
    26 import org.apache.log4j.Logger;
    20 import org.apache.tools.ant.BuildEvent;
    27 import org.apache.tools.ant.BuildEvent;
       
    28 import org.apache.tools.ant.Project;
    21 import org.apache.tools.ant.Target;
    29 import org.apache.tools.ant.Target;
    22 import org.apache.tools.ant.Project;
    30 import com.nokia.helium.core.ant.types.TargetMessageTrigger;
    23 
    31 import com.nokia.helium.core.ant.Message;
    24 import java.util.ArrayList;
       
    25 import java.util.List;
       
    26 import java.util.Hashtable;
       
    27 import java.util.Map;
       
    28 import java.io.File;
       
    29 import org.apache.log4j.Logger;
       
    30 import com.nokia.helium.core.PropertiesSource;
       
    31 import com.nokia.helium.core.TemplateInputSource;
       
    32 import com.nokia.helium.core.XMLTemplateSource;
       
    33 
    32 
    34 /**
    33 /**
    35  * Listener sending data based on target configuration to diamonds.
    34  * Listener sending data based on target configuration to diamonds.
    36  */
    35  */
    37 public class TargetDiamondsListener extends DiamondsListenerImpl {
    36 public class TargetDiamondsListener extends DiamondsListenerImpl {
    38 
    37 
    39     private Project project;
    38 
    40     
       
    41     private Logger log = Logger.getLogger(TargetDiamondsListener.class);
    39     private Logger log = Logger.getLogger(TargetDiamondsListener.class);
    42 
    40 
    43     private Map<String, com.nokia.helium.diamonds.Target> targetsMap;
    41     private Map<String, TargetMessageTrigger> targetsMap;
    44 
    42 
    45     private String currentTarget;
    43     private String currentTarget;
    46 
    44 
    47     /**
    45     /**
    48      * Default constructor
    46      * Default constructor
    49      */
    47      */
    50     public TargetDiamondsListener() {
    48     public TargetDiamondsListener() {
    51         targetsMap = DiamondsConfig.getTargets();
    49         targetsMap = DiamondsConfig.getTargetsMap();
       
    50         for (String key : targetsMap.keySet()) {
       
    51             log.debug("target name: " + key);
       
    52         }
    52     }
    53     }
    53 
    54 
    54     private boolean isTargetsToExecute(BuildEvent buildEvent) {
    55     private boolean isTargetsToExecute(BuildEvent buildEvent) {
    55         project = buildEvent.getProject();
    56         Project projectInTarget = buildEvent.getProject();
    56         Target target = buildEvent.getTarget();
    57         Target target = buildEvent.getTarget();
    57         boolean retValue = false;
    58         boolean retValue = false;
    58         com.nokia.helium.diamonds.Target currentTarget = targetsMap
    59         log.debug("isTargetsToExecute: target:" + target.getName() );
    59                 .get(target.getName());
    60         TargetMessageTrigger targetInMap = targetsMap.get(target.getName());
    60         String currentTargetName = null;
    61         log.debug("isTargetsToExecute: targetInMap:" + targetInMap );
    61         if (currentTarget != null) {
    62         if (targetInMap != null) {
    62             currentTargetName = currentTarget.getTargetName();
    63             log.debug("target: " + target.getName());
    63         }
    64             log.debug("targetInMap: " + targetInMap);
    64         if (currentTargetName != null) {
    65             String targetNameInMap = targetInMap.getTargetName();
    65             String ifCondition = target.getIf();
    66             log.debug("targetNameInMap: " + targetInMap.getTargetName());
    66             String unlessCondition = target.getUnless();
    67             if (targetNameInMap != null) {
    67             if (ifCondition == null && unlessCondition == null) {
       
    68                 retValue = true;
    68                 retValue = true;
    69             } else {
    69                 String ifCondition = target.getIf();
    70                 String ifProperty = project.getProperty(ifCondition);
    70                 if ((ifCondition != null) && (projectInTarget.getProperty(
    71                 String unlessProperty = project.getProperty(unlessCondition);
    71                         projectInTarget.replaceProperties(ifCondition)) == null)) {
    72                 if (ifProperty != null || unlessProperty == null) {
    72                     retValue = false;
    73                     retValue = true;
    73                 }
       
    74                 String unlessCondition = target.getUnless();
       
    75                 if (unlessCondition != null && (projectInTarget.getProperty(
       
    76                         projectInTarget.replaceProperties(unlessCondition)) != null)) {
       
    77                     retValue = false;
    74                 }
    78                 }
    75             }
    79             }
    76         }
    80         }
    77         return retValue;
    81         return retValue;
    78     }
    82     }
    79 
    83 
    80     /**
    84     /**
    81      * Function to process logging info during beginning of target execution.
    85      * Function to process logging info during beginning of target execution. This checks that the
    82      * This checks that the current target execution is in config and requires
    86      * current target execution is in config and requires some data to be send it to diamonds.
    83      * some data to be send it to diamonds.
       
    84      * 
    87      * 
    85      * @param event
    88      * @param event of target execution.
    86      *            of target execution.
       
    87      */
    89      */
    88     public void targetBegin(BuildEvent buildEvent) throws DiamondsException {
    90     public void targetBegin(BuildEvent buildEvent) throws DiamondsException {
       
    91         initDiamondsClient();
    89         String targetName = buildEvent.getTarget().getName();
    92         String targetName = buildEvent.getTarget().getName();
    90         if (isTargetsToExecute(buildEvent)) {
    93         if (isTargetsToExecute(buildEvent)) {
    91             currentTarget = targetName;
    94             currentTarget = targetName;
    92             com.nokia.helium.diamonds.Target target = targetsMap
    95         }
    93                     .get(currentTarget);
    96     }
    94             if (!target.isDefer()) {
    97 
    95                 initDiamondsClient();
    98     /**
       
    99      * Function to process logging info during end of build. If the target in config, sends the data
       
   100      * to diamonds (uses the template conversion if needed).
       
   101      * 
       
   102      * @param event of target execution.
       
   103      */
       
   104     public void targetEnd(BuildEvent buildEvent) throws DiamondsException {
       
   105         String targetName = buildEvent.getTarget().getName();
       
   106         if (isTargetsToExecute(buildEvent)) {
       
   107             if (currentTarget != null && currentTarget.equals(targetName)) {
       
   108                 log.debug("targetEnd: " + targetName);
       
   109                 if (getIsInitialized()) {
       
   110                     log.debug("diamonds:TargetDiamondsListener:finished recording, sending data to diamonds for target: "
       
   111                         + buildEvent.getTarget().getName());
       
   112                     sendTargetData(buildEvent, buildEvent.getTarget().getProject());
       
   113                 }
       
   114                 currentTarget = null;
    96             }
   115             }
    97         }
   116         }
    98     }
   117     }
    99 
   118 
       
   119     private void sendData(InputStream stream) throws DiamondsException {
       
   120         String urlPath = DiamondsConfig.getBuildId();
       
   121         getDiamondsClient().sendData(stream, urlPath);
       
   122         log.debug("urlPath:" + urlPath);
       
   123     }
   100     /**
   124     /**
   101      * Function to process logging info during end of build. If the target in
   125      * Sends the data to diamonds. First it looks if the template with target name exists, then it
   102      * config, sends the data to diamonds (uses the template conversion if
   126      * looks for input source file from config, if ant properties required from config, it uses it
   103      * needed).
   127      * for template conversion. If no template file exists, sends the data directly.
   104      * 
   128      * 
   105      * @param event
   129      * @param event of target execution.
   106      *            of target execution.
       
   107      */
   130      */
   108     public void targetEnd(BuildEvent buildEvent) throws DiamondsException {
   131     private void sendTargetData(BuildEvent buildEvent, Project project) throws DiamondsException {
   109         String targetName = buildEvent.getTarget().getName();
   132         TargetMessageTrigger targetMap = targetsMap.get(currentTarget);
   110         if (currentTarget != null && currentTarget.equals(targetName)) {           
   133         if (targetMap != null) {
   111             if (getIsInitialized()) {
   134             
   112                 log
   135             List<Message> messageList = targetMap.getMessageList();
   113                 .debug("diamonds:TargetDiamondsListener:finished recording, sending data to diamonds for target: "
   136             for ( Message message : messageList ) {
   114                         + buildEvent.getTarget().getName());
   137                 try {
   115                 sendTargetData(buildEvent, buildEvent.getTarget().getProject());
   138                     File tempFile = streamToTempFile(message.getInputStream());
       
   139                     tempFile.deleteOnExit();
       
   140                     sendData(new FileInputStream(tempFile));
       
   141                     mergeToFullResults(new FileInputStream(tempFile));
       
   142                 } catch (IOException iex) {
       
   143                     throw new DiamondsException("error closing the stream while sending data");
       
   144                 }
       
   145                 catch (com.nokia.helium.core.MessageCreationException mex) {
       
   146                     log.debug("IOException while retriving message:", mex);
       
   147                     throw new DiamondsException("error during message retrival");
       
   148                 }
   116             }
   149             }
   117             currentTarget = null;
       
   118         }
   150         }
   119     }
   151     }
   120 
       
   121     /**
       
   122      * Sends the data to diamonds. First it looks if the template with target
       
   123      * name exists, then it looks for input source file from config, if ant
       
   124      * properties required from config, it uses it for template conversion. If
       
   125      * no template file exists, sends the data directly.
       
   126      * 
       
   127      * @param event
       
   128      *            of target execution.
       
   129      */
       
   130     @SuppressWarnings("unchecked")
       
   131     private void sendTargetData(BuildEvent buildEvent, Project project)
       
   132             throws DiamondsException {
       
   133         com.nokia.helium.diamonds.Target target = targetsMap
       
   134                 .get(currentTarget);
       
   135         String sourceFile = target.getSource();
       
   136         if (sourceFile == null) {
       
   137             sourceFile = getSourceFile(target.getTargetName());
       
   138         }
       
   139         Hashtable<String, String> antProperties = null;
       
   140         String targetTemplateFile = target.getTemplateFile();
       
   141         if (targetTemplateFile == null) {
       
   142             targetTemplateFile = target.getTargetName() + ".xml.ftl";
       
   143         }
       
   144         String output = DiamondsConfig.getOutputDir() + File.separator
       
   145                 + target.getTargetName() + ".xml";
       
   146         File templateFile = new File(DiamondsConfig.getTemplateDir(),
       
   147                 targetTemplateFile);
       
   148         List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>();
       
   149         if (sourceFile == null) {
       
   150             log
       
   151                     .debug("diamonds:TargetDiamondsListener:sourceFile not defined.");
       
   152         } else {
       
   153             sourceList.add(new XMLTemplateSource("doc", new File(sourceFile)));
       
   154         }
       
   155         // Ant?
       
   156         if (target.isAntPropertiesRequired()) {
       
   157             sourceList
       
   158                     .add(new PropertiesSource("ant", project.getProperties()));
       
   159         }
       
   160         try {
       
   161             if (templateFile.exists()) {
       
   162                 if (!(antProperties == null && sourceFile == null)) {
       
   163                     getTemplateProcessor().convertTemplate(DiamondsConfig
       
   164                             .getTemplateDir(), targetTemplateFile, output,
       
   165                             sourceList);
       
   166                 }
       
   167             } else {
       
   168                 log
       
   169                         .debug("sendTargetData: exists("
       
   170                                 + templateFile.getAbsolutePath() + ") => false");
       
   171 
       
   172             }
       
   173         } catch (com.nokia.helium.core.TemplateProcessorException e1) {
       
   174             throw new DiamondsException("template conversion error while sending data for target:"
       
   175                     + target + ":" + e1.getMessage());
       
   176         }
       
   177         File outputFile = new File(output);
       
   178         if (outputFile.exists()) {
       
   179             mergeToFullResults(outputFile);
       
   180             if (!target.isDefer()) {
       
   181                 getDiamondsClient().sendData(outputFile.getAbsolutePath(),
       
   182                         DiamondsConfig.getDiamondsProperties()
       
   183                                 .getDiamondsBuildID());
       
   184             } else {
       
   185                 log.debug("diamonds:TargetDiamondsListener:defer logging for: "
       
   186                         + outputFile);
       
   187                 getDeferLogList().add(output);
       
   188             }
       
   189         } else {
       
   190             log.debug("diamonds:TargetDiamondsListener:outputfile "
       
   191                     + outputFile + " does not exist");
       
   192         }
       
   193     }
       
   194 
       
   195 }
   152 }