587
|
1 |
/*
|
|
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 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.signal.ant;
|
|
19 |
|
628
|
20 |
import java.util.ArrayList;
|
|
21 |
import java.util.Date;
|
|
22 |
import java.util.Enumeration;
|
|
23 |
import java.util.HashMap;
|
|
24 |
import java.util.Hashtable;
|
|
25 |
import java.util.List;
|
|
26 |
import java.util.Vector;
|
|
27 |
|
|
28 |
import org.apache.log4j.Logger;
|
587
|
29 |
import org.apache.tools.ant.BuildException;
|
|
30 |
import org.apache.tools.ant.Project;
|
|
31 |
import org.apache.tools.ant.Target;
|
628
|
32 |
import org.apache.tools.ant.taskdefs.condition.Condition;
|
|
33 |
|
587
|
34 |
import com.nokia.helium.signal.Notifier;
|
|
35 |
import com.nokia.helium.signal.SignalStatus;
|
|
36 |
import com.nokia.helium.signal.SignalStatusList;
|
628
|
37 |
import com.nokia.helium.signal.ant.types.NotifierInput;
|
587
|
38 |
import com.nokia.helium.signal.ant.types.SignalInput;
|
628
|
39 |
import com.nokia.helium.signal.ant.types.SignalListenerConfig;
|
587
|
40 |
import com.nokia.helium.signal.ant.types.SignalNotifierInput;
|
|
41 |
import com.nokia.helium.signal.ant.types.SignalNotifierList;
|
|
42 |
import com.nokia.helium.signal.ant.types.TargetCondition;
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Helper class to store the list of notifiers.
|
|
46 |
*/
|
|
47 |
public class SignalList {
|
|
48 |
|
|
49 |
// default id list name
|
|
50 |
public static final String DEFAULT_NOTIFIER_LIST_REFID = "defaultSignalInput";
|
|
51 |
|
|
52 |
|
|
53 |
private Hashtable<String, SignalListenerConfig> signalListenerConfigs = new Hashtable<String, SignalListenerConfig>();
|
|
54 |
|
|
55 |
private HashMap<String, List<SignalListenerConfig>> targetsMap = new HashMap<String, List<SignalListenerConfig>>();
|
|
56 |
|
|
57 |
private Project project;
|
|
58 |
|
|
59 |
private Logger log = Logger.getLogger(this.getClass());
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Constructor
|
|
63 |
*/
|
|
64 |
@SuppressWarnings("unchecked")
|
|
65 |
public SignalList(Project project) {
|
|
66 |
this.project = project;
|
|
67 |
Hashtable<String, Object> references = project.getReferences();
|
|
68 |
Enumeration<String> keyEnum = references.keys();
|
|
69 |
while (keyEnum.hasMoreElements()) {
|
|
70 |
String key = keyEnum.nextElement();
|
|
71 |
if (references.get(key) instanceof SignalListenerConfig) {
|
|
72 |
log.debug("SignalList: Found reference: " + key);
|
|
73 |
SignalListenerConfig config = (SignalListenerConfig) references
|
|
74 |
.get(key);
|
|
75 |
config.setConfigId(key);
|
|
76 |
signalListenerConfigs.put(key, config);
|
|
77 |
String targetName = config.getTargetName();
|
|
78 |
List<SignalListenerConfig> list;
|
|
79 |
if (targetsMap.get(targetName) == null) {
|
|
80 |
list = new ArrayList<SignalListenerConfig>();
|
|
81 |
} else {
|
|
82 |
list = targetsMap.get(targetName);
|
|
83 |
}
|
|
84 |
list.add(config);
|
|
85 |
targetsMap.put(targetName, list);
|
|
86 |
}
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
public Project getProject() {
|
|
91 |
return project;
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Returns the list of SignalListenerConfig discovered.
|
|
96 |
* @return a Vector of SignalList objects.
|
|
97 |
*/
|
|
98 |
public Vector<SignalListenerConfig> getSignalListenerConfigList() {
|
|
99 |
return new Vector<SignalListenerConfig>(signalListenerConfigs.values());
|
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Check if targetName is defined is defined by a targetCondition.
|
|
104 |
* @param targetName the target name
|
|
105 |
* @return a boolean, true if found, false otherwise.
|
|
106 |
*/
|
|
107 |
public boolean isTargetInSignalList(String targetName) {
|
|
108 |
return targetsMap.get(targetName) != null;
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Return the list of SignalListenerConfig defining a target.
|
|
113 |
* @param targetName
|
|
114 |
* @return
|
|
115 |
*/
|
|
116 |
public List<SignalListenerConfig> getSignalListenerConfig(String targetName) {
|
|
117 |
return targetsMap.get(targetName);
|
|
118 |
}
|
|
119 |
|
|
120 |
protected void sendNotifications(Vector<Notifier> notifierList, String signalName, String errorMessage ) {
|
|
121 |
sendNotifications( notifierList, signalName, false, null, errorMessage );
|
|
122 |
}
|
|
123 |
|
|
124 |
public void processForSignal(Project prj, SignalNotifierInput signalNotifierInput, String signalName, String targetName,
|
|
125 |
String errorMessage, boolean failBuild) {
|
|
126 |
SignalInput signalInput = signalNotifierInput.getSignalInput();
|
|
127 |
Vector<Notifier> notifierList = signalInput.getSignalNotifierList();
|
|
128 |
if (notifierList == null) {
|
|
129 |
Object obj = (Object) prj
|
|
130 |
.getReference(DEFAULT_NOTIFIER_LIST_REFID);
|
|
131 |
if (obj instanceof SignalNotifierList) {
|
|
132 |
notifierList = ((SignalNotifierList) obj)
|
|
133 |
.getNotifierList();
|
|
134 |
}
|
|
135 |
}
|
|
136 |
NotifierInput notifierInput = signalNotifierInput.getNotifierInput();
|
|
137 |
sendNotifications(notifierList, signalName, failBuild,
|
|
138 |
notifierInput, errorMessage );
|
|
139 |
if (failBuild) {
|
|
140 |
String failStatus = "now";
|
|
141 |
if (signalInput != null) {
|
|
142 |
failStatus = signalInput.getFailBuild();
|
|
143 |
} else {
|
|
144 |
log.debug("Could not find config for signal: " + signalName);
|
|
145 |
}
|
|
146 |
if (failStatus == null || failStatus.equals("now")) {
|
|
147 |
log.debug("Adding now signal. Signal name is " + signalName);
|
|
148 |
SignalStatusList.getNowSignalList().addSignalStatus(new SignalStatus(signalName,
|
|
149 |
errorMessage, targetName, new Date()));
|
|
150 |
throw new BuildException(new SignalStatus(signalName,
|
|
151 |
errorMessage, targetName, new Date()).toString());
|
|
152 |
} else if (failStatus.equals("defer")) {
|
|
153 |
log.debug("Adding deffer signal. Signal " + signalName + " will be deferred.");
|
|
154 |
SignalStatusList.getDeferredSignalList().addSignalStatus(new SignalStatus(
|
|
155 |
signalName, errorMessage, targetName, new Date()));
|
|
156 |
} else if (failStatus.equals("never")) {
|
|
157 |
log.debug("Adding never signal. Signal name is " + signalName);
|
|
158 |
SignalStatusList.getNeverSignalList().addSignalStatus(new SignalStatus(signalName,
|
|
159 |
errorMessage, targetName, new Date()));
|
|
160 |
} else if (!failStatus.equals("never")) {
|
|
161 |
SignalStatusList.getNowSignalList().addSignalStatus(new SignalStatus(signalName,
|
|
162 |
errorMessage, targetName, new Date()));
|
|
163 |
throw new BuildException(new SignalStatus(signalName,
|
|
164 |
errorMessage, targetName, new Date()).toString());
|
|
165 |
} else {
|
|
166 |
log.info("Signal " + signalName
|
|
167 |
+ " set to be ignored by the configuration.");
|
|
168 |
}
|
|
169 |
}
|
|
170 |
}
|
|
171 |
/**
|
|
172 |
* Send notification using the notification list.
|
|
173 |
*
|
|
174 |
* @param notifierList
|
|
175 |
*/
|
|
176 |
protected void sendNotifications(Vector<Notifier> notifierList, String signalName,
|
|
177 |
boolean failStatus, NotifierInput notifierInput, String errorMessage ) {
|
|
178 |
if (notifierList == null) {
|
|
179 |
return;
|
|
180 |
}
|
|
181 |
for (Notifier notifier : notifierList) {
|
|
182 |
if (notifier != null) {
|
|
183 |
notifier.sendData(signalName, failStatus, notifierInput, errorMessage );
|
|
184 |
}
|
|
185 |
}
|
|
186 |
}
|
|
187 |
|
|
188 |
public boolean checkAndNotifyFailure(Target target, Project prj) {
|
|
189 |
String targetName = target.getName();
|
|
190 |
String signalName = "unknown";
|
|
191 |
boolean retValue = false;
|
|
192 |
|
|
193 |
if (isTargetInSignalList(targetName)) {
|
|
194 |
retValue = true;
|
|
195 |
for (SignalListenerConfig config : getSignalListenerConfig(targetName))
|
|
196 |
{
|
|
197 |
TargetCondition targetCondition = config
|
|
198 |
.getTargetCondition();
|
|
199 |
String errorMessage = null;
|
|
200 |
log.debug("targetcondition:" + targetCondition);
|
|
201 |
Condition condition = null;
|
|
202 |
if (targetCondition != null) {
|
|
203 |
condition = getFailureCondition(targetCondition);
|
|
204 |
}
|
|
205 |
errorMessage = config.getErrorMessage();
|
|
206 |
String refid = config.getConfigId();
|
|
207 |
log.debug("refid:" + refid);
|
|
208 |
Object configCurrent = prj.getReference(refid);
|
|
209 |
if (configCurrent != null && configCurrent instanceof SignalListenerConfig) {
|
|
210 |
signalName = refid;
|
|
211 |
}
|
|
212 |
processForSignal(prj, config.getSignalNotifierInput(), signalName,
|
|
213 |
targetName, errorMessage, condition != null);
|
|
214 |
log.debug("checkAndNotifyFailure: SignalName: " + signalName);
|
|
215 |
}
|
|
216 |
}
|
|
217 |
return retValue;
|
|
218 |
}
|
|
219 |
|
|
220 |
private Condition getFailureCondition(TargetCondition targetCondition) {
|
|
221 |
Condition retCondition = null;
|
|
222 |
Vector<Condition> conditionList = targetCondition.getConditions();
|
|
223 |
for (Condition condition : conditionList) {
|
|
224 |
log.debug("getFailureCondition:" + condition.eval());
|
|
225 |
if (condition.eval()) {
|
|
226 |
retCondition = condition;
|
|
227 |
break;
|
|
228 |
}
|
|
229 |
}
|
|
230 |
return retCondition;
|
|
231 |
}
|
|
232 |
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Handle the signal, either fail now, or defer the failure.
|
|
236 |
*
|
|
237 |
* @param targetName
|
|
238 |
* , target where the failure happened.
|
|
239 |
* @param errMsg
|
|
240 |
* , the error message
|
|
241 |
*/
|
|
242 |
public void fail(String signalName, String targetName, String errorMessage)
|
|
243 |
{
|
|
244 |
String failStatus = "now";
|
|
245 |
log.debug("Could not find config for signal: " + signalName);
|
|
246 |
log.debug("failStatus: " + failStatus);
|
|
247 |
log.debug("Adding now signal. Signal name is " + signalName);
|
|
248 |
SignalStatusList.getNowSignalList().addSignalStatus(new SignalStatus(signalName,
|
|
249 |
errorMessage, targetName, new Date()));
|
|
250 |
throw new BuildException(new SignalStatus(signalName,
|
|
251 |
errorMessage, targetName, new Date()).toString());
|
|
252 |
}
|
|
253 |
} |