buildframework/helium/sf/java/diamonds/src/com/nokia/helium/diamonds/DiamondsListenerImpl.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
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
/*
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     3
 * All rights reserved.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     4
 * This component and the accompanying materials are made available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     6
 * which accompanies this distribution, and is available
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     8
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
     9
 * Initial Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    10
 * Nokia Corporation - initial contribution.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    11
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    12
 * Contributors:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    13
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    14
 * Description:  
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    15
 *
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    16
 */
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
package com.nokia.helium.diamonds;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
import org.apache.tools.ant.BuildEvent;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    21
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
import org.apache.tools.ant.Project;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import java.util.Date;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import java.io.File;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    25
import java.io.FileInputStream;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    26
import java.io.FileOutputStream;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
import java.io.IOException;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
import java.text.SimpleDateFormat;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
import java.util.ArrayList;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
import org.apache.log4j.Logger;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    31
import com.nokia.helium.core.EmailSendException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    32
import com.nokia.helium.core.ant.Message;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    33
import com.nokia.helium.core.MessageCreationException;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
import com.nokia.helium.diamonds.XMLMerger.XMLMergerException;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    35
import java.io.InputStream;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
/**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    38
 * Base diamonds logger implementation. The common implementation like initialization done here and
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    39
 * used by sub classes.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
public class DiamondsListenerImpl implements DiamondsListener {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    43
    private static ArrayList<InputStream> finalStreamList = new ArrayList<InputStream>();
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
    private static DiamondsClient diamondsClient;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
    private static boolean isInitialized;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
    private static ArrayList<String> deferLogList = new ArrayList<String>();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    51
    private static Logger log = Logger.getLogger(DiamondsListenerImpl.class);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    53
    private static Project project;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    54
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    55
    private static Object mutexObject = new Object();;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
    
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    57
    private static SimpleDateFormat timeFormat;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
     * Default constructor
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
    public DiamondsListenerImpl() {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    63
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    64
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    65
    public static void initialize(Project prj) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    66
        project = prj;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    67
        log.debug("buildbegin:" + project);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    68
        Date date = new Date();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    69
        timeFormat = new SimpleDateFormat(DiamondsConfig.getTimeFormat());
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    70
        log.debug("build.start.time:" + date);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    71
        project.setProperty("build.start.time", timeFormat.format(date));
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
     * Function to process logging info during end of build
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
     * 
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    77
     * @param event of target execution.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    79
    public final void buildBegin(BuildEvent buildEvent) throws DiamondsException {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
     * Function to process logging info during end of build
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
     * 
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    85
     * @param event of target execution.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
    @SuppressWarnings("unchecked")
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    88
    public void buildEnd(BuildEvent buildEvent) throws DiamondsException {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    89
        log.debug("build end: " + isInitialized());
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
        if (isInitialized()) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    91
            project.setProperty("build.end.time", timeFormat.format(new Date()));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    92
            sendMessage("final.message");
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
            isInitialized = false;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    94
            InputStream first = finalStreamList.remove(0);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
            try {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    96
                //printStreamContent(first);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    97
                File fullResultsFile = File.createTempFile("diamonds-full-results", ".xml");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    98
                XMLMerger merger = new XMLMerger(first, fullResultsFile);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    99
                String smtpServer = DiamondsConfig.getSMTPServer();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   100
                String ldapServer = DiamondsConfig.getLDAPServer();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   101
                for (InputStream stream : finalStreamList) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
                    try {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   103
                        merger.merge(stream);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   104
                    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   105
                    catch (XMLMerger.XMLMergerException xe) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
                        log.debug("Error during the merge: ", xe);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
                    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
                }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   109
                diamondsClient.sendDataByMail(fullResultsFile.getAbsolutePath(), smtpServer, ldapServer);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   110
            } catch (EmailSendException ese) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   111
                log.warn("Error occured while sending mail: " + ese.getMessage());
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   112
                
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
            } catch (IOException e) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   114
                log.error("Error sending diamonds final log: IOException", e);
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   115
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   116
            catch (XMLMergerException e) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   117
                log.error("Error sending diamonds final log: XMLMergerException ", e);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   118
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   119
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   120
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   121
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   122
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   123
     * Function to process logging info during begining of target execution
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   124
     * 
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   125
     * @param event of target execution.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   127
    public void targetBegin(BuildEvent buildEvent) throws DiamondsException {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   128
        initDiamondsClient();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   129
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   130
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   131
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   132
     * Function to process logging info during end of target execution
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   133
     * 
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   134
     * @param event of target execution.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   135
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   136
    public void targetEnd(BuildEvent buildEvent) throws DiamondsException {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   137
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   138
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   139
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   140
     * returns true if diamonds is already initialized for the build.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   141
     * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   142
     * @param true diamonds initialized otherwise false.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   143
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   144
    public static boolean isInitialized() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   145
        return isInitialized;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   146
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   147
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   148
    public static void mergeToFullResults(InputStream stream) throws DiamondsException {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   149
        finalStreamList.add(stream);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   150
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   151
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   152
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   153
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   154
     * Helper function to return the default project passed to messages.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   155
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   156
    static Project getProject() {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   157
        return project;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   158
    }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   159
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   160
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   161
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   162
    protected DiamondsClient getDiamondsClient() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   163
        return diamondsClient;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   164
    }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   165
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   166
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   167
    protected boolean getIsInitialized() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   168
        return isInitialized;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   169
    }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   170
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   171
    protected static SimpleDateFormat getTimeFormat() {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   172
        return timeFormat;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   173
    }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   174
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   175
    protected ArrayList<String> getDeferLogList() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   176
        return deferLogList;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   177
    }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   178
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   179
    protected static File streamToTempFile(InputStream stream) throws IOException {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   180
        File temp = File.createTempFile("diamonds", "xml");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   181
        FileOutputStream out = new FileOutputStream(temp);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   182
        int read = 0;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   183
        byte[] bytes = new byte[1024];
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   184
 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   185
        while ((read = stream.read(bytes)) != -1) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   186
            out.write(bytes, 0, read);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   187
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   188
        out.flush();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   189
        out.close();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   190
        stream.close();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   191
        return temp;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   192
    }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   193
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   194
    private static void sendMessage(Message message, String buildID) throws DiamondsException {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   195
        try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   196
            File tempFile = streamToTempFile(message.getInputStream());
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   197
            tempFile.deleteOnExit();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   198
            if (buildID == null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   199
                buildID = diamondsClient.getBuildId(new FileInputStream(tempFile));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   200
                if (buildID != null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   201
                    project.setProperty(DiamondsConfig.getBuildIdProperty(), buildID);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   202
                    log.info("got Build ID from diamonds:" + buildID);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   203
                }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   204
            } else {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   205
                diamondsClient.sendData(new FileInputStream(tempFile), buildID);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   206
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   207
            mergeToFullResults(new FileInputStream(tempFile));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   208
        } catch (IOException iex) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   209
            log.debug("IOException while retriving message:", iex);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   210
            throw new DiamondsException("IOException while retriving message");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   211
        } 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   212
        catch (MessageCreationException mex) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   213
            log.debug("IOException while retriving message:", mex);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   214
            throw new DiamondsException("error during message retrival");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   215
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   216
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   217
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   218
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   219
     * Send message to diamonds.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   220
     * @param messageId - id to look from in the ant config and to send it to diamonds.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   221
     *                    id is pointing to an fmpp message.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   222
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   223
    public static void sendMessage(String messageId) throws DiamondsException {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   224
        log.debug("send-message:" + messageId);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   225
        synchronized (mutexObject) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   226
            String buildID = project.getProperty(DiamondsConfig.getBuildIdProperty());
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   227
            Object obj = project.getReference(messageId);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   228
            if (obj != null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   229
                if (obj instanceof Message) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   230
                    sendMessage((Message)obj, buildID);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   231
                }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   232
            } else {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   233
                log.debug("Message not sent for message id: " + messageId);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   234
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   235
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   236
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   237
    
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   238
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   239
     * Initializes the diamonds client and sends the initial data
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   240
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   241
    @SuppressWarnings("unchecked")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   242
    protected void initDiamondsClient() throws DiamondsException {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   243
        if (!isInitialized) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   244
            diamondsClient = new DiamondsClient(DiamondsConfig.getHost(), 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   245
                    DiamondsConfig.getPort(), 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   246
                    DiamondsConfig.getPath(), 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   247
                    DiamondsConfig.getMailInfo());
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   248
            String buildID = project.getProperty(DiamondsConfig.getBuildIdProperty());
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   249
            if (buildID == null ) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   250
                sendMessage("initial.message");
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   251
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   252
            isInitialized = true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   253
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   254
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   255
}