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