587
|
1 |
/*
|
628
|
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 |
*/
|
587
|
17 |
|
|
18 |
package com.nokia.helium.diamonds;
|
|
19 |
|
628
|
20 |
import java.io.File;
|
|
21 |
import java.io.FileInputStream;
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.io.InputStream;
|
587
|
24 |
import java.util.List;
|
|
25 |
import java.util.Map;
|
|
26 |
import org.apache.log4j.Logger;
|
628
|
27 |
import org.apache.tools.ant.BuildEvent;
|
|
28 |
import org.apache.tools.ant.Project;
|
|
29 |
import org.apache.tools.ant.Target;
|
|
30 |
import com.nokia.helium.core.ant.types.TargetMessageTrigger;
|
|
31 |
import com.nokia.helium.core.ant.Message;
|
587
|
32 |
|
|
33 |
/**
|
|
34 |
* Listener sending data based on target configuration to diamonds.
|
|
35 |
*/
|
|
36 |
public class TargetDiamondsListener extends DiamondsListenerImpl {
|
|
37 |
|
628
|
38 |
|
587
|
39 |
private Logger log = Logger.getLogger(TargetDiamondsListener.class);
|
|
40 |
|
628
|
41 |
private Map<String, TargetMessageTrigger> targetsMap;
|
587
|
42 |
|
|
43 |
private String currentTarget;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Default constructor
|
|
47 |
*/
|
|
48 |
public TargetDiamondsListener() {
|
628
|
49 |
targetsMap = DiamondsConfig.getTargetsMap();
|
|
50 |
for (String key : targetsMap.keySet()) {
|
|
51 |
log.debug("target name: " + key);
|
|
52 |
}
|
587
|
53 |
}
|
|
54 |
|
|
55 |
private boolean isTargetsToExecute(BuildEvent buildEvent) {
|
628
|
56 |
Project projectInTarget = buildEvent.getProject();
|
587
|
57 |
Target target = buildEvent.getTarget();
|
|
58 |
boolean retValue = false;
|
628
|
59 |
log.debug("isTargetsToExecute: target:" + target.getName() );
|
|
60 |
TargetMessageTrigger targetInMap = targetsMap.get(target.getName());
|
|
61 |
log.debug("isTargetsToExecute: targetInMap:" + targetInMap );
|
|
62 |
if (targetInMap != null) {
|
|
63 |
log.debug("target: " + target.getName());
|
|
64 |
log.debug("targetInMap: " + targetInMap);
|
|
65 |
String targetNameInMap = targetInMap.getTargetName();
|
|
66 |
log.debug("targetNameInMap: " + targetInMap.getTargetName());
|
|
67 |
if (targetNameInMap != null) {
|
587
|
68 |
retValue = true;
|
628
|
69 |
String ifCondition = target.getIf();
|
|
70 |
if ((ifCondition != null) && (projectInTarget.getProperty(
|
|
71 |
projectInTarget.replaceProperties(ifCondition)) == null)) {
|
|
72 |
retValue = false;
|
|
73 |
}
|
|
74 |
String unlessCondition = target.getUnless();
|
|
75 |
if (unlessCondition != null && (projectInTarget.getProperty(
|
|
76 |
projectInTarget.replaceProperties(unlessCondition)) != null)) {
|
|
77 |
retValue = false;
|
587
|
78 |
}
|
|
79 |
}
|
|
80 |
}
|
|
81 |
return retValue;
|
|
82 |
}
|
|
83 |
|
|
84 |
/**
|
628
|
85 |
* Function to process logging info during beginning of target execution. This checks that the
|
|
86 |
* current target execution is in config and requires some data to be send it to diamonds.
|
587
|
87 |
*
|
628
|
88 |
* @param event of target execution.
|
587
|
89 |
*/
|
|
90 |
public void targetBegin(BuildEvent buildEvent) throws DiamondsException {
|
628
|
91 |
initDiamondsClient();
|
587
|
92 |
String targetName = buildEvent.getTarget().getName();
|
|
93 |
if (isTargetsToExecute(buildEvent)) {
|
|
94 |
currentTarget = targetName;
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
628
|
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).
|
587
|
101 |
*
|
628
|
102 |
* @param event of target execution.
|
587
|
103 |
*/
|
628
|
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());
|
587
|
113 |
}
|
628
|
114 |
currentTarget = null;
|
587
|
115 |
}
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
628
|
119 |
private void sendData(InputStream stream) throws DiamondsException {
|
|
120 |
String urlPath = DiamondsConfig.getBuildId();
|
|
121 |
getDiamondsClient().sendData(stream, urlPath);
|
|
122 |
log.debug("urlPath:" + urlPath);
|
|
123 |
}
|
|
124 |
/**
|
|
125 |
* Sends the data to diamonds. First it looks if the template with target name exists, then it
|
|
126 |
* looks for input source file from config, if ant properties required from config, it uses it
|
|
127 |
* for template conversion. If no template file exists, sends the data directly.
|
|
128 |
*
|
|
129 |
* @param event of target execution.
|
|
130 |
*/
|
|
131 |
private void sendTargetData(BuildEvent buildEvent, Project project) throws DiamondsException {
|
|
132 |
TargetMessageTrigger targetMap = targetsMap.get(currentTarget);
|
|
133 |
if (targetMap != null) {
|
|
134 |
|
|
135 |
List<Message> messageList = targetMap.getMessageList();
|
|
136 |
for ( Message message : messageList ) {
|
|
137 |
try {
|
|
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 |
}
|
|
149 |
}
|
|
150 |
}
|
|
151 |
}
|
587
|
152 |
} |