]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/process/SlcExecutionStep.java
Improve execution
[gpl/argeo-slc.git] / runtime / org.argeo.slc.specs / src / main / java / org / argeo / slc / process / SlcExecutionStep.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.process;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.StringTokenizer;
24 import java.util.UUID;
25
26 public class SlcExecutionStep implements Serializable {
27 private static final long serialVersionUID = -7308643628104726471L;
28
29 public final static String START = "START";
30 public final static String END = "END";
31 public final static String PHASE_START = "PHASE_START";
32 public final static String PHASE_END = "PHASE_END";
33 public final static String ERROR = "ERROR";
34 public final static String WARNING = "WARNING";
35 public final static String INFO = "INFO";
36 public final static String DEBUG = "DEBUG";
37 public final static String TRACE = "TRACE";
38
39 private String uuid = UUID.randomUUID().toString();
40 private String type;
41 private String thread;
42 private Date timestamp = new Date();
43 private List<String> logLines = new ArrayList<String>();
44
45 /** Empty constructor */
46 public SlcExecutionStep() {
47 thread = Thread.currentThread().getName();
48 }
49
50 /** Creates a step at the current date of type INFO */
51 public SlcExecutionStep(String log) {
52 this(new Date(), INFO, log);
53 }
54
55 /** Creates a step at the current date */
56 public SlcExecutionStep(String type, String log) {
57 this(new Date(), type, log);
58 }
59
60 /** Creates a step of the given type. */
61 public SlcExecutionStep(Date timestamp, String type, String log) {
62 this(timestamp, type, log, Thread.currentThread().getName());
63 }
64
65 public SlcExecutionStep(Date timestamp, String type, String log,
66 String thread) {
67 this.type = type;
68 this.timestamp = timestamp;
69 this.thread = thread;
70 addLog(log);
71 }
72
73 public String getUuid() {
74 return uuid;
75 }
76
77 public void setUuid(String uuid) {
78 this.uuid = uuid;
79 }
80
81 public String getType() {
82 return type;
83 }
84
85 public void setType(String type) {
86 this.type = type;
87 }
88
89 public Date getTimestamp() {
90 return timestamp;
91 }
92
93 public void setTimestamp(Date begin) {
94 this.timestamp = begin;
95 }
96
97 public String getThread() {
98 return thread;
99 }
100
101 public void setThread(String thread) {
102 this.thread = thread;
103 }
104
105 public List<String> getLogLines() {
106 return logLines;
107 }
108
109 public void setLogLines(List<String> logLines) {
110 this.logLines = logLines;
111 }
112
113 public void addLog(String log) {
114 if (log == null)
115 return;
116
117 StringTokenizer st = new StringTokenizer(log, "\n");
118 while (st.hasMoreTokens())
119 logLines.add(removeNonXmlChars(st.nextToken()));
120 }
121
122 /**
123 * Removes non XML compliant characters (from
124 * http://stackoverflow.com/questions
125 * /20762/how-do-you-remove-invalid-hexadecimal
126 * -characters-from-an-xml-based-data-source-pr)
127 */
128 private static String removeNonXmlChars(String inString) {
129 if (inString == null)
130 return null;
131
132 StringBuilder newString = new StringBuilder();
133 char ch;
134
135 for (int i = 0; i < inString.length(); i++) {
136
137 ch = inString.charAt(i);
138 // remove any characters outside the valid UTF-8 range as well as
139 // all control characters
140 // except tabs and new lines
141 if ((ch < 0x00FD && ch > 0x001F) || ch == '\t' || ch == '\n'
142 || ch == '\r') {
143 newString.append(ch);
144 }
145 }
146 return newString.toString();
147
148 }
149
150 @Override
151 public String toString() {
152 return getClass().getSimpleName() + "#" + uuid;
153 }
154
155 }