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.util.ArrayList; |
|
21 import java.util.Date; |
|
22 import java.util.Enumeration; |
|
23 import java.util.HashMap; |
|
24 import java.util.Iterator; |
|
25 import java.util.LinkedHashMap; |
|
26 import java.util.List; |
|
27 import java.util.Map; |
|
28 import java.util.Set; |
|
29 import java.util.Vector; |
|
30 |
|
31 import org.apache.log4j.Logger; |
20 import org.apache.tools.ant.BuildEvent; |
32 import org.apache.tools.ant.BuildEvent; |
21 |
33 import org.apache.tools.ant.BuildException; |
22 |
34 import org.apache.tools.ant.Project; |
23 import org.apache.tools.ant.Target; |
35 import org.apache.tools.ant.Target; |
24 import org.apache.tools.ant.Project; |
36 import com.nokia.helium.core.ant.types.Stage; |
25 import org.apache.tools.ant.BuildException; |
|
26 import java.util.Date; |
|
27 import java.util.LinkedHashMap; |
|
28 import java.util.Set; |
|
29 import java.util.HashMap; |
|
30 import java.util.List; |
|
31 import java.util.Enumeration; |
|
32 import java.util.Map; |
|
33 import java.io.File; |
|
34 import java.util.Iterator; |
|
35 import java.util.Vector; |
|
36 import java.util.ArrayList; |
|
37 import org.apache.log4j.Logger; |
|
38 import com.nokia.helium.core.PropertiesSource; |
|
39 import com.nokia.helium.core.TemplateInputSource; |
|
40 import com.nokia.helium.core.XMLTemplateSource; |
|
41 |
37 |
42 /** |
38 /** |
43 * Diamonds client used to connect to get build id and also to send the build |
39 * Diamonds client used to connect to get build id and also to send the build results |
44 * results |
|
45 * |
40 * |
46 */ |
41 */ |
47 public class StageDiamondsListener extends DiamondsListenerImpl { |
42 public class StageDiamondsListener extends DiamondsListenerImpl { |
48 |
43 |
49 private static final Date INVALID_DATE = new Date(-1); |
44 private static final Date INVALID_DATE = new Date(-1); |
50 |
45 |
|
46 private static Object mutexObject = new Object();; |
|
47 |
51 private Logger log = Logger.getLogger(StageDiamondsListener.class); |
48 private Logger log = Logger.getLogger(StageDiamondsListener.class); |
52 |
49 |
53 private List<Map<String, Date>> stageTargetBeginList = new ArrayList<Map<String, Date>>(); |
50 private List<Map<String, Date>> stageTargetBeginList = new ArrayList<Map<String, Date>>(); |
54 |
51 |
55 private Map<String, Stage> stageTargetEndMap = new HashMap<String, Stage>(); |
52 private Map<String, List<Stage>> stageTargetEndMap = new HashMap<String, List<Stage>>(); |
56 |
53 |
57 private Map<String, String> stageStartTargetMap = new HashMap<String, String>(); |
54 private Map<String, String> stageStartTargetMap = new HashMap<String, String>(); |
58 |
55 |
59 private Map<String, Date> stageStartTargetTimeMap = new HashMap<String, Date>(); |
56 private Map<String, Date> stageStartTargetTimeMap = new HashMap<String, Date>(); |
60 |
57 |
61 private boolean isTargetMapInitialized; |
58 private boolean isTargetMapInitialized; |
62 |
59 |
63 private Project project; |
60 private Map<String, Stage> stages; |
64 |
|
65 private List<Stage> stages; |
|
66 |
61 |
67 |
62 |
68 public StageDiamondsListener() { |
63 public StageDiamondsListener() { |
69 stages = DiamondsConfig.getStages(); |
64 stages = DiamondsConfig.getStages(); |
70 } |
65 } |
71 |
66 |
72 public void targetBegin(BuildEvent buildEvent) throws DiamondsException { |
67 public void targetBegin(BuildEvent buildEvent) throws DiamondsException { |
73 project = buildEvent.getProject(); |
68 initDiamondsClient(); |
74 int hashCode = project.hashCode(); |
69 Project projectInStage = buildEvent.getProject(); |
|
70 int hashCode = projectInStage.hashCode(); |
75 String targetName = buildEvent.getTarget().getName(); |
71 String targetName = buildEvent.getTarget().getName(); |
76 if (!isTargetMapInitialized && stages != null) { |
72 if (!isTargetMapInitialized && stages != null) { |
77 log |
73 log.debug("diamonds:StageDiamondsListener: initializing for all stages."); |
78 .debug("diamonds:StageDiamondsListener: initializing for all stages."); |
74 initStageTargetsMap(projectInStage); |
79 initStageTargetsMap(); |
|
80 isTargetMapInitialized = true; |
75 isTargetMapInitialized = true; |
81 } |
76 } |
82 String targetNameWithHashCode = targetName + "-" + hashCode; |
77 String targetNameWithHashCode = targetName + "-" + hashCode; |
83 log.debug("targetBegin: targetNameWithHashCode: " + targetNameWithHashCode); |
78 log.debug("targetBegin: targetNameWithHashCode: " + targetNameWithHashCode); |
84 log.debug("targetBegin targetName: " + targetName + |
79 log.debug("targetBegin targetName: " + targetName + " - currentStartTargetName:" |
85 " - currentStartTargetName:" + stageStartTargetMap.get(targetNameWithHashCode)); |
80 + stageStartTargetMap.get(targetNameWithHashCode)); |
86 if (stageStartTargetMap.get(targetNameWithHashCode) == null) { |
81 if (stageStartTargetMap.get(targetNameWithHashCode) == null) { |
87 log.debug("looking for start target match and associating time to it"); |
82 log.debug("looking for start target match and associating time to it"); |
88 findAndSetStartTimeForTargetInStageList(targetName, targetNameWithHashCode); |
83 findAndSetStartTimeForTargetInStageList(targetName, targetNameWithHashCode); |
89 } |
84 } |
90 } |
85 } |
91 |
86 |
92 private Date getStartTime(Stage stage, int hasCode) { |
87 private Date getStartTime(Stage stage) { |
93 String startTargetName = stage.getStartTargetName(); |
88 String startTargetName = stage.getStartTarget(); |
94 String endTargetName = stage.getEndTargetName(); |
89 for (Iterator<Map<String, Date>> listIter = stageTargetBeginList.iterator(); listIter.hasNext();) { |
95 for (Iterator<Map<String, Date>> listIter = stageTargetBeginList.iterator(); listIter |
|
96 .hasNext();) { |
|
97 Map<String, Date> stageMap = listIter.next(); |
90 Map<String, Date> stageMap = listIter.next(); |
98 if (stageMap.get(startTargetName) != null) { |
91 if (stageMap.get(startTargetName) != null) { |
99 Set<String> targetSet = stageMap.keySet(); |
92 Set<String> targetSet = stageMap.keySet(); |
100 for (String key : targetSet) { |
93 for (String key : targetSet) { |
101 log.debug("key: " + key); |
94 log.debug("key: " + key); |
102 Date time = stageMap.get(key); |
95 Date time = stageMap.get(key); |
103 log.debug("time: " + time); |
96 log.debug("time: " + time); |
104 if ( time != INVALID_DATE) { |
97 if (time != INVALID_DATE) { |
105 return time; |
98 return time; |
106 } |
99 } |
107 } |
100 } |
108 } |
101 } |
109 } |
102 } |
110 throw new BuildException("No time recorded " + |
103 throw new BuildException("No time recorded " + "for stage:" + stage.getStageName()); |
111 "for stage:" + stage.getStageName()); |
104 } |
112 } |
105 |
113 |
106 private void sendStageInfo(String targetName, int hashCode) throws DiamondsException { |
114 private Stage getStageBasedOnEndTarget(String targetName, int hashCode) { |
107 List<Stage> stageList = stageTargetEndMap.get(targetName); |
115 Stage stage = stageTargetEndMap.get(targetName); |
108 synchronized (mutexObject) { |
116 if (stage != null) { |
109 if (stageList != null) { |
117 String currentStageTargetName = stageStartTargetMap.get( |
110 for (Stage stage : stageList) { |
118 stage.getStartTargetName() + "-" + hashCode); |
111 if (stage != null) { |
119 log.debug("getStageBasedOnEndTarget: currentStargetTargetName" + currentStageTargetName); |
112 log.debug("stage.name: " + stage.getStageName()); |
120 if (currentStageTargetName != null) { |
113 log.debug("stage.start target name: " + stage.getStartTarget()); |
121 return stage; |
114 String currentStageTargetName = stageStartTargetMap.get(stage.getStartTarget() |
122 } |
115 + "-" + hashCode); |
123 } |
116 log.debug("getStageBasedOnEndTarget: currentStargetTargetName" + currentStageTargetName); |
124 return null; |
117 if (currentStageTargetName != null) { |
125 } |
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 } |
|
133 } |
|
134 } |
|
135 } |
|
136 } |
|
137 |
126 @SuppressWarnings("unchecked") |
138 @SuppressWarnings("unchecked") |
127 public void targetEnd(BuildEvent buildEvent) throws DiamondsException { |
139 public void targetEnd(BuildEvent buildEvent) throws DiamondsException { |
128 String targetName = buildEvent.getTarget().getName(); |
140 String targetName = buildEvent.getTarget().getName(); |
129 project = buildEvent.getProject(); |
141 Project prj = buildEvent.getProject(); |
130 int hashCode = project.hashCode(); |
142 int hashCode = prj.hashCode(); |
131 String targetNameWithHashCode = targetName + "-" + hashCode; |
143 String targetNameWithHashCode = targetName + "-" + hashCode; |
132 log.debug("targetEnd: targetNamewith-hashcode: " + targetNameWithHashCode); |
144 log.debug("targetEnd: targetNamewith-hashcode: " + targetNameWithHashCode); |
133 |
145 sendStageInfo(targetName, hashCode); |
134 Stage stage = getStageBasedOnEndTarget(targetName, hashCode); |
|
135 if (stage != null && getIsInitialized() ) { |
|
136 //initDiamondsClient(); |
|
137 String stageName = stage.getStageName(); |
|
138 String sourceFile = stage.getSourceFile(); |
|
139 log.debug("targetEnd: stageName: " + stageName); |
|
140 log |
|
141 .debug("diamonds:StageDiamondsListener: finished recording for stage: " |
|
142 + stageName); |
|
143 if (sourceFile == null) { |
|
144 sourceFile = getSourceFile(stageName); |
|
145 } |
|
146 log.debug("targetEnd: logical.stage: " + stageName); |
|
147 Date startTime = getStartTime(stage, hashCode); |
|
148 log.debug("targetEnd: starttime: " + startTime); |
|
149 log.debug("targetEnd: endtime: " + |
|
150 getTimeFormat().format(new Date())); |
|
151 project.setProperty("logical.stage", stageName); |
|
152 project.setProperty("stage.start.time", getTimeFormat() |
|
153 .format(startTime)); |
|
154 project.setProperty("stage.end.time", getTimeFormat() |
|
155 .format(new Date())); |
|
156 // Look for template file with stage name |
|
157 String stageTemplateFileName = stageName + ".xml.ftl"; |
|
158 File stageTemplateFile = new File(stageTemplateFileName); |
|
159 if (stageTemplateFile.exists()) { |
|
160 String output = DiamondsConfig.getOutputDir() |
|
161 + File.separator + stageName + ".xml"; |
|
162 try { |
|
163 List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>(); |
|
164 sourceList.add(new PropertiesSource("ant", project |
|
165 .getProperties())); |
|
166 sourceList |
|
167 .add(new XMLTemplateSource("doc", new File(sourceFile))); |
|
168 getTemplateProcessor().convertTemplate(DiamondsConfig |
|
169 .getTemplateDir(), stageTemplateFileName, |
|
170 output, sourceList); |
|
171 mergeToFullResults(new File(output)); |
|
172 log.info("Sending data to diamonds for stage: " |
|
173 + stageName); |
|
174 getDiamondsClient().sendData(output, DiamondsConfig |
|
175 .getDiamondsProperties().getDiamondsBuildID()); |
|
176 } catch (com.nokia.helium.core.TemplateProcessorException e1) { |
|
177 throw new DiamondsException( |
|
178 "template conversion error for stage: " |
|
179 + stageName + " : " + e1.getMessage()); |
|
180 } |
|
181 } else { |
|
182 log.debug("diamonds:StageDiamondsListener:tempalte file: " |
|
183 + stageTemplateFile + " does not exist"); |
|
184 } |
|
185 |
|
186 String output = DiamondsConfig.getOutputDir() + File.separator |
|
187 + stageName + "-time.xml"; |
|
188 log.debug("targetEnd: output " + output); |
|
189 // Store the time for the current stage and send it |
|
190 stageTemplateFileName = "diamonds_stage.xml.ftl"; |
|
191 try { |
|
192 List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>(); |
|
193 sourceList.add(new PropertiesSource("ant", project |
|
194 .getProperties())); |
|
195 sourceList.add(new XMLTemplateSource("doc", new File(sourceFile))); |
|
196 getTemplateProcessor().convertTemplate(DiamondsConfig |
|
197 .getTemplateDir(), stageTemplateFileName, output, |
|
198 sourceList); |
|
199 mergeToFullResults(new File(output)); |
|
200 // List filesToMerge = new ArrayList(); |
|
201 |
|
202 // mergedFile = mergeFiles(output); |
|
203 getDiamondsClient().sendData(output, DiamondsConfig |
|
204 .getDiamondsProperties().getDiamondsBuildID()); |
|
205 } catch (com.nokia.helium.core.TemplateProcessorException e1) { |
|
206 throw new DiamondsException("template conversion error while sending data for stage: " |
|
207 + stageName + " : " + e1.getMessage()); |
|
208 } |
|
209 } |
|
210 } |
146 } |
211 |
147 |
212 private void findAndSetStartTimeForTargetInStageList(String targetName, |
148 private void findAndSetStartTimeForTargetInStageList(String targetName, |
213 String targetNameWithHashCode) throws DiamondsException { |
149 String targetNameWithHashCode) throws DiamondsException { |
214 for (Iterator<Map<String, Date>> listIter = stageTargetBeginList.iterator(); listIter |
150 for (Iterator<Map<String, Date>> listIter = stageTargetBeginList.iterator(); listIter.hasNext();) { |
215 .hasNext();) { |
|
216 Map<String, Date> stageMap = listIter.next(); |
151 Map<String, Date> stageMap = listIter.next(); |
217 Date targetTime = stageMap.get(targetName); |
152 Date targetTime = stageMap.get(targetName); |
218 if (targetTime != null && targetTime.equals(INVALID_DATE)) { |
153 if (targetTime != null && targetTime.equals(INVALID_DATE)) { |
219 log.debug("diamonds:StageDiamondsListener: started recording for stage-target-----: " |
154 log.debug("diamonds:StageDiamondsListener: started recording for stage-target-----: " |
220 + targetName); |
155 + targetName); |
221 log.debug("findtime: targetNamewith-hashcode: " + targetNameWithHashCode); |
156 log.debug("findtime: targetNamewith-hashcode: " + targetNameWithHashCode); |
222 log.debug("findtime: time: " + new Date()); |
157 log.debug("findtime: time: " + new Date()); |
223 stageMap.put(targetName, new Date()); |
158 stageMap.put(targetName, new Date()); |
224 stageStartTargetMap.put(targetNameWithHashCode, targetName); |
159 stageStartTargetMap.put(targetNameWithHashCode, targetName); |
225 stageStartTargetTimeMap.put(targetNameWithHashCode, new Date()); |
160 stageStartTargetTimeMap.put(targetNameWithHashCode, new Date()); |
226 } |
161 } |
227 } |
162 } |
228 } |
163 } |
229 |
164 |
230 @SuppressWarnings("unchecked") |
165 @SuppressWarnings("unchecked") |
231 private void initStageTargetsMap() { |
166 private void initStageTargetsMap(Project projectInStage) { |
232 Iterator<Stage> iter = stages.iterator(); |
167 for (String key : stages.keySet()) { |
233 while (iter.hasNext()) { |
168 Stage stage = stages.get(key); |
234 // stage begin process |
169 String startTargetName = stage.getStartTarget(); |
235 Stage stage = iter.next(); |
|
236 String startTargetName = stage.getStartTargetName(); |
|
237 Map<String, Date> stageMap = new LinkedHashMap<String, Date>(); |
170 Map<String, Date> stageMap = new LinkedHashMap<String, Date>(); |
238 Vector<Target> arrayList = null; |
171 Vector<Target> arrayList = null; |
239 try { |
172 try { |
240 arrayList = project.topoSort(startTargetName, project |
173 arrayList = projectInStage.topoSort(startTargetName, projectInStage.getTargets(), false); |
241 .getTargets(), false); |
174 } |
242 } catch (BuildException be) { |
175 catch (BuildException be) { |
243 log.debug("Diamonds target missing: ", be); |
176 log.debug("Diamonds target missing: ", be); |
244 } |
177 } |
245 if (arrayList != null) { |
178 if (arrayList != null) { |
246 log.debug(" + Stage definition: " + stage.getStageName()); |
179 log.debug(" + Stage definition: " + stage.getStageName()); |
247 Enumeration<Target> targetEnum = arrayList.elements(); |
180 Enumeration<Target> targetEnum = arrayList.elements(); |