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