buildframework/helium/sf/java/diamonds/src/com/nokia/helium/diamonds/TargetDiamondsListener.java
changeset 628 7c4a911dc066
parent 587 85df38eb4012
--- a/buildframework/helium/sf/java/diamonds/src/com/nokia/helium/diamonds/TargetDiamondsListener.java	Wed Jun 16 16:51:40 2010 +0300
+++ b/buildframework/helium/sf/java/diamonds/src/com/nokia/helium/diamonds/TargetDiamondsListener.java	Fri Aug 13 14:59:05 2010 +0300
@@ -1,46 +1,44 @@
 /*
-* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
-* All rights reserved.
-* This component and the accompanying materials are made available
-* under the terms of the License "Eclipse Public License v1.0"
-* which accompanies this distribution, and is available
-* at the URL "http://www.eclipse.org/legal/epl-v10.html".
-*
-* Initial Contributors:
-* Nokia Corporation - initial contribution.
-*
-* Contributors:
-*
-* Description:  
-*
-*/
+ * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of the License "Eclipse Public License v1.0"
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
+ *
+ * Initial Contributors:
+ * Nokia Corporation - initial contribution.
+ *
+ * Contributors:
+ *
+ * Description:  
+ *
+ */
 
 package com.nokia.helium.diamonds;
 
-import org.apache.tools.ant.BuildEvent;
-import org.apache.tools.ant.Target;
-import org.apache.tools.ant.Project;
-
-import java.util.ArrayList;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
-import java.util.Hashtable;
 import java.util.Map;
-import java.io.File;
 import org.apache.log4j.Logger;
