]> git.argeo.org Git - gpl/argeo-slc.git/blob - SlcExecutionBuildListener.java
cff4c171950fff42ac8c8289abf147affefbdee1
[gpl/argeo-slc.git] / SlcExecutionBuildListener.java
1 package org.argeo.slc.ant;
2
3 import java.net.InetAddress;
4 import java.net.UnknownHostException;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.UUID;
9 import java.util.Vector;
10
11 import org.apache.log4j.AppenderSkeleton;
12 import org.apache.log4j.LogManager;
13 import org.apache.log4j.spi.LoggingEvent;
14 import org.apache.tools.ant.BuildEvent;
15 import org.apache.tools.ant.BuildListener;
16 import org.apache.tools.ant.Project;
17
18 import org.argeo.slc.core.process.SlcExecution;
19 import org.argeo.slc.core.process.SlcExecutionNotifier;
20 import org.argeo.slc.core.process.SlcExecutionStep;
21
22 public class SlcExecutionBuildListener extends AppenderSkeleton implements
23 BuildListener {
24 public static final String ANT_TYPE = "org.apache.tools.ant";
25 public static final String SLC_ANT_TYPE = "org.argeo.slc.ant";
26
27 public static final String REF_SLC_EXECUTION = "slcExecution";
28
29 private Project project;
30
31 // to avoid stack overflow when logging for log4j
32 private boolean isLogging = false;
33
34 private List<SlcExecutionNotifier> notifiers = new Vector<SlcExecutionNotifier>();
35
36 private Map<SlcExecution, SlcExecutionStep> currentStep = new HashMap<SlcExecution, SlcExecutionStep>();
37
38 public void buildStarted(BuildEvent event) {
39 // SlcExecution slcExecution = getSlcExecution(event);
40
41 }
42
43 public void buildFinished(BuildEvent event) {
44 SlcExecution slcExecution = getSlcExecution(event);
45 slcExecution.setStatus(SlcExecution.STATUS_FINISHED);
46
47 for (SlcExecutionNotifier notifier : notifiers) {
48 notifier.updateExecution(slcExecution);
49 }
50 }
51
52 public void messageLogged(BuildEvent event) {
53 SlcExecution slcExecution = getSlcExecution(event);
54 if (slcExecution != null) {
55 SlcExecutionStep step = currentStep.get(slcExecution);
56 if (step == null) {
57 step = new SlcExecutionStep("LOG", event.getMessage());
58 notifyStep(slcExecution, step);
59 } else {
60 step.addLog(event.getMessage());
61 }
62 } else {
63 // TODO: log before initialization?
64 }
65 }
66
67 public void targetStarted(BuildEvent event) {
68 addLogStep(event, "Target " + event.getTarget().getName() + " started");
69 }
70
71 public void targetFinished(BuildEvent event) {
72 addLogStep(event, "Target " + event.getTarget().getName() + " finished");
73 }
74
75 public void taskStarted(BuildEvent event) {
76 SlcExecution slcExecution = getSlcExecution(event);
77 SlcExecutionStep currStep = currentStep.get(slcExecution);
78 if (currStep != null) {
79 notifyStep(slcExecution, currStep);
80 currentStep.remove(currStep);
81 }
82
83 SlcExecutionStep step = new SlcExecutionStep("LOG", "Task "
84 + event.getTask().getTaskName() + " started");
85 currentStep.put(slcExecution, step);
86 }
87
88 public void taskFinished(BuildEvent event) {
89 SlcExecution slcExecution = getSlcExecution(event);
90 SlcExecutionStep step = currentStep.get(slcExecution);
91 if (step != null) {
92 step.addLog("Task " + event.getTask().getTaskName() + " finished");
93
94 slcExecution.getSteps().add(step);
95 notifyStep(slcExecution, step);
96
97 currentStep.remove(slcExecution);
98 }
99 }
100
101 public void setNotifiers(List<SlcExecutionNotifier> notifiers) {
102 this.notifiers = notifiers;
103 }
104
105 protected SlcExecution getSlcExecution(BuildEvent event) {
106 Project project = event.getProject();
107 SlcExecution slcExecution = (SlcExecution) project
108 .getReference(REF_SLC_EXECUTION);
109 if (slcExecution == null) {
110 // for log4j
111 this.project = project;// FIXME
112 if (!LogManager.getRootLogger().isAttached(this)) {
113 LogManager.getRootLogger().addAppender(this);
114 }
115
116 slcExecution = new SlcExecution();
117 slcExecution.setUuid(UUID.randomUUID().toString());
118 try {
119 slcExecution.setHost(InetAddress.getLocalHost().getHostName());
120 } catch (UnknownHostException e) {
121 slcExecution.setHost(SlcExecution.UNKOWN_HOST);
122 }
123
124 if (project.getReference(SlcProjectHelper.REF_ROOT_CONTEXT) != null) {
125 slcExecution.setType(SLC_ANT_TYPE);
126 } else {
127 slcExecution.setType(ANT_TYPE);
128 }
129
130 slcExecution.setPath(project.getProperty("ant.file"));
131 slcExecution.setStatus(SlcExecution.STATUS_RUNNING);
132
133 project.addReference(REF_SLC_EXECUTION, slcExecution);
134
135 for (SlcExecutionNotifier notifier : notifiers) {
136 notifier.newExecution(slcExecution);
137 }
138
139 }
140 return slcExecution;
141 }
142
143 protected void addLogStep(BuildEvent event, String msg) {
144 SlcExecutionStep step = new SlcExecutionStep("LOG", msg);
145 SlcExecution slcExecution = getSlcExecution(event);
146 slcExecution.getSteps().add(step);
147
148 notifyStep(slcExecution, step);
149 }
150
151 protected void notifyStep(SlcExecution slcExecution, SlcExecutionStep step) {
152 Vector<SlcExecutionStep> additionalSteps = new Vector<SlcExecutionStep>();
153 additionalSteps.add(step);
154 notifySteps(slcExecution, additionalSteps);
155 }
156
157 protected void notifySteps(SlcExecution slcExecution,
158 List<SlcExecutionStep> additionalSteps) {
159 for (SlcExecutionNotifier notifier : notifiers) {
160 notifier.addSteps(slcExecution, additionalSteps);
161 }
162 }
163
164 /* Log4j methods */
165
166 @Override
167 protected void append(LoggingEvent event) {
168 if(isLogging){
169 // avoid StackOverflow if notification calls Log4j itself.
170 return;
171 }
172 isLogging = true;
173
174 try {
175 SlcExecution slcExecution = (SlcExecution) project
176 .getReference(REF_SLC_EXECUTION);
177 if (slcExecution != null) {
178 SlcExecutionStep step = currentStep.get(slcExecution);
179 if (step == null) {
180 step = new SlcExecutionStep("LOG", event.getMessage()
181 .toString());
182 notifyStep(slcExecution, step);
183 } else {
184 step.addLog(event.getMessage().toString());
185 }
186 } else {
187 // TODO: log before initialization?
188 }
189 } finally{
190 isLogging = false;
191 }
192
193
194 }
195
196 @Override
197 public void close() {
198 // TODO Auto-generated method stub
199
200 }
201
202 @Override
203 public boolean requiresLayout() {
204 // TODO Auto-generated method stub
205 return false;
206 }
207
208 }