]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/msg/ExecutionAnswer.java
Move NameVersion
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / msg / ExecutionAnswer.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.msg;
18
19 import java.io.PrintWriter;
20 import java.io.Serializable;
21 import java.io.StringWriter;
22
23 import org.apache.commons.io.IOUtils;
24 import org.argeo.slc.SlcException;
25
26 /** Answer to an execution of a remote service which performed changes. */
27 public class ExecutionAnswer implements Serializable {
28 private static final long serialVersionUID = -3268867743181316160L;
29 public final static String OK = "OK";
30 public final static String ERROR = "ERROR";
31
32 private String status = OK;
33 private String message = "";
34
35 /** Canonical constructor */
36 public ExecutionAnswer(String status, String message) {
37 setStatus(status);
38 if (message == null)
39 throw new SlcException("Message cannot be null");
40 this.message = message;
41 }
42
43 /** Empty constructor */
44 public ExecutionAnswer() {
45 }
46
47 public String getStatus() {
48 return status;
49 }
50
51 public void setStatus(String status) {
52 if (status == null || (!status.equals(OK) && !status.equals(ERROR)))
53 throw new SlcException("Bad status format: " + status);
54 this.status = status;
55 }
56
57 public String getMessage() {
58 return message;
59 }
60
61 public void setMessage(String message) {
62 this.message = message;
63 }
64
65 public Boolean isOk() {
66 return status.equals(OK);
67 }
68
69 public Boolean isError() {
70 return status.equals(ERROR);
71 }
72
73 public static ExecutionAnswer error(String message) {
74 return new ExecutionAnswer(ERROR, message);
75 }
76
77 public static ExecutionAnswer error(Throwable e) {
78 StringWriter writer = new StringWriter();
79 try {
80 e.printStackTrace(new PrintWriter(writer));
81 return new ExecutionAnswer(ERROR, writer.toString());
82 } finally {
83 IOUtils.closeQuietly(writer);
84 }
85 }
86
87 public static ExecutionAnswer ok(String message) {
88 return new ExecutionAnswer(OK, message);
89 }
90
91 }