1 package org
.argeo
.slc
.ant
;
4 import java
.util
.Vector
;
6 import org
.apache
.log4j
.AppenderSkeleton
;
7 import org
.apache
.log4j
.LogManager
;
8 import org
.apache
.log4j
.spi
.LoggingEvent
;
9 import org
.apache
.tools
.ant
.BuildEvent
;
10 import org
.apache
.tools
.ant
.Project
;
12 import org
.argeo
.slc
.core
.process
.SlcExecution
;
13 import org
.argeo
.slc
.core
.process
.SlcExecutionNotifier
;
14 import org
.argeo
.slc
.core
.process
.SlcExecutionStep
;
15 import org
.argeo
.slc
.core
.process
.WebServiceSlcExecutionNotifier
;
17 public class SlcExecutionBuildListener
extends AppenderSkeleton
implements
18 ProjectRelatedBuildListener
{
19 public static final String ANT_TYPE
= "org.apache.tools.ant";
20 public static final String SLC_ANT_TYPE
= "org.argeo.slc.ant";
22 public static final String REF_SLC_EXECUTION
= "slcExecution";
24 private Project project
;
26 // to avoid stack overflow when logging for log4j
27 private boolean isLogging
= false;
29 private List
<SlcExecutionNotifier
> notifiers
= new Vector
<SlcExecutionNotifier
>();
31 private boolean currentStepNotified
= true;
34 private boolean logBeforeFirstTarget
= false;
35 private boolean firstTargetStarted
= false;
37 private boolean logTaskStartFinish
= true;
39 public void init(Project project
) {
40 if (this.project
!= null) {
41 throw new SlcAntException("Build listener already initialized");
44 this.project
= project
;
46 if (!LogManager
.getRootLogger().isAttached(this)) {
47 LogManager
.getRootLogger().addAppender(this);
50 SlcExecution slcExecution
= (SlcExecution
) project
51 .getReference(REF_SLC_EXECUTION
);
52 if (slcExecution
== null)
53 throw new SlcAntException("No SLC Execution registered.");
55 for (SlcExecutionNotifier notifier
: notifiers
) {
56 notifier
.newExecution(slcExecution
);
61 public void buildStarted(BuildEvent event
) {
64 public void buildFinished(BuildEvent event
) {
65 SlcExecution slcExecution
= getSlcExecution(event
);
66 String oldStatus
= slcExecution
.getStatus();
67 slcExecution
.setStatus(SlcExecution
.STATUS_FINISHED
);
69 for (SlcExecutionNotifier notifier
: notifiers
) {
70 notifier
.updateStatus(slcExecution
, oldStatus
, slcExecution
75 public void messageLogged(BuildEvent event
) {
79 SlcExecution slcExecution
= getSlcExecution(event
);
80 if (slcExecution
!= null) {
81 if (currentStepNotified
) {
82 slcExecution
.getSteps().add(
83 new SlcExecutionStep("LOG", event
.getMessage()));
84 notifyStep(slcExecution
, slcExecution
.currentStep());
85 currentStepNotified
= true;
87 slcExecution
.currentStep().addLog(event
.getMessage());
90 // TODO: log before initialization?
94 public void targetStarted(BuildEvent event
) {
95 if (!firstTargetStarted
)
96 firstTargetStarted
= true;
98 addLogStep(event
, "Target " + event
.getTarget().getName() + " started");
101 public void targetFinished(BuildEvent event
) {
102 addLogStep(event
, "Target " + event
.getTarget().getName() + " finished");
105 public void taskStarted(BuildEvent event
) {
109 SlcExecution slcExecution
= getSlcExecution(event
);
110 if (!currentStepNotified
) {
111 notifyStep(slcExecution
, slcExecution
.currentStep());
112 currentStepNotified
= true;
116 if (logTaskStartFinish
)
117 msg
= "Task " + event
.getTask().getTaskName() + " started";
119 slcExecution
.getSteps().add(new SlcExecutionStep("LOG", msg
));
121 currentStepNotified
= false;
124 public void taskFinished(BuildEvent event
) {
128 SlcExecution slcExecution
= getSlcExecution(event
);
129 if (!currentStepNotified
) {
131 if (logTaskStartFinish
)
132 slcExecution
.currentStep().addLog(
133 "Task " + event
.getTask().getTaskName() + " finished");
135 notifyStep(slcExecution
, slcExecution
.currentStep());
136 currentStepNotified
= true;
140 public void setNotifiers(List
<SlcExecutionNotifier
> notifiers
) {
141 this.notifiers
= notifiers
;
144 protected SlcExecution
getSlcExecution(BuildEvent event
) {
145 Project projectEvt
= event
.getProject();
146 if (!projectEvt
.equals(project
))
147 throw new SlcAntException("Event project " + projectEvt
148 + " not consistent with listener project " + project
);
150 SlcExecution slcExecution
= (SlcExecution
) project
151 .getReference(REF_SLC_EXECUTION
);
153 if (slcExecution
== null)
154 throw new SlcAntException("No SLC Execution registered.");
158 protected void addLogStep(BuildEvent event
, String msg
) {
159 SlcExecution slcExecution
= getSlcExecution(event
);
160 slcExecution
.getSteps().add(new SlcExecutionStep("LOG", msg
));
162 notifyStep(slcExecution
, slcExecution
.currentStep());
163 currentStepNotified
= true;
166 protected void notifyStep(SlcExecution slcExecution
, SlcExecutionStep step
) {
167 Vector
<SlcExecutionStep
> additionalSteps
= new Vector
<SlcExecutionStep
>();
168 additionalSteps
.add(step
);
169 notifySteps(slcExecution
, additionalSteps
);
172 protected void notifySteps(SlcExecution slcExecution
,
173 List
<SlcExecutionStep
> additionalSteps
) {
174 for (SlcExecutionNotifier notifier
: notifiers
) {
175 notifier
.addSteps(slcExecution
, additionalSteps
);
182 protected void append(LoggingEvent event
) {
184 // avoid StackOverflow if notification calls Log4j itself.
188 if (event
.getLoggerName().equals(
189 WebServiceSlcExecutionNotifier
.class.getName())) {
196 SlcExecution slcExecution
= (SlcExecution
) project
197 .getReference(REF_SLC_EXECUTION
);
198 if (slcExecution
!= null) {
199 if (currentStepNotified
) {
200 slcExecution
.getSteps().add(
201 new SlcExecutionStep("LOG", event
.getMessage()
203 currentStepNotified
= false;
205 slcExecution
.currentStep()
206 .addLog(event
.getMessage().toString());
208 // TODO: log before initialization?
216 protected boolean shouldLog() {
217 return logBeforeFirstTarget
|| firstTargetStarted
;
221 public void close() {
225 public boolean requiresLayout() {
229 public Project
getProject() {