]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.core/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java
Improve SlcExecution notifications
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / ant / SlcExecutionBuildListener.java
index cff4c171950fff42ac8c8289abf147affefbdee1..f2bedce400527b3f8ed0e484d75edf0e346355fe 100644 (file)
@@ -1,39 +1,60 @@
 package org.argeo.slc.ant;\r
 \r
-import java.net.InetAddress;\r
-import java.net.UnknownHostException;\r
-import java.util.HashMap;\r
 import java.util.List;\r
-import java.util.Map;\r
-import java.util.UUID;\r
 import java.util.Vector;\r
 \r
 import org.apache.log4j.AppenderSkeleton;\r
 import org.apache.log4j.LogManager;\r
 import org.apache.log4j.spi.LoggingEvent;\r
 import org.apache.tools.ant.BuildEvent;\r
-import org.apache.tools.ant.BuildListener;\r
 import org.apache.tools.ant.Project;\r
 \r
 import org.argeo.slc.core.process.SlcExecution;\r
 import org.argeo.slc.core.process.SlcExecutionNotifier;\r
 import org.argeo.slc.core.process.SlcExecutionStep;\r
+import org.argeo.slc.core.process.WebServiceSlcExecutionNotifier;\r
 \r
 public class SlcExecutionBuildListener extends AppenderSkeleton implements\r
