]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/process/SlcExecutionStep.java
Remove runtime packages
[gpl/argeo-slc.git] / runtime / org.argeo.slc.specs / src / main / java / org / argeo / slc / process / SlcExecutionStep.java
index 59cf231d834cdee6299e0008d0ed493f1189f36a..f88cf2533d7d56f06ef027f24ac36a83d98fd3be 100644 (file)
@@ -1,32 +1,62 @@
+/*\r
+ * Copyright (C) 2007-2012 Argeo GmbH\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *         http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
 package org.argeo.slc.process;\r
 \r
-import java.io.IOException;\r
-import java.io.StringReader;\r
-import java.io.StringWriter;\r
+import java.util.ArrayList;\r
 import java.util.Date;\r
 import java.util.List;\r
+import java.util.StringTokenizer;\r
 import java.util.UUID;\r
-import java.util.Vector;\r
 \r
-import org.apache.commons.io.IOUtils;\r
+import org.argeo.slc.execution.ExecutionStep;\r
 \r
-public class SlcExecutionStep {\r
-       public final static String TYPE_LOG = "LOG";\r
+/**\r
+ * An atomic step to be notified in during an {@link SlcExecution}. Can be a log\r
+ * or the start/end of a phase, etc.\r
+ * \r
+ * @deprecated use {@link ExecutionStep} instead\r
+ */\r
+public class SlcExecutionStep extends ExecutionStep {\r
+       private static final long serialVersionUID = -7308643628104726471L;\r
 \r
-       private String uuid;\r
-       private String type;\r
-       private Date begin;\r
-       private List<String> logLines = new Vector<String>();\r
+       private String uuid = UUID.randomUUID().toString();\r
+       private List<String> logLines = new ArrayList<String>();\r
 \r
        /** Empty constructor */\r
        public SlcExecutionStep() {\r
        }\r
 \r
+       /** Creates a step at the current date of type INFO */\r
        public SlcExecutionStep(String log) {\r
-               this.type = TYPE_LOG;\r
-               this.begin = new Date();\r
-               this.uuid = UUID.randomUUID().toString();\r
-               addLog(log);\r
+               this(new Date(), INFO, log);\r
+       }\r
+\r
+       /** Creates a step at the current date */\r
+       public SlcExecutionStep(String type, String log) {\r
+               this(new Date(), type, log);\r
+       }\r
+\r
+       /** Creates a step of the given type. */\r
+       public SlcExecutionStep(Date timestamp, String type, String log) {\r
+               this(timestamp, type, log, Thread.currentThread().getName());\r
+       }\r
+\r
+       public SlcExecutionStep(Date timestamp, String type, String log,\r
+                       String thread) {\r
+               super("UNKOWN_LOCATION", timestamp, type, log, thread);\r
        }\r
 \r
        public String getUuid() {\r
@@ -37,20 +67,16 @@ public class SlcExecutionStep {
                this.uuid = uuid;\r
        }\r
 \r
-       public String getType() {\r
-               return type;\r
-       }\r
-\r
        public void setType(String type) {\r
                this.type = type;\r
        }\r
 \r
-       public Date getBegin() {\r
-               return begin;\r
+       public void setTimestamp(Date begin) {\r
+               this.timestamp = begin;\r
        }\r
 \r
-       public void setBegin(Date begin) {\r
-               this.begin = begin;\r
+       public void setThread(String thread) {\r
+               this.thread = thread;\r
        }\r
 \r
        public List<String> getLogLines() {\r
@@ -61,23 +87,46 @@ public class SlcExecutionStep {
                this.logLines = logLines;\r
        }\r
 \r
-       public String logAsString() {\r
-               StringWriter writer = new StringWriter();\r
-               String log = writer.toString();\r
-               IOUtils.closeQuietly(writer);\r
-               return log;\r
-       }\r
+       /** public for legacy reasons */\r
+       public String addLog(String log) {\r
+               if (logLines == null)\r
+                       logLines = new ArrayList<String>();\r
 \r
-       public void addLog(String log) {\r
                if (log == null)\r
-                       return;\r
+                       return null;\r
 \r
-               try {\r
-                       List<String> lines = IOUtils.readLines(new StringReader(log));\r
-                       logLines.addAll(lines);\r
-               } catch (IOException e) {\r
-                       throw new RuntimeException("Cannot add log", e);\r
+               StringTokenizer st = new StringTokenizer(log, "\n");\r
+               while (st.hasMoreTokens())\r
+                       logLines.add(removeNonXmlChars(st.nextToken()));\r
+               return null;\r
+       }\r
+\r
+       /**\r
+        * Removes non XML compliant characters (from\r
+        * http://stackoverflow.com/questions\r
+        * /20762/how-do-you-remove-invalid-hexadecimal\r
+        * -characters-from-an-xml-based-data-source-pr)\r
+        */\r
+       private static String removeNonXmlChars(String inString) {\r
+               if (inString == null)\r
+                       return null;\r
+\r
+               StringBuilder newString = new StringBuilder();\r
+               char ch;\r
+\r
+               for (int i = 0; i < inString.length(); i++) {\r
+\r
+                       ch = inString.charAt(i);\r
+                       // remove any characters outside the valid UTF-8 range as well as\r
+                       // all control characters\r
+                       // except tabs and new lines\r
+                       if ((ch < 0x00FD && ch > 0x001F) || ch == '\t' || ch == '\n'\r
+                                       || ch == '\r') {\r
+                               newString.append(ch);\r
+                       }\r
                }\r
+               return newString.toString();\r
+\r
        }\r
 \r
        @Override\r