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