1 package org
.argeo
.slc
.ant
;
3 import java
.net
.InetAddress
;
4 import java
.net
.UnknownHostException
;
5 import java
.util
.HashMap
;
9 import java
.util
.Vector
;
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
;
18 import org
.argeo
.slc
.core
.process
.SlcExecution
;
19 import org
.argeo
.slc
.core
.process
.SlcExecutionNotifier
;
20 import org
.argeo
.slc
.core
.process
.SlcExecutionStep
;
22 public class SlcExecutionBuildListener
extends AppenderSkeleton
implements
24 public static final String ANT_TYPE
= "org.apache.tools.ant";
25 public static final String SLC_ANT_TYPE
= "org.argeo.slc.ant";
27 public static final String REF_SLC_EXECUTION
= "slcExecution";
29 private Project project
;
31 // to avoid stack overflow when logging for log4j
32 private boolean isLogging
= false;
34 private List
<SlcExecutionNotifier
> notifiers
= new Vector
<SlcExecutionNotifier
>();
36 private Map
<SlcExecution
, SlcExecutionStep
> currentStep
= new HashMap
<SlcExecution
, SlcExecutionStep
>();
38 public void buildStarted(BuildEvent event
) {
39 // SlcExecution slcExecution = getSlcExecution(event);
43 public void buildFinished(BuildEvent event
) {
44 SlcExecution slcExecution
= getSlcExecution(event
);
45 slcExecution
.setStatus(SlcExecution
.STATUS_FINISHED
);
47 for (SlcExecutionNotifier notifier
: notifiers
) {
48 notifier
.updateExecution(slcExecution
);
52 public void messageLogged(BuildEvent event
) {
53 SlcExecution slcExecution
= getSlcExecution(event
);
54 if (slcExecution
!= null) {
55 SlcExecutionStep step
= currentStep
.get(slcExecution
);
57 step
= new SlcExecutionStep("LOG", event
.getMessage());
58 notifyStep(slcExecution
, step
);
60 step
.addLog(event
.getMessage());
63 // TODO: log before initialization?
67 public void targetStarted(BuildEvent event
) {
68 addLogStep(event
, "Target " + event
.getTarget().getName() + " started");
71 public void targetFinished(BuildEvent event
) {
72 addLogStep(event
, "Target " + event
.getTarget().getName() + " finished");
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
);
83 SlcExecutionStep step
= new SlcExecutionStep("LOG", "Task "
84 + event
.getTask().getTaskName() + " started");
85 currentStep
.put(slcExecution
, step
);
88 public void taskFinished(BuildEvent event
) {
89 SlcExecution slcExecution
= getSlcExecution(event
);
90 SlcExecutionStep step
= currentStep
.get(slcExecution
);
92 step
.addLog("Task " + event
.getTask().getTaskName() + " finished");
94 slcExecution
.getSteps().add(step
);
95 notifyStep(slcExecution
, step
);
97 currentStep
.remove(slcExecution
);
101 public void setNotifiers(List
<SlcExecutionNotifier
> notifiers
) {
102 this.notifiers
= notifiers
;
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) {
111 this.project
= project
;// FIXME
112 if (!LogManager
.getRootLogger().isAttached(this)) {
113 LogManager
.getRootLogger().addAppender(this);
116 slcExecution
= new SlcExecution();
117 slcExecution
.setUuid(UUID
.randomUUID().toString());
119 slcExecution
.setHost(InetAddress
.getLocalHost().getHostName());
120 } catch (UnknownHostException e
) {
121 slcExecution
.setHost(SlcExecution
.UNKOWN_HOST
);
124 if (project
.getReference(SlcProjectHelper
.REF_ROOT_CONTEXT
) != null) {
125 slcExecution
.setType(SLC_ANT_TYPE
);
127 slcExecution
.setType(ANT_TYPE
);
130 slcExecution
.setPath(project
.getProperty("ant.file"));
131 slcExecution
.setStatus(SlcExecution
.STATUS_RUNNING
);
133 project
.addReference(REF_SLC_EXECUTION
, slcExecution
);
135 for (SlcExecutionNotifier notifier
: notifiers
) {
136 notifier
.newExecution(slcExecution
);
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
);
148 notifyStep(slcExecution
, step
);
151 protected void notifyStep(SlcExecution slcExecution
, SlcExecutionStep step
) {
152 Vector
<SlcExecutionStep
> additionalSteps
= new Vector
<SlcExecutionStep
>();
153 additionalSteps
.add(step
);
154 notifySteps(slcExecution
, additionalSteps
);
157 protected void notifySteps(SlcExecution slcExecution
,
158 List
<SlcExecutionStep
> additionalSteps
) {
159 for (SlcExecutionNotifier notifier
: notifiers
) {
160 notifier
.addSteps(slcExecution
, additionalSteps
);
167 protected void append(LoggingEvent event
) {
169 // avoid StackOverflow if notification calls Log4j itself.
175 SlcExecution slcExecution
= (SlcExecution
) project
176 .getReference(REF_SLC_EXECUTION
);
177 if (slcExecution
!= null) {
178 SlcExecutionStep step
= currentStep
.get(slcExecution
);
180 step
= new SlcExecutionStep("LOG", event
.getMessage()
182 notifyStep(slcExecution
, step
);
184 step
.addLog(event
.getMessage().toString());
187 // TODO: log before initialization?
197 public void close() {
198 // TODO Auto-generated method stub
203 public boolean requiresLayout() {
204 // TODO Auto-generated method stub