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.util.ArrayList;
|
587
|
21 |
import java.util.Date;
|
628
|
22 |
import java.util.Enumeration;
|
|
23 |
import java.util.HashMap;
|
|
24 |
import java.util.Iterator;
|
588
|
25 |
import java.util.LinkedHashMap;
|
587
|
26 |
import java.util.List;
|
|
27 |
import java.util.Map;
|
628
|
28 |
import java.util.Set;
|
587
|
29 |
import java.util.Vector;
|
628
|
30 |
|
587
|
31 |
import org.apache.log4j.Logger;
|
628
|
32 |
import org.apache.tools.ant.BuildEvent;
|
|
33 |
import org.apache.tools.ant.BuildException;
|
|
34 |
import org.apache.tools.ant.Project;
|
|
35 |
import org.apache.tools.ant.Target;
|
|
36 |
import com.nokia.helium.core.ant.types.Stage;
|
587
|
37 |
|
|
38 |
/**
|
628
|
39 |
* Diamonds client used to connect to get build id and also to send the build results
|
587
|
40 |
*
|
|
41 |
*/
|
|
42 |
public class StageDiamondsListener extends DiamondsListenerImpl {
|
|
43 |
|
|
44 |
private static final Date INVALID_DATE = new Date(-1);
|
|
45 |
|
628
|
46 |
private static Object mutexObject = new Object();;
|
|
47 |
|
587
|
48 |
private Logger log = Logger.getLogger(StageDiamondsListener.class);
|
628
|
49 |
|
587
|
50 |
private List<Map<String, Date>> stageTargetBeginList = new ArrayList<Map<String, Date>>();
|
|
51 |
|
628
|
52 |
private Map<String, List<Stage>> stageTargetEndMap = new HashMap<String, List<Stage>>();
|
587
|
53 |
|
588
|
54 |
private Map<String, String> stageStartTargetMap = new HashMap<String, String>();
|
|
55 |
|
|
56 |
private Map<String, Date> stageStartTargetTimeMap = new HashMap<String, Date>();
|
|
57 |
|
587
|
58 |
private boolean isTargetMapInitialized;
|
|
59 |
|
628
|
60 |
private Map<String, Stage> stages;
|
587
|
61 |
|
|
62 |
|
|
63 |
public StageDiamondsListener() {
|
|
64 |
stages = DiamondsConfig.getStages();
|
|
65 |
}
|
|
66 |
|
|
67 |
public void targetBegin(BuildEvent buildEvent) throws DiamondsException {
|
628
|
68 |
initDiamondsClient();
|
|
69 |
Project projectInStage = buildEvent.getProject();
|
|
70 |
int hashCode = projectInStage.hashCode();
|
587
|
71 |
String targetName = buildEvent.getTarget().getName();
|
|
72 |
if (!isTargetMapInitialized && stages != null) {
|
628
|
73 |
log.debug("diamonds:StageDiamondsListener: initializing for all stages.");
|
|
74 |
initStageTargetsMap(projectInStage);
|
587
|
75 |
isTargetMapInitialized = true;
|
|
76 |
}
|
588
|
77 |
String targetNameWithHashCode = targetName + "-" + hashCode;
|
|
78 |
log.debug("targetBegin: targetNameWithHashCode: " + targetNameWithHashCode);
|
628
|
79 |
log.debug("targetBegin targetName: " + targetName + " - currentStartTargetName:"
|
|
80 |
+ stageStartTargetMap.get(targetNameWithHashCode));
|
588
|
81 |
if (stageStartTargetMap.get(targetNameWithHashCode) == null) {
|
|
82 |
log.debug("looking for start target match and associating time to it");
|
|
83 |
findAndSetStartTimeForTargetInStageList(targetName, targetNameWithHashCode);
|
587
|
84 |
}
|
|
85 |
}
|
628
|
86 |
|
|
87 |
private Date getStartTime(Stage stage) {
|
|
88 |
String startTargetName = stage.getStartTarget();
|
|
89 |
for (Iterator<Map<String, Date>> listIter = stageTargetBeginList.iterator(); listIter.hasNext();) {
|
588
|
90 |
Map<String, Date> stageMap = listIter.next();
|
|
91 |
if (stageMap.get(startTargetName) != null) {
|
|
92 |
Set<String> targetSet = stageMap.keySet();
|
|
93 |
for (String key : targetSet) {
|
|
94 |
log.debug("key: " + key);
|
|
95 |
Date time = stageMap.get(key);
|
|
96 |
log.debug("time: " + time);
|
628
|
97 |
if (time != INVALID_DATE) {
|
588
|
98 |
return time;
|
|
99 |
}
|
|
100 |
}
|
|
101 |
}
|
|
102 |
}
|
628
|
103 |
throw new BuildException("No time recorded " + "for stage:" + stage.getStageName());
|
588
|
104 |
}
|
587
|
105 |
|
628
|
106 |
private void sendStageInfo(String targetName, int hashCode) throws DiamondsException {
|
|
107 |
List<Stage> stageList = stageTargetEndMap.get(targetName);
|
|
108 |
synchronized (mutexObject) {
|
|
109 |
if (stageList != null) {
|
|
110 |
for (Stage stage : stageList) {
|
|
111 |
if (stage != null) {
|
|
112 |
log.debug("stage.name: " + stage.getStageName());
|
|
113 |
log.debug("stage.start target name: " + stage.getStartTarget());
|
|
114 |
String currentStageTargetName = stageStartTargetMap.get(stage.getStartTarget()
|
|
115 |
+ "-" + hashCode);
|
|
116 |
log.debug("getStageBasedOnEndTarget: currentStargetTargetName" + currentStageTargetName);
|
|
117 |
if (currentStageTargetName != null) {
|
|
118 |
log.debug("stage in targetend: " + stage);
|
|
119 |
if (stage != null && getIsInitialized()) {
|
|
120 |
//initDiamondsClient();
|
|
121 |
String stageName = stage.getStageName();
|
|
122 |
log.debug("stageName in targetend: " + stageName);
|
|
123 |
String stageMessage = stageName + ".id";
|
|
124 |
sendMessage(stageMessage);
|
|
125 |
Date startTime = getStartTime(stage);
|
|
126 |
getProject().setProperty("logical.stage", stageName);
|
|
127 |
getProject().setProperty("stage.start.time", getTimeFormat().format(startTime));
|
|
128 |
getProject().setProperty("stage.end.time", getTimeFormat().format(new Date()));
|
|
129 |
sendMessage("stage.time.message");
|
|
130 |
}
|
|
131 |
}
|
|
132 |
}
|
587
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
628
|
138 |
@SuppressWarnings("unchecked")
|
|
139 |
public void targetEnd(BuildEvent buildEvent) throws DiamondsException {
|
|
140 |
String targetName = buildEvent.getTarget().getName();
|
|
141 |
Project prj = buildEvent.getProject();
|
|
142 |
int hashCode = prj.hashCode();
|
|
143 |
String targetNameWithHashCode = targetName + "-" + hashCode;
|
|
144 |
log.debug("targetEnd: targetNamewith-hashcode: " + targetNameWithHashCode);
|
|
145 |
sendStageInfo(targetName, hashCode);
|
|
146 |
}
|
|
147 |
|
588
|
148 |
private void findAndSetStartTimeForTargetInStageList(String targetName,
|
628
|
149 |
String targetNameWithHashCode) throws DiamondsException {
|
|
150 |
for (Iterator<Map<String, Date>> listIter = stageTargetBeginList.iterator(); listIter.hasNext();) {
|
587
|
151 |
Map<String, Date> stageMap = listIter.next();
|
|
152 |
Date targetTime = stageMap.get(targetName);
|
|
153 |
if (targetTime != null && targetTime.equals(INVALID_DATE)) {
|
588
|
154 |
log.debug("diamonds:StageDiamondsListener: started recording for stage-target-----: "
|
628
|
155 |
+ targetName);
|
588
|
156 |
log.debug("findtime: targetNamewith-hashcode: " + targetNameWithHashCode);
|
|
157 |
log.debug("findtime: time: " + new Date());
|
587
|
158 |
stageMap.put(targetName, new Date());
|
588
|
159 |
stageStartTargetMap.put(targetNameWithHashCode, targetName);
|
|
160 |
stageStartTargetTimeMap.put(targetNameWithHashCode, new Date());
|
587
|
161 |
}
|
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
@SuppressWarnings("unchecked")
|
628
|
166 |
private void initStageTargetsMap(Project projectInStage) {
|
|
167 |
for (String key : stages.keySet()) {
|
|
168 |
Stage stage = stages.get(key);
|
|
169 |
String startTargetName = stage.getStartTarget();
|
588
|
170 |
Map<String, Date> stageMap = new LinkedHashMap<String, Date>();
|
587
|
171 |
Vector<Target> arrayList = null;
|
|
172 |
try {
|
628
|
173 |
arrayList = projectInStage.topoSort(startTargetName, projectInStage.getTargets(), false);
|
|
174 |
}
|
|
175 |
catch (BuildException be) {
|
587
|
176 |
log.debug("Diamonds target missing: ", be);
|
|
177 |
}
|
|
178 |
if (arrayList != null) {
|
|
179 |
log.debug(" + Stage definition: " + stage.getStageName());
|
|
180 |
Enumeration<Target> targetEnum = arrayList.elements();
|
|
181 |
while (targetEnum.hasMoreElements()) {
|
|
182 |
// fast lookup
|
|
183 |
Target target = targetEnum.nextElement();
|
|
184 |
stageMap.put(target.getName(), INVALID_DATE);
|
|
185 |
log.debug(" - Start target: " + target.getName());
|
|
186 |
}
|
|
187 |
stageTargetBeginList.add(stageMap);
|
|
188 |
|
|
189 |
// stage end process
|
628
|
190 |
String endTargetName = stage.getEndTarget();
|
587
|
191 |
// fast lookup
|
628
|
192 |
List<Stage> existingStageList = stageTargetEndMap.get(endTargetName);
|
|
193 |
if (existingStageList == null) {
|
|
194 |
existingStageList = new ArrayList<Stage>();
|
|
195 |
stageTargetEndMap.put(endTargetName, existingStageList);
|
588
|
196 |
}
|
628
|
197 |
existingStageList.add(stage);
|
587
|
198 |
log.debug(" - End target: " + endTargetName);
|
|
199 |
}
|
|
200 |
}
|
|
201 |
}
|
|
202 |
} |