001 /*
002 * Copyright (C) 2003-2009 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 org.crsh.command.ScriptException;
023
024 import java.util.Collections;
025
026 /**
027 * The response of a shell invocation.
028 *
029 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
030 * @version $Revision$
031 */
032 public abstract class ShellResponse {
033
034 public static UnknownCommand unknownCommand(String name) {
035 return new UnknownCommand(name);
036 }
037
038 public static NoCommand noCommand() {
039 return NoCommand.INSTANCE;
040 }
041
042 public static Ok ok(Iterable<?> produced) {
043 return new Ok(produced);
044 }
045
046 public static Ok ok() {
047 return new Ok();
048 }
049
050 public static Display display(String text) {
051 return new Display(text);
052 }
053
054 public static Display display(Iterable<?> produced, String text) {
055 return new Display(produced, text);
056 }
057
058 public static Error evalError(String msg, Throwable throwable) {
059 return new Error(ErrorType.EVALUATION, msg, throwable);
060 }
061
062 public static Error evalError(String msg) {
063 return new Error(ErrorType.EVALUATION, msg);
064 }
065
066 public static Error internalError(String msg, Throwable throwable) {
067 return new Error(ErrorType.INTERNAL, msg, throwable);
068 }
069
070 public static Error internalError(String msg) {
071 return new Error(ErrorType.INTERNAL, msg);
072 }
073
074 public static Error error(ErrorType type, String msg, Throwable throwable) {
075 return new Error(type, msg, throwable);
076 }
077
078 public static Error error(ErrorType type, String msg) {
079 return new Error(type, msg);
080 }
081
082 public static Cancelled cancelled() {
083 return Cancelled.INSTANCE;
084 }
085
086 public static Close close() {
087 return Close.INSTANCE;
088 }
089
090 public abstract String getText();
091
092 public static class UnknownCommand extends ShellResponse {
093
094 /** . */
095 private final String name;
096
097 private UnknownCommand(String name) {
098 this.name = name;
099 }
100
101 public String getName() {
102 return name;
103 }
104
105 @Override
106 public String getText() {
107 return name + ": command not found";
108 }
109
110 @Override
111 public String toString() {
112 return "UnknownCommand[" + name + "]";
113 }
114 }
115
116 public static class NoCommand extends ShellResponse {
117
118 /** . */
119 private static final NoCommand INSTANCE = new NoCommand();
120
121 private NoCommand() {
122 }
123
124 @Override
125 public String getText() {
126 return "Please type something";
127 }
128 }
129
130 public static class Close extends ShellResponse {
131
132 /** . */
133 private static final Close INSTANCE = new Close();
134
135 private Close() {
136 }
137
138 @Override
139 public String getText() {
140 return "Have a good day!\r\n";
141 }
142 }
143
144 /**
145 * Command execution is terminated.
146 */
147 public static class Ok extends ShellResponse {
148
149 /** . */
150 private final Iterable<?> produced;
151
152 private Ok() {
153 this(Collections.<Object>emptyList());
154 }
155
156 private Ok(Iterable<?> produced) {
157 this.produced = produced;
158 }
159
160 public Iterable<?> getProduced() {
161 return produced;
162 }
163
164 @Override
165 public String getText() {
166 return "";
167 }
168 }
169
170 public static class Display extends Ok {
171
172 /** . */
173 private final String text;
174
175 private Display(String text) {
176 this.text = text;
177 }
178
179 private Display(Iterable<?> produced, String text) {
180 super(produced);
181
182 //
183 this.text = text;
184 }
185
186 @Override
187 public boolean equals(Object obj) {
188 if (obj == this) {
189 return true;
190 }
191 if (obj instanceof Display) {
192 Display that = (Display)obj;
193 return text.equals(that.text);
194 }
195 return false;
196 }
197
198 @Override
199 public String getText() {
200 return text;
201 }
202 }
203
204 public static class Cancelled extends ShellResponse {
205
206 /** . */
207 private static final Cancelled INSTANCE = new Cancelled();
208
209 private Cancelled() {
210 }
211
212 @Override
213 public String getText() {
214 return "cancelled" ;
215 }
216 }
217
218 public static class Error extends ShellResponse {
219
220 /** . */
221 private final ErrorType type;
222
223 /** . */
224 private final Throwable throwable;
225
226 /** . */
227 private final String msg;
228
229 private Error(ErrorType type, String msg) {
230 this.type = type;
231 this.msg = msg;
232 this.throwable = null;
233 }
234
235 private Error(ErrorType type, String msg, Throwable throwable) {
236 this.type = type;
237 this.msg = msg;
238 this.throwable = throwable;
239 }
240
241 public ErrorType getType() {
242 return type;
243 }
244
245 public Throwable getThrowable() {
246 return throwable;
247 }
248
249 @Override
250 public String getText() {
251 return msg;
252 }
253
254 public String toString() {
255 return "ShellResponse.Error[type=" + type + ",msg=" + msg + "]";
256 }
257 }
258 }