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