-import com.nokia.helium.core.PropertiesSource;
-import com.nokia.helium.core.TemplateInputSource;
-import com.nokia.helium.core.XMLTemplateSource;
+import org.apache.tools.ant.BuildEvent;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Target;
+import com.nokia.helium.core.ant.types.TargetMessageTrigger;
+import com.nokia.helium.core.ant.Message;
 
 /**
  * Listener sending data based on target configuration to diamonds.
  */
 public class TargetDiamondsListener extends DiamondsListenerImpl {
 
-    private Project project;
-    
+
     private Logger log = Logger.getLogger(TargetDiamondsListener.class);
 
-    private Map<String, com.nokia.helium.diamonds.Target> targetsMap;
+    private Map<String, TargetMessageTrigger> targetsMap;
 
     private String currentTarget;
 
@@ -48,29 +46,35 @@
      * Default constructor
      */
     public TargetDiamondsListener() {
-        targetsMap = DiamondsConfig.getTargets();
+        targetsMap = DiamondsConfig.getTargetsMap();
+        for (String key : targetsMap.keySet()) {
+            log.debug("target name: " + key);
+        }
     }
 
     private boolean isTargetsToExecute(BuildEvent buildEvent) {
-        project = buildEvent.getProject();
+        Project projectInTarget = buildEvent.getProject();
         Target target = buildEvent.getTarget();
         boolean retValue = false;
-        com.nokia.helium.diamonds.Target currentTarget = targetsMap
-                .get(target.getName());
-        String currentTargetName = null;
-        if (currentTarget != null) {
-            currentTargetName = currentTarget.getTargetName();
-        }
-        if (currentTargetName != null) {
-            String ifCondition = target.getIf();
-            String unlessCondition = target.getUnless();
-            if (ifCondition == null && unlessCondition == null) {
+        log.debug("isTargetsToExecute: target:" + target.getName() );
+        TargetMessageTrigger targetInMap = targetsMap.get(target.getName());
+        log.debug("isTargetsToExecute: targetInMap:" + targetInMap );
+        if (targetInMap != null) {
+            log.debug("target: " + target.getName());
+            log.debug("targetInMap: " + targetInMap);
+            String targetNameInMap = targetInMap.getTargetName();
+            log.debug("targetNameInMap: " + targetInMap.getTargetName());
+            if (targetNameInMap != null) {
                 retValue = true;
-            } else {
-                String ifProperty = project.getProperty(ifCondition);
-                String unlessProperty = project.getProperty(unlessCondition);
-                if (ifProperty != null || unlessProperty == null) {
-                    retValue = true;
+                String ifCondition = target.getIf();
+                if ((ifCondition != null) && (projectInTarget.getProperty(
+                        projectInTarget.replaceProperties(ifCondition)) == null)) {
+                    retValue = false;
+                }
+                String unlessCondition = target.getUnless();
+                if (unlessCondition != null && (projectInTarget.getProperty(
+                        projectInTarget.replaceProperties(unlessCondition)) != null)) {
+                    retValue = false;
                 }
             }
         }
@@ -78,118 +82,71 @@
     }
 
     /**
-     * Function to process logging info during beginning of target execution.
-     * This checks that the current target execution is in config and requires
-     * some data to be send it to diamonds.
+     * Function to process logging info during beginning of target execution. This checks that the
+     * current target execution is in config and requires some data to be send it to diamonds.
      * 
-     * @param event
-     *            of target execution.
+     * @param event of target execution.
      */
     public void targetBegin(BuildEvent buildEvent) throws DiamondsException {
+        initDiamondsClient();
         String targetName = buildEvent.getTarget().getName();
         if (isTargetsToExecute(buildEvent)) {
             currentTarget = targetName;
-            com.nokia.helium.diamonds.Target target = targetsMap
-                    .get(currentTarget);
-            if (!target.isDefer()) {
-                initDiamondsClient();
-            }
-        }
-    }
-
-    /**
-     * Function to process logging info during end of build. If the target in
-     * config, sends the data to diamonds (uses the template conversion if
-     * needed).
-     * 
-     * @param event
-     *            of target execution.
-     */
-    public void targetEnd(BuildEvent buildEvent) throws DiamondsException {
-        String targetName = buildEvent.getTarget().getName();
-        if (currentTarget != null && currentTarget.equals(targetName)) {           
-            if (getIsInitialized()) {
-                log
-                .debug("diamonds:TargetDiamondsListener:finished recording, sending data to diamonds for target: "
-                        + buildEvent.getTarget().getName());
-                sendTargetData(buildEvent, buildEvent.getTarget().getProject());
-            }
-            currentTarget = null;
         }
     }
 
     /**
-     * Sends the data to diamonds. First it looks if the template with target
-     * name exists, then it looks for input source file from config, if ant
-     * properties required from config, it uses it for template conversion. If
-     * no template file exists, sends the data directly.
+     * Function to process logging info during end of build. If the target in config, sends the data
+     * to diamonds (uses the template conversion if needed).
      * 
-     * @param event
-     *            of target execution.
+     * @param event of target execution.
      */
-    @SuppressWarnings("unchecked")
-    private void sendTargetData(BuildEvent buildEvent, Project project)
-            throws DiamondsException {
-        com.nokia.helium.diamonds.Target target = targetsMap
-                .get(currentTarget);
-        String sourceFile = target.getSource();
-        if (sourceFile == null) {
-            sourceFile = getSourceFile(target.getTargetName());
-        }
-        Hashtable<String, String> antProperties = null;
-        String targetTemplateFile = target.getTemplateFile();
-        if (targetTemplateFile == null) {
-            targetTemplateFile = target.getTargetName() + ".xml.ftl";
-        }
-        String output = DiamondsConfig.getOutputDir() + File.separator
-                + target.getTargetName() + ".xml";
-        File templateFile = new File(DiamondsConfig.getTemplateDir(),
-                targetTemplateFile);
-        List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>();
-        if (sourceFile == null) {
-            log
-                    .debug("diamonds:TargetDiamondsListener:sourceFile not defined.");
-        } else {
-            sourceList.add(new XMLTemplateSource("doc", new File(sourceFile)));
-        }
-        // Ant?
-        if (target.isAntPropertiesRequired()) {
-            sourceList
-                    .add(new PropertiesSource("ant", project.getProperties()));
-        }
-        try {
-            if (templateFile.exists()) {
-                if (!(antProperties == null && sourceFile == null)) {
-                    getTemplateProcessor().convertTemplate(DiamondsConfig
-                            .getTemplateDir(), targetTemplateFile, output,
-                            sourceList);
+    public void targetEnd(BuildEvent buildEvent) throws DiamondsException {
+        String targetName = buildEvent.getTarget().getName();
+        if (isTargetsToExecute(buildEvent)) {
+            if (currentTarget != null && currentTarget.equals(targetName)) {
+                log.debug("targetEnd: " + targetName);
+                if (getIsInitialized()) {
+                    log.debug("diamonds:TargetDiamondsListener:finished recording, sending data to diamonds for target: "
+                        + buildEvent.getTarget().getName());
+                    sendTargetData(buildEvent, buildEvent.getTarget().getProject());
                 }
-            } else {
-                log
-                        .debug("sendTargetData: exists("
-                                + templateFile.getAbsolutePath() + ") => false");
-
+                currentTarget = null;
             }
-        } catch (com.nokia.helium.core.TemplateProcessorException e1) {
-            throw new DiamondsException("template conversion error while sending data for target:"
-                    + target + ":" + e1.getMessage());
-        }
-        File outputFile = new File(output);
-        if (outputFile.exists()) {
-            mergeToFullResults(outputFile);
-            if (!target.isDefer()) {
-                getDiamondsClient().sendData(outputFile.getAbsolutePath(),
-                        DiamondsConfig.getDiamondsProperties()
-                                .getDiamondsBuildID());
-            } else {
-                log.debug("diamonds:TargetDiamondsListener:defer logging for: "
-                        + outputFile);
-                getDeferLogList().add(output);
-            }
-        } else {
-            log.debug("diamonds:TargetDiamondsListener:outputfile "
-                    + outputFile + " does not exist");
         }
     }
 
+    private void sendData(InputStream stream) throws DiamondsException {
+        String urlPath = DiamondsConfig.getBuildId();
+        getDiamondsClient().sendData(stream, urlPath);
+        log.debug("urlPath:" + urlPath);
+    }
+    /**
+     * Sends the data to diamonds. First it looks if the template with target name exists, then it
+     * looks for input source file from config, if ant properties required from config, it uses it
+     * for template conversion. If no template file exists, sends the data directly.
+     * 
+     * @param event of target execution.
+     */
+    private void sendTargetData(BuildEvent buildEvent, Project project) throws DiamondsException {
+        TargetMessageTrigger targetMap = targetsMap.get(currentTarget);
+        if (targetMap != null) {
+            
+            List<Message> messageList = targetMap.getMessageList();
+            for ( Message message : messageList ) {
+                try {
+                    File tempFile = streamToTempFile(message.getInputStream());
+                    tempFile.deleteOnExit();
+                    sendData(new FileInputStream(tempFile));
+                    mergeToFullResults(new FileInputStream(tempFile));
+                } catch (IOException iex) {
+                    throw new DiamondsException("error closing the stream while sending data");
+                }
+                catch (com.nokia.helium.core.MessageCreationException mex) {
+                    log.debug("IOException while retriving message:", mex);
+                    throw new DiamondsException("error during message retrival");
+                }
+            }
+        }
+    }
 }
\ No newline at end of file