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     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
028     * @version $Revision$
029     */
030    public abstract class ShellResponse {
031    
032      public abstract String getText();
033    
034      public static class SyntaxError extends ShellResponse {
035    
036        public SyntaxError() {
037        }
038    
039        @Override
040        public String getText() {
041          return "Syntax error";
042        }
043      }
044    
045      public static class UnknownCommand extends ShellResponse {
046    
047        /** . */
048        private final String name;
049    
050        public UnknownCommand(String name) {
051          this.name = name;
052        }
053    
054        public String getName() {
055          return name;
056        }
057    
058        @Override
059        public String getText() {
060          return "Unknown command " + name;
061        }
062      }
063    
064      public static class NoCommand extends ShellResponse {
065        @Override
066        public String getText() {
067          return "Please type something";
068        }
069      }
070    
071      public static class Close extends ShellResponse {
072    
073        @Override
074        public String getText() {
075          return "Have a good day!\r\n";
076        }
077      }
078    
079      /**
080       * Command execution is terminated.
081       */
082      public static class Ok extends ShellResponse {
083    
084        /** . */
085        private final Iterable<?> produced;
086    
087        public Ok() {
088          this(Collections.<Object>emptyList());
089        }
090    
091        public Ok(Iterable<?> produced) {
092          this.produced = produced;
093        }
094    
095        public Iterable<?> getProduced() {
096          return produced;
097        }
098    
099        @Override
100        public String getText() {
101          return "";
102        }
103      }
104    
105      public static class Display extends Ok {
106    
107        /** . */
108        private final String text;
109    
110        public Display(String text) {
111          this.text = text;
112        }
113    
114        public Display(Iterable<?> produced, String text) {
115          super(produced);
116    
117          //
118          this.text = text;
119        }
120    
121        @Override
122        public String getText() {
123          return text;
124        }
125      }
126    
127      public static class Cancelled extends ShellResponse {
128        @Override
129        public String getText() {
130          return "cancelled" ;
131        }
132      }
133    
134      public static class Error extends ShellResponse {
135    
136        /** . */
137        private final ErrorType type;
138    
139        /** . */
140        private final Throwable throwable;
141    
142        private final String msg;
143    
144        public Error(ErrorType type, Throwable throwable) {
145          this.type = type;
146          this.msg = build(throwable);
147          this.throwable = throwable;
148        }
149    
150        public Error(ErrorType type, String msg) {
151          this.type = type;
152          this.msg = msg;
153          this.throwable = null;
154        }
155    
156        public Error(ErrorType type, String msg, Throwable throwable) {
157          this.type = type;
158          this.msg = msg;
159          this.throwable = throwable;
160        }
161    
162        public ErrorType getType() {
163          return type;
164        }
165    
166        public Throwable getThrowable() {
167          return throwable;
168        }
169    
170        @Override
171        public String getText() {
172          return msg;
173        }
174    
175        private static String build(Throwable throwable) {
176          String result;
177          String msg = throwable.getMessage();
178          if (msg == null) {
179            msg = throwable.getClass().getSimpleName();
180          }
181          if (throwable instanceof ScriptException) {
182            result = "Error: " + msg;
183          } else if (throwable instanceof RuntimeException) {
184            result = "Unexpected exception: " + msg;
185          } else if (throwable instanceof Exception) {
186            result = "Unexpected exception: " + msg;
187          } else if (throwable instanceof java.lang.Error) {
188            result = "Unexpected error: " + msg;
189          } else {
190            result = "Unexpected throwable: " + msg;
191          }
192          return result;
193        }
194    
195        public String toString() {
196          return "ShellResponse.Error[type=" + type + ",msg=" + msg + "]";
197        }
198      }
199    }