+/*\r
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>\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
+\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.io.Serializable;\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
+public class SlcExecutionStep implements Serializable {\r
+ private static final long serialVersionUID = -7308643628104726471L;\r
\r
-public class SlcExecutionStep {\r
- public final static String TYPE_LOG = "LOG";\r
+ public final static String START = "START";\r
+ public final static String END = "END";\r
+ public final static String PHASE_START = "PHASE_START";\r
+ public final static String PHASE_END = "PHASE_END";\r
+ public final static String ERROR = "ERROR";\r
+ public final static String WARNING = "WARNING";\r
+ public final static String INFO = "INFO";\r
+ public final static String DEBUG = "DEBUG";\r
+ public final static String TRACE = "TRACE";\r
\r
- private String uuid;\r
+ private String uuid = UUID.randomUUID().toString();\r
private String type;\r
- private Date begin;\r
- private List<String> logLines = new Vector<String>();\r
+ private String thread;\r
+ private Date timestamp = new Date();\r
+ private List<String> logLines = new ArrayList<String>();\r
\r
/** Empty constructor */\r
public SlcExecutionStep() {\r
+ thread = Thread.currentThread().getName();\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
+ 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
+ this.type = type;\r
+ this.timestamp = timestamp;\r
+ this.thread = thread;\r
addLog(log);\r
}\r
\r
this.type = type;\r
}\r
\r
- public Date getBegin() {\r
- return begin;\r
+ public Date getTimestamp() {\r
+ return timestamp;\r
}\r
\r
- public void setBegin(Date begin) {\r
- this.begin = begin;\r
+ public void setTimestamp(Date begin) {\r
+ this.timestamp = begin;\r
+ }\r
+\r
+ public String getThread() {\r
+ return thread;\r
+ }\r
+\r
+ public void setThread(String thread) {\r
+ this.thread = thread;\r
}\r
\r
public List<String> getLogLines() {\r
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
-\r
public void addLog(String log) {\r
if (log == null)\r
return;\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
+ }\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