1 package org.ibex.xt.shell;
6 public class Shell extends Env {
8 public static void main(String[] args) throws Exception {
9 if (args.length == 0 || args.length > 2|| !args[0].startsWith("http://")) {
12 Shell shell = new Shell(new URL(args[0]));
14 if (args.length == 2) {
17 shell.listen(new InputStreamReader(System.in), new OutputStreamWriter(System.out));
21 private static void printUsage() {
22 System.out.println("Usage: xish url [command]");
28 /** Server cookie. Reduces server load. */
29 private String cookie = null;
31 /** Create a new Shell using the given url for the server. */
32 public Shell(URL url) {
34 commands = new Command[] {
43 public void listen(Reader r, Writer w) throws IOException {
44 LineNumberReader in = new LineNumberReader(r);
45 PrintWriter out = new PrintWriter(w);
47 out.println("ibex xt shell: type help or exit");
53 while ((line = in.readLine()) != null) {
54 if (line.length() > 0) {
55 if (line.startsWith("exit")) return;
56 if (line.charAt(line.length() - 1) == '\\') {
57 buffer += line.substring(0, line.length() - 1);
59 out.flush(); continue;
65 if (buffer.length() > 0) {
66 String[] c = buffer.split(" ");
67 Command cmd = command(c[0]);
71 w.write(": command not found\n");
72 } else cmd.execute(out, c, this);
81 public Request.Response send(Request request) throws IOException {
82 URLConnection c = server.openConnection();
83 ((HttpURLConnection)c).setRequestMethod("POST");
85 if (cookie != null) c.setRequestProperty("Cookie", cookie);
89 ObjectOutputStream out = new ObjectOutputStream(c.getOutputStream());
90 out.writeObject(request);
93 String cook = c.getHeaderField("Set-Cookie");
94 if (cook != null && !cook.equals("")) cookie = cook.substring(0, cook.indexOf(';'));
97 Object o = new ObjectInputStream(c.getInputStream()).readObject();
99 throw new IOException("unexpected null object returned");
100 } else if (!(o instanceof Request.Response)) {
101 throw new IOException("unexpected object returned: "+o.getClass().getName());
103 return (Request.Response)o;
105 } catch (ClassNotFoundException e) {
107 throw new IOException("unexpected ClassNotFoundException");