X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fant%2FSlcExecutionBuildListener.java;h=a8dd15c7d76f89ea5c8de3f8f672fee08458ef78;hb=58db7291c1746d4e7623926f34108c07e2ccae4f;hp=cff4c171950fff42ac8c8289abf147affefbdee1;hpb=07315a82eb7fa5b84db60209dbf03bd94568321c;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java index cff4c1719..a8dd15c7d 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java @@ -1,63 +1,90 @@ package org.argeo.slc.ant; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.UUID; import java.util.Vector; import org.apache.log4j.AppenderSkeleton; import org.apache.log4j.LogManager; import org.apache.log4j.spi.LoggingEvent; import org.apache.tools.ant.BuildEvent; -import org.apache.tools.ant.BuildListener; import org.apache.tools.ant.Project; import org.argeo.slc.core.process.SlcExecution; import org.argeo.slc.core.process.SlcExecutionNotifier; import org.argeo.slc.core.process.SlcExecutionStep; +import org.argeo.slc.core.process.WebServiceSlcExecutionNotifier; public class SlcExecutionBuildListener extends AppenderSkeleton implements - BuildListener { + ProjectRelatedBuildListener { public static final String ANT_TYPE = "org.apache.tools.ant"; public static final String SLC_ANT_TYPE = "org.argeo.slc.ant"; public static final String REF_SLC_EXECUTION = "slcExecution"; private Project project; - + // to avoid stack overflow when logging for log4j private boolean isLogging = false; private List notifiers = new Vector(); - private Map currentStep = new HashMap(); + private boolean currentStepNotified = true; - public void buildStarted(BuildEvent event) { - // SlcExecution slcExecution = getSlcExecution(event); + // CUSTOMIZATIONS + private boolean logBeforeFirstTarget = false; + private boolean firstTargetStarted = false; + + private boolean logTaskStartFinish = true; + + public void init(Project project) { + if (this.project != null) { + throw new SlcAntException("Build listener already initialized"); + } + + this.project = project; + + if (!LogManager.getRootLogger().isAttached(this)) { + LogManager.getRootLogger().addAppender(this); + } + + SlcExecution slcExecution = (SlcExecution) project + .getReference(REF_SLC_EXECUTION); + if (slcExecution == null) + throw new SlcAntException("No SLC Execution registered."); + + for (SlcExecutionNotifier notifier : notifiers) { + notifier.newExecution(slcExecution); + } + + } + public void buildStarted(BuildEvent event) { } public void buildFinished(BuildEvent event) { SlcExecution slcExecution = getSlcExecution(event); + String oldStatus = slcExecution.getStatus(); slcExecution.setStatus(SlcExecution.STATUS_FINISHED); for (SlcExecutionNotifier notifier : notifiers) { - notifier.updateExecution(slcExecution); + notifier.updateStatus(slcExecution, oldStatus, slcExecution + .getStatus()); } } public void messageLogged(BuildEvent event) { + if (!shouldLog()) + return; + SlcExecution slcExecution = getSlcExecution(event); if (slcExecution != null) { - SlcExecutionStep step = currentStep.get(slcExecution); - if (step == null) { - step = new SlcExecutionStep("LOG", event.getMessage()); - notifyStep(slcExecution, step); + if (currentStepNotified) { + slcExecution.getSteps().add( + new SlcExecutionStep("LOG", event.getMessage())); + notifyStep(slcExecution, slcExecution.currentStep()); + currentStepNotified = true; } else { - step.addLog(event.getMessage()); + slcExecution.currentStep().addLog(event.getMessage()); } } else { // TODO: log before initialization? @@ -65,6 +92,9 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements } public void targetStarted(BuildEvent event) { + if (!firstTargetStarted) + firstTargetStarted = true; + addLogStep(event, "Target " + event.getTarget().getName() + " started"); } @@ -73,28 +103,37 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements } public void taskStarted(BuildEvent event) { + if (!shouldLog()) + return; + SlcExecution slcExecution = getSlcExecution(event); - SlcExecutionStep currStep = currentStep.get(slcExecution); - if (currStep != null) { - notifyStep(slcExecution, currStep); - currentStep.remove(currStep); + if (!currentStepNotified) { + notifyStep(slcExecution, slcExecution.currentStep()); + currentStepNotified = true; } - SlcExecutionStep step = new SlcExecutionStep("LOG", "Task " - + event.getTask().getTaskName() + " started"); - currentStep.put(slcExecution, step); + String msg = null; + if (logTaskStartFinish) + msg = "Task " + event.getTask().getTaskName() + " started"; + + slcExecution.getSteps().add(new SlcExecutionStep("LOG", msg)); + + currentStepNotified = false; } public void taskFinished(BuildEvent event) { + if (!shouldLog()) + return; + SlcExecution slcExecution = getSlcExecution(event); - SlcExecutionStep step = currentStep.get(slcExecution); - if (step != null) { - step.addLog("Task " + event.getTask().getTaskName() + " finished"); + if (!currentStepNotified) { - slcExecution.getSteps().add(step); - notifyStep(slcExecution, step); + if (logTaskStartFinish) + slcExecution.currentStep().addLog( + "Task " + event.getTask().getTaskName() + " finished"); - currentStep.remove(slcExecution); + notifyStep(slcExecution, slcExecution.currentStep()); + currentStepNotified = true; } } @@ -103,49 +142,25 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements } protected SlcExecution getSlcExecution(BuildEvent event) { - Project project = event.getProject(); + Project projectEvt = event.getProject(); + if (!projectEvt.equals(project)) + throw new SlcAntException("Event project " + projectEvt + + " not consistent with listener project " + project); + SlcExecution slcExecution = (SlcExecution) project .getReference(REF_SLC_EXECUTION); - if (slcExecution == null) { - // for log4j - this.project = project;// FIXME - if (!LogManager.getRootLogger().isAttached(this)) { - LogManager.getRootLogger().addAppender(this); - } - - slcExecution = new SlcExecution(); - slcExecution.setUuid(UUID.randomUUID().toString()); - try { - slcExecution.setHost(InetAddress.getLocalHost().getHostName()); - } catch (UnknownHostException e) { - slcExecution.setHost(SlcExecution.UNKOWN_HOST); - } - - if (project.getReference(SlcProjectHelper.REF_ROOT_CONTEXT) != null) { - slcExecution.setType(SLC_ANT_TYPE); - } else { - slcExecution.setType(ANT_TYPE); - } - - slcExecution.setPath(project.getProperty("ant.file")); - slcExecution.setStatus(SlcExecution.STATUS_RUNNING); - - project.addReference(REF_SLC_EXECUTION, slcExecution); - - for (SlcExecutionNotifier notifier : notifiers) { - notifier.newExecution(slcExecution); - } - } + if (slcExecution == null) + throw new SlcAntException("No SLC Execution registered."); return slcExecution; } protected void addLogStep(BuildEvent event, String msg) { - SlcExecutionStep step = new SlcExecutionStep("LOG", msg); SlcExecution slcExecution = getSlcExecution(event); - slcExecution.getSteps().add(step); + slcExecution.getSteps().add(new SlcExecutionStep("LOG", msg)); - notifyStep(slcExecution, step); + notifyStep(slcExecution, slcExecution.currentStep()); + currentStepNotified = true; } protected void notifyStep(SlcExecution slcExecution, SlcExecutionStep step) { @@ -165,44 +180,54 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements @Override protected void append(LoggingEvent event) { - if(isLogging){ + if (isLogging) { // avoid StackOverflow if notification calls Log4j itself. return; } + + if (event.getLoggerName().equals( + WebServiceSlcExecutionNotifier.class.getName())) { + return; + } + isLogging = true; - + try { SlcExecution slcExecution = (SlcExecution) project .getReference(REF_SLC_EXECUTION); if (slcExecution != null) { - SlcExecutionStep step = currentStep.get(slcExecution); - if (step == null) { - step = new SlcExecutionStep("LOG", event.getMessage() - .toString()); - notifyStep(slcExecution, step); - } else { - step.addLog(event.getMessage().toString()); + if (currentStepNotified) { + slcExecution.getSteps().add( + new SlcExecutionStep("LOG", event.getMessage() + .toString())); + currentStepNotified = false; } + slcExecution.currentStep() + .addLog(event.getMessage().toString()); } else { // TODO: log before initialization? } - } finally{ + } finally { isLogging = false; } - - + + } + + protected boolean shouldLog() { + return logBeforeFirstTarget || firstTargetStarted; } @Override public void close() { - // TODO Auto-generated method stub - } @Override public boolean requiresLayout() { - // TODO Auto-generated method stub return false; } + public Project getProject() { + return project; + } + }