001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.shell;
021
022 import java.io.Serializable;
023 import java.util.Collections;
024
025 public abstract class ShellResponse implements Serializable {
026
027 public static UnknownCommand unknownCommand(String name) {
028 return new UnknownCommand(name);
029 }
030
031 public static NoCommand noCommand() {
032 return NoCommand.INSTANCE;
033 }
034
035 public static Ok ok(Iterable<?> produced) {
036 return new Ok(produced);
037 }
038
039 public static Ok ok() {
040 return new Ok();
041 }
042
043 public static Error evalError(String msg, Throwable throwable) {
044 return new Error(ErrorType.EVALUATION, msg, throwable);
045 }
046
047 public static Error evalError(String msg) {
048 return new Error(ErrorType.EVALUATION, msg);
049 }
050
051 public static Error internalError(String msg, Throwable throwable) {
052 return new Error(ErrorType.INTERNAL, msg, throwable);
053 }
054
055 public static Error internalError(String msg) {
056 return new Error(ErrorType.INTERNAL, msg);
057 }
058
059 public static Error error(ErrorType type, String msg, Throwable throwable) {
060 return new Error(type, msg, throwable);
061 }
062
063 public static Error error(ErrorType type, String msg) {
064 return new Error(type, msg);
065 }
066
067 public static Cancelled cancelled() {
068 return Cancelled.INSTANCE;
069 }
070
071 public static Close close() {
072 return Close.INSTANCE;
073 }
074
075 public abstract String getMessage();
076
077 public static class UnknownCommand extends ShellResponse {
078
079 /** . */
080 private final String name;
081
082 private UnknownCommand(String name) {
083 this.name = name;
084 }
085
086 public String getName() {
087 return name;
088 }
089
090 @Override
091 public String getMessage() {
092 return name + ": command not found";
093 }
094
095 @Override
096 public String toString() {
097 return "UnknownCommand[" + name + "]";
098 }
099 }
100
101 public static class NoCommand extends ShellResponse {
102
103 /** . */
104 private static final NoCommand INSTANCE = new NoCommand();
105
106 private NoCommand() {
107 }
108
109 @Override
110 public String getMessage() {
111 return "";
112 }
113 }
114
115 public static class Close extends ShellResponse {
116
117 /** . */
118 private static final Close INSTANCE = new Close();
119
120 private Close() {
121 }
122
123 @Override
124 public String getMessage() {
125 return "Have a good day!\r\n";
126 }
127 }
128
129 /**
130 * Command execution is terminated.
131 */
132 public static class Ok extends ShellResponse {
133
134 /** . */
135 private final transient Iterable<?> produced;
136
137 private Ok() {
138 this(Collections.<Object>emptyList());
139 }
140
141 private Ok(Iterable<?> produced) {
142 this.produced = produced;
143 }
144
145 public Iterable<?> getProduced() {
146 return produced;
147 }
148
149 @Override
150 public String getMessage() {
151 return "";
152 }
153 }
154
155 public static class Cancelled extends ShellResponse {
156
157 /** . */
158 private static final Cancelled INSTANCE = new Cancelled();
159
160 private Cancelled() {
161 }
162
163 @Override
164 public String getMessage() {
165 return "";
166 }
167 }
168
169 public static class Error extends ShellResponse {
170
171 /** . */
172 private final ErrorType type;
173
174 /** The throwable. */
175 private final Throwable throwable;
176
177 /** . */
178 private final String msg;
179
180 private Error(ErrorType type, String msg) {
181 this.type = type;
182 this.msg = msg;
183 this.throwable = null;
184 }
185
186 private Error(ErrorType type, String msg, Throwable throwable) {
187 this.type = type;
188 this.msg = msg;
189 this.throwable = throwable;
190 }
191
192 public ErrorType getType() {
193 return type;
194 }
195
196 public Throwable getThrowable() {
197 return throwable;
198 }
199
200 @Override
201 public String getMessage() {
202 return msg;
203 }
204
205 public String toString() {
206 return "ShellResponse.Error[type=" + type + ",msg=" + msg + "]";
207 }
208 }
209 }