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 new NoCommand();
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(Throwable throwable) {
059        return new Error(ErrorType.EVALUATION, throwable);
060      }
061    
062      public static Error evalError(String msg, Throwable throwable) {
063        return new Error(ErrorType.EVALUATION, msg, throwable);
064      }
065    
066      public static Error evalError(String msg) {
067        return new Error(ErrorType.EVALUATION, msg);
068      }
069    
070      public static Error internalError(Throwable throwable) {
071        return new Error(ErrorType.INTERNAL, throwable);
072      }
073    
074      public static Error internalError(String msg, Throwable throwable) {
075        return new Error(ErrorType.INTERNAL, msg, throwable);
076      }
077    
078      public static Error internalError(String msg) {
079        return new Error(ErrorType.INTERNAL, msg);
080      }
081    
082      public static Error error(ErrorType type, Throwable throwable) {
083        return new Error(type, throwable);
084      }
085    
086      public static Error error(ErrorType type, String msg, Throwable throwable) {
087        return new Error(type, msg, throwable);
088      }
089    
090      public static Error error(ErrorType type, String msg) {
091        return new Error(type, msg);
092      }
093    
094      public static Cancelled cancelled() {
095        return new Cancelled();
096      }
097    
098      public abstract String getText();
099    
100      public static class UnknownCommand extends ShellResponse {
101    
102        /** . */
103        private final String name;
104    
105        private UnknownCommand(String name) {
106          this.name = name;
107        }
108    
109        public String getName() {
110          return name;
111        }
112    
113        @Override
114        public String getText() {
115          return "Unknown command " + name;
116        }
117      }
118    
119      public static class NoCommand extends ShellResponse {
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        @Override
133        public String getText() {
134          return "Have a good day!\r\n";
135        }
136      }
137    
138      /**
139       * Command execution is terminated.
140       */
141      public static class Ok extends ShellResponse {
142    
143        /** . */
144        private final Iterable<?> produced;
145    
146        private Ok() {
147          this(Collections.<Object>emptyList());
148        }
149    
150        private Ok(Iterable<?> produced) {
151          this.produced = produced;
152        }
153    
154        public Iterable<?> getProduced() {
155          return produced;
156        }
157    
158        @Override
159        public String getText() {
160          return "";
161        }
162      }
163    
164      public static class Display extends Ok {
165    
166        /** . */
167        private final String text;
168    
169        private Display(String text) {
170          this.text = text;
171        }
172    
173        private Display(Iterable<?> produced, String text) {
174          super(produced);
175    
176          //
177          this.text = text;
178        }
179    
180        @Override
181        public boolean equals(Object obj) {
182          if (obj == this) {
183            return true;
184          }
185          if (obj instanceof Display) {
186            Display that = (Display)obj;
187            return text.equals(that.text);
188          }
189          return false;
190        }
191    
192        @Override
193        public String getText() {
194          return text;
195        }
196      }
197    
198      public static class Cancelled extends ShellResponse {
199        private Cancelled() {
200        }
201    
202        @Override
203        public String getText() {
204          return "cancelled" ;
205        }
206      }
207    
208      public static class Error extends ShellResponse {
209    
210        /** . */
211        private final ErrorType type;
212    
213        /** . */
214        private final Throwable throwable;
215    
216        private final String msg;
217    
218        private Error(ErrorType type, Throwable throwable) {
219          this.type = type;
220          this.msg = build(throwable);
221          this.throwable = throwable;
222        }
223    
224        private Error(ErrorType type, String msg) {
225          this.type = type;
226          this.msg = msg;
227          this.throwable = null;
228        }
229    
230        private Error(ErrorType type, String msg, Throwable throwable) {
231          this.type = type;
232          this.msg = msg;
233          this.throwable = throwable;
234        }
235    
236        public ErrorType getType() {
237          return type;
238        }
239    
240        public Throwable getThrowable() {
241          return throwable;
242        }
243    
244        @Override
245        public String getText() {
246          return msg;
247        }
248    
249        private static String build(Throwable throwable) {
250          String result;
251          String msg = throwable.getMessage();
252          if (msg == null) {
253            msg = throwable.getClass().getSimpleName();
254          }
255          if (throwable instanceof ScriptException) {
256            result = "Error: " + msg;
257          } else if (throwable instanceof RuntimeException) {
258            result = "Unexpected exception: " + msg;
259          } else if (throwable instanceof Exception) {
260            result = "Unexpected exception: " + msg;
261          } else if (throwable instanceof java.lang.Error) {
262            result = "Unexpected error: " + msg;
263          } else {
264            result = "Unexpected throwable: " + msg;
265          }
266          return result;
267        }
268    
269        public String toString() {
270          return "ShellResponse.Error[type=" + type + ",msg=" + msg + "]";
271        }
272      }
273    }