]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/process/SlcExecutionStep.java
Document and improve execution model
[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 /**
27 * An atomic step to be notified in during an {@link SlcExecution}. Can be a log
28 * or the start/end of a phase, etc.
29 */
30 public class SlcExecutionStep implements Serializable {
31 private static final long serialVersionUID = -7308643628104726471L;
32
33 public final static String START = "START";
34 public final static String END = "END";
35 public final static String PHASE_START = "PHASE_START";
36 public final static String PHASE_END = "PHASE_END";
37 public final static String ERROR = "ERROR";
38 public final static String WARNING = "WARNING";
39 public final static String INFO = "INFO";
40 public final static String DEBUG = "DEBUG";
41 public final static String TRACE = "TRACE";
42
43 private String uuid = UUID.randomUUID().toString();
44 private String type;
45 private String thread;
46 private Date timestamp = new Date();
47 private List<String> logLines = new ArrayList<String>();
48
49 /** Empty constructor */
50 public SlcExecutionStep() {
51 thread = Thread.currentThread().getName();
52 }
53
54 /** Creates a step at the current date of type INFO */
55 public SlcExecutionStep(String log) {
56 this(new Date(), INFO, log);
57 }
58
59 /** Creates a step at the current date */
60 public SlcExecutionStep(String type, String log) {
61 this(new Date(), type, log);
62 }
63
64 /** Creates a step of the given type. */
65 public SlcExecutionStep(Date timestamp, String type, String log) {
66 this(timestamp, type, log, Thread.currentThread().getName());
67 }
68
69 public SlcExecutionStep(Date timestamp, String type, String log,
70 String thread) {
71 this.type = type;
72 this.timestamp = timestamp;
73 this.thread = thread;
74 addLog(log);
75 }
76
77 public String getUuid() {
78 return uuid;
79 }
80
81 public void setUuid(String uuid) {
82 this.uuid = uuid;
83 }
84
85 public String getType() {
86 return type;
87 }
88
89 public void setType(String type) {
90 this.type = type;
91 }
92
93 public Date getTimestamp() {
94 return timestamp;
95 }
96
97 public void setTimestamp(Date begin) {
98 this.timestamp = begin;
99 }
100
101 public String getThread() {
102 return thread;
103 }
104
105 public void setThread(String thread) {
106 this.thread = thread;
107 }
108
109 public List<String> getLogLines() {
110 return logLines;
111 }
112
113 public void setLogLines(List<String> logLines) {
114 this.logLines = logLines;
115 }
116
117 public void addLog(String log) {
118 if (log == null)
119 return;
120
121 StringTokenizer st = new StringTokenizer(log, "\n");
122 while (st.hasMoreTokens())
123 logLines.add(removeNonXmlChars(st.nextToken()));
124 }
125
126 /**
127 * Removes non XML compliant characters (from
128 * http://stackoverflow.com/questions
129 * /20762/how-do-you-remove-invalid-hexadecimal
130 * -characters-from-an-xml-based-data-source-pr)
131 */
132 private static String removeNonXmlChars(String inString) {
133 if (inString == null)
134 return null;
135
136 StringBuilder newString = new StringBuilder();
137 char ch;
138
139 for (int i = 0; i < inString.length(); i++) {
140
141 ch = inString.charAt(i);
142 // remove any characters outside the valid UTF-8 range as well as
143 // all control characters
144 // except tabs and new lines
145 if ((ch < 0x00FD && ch > 0x001F) || ch == '\t' || ch == '\n'
146 || ch == '\r') {
147 newString.append(ch);
148 }
149 }
150 return newString.toString();
151
152 }
153
154 @Override
155 public String toString() {
156 return getClass().getSimpleName() + "#" + uuid;
157 }
158
159 }