-               BuildListener {\r
+               ProjectRelatedBuildListener {\r
        public static final String ANT_TYPE = "org.apache.tools.ant";\r
        public static final String SLC_ANT_TYPE = "org.argeo.slc.ant";\r
 \r
        public static final String REF_SLC_EXECUTION = "slcExecution";\r
 \r
        private Project project;\r
-       \r
+\r
        // to avoid stack overflow when logging for log4j\r
        private boolean isLogging = false;\r
 \r
        private List<SlcExecutionNotifier> notifiers = new Vector<SlcExecutionNotifier>();\r
 \r
-       private Map<SlcExecution, SlcExecutionStep> currentStep = new HashMap<SlcExecution, SlcExecutionStep>();\r
+       // private Map<SlcExecution, SlcExecutionStep> currentStep = new\r
+       // HashMap<SlcExecution, SlcExecutionStep>();\r
+\r
+       // private SlcExecutionStep currentStep = null;\r
+       private boolean currentStepNotified = true;\r
+\r
+       public void init(Project project) {\r
+               if (this.project != null) {\r
+                       throw new SlcAntException("Build listener already initialized");\r
+               }\r
+\r
+               this.project = project;\r
+\r
+               if (!LogManager.getRootLogger().isAttached(this)) {\r
+                       LogManager.getRootLogger().addAppender(this);\r
+               }\r
+\r
+               SlcExecution slcExecution = (SlcExecution) project\r
+                               .getReference(REF_SLC_EXECUTION);\r
+               if (slcExecution == null)\r
+                       throw new SlcAntException("No SLC Execution registered.");\r
+\r
+               for (SlcExecutionNotifier notifier : notifiers) {\r
+                       notifier.newExecution(slcExecution);\r
+               }\r
+\r
+       }\r
 \r
        public void buildStarted(BuildEvent event) {\r
                // SlcExecution slcExecution = getSlcExecution(event);\r
@@ -52,12 +73,13 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements
        public void messageLogged(BuildEvent event) {\r
                SlcExecution slcExecution = getSlcExecution(event);\r
                if (slcExecution != null) {\r
-                       SlcExecutionStep step = currentStep.get(slcExecution);\r
-                       if (step == null) {\r
-                               step = new SlcExecutionStep("LOG", event.getMessage());\r
-                               notifyStep(slcExecution, step);\r
+                       if (currentStepNotified) {\r
+                               slcExecution.getSteps().add(\r
+                                               new SlcExecutionStep("LOG", event.getMessage()));\r
+                               notifyStep(slcExecution, slcExecution.currentStep());\r
+                               currentStepNotified = true;\r
                        } else {\r
-                               step.addLog(event.getMessage());\r
+                               slcExecution.currentStep().addLog(event.getMessage());\r
                        }\r
                } else {\r
                        // TODO: log before initialization?\r
@@ -74,27 +96,24 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements
 \r
        public void taskStarted(BuildEvent event) {\r
                SlcExecution slcExecution = getSlcExecution(event);\r
-               SlcExecutionStep currStep = currentStep.get(slcExecution);\r
-               if (currStep != null) {\r
-                       notifyStep(slcExecution, currStep);\r
-                       currentStep.remove(currStep);\r
+               if (!currentStepNotified) {\r
+                       notifyStep(slcExecution, slcExecution.currentStep());\r
+                       currentStepNotified = true;\r
                }\r
 \r
-               SlcExecutionStep step = new SlcExecutionStep("LOG", "Task "\r
-                               + event.getTask().getTaskName() + " started");\r
-               currentStep.put(slcExecution, step);\r
+               slcExecution.getSteps().add(\r
+                               new SlcExecutionStep("LOG", "Task "\r
+                                               + event.getTask().getTaskName() + " started"));\r
+               currentStepNotified = false;\r
        }\r
 \r
        public void taskFinished(BuildEvent event) {\r
                SlcExecution slcExecution = getSlcExecution(event);\r
-               SlcExecutionStep step = currentStep.get(slcExecution);\r
-               if (step != null) {\r
-                       step.addLog("Task " + event.getTask().getTaskName() + " finished");\r
-\r
-                       slcExecution.getSteps().add(step);\r
-                       notifyStep(slcExecution, step);\r
-\r
-                       currentStep.remove(slcExecution);\r
+               if (!currentStepNotified) {\r
+                       slcExecution.currentStep().addLog(\r
+                                       "Task " + event.getTask().getTaskName() + " finished");\r
+                       notifyStep(slcExecution, slcExecution.currentStep());\r
+                       currentStepNotified = true;\r
                }\r
        }\r
 \r
@@ -103,49 +122,25 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements
        }\r
 \r
        protected SlcExecution getSlcExecution(BuildEvent event) {\r
-               Project project = event.getProject();\r
+               Project projectEvt = event.getProject();\r
+               if (!projectEvt.equals(project))\r
+                       throw new SlcAntException("Event project " + projectEvt\r
+                                       + " not consistent with listener project " + project);\r
+\r
                SlcExecution slcExecution = (SlcExecution) project\r
                                .getReference(REF_SLC_EXECUTION);\r
-               if (slcExecution == null) {\r
-                       // for log4j\r
-                       this.project = project;// FIXME\r
-                       if (!LogManager.getRootLogger().isAttached(this)) {\r
-                               LogManager.getRootLogger().addAppender(this);\r
-                       }\r
-                       \r
-                       slcExecution = new SlcExecution();\r
-                       slcExecution.setUuid(UUID.randomUUID().toString());\r
-                       try {\r
-                               slcExecution.setHost(InetAddress.getLocalHost().getHostName());\r
-                       } catch (UnknownHostException e) {\r
-                               slcExecution.setHost(SlcExecution.UNKOWN_HOST);\r
-                       }\r
-\r
-                       if (project.getReference(SlcProjectHelper.REF_ROOT_CONTEXT) != null) {\r
-                               slcExecution.setType(SLC_ANT_TYPE);\r
-                       } else {\r
-                               slcExecution.setType(ANT_TYPE);\r
-                       }\r
-\r
-                       slcExecution.setPath(project.getProperty("ant.file"));\r
-                       slcExecution.setStatus(SlcExecution.STATUS_RUNNING);\r
-\r
-                       project.addReference(REF_SLC_EXECUTION, slcExecution);\r
-\r
-                       for (SlcExecutionNotifier notifier : notifiers) {\r
-                               notifier.newExecution(slcExecution);\r
-                       }\r
 \r
-               }\r
+               if (slcExecution == null)\r
+                       throw new SlcAntException("No SLC Execution registered.");\r
                return slcExecution;\r
        }\r
 \r
        protected void addLogStep(BuildEvent event, String msg) {\r
-               SlcExecutionStep step = new SlcExecutionStep("LOG", msg);\r
                SlcExecution slcExecution = getSlcExecution(event);\r
-               slcExecution.getSteps().add(step);\r
+               slcExecution.getSteps().add(new SlcExecutionStep("LOG", msg));\r
 \r
-               notifyStep(slcExecution, step);\r
+               notifyStep(slcExecution, slcExecution.currentStep());\r
+               currentStepNotified = true;\r
        }\r
 \r
        protected void notifyStep(SlcExecution slcExecution, SlcExecutionStep step) {\r
@@ -165,32 +160,37 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements
 \r
        @Override\r
        protected void append(LoggingEvent event) {\r
-               if(isLogging){\r
+               if (isLogging) {\r
                        // avoid StackOverflow if notification calls Log4j itself.\r
                        return;\r
                }\r
+\r
+               if (event.getLoggerName().equals(\r
+                               WebServiceSlcExecutionNotifier.class.getName())) {\r
+                       return;\r
+               }\r
+\r
                isLogging = true;\r
-               \r
+\r
                try {\r
                        SlcExecution slcExecution = (SlcExecution) project\r
                                        .getReference(REF_SLC_EXECUTION);\r
                        if (slcExecution != null) {\r
-                               SlcExecutionStep step = currentStep.get(slcExecution);\r
-                               if (step == null) {\r
-                                       step = new SlcExecutionStep("LOG", event.getMessage()\r
-                                                       .toString());\r
-                                       notifyStep(slcExecution, step);\r
-                               } else {\r
-                                       step.addLog(event.getMessage().toString());\r
+                               if (currentStepNotified) {\r
+                                       slcExecution.getSteps().add(\r
+                                                       new SlcExecutionStep("LOG", event.getMessage()\r
+                                                                       .toString()));\r
+                                       currentStepNotified = false;\r
                                }\r
+                               slcExecution.currentStep()\r
+                                               .addLog(event.getMessage().toString());\r
                        } else {\r
                                // TODO: log before initialization?\r
                        }\r
-               } finally{\r
+               } finally {\r
                        isLogging = false;\r
                }\r
-               \r
-               \r
+\r
        }\r
 \r
        @Override\r
@@ -205,4 +205,8 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements
                return false;\r
        }\r
 \r
+       public Project getProject() {\r
+               return project;\r
+       }\r
+\r
 }\r