2003/12/13 00:38:34
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:42:46 +0000 (07:42 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:42:46 +0000 (07:42 +0000)
darcs-hash:20040130074246-2ba56-a447ebd143b45bfc192e25f868a67774be865566.gz

src/org/xwt/SOAP.java
src/org/xwt/XMLRPC.java
src/org/xwt/XWT.java
src/org/xwt/js/JS.java
src/org/xwt/util/AccessibleCharArrayWriter.java [new file with mode: 0644]
src/org/xwt/util/Log.java

index 4d5a642..104e11d 100644 (file)
@@ -19,15 +19,15 @@ import org.bouncycastle.util.encoders.Base64;
  *      <li> WSDL support
  *  </ul>
  */
-class SOAP extends XMLRPC {
-
-    /** the desired content of the SOAPAction header */
+class SOAP /*extends XMLRPC*/ {
+    /*
+    /&* the desired content of the SOAPAction header &/
     String action = null;
 
-    /** the namespace to use */
+    /&* the namespace to use &/
     String nameSpace = null;
 
-    /** When you get a property from an SOAP, it just returns another SOAP with the property name tacked onto methodname. */
+    /&* When you get a property from an SOAP, it just returns another SOAP with the property name tacked onto methodname. &/
     public Object get(String name) {
         return new SOAP(url.toString(), (methodname.equals("") ? "" : methodname + ".") + name, http, action, nameSpace);
     }
@@ -163,7 +163,7 @@ class SOAP extends XMLRPC {
 
     }
 
-    /** Appends the SOAP representation of <code>o</code> to <code>sb</code> */
+    /&* Appends the SOAP representation of <code>o</code> to <code>sb</code> &/
     void appendObject(String name, Object o, StringBuffer sb) throws JSExn {
         if (o instanceof Number) {
             if ((double)((Number)o).intValue() == ((Number)o).doubleValue()) {
@@ -276,5 +276,5 @@ class SOAP extends XMLRPC {
         this.action = action;
         this.nameSpace = nameSpace;
     }
-
+*/
 }
index 4d541fd..0bdca37 100644 (file)
@@ -32,17 +32,19 @@ import org.bouncycastle.util.encoders.Base64;
  */
 class XMLRPC extends JS {
 
-    /** the url to connect to */
-    protected String url = null;
+    public XMLRPC(String url, String method) { this(url, method, new HTTP(url)); }
+    public XMLRPC(String url, String method, HTTP http) { this.http = http; this.url = url; this.method = method; }
+    public Object get(Object name) { return new XMLRPC(url, (method.equals("") ? "" : method + ".") + name.toString(), http); }
 
-    /** the method name to invoke on the remove server */
-    protected String methodname = null;
 
     /** this holds character content as we read it in -- since there is only one per instance, we don't support mixed content */
     protected AccessibleCharArrayWriter content = new AccessibleCharArrayWriter(100);
+    protected String url = null;       ///< the url to connect to
+    protected String method = null;    ///< the method name to invoke on the remove server
+    protected HTTP http = null;        ///< the HTTP connection to use
+    private Hash tracker;                ///< used to detect multi-ref data
+    protected boolean fault = false;     ///< True iff the return value is a fault (and should be thrown as an exception)
 
-    /** the HTTP connection to use */
-    protected HTTP http = null;
 
     /** The object stack. As we process xml elements, pieces of the
      *  return value are pushed onto and popped off of this stack.
@@ -67,14 +69,8 @@ class XMLRPC extends JS {
      */
     protected Vec objects = null;
 
-    /** used to detect multi-ref data */
-    private Hash tracker;
 
-    /** True iff the return value is a fault (and should be thrown as an exception) */
-    protected boolean fault = false;
-
-
-    // Methods to Recieve and parse XML-RPC Response ////////////////////////////////////////////////////
+    // Recieve ////////////////////////////////////////////////////////////////
 
     private class Helper extends XML {
         public Helper() { super(BUFFER_SIZE); }
@@ -162,7 +158,26 @@ class XMLRPC extends JS {
         public void whitespace(char[] ch, int start, int length) {}
     }
 
-    // Methods to make outbound XML-RPC request ///////////////////////////////////////////////////
+    // Send ///////////////////////////////////////////////////////////////////////////
+
+    protected String buildRequest(JSArray args) throws JSExn, IOException {
+        StringBuffer content = new StringBuffer();
+        content.append("\r\n");
+        content.append("<?xml version=\"1.0\"?>\n");
+        content.append("    <methodCall>\n");
+        content.append("        <method>");
+        content.append(method);
+        content.append("</method>\n");
+        content.append("        <params>\n");
+        for(int i=0; i<args.length(); i++) {
+            content.append("            <param>\n");
+            appendObject(args.elementAt(i), content);
+            content.append("            </param>\n");
+        }
+        content.append("        </params>\n");
+        content.append("    </methodCall>");
+        return content.toString();
+    }
 
     /** Appends the XML-RPC representation of <code>o</code> to <code>sb</code> */
     void appendObject(Object o, StringBuffer sb) throws JSExn {
@@ -277,142 +292,53 @@ class XMLRPC extends JS {
         }
     }
 
-    public Object call_(JSArray args) throws JSExn, IOException {
-        if (Log.verbose) Log.log(this, "call to " + url + " : " + methodname);
-
-        if (tracker == null) tracker = new Hash();
-        else tracker.clear();
-
-        if (objects == null) objects = new Vec();
-        else objects.setSize(0);
-
-        final String content = send(args, http);
-        if (Log.verbose) {
-            String s;
-            BufferedReader br2 = new BufferedReader(new StringReader(content));
-            while ((s = br2.readLine()) != null) Log.log(this, "send: " + s);
-        }
-
-        InputStream is = http.POST("text/xml", content);
-        try {
-            BufferedReader br = !Log.verbose ?
-                new BufferedReader(new InputStreamReader(is)) :
-                new BufferedReader(new FilterReader(new InputStreamReader(is)) {
-                        public int read() throws IOException {
-                            int i = super.read();
-                            if (Log.on) Log.log(this, "recv: " + ((char)i));
-                            return i;
-                        }
-                        public int read(char[] c, int off, int len) throws IOException {
-                            int ret = super.read(c, off, len);
-                            if (ret == -1) return ret;
-                            String s;
-                            BufferedReader br2 = new BufferedReader(new StringReader(new String(c, off, ret)));
-                            while ((s = br2.readLine()) != null) Log.log(this, "recv: " + s);
-                            return ret;
-                        }
-                    });
-            return null;
-        } finally {
-            is.close();
-        }
-    }
 
-    protected String send(JSArray args, HTTP http) throws JSExn, IOException {
-        StringBuffer content = new StringBuffer();
-        content.append("\r\n");
-        content.append("<?xml version=\"1.0\"?>\n");
-        content.append("    <methodCall>\n");
-        content.append("        <methodName>");
-        content.append(methodname);
-        content.append("</methodName>\n");
-        content.append("        <params>\n");
-        for(int i=0; i<args.length(); i++) {
-            content.append("            <param>\n");
-            appendObject(args.elementAt(i), content);
-            content.append("            </param>\n");
-        }
-        content.append("        </params>\n");
-        content.append("    </methodCall>");
-        return content.toString();
-    }
-        
-    protected Object recieve(BufferedReader br) throws JSExn, IOException {
-        // parse XML reply
-        try {
-            new Helper().parse(br);
-        } catch (XML.XMLException e) {
-            if (Log.on) Log.log(this, "reply from server was not well-formed XML: " + e);
-            throw new JSExn("reply from server was not well-formed XML: " + e);
-        }
-        
-        if (fault) throw new JSExn(objects.elementAt(0));
-        if (objects.size() == 0) return null;
-        return objects.elementAt(0);
-    }
+    // Call Sequence //////////////////////////////////////////////////////////////////////////
 
     public final Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
         JSArray args = new JSArray();
         for(int i=0; i<nargs; i++) args.addElement(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
         return call(args);
     }
+
     public final Object call(final JSArray args) throws JSExn {
         try {
             final JS.UnpauseCallback callback = JS.pause();
-            new java.lang.Thread() {
-                public void run() {
-                    try {
-                        try {
-                            final Object ret = call_(args);
-                            Scheduler.add(new Scheduler.Task() {
-                                    public void perform() {
-                                        try {
-                                            callback.unpause(null);
-                                        } catch (JS.PausedException pe) {
-                                            // okay
-                                        } catch (JSExn e) {
-                                            // FIXME
-                                            throw new Error("FIXME");
-                                        }
-                                    }
-                                });
-                        } catch (IOException se) {
-                            if (Log.on) Log.log(this, se);
-                            throw new JSExn("socket exception: " + se);
-                        }
-                    } catch (JSExn e) {
-                        // FIXME
-                        throw new Error("FIXME");
-                    }
-                } }.start();
-            return null;
+            new java.lang.Thread() { public void run() { call(callback, args); }  }.start();
+            return null; // doesn't matter since we paused
         } catch (NotPauseableException npe) {
             throw new JSExn("cannot invoke an XML-RPC call in the foreground thread");
         }
     }
 
-    /** When you get a property from an XMLRPC, it just returns another XMLRPC with the property name tacked onto methodname. */
-    public Object get(Object name) {
-        return new XMLRPC(url, (methodname.equals("") ? "" : methodname + ".") + name.toString(), http);
-    }
-
-    public XMLRPC(String url, String methodname) {
-        this(url, methodname, new HTTP(url));
-    }
-
-    public XMLRPC(String url, String methodname, HTTP http) {
-        this.http = http;
-        this.url = url;
-        this.methodname = methodname;
-    }
-
-
-    // Helper Classes ///////////////////////////////////////////////////////////////////////////////////
-
-    /** CharArrayWriter that lets us touch its buffer */
-    protected static class AccessibleCharArrayWriter extends CharArrayWriter {
-        public char[] getBuf() { return buf; }
-        public AccessibleCharArrayWriter(int i) { super(i); }
+    final void call(final JS.UnpauseCallback callback, final JSArray args) {
+        try {
+            if (Log.verbose) Log.log(this, "call to " + url + " : " + method);
+            String request = buildRequest(args);
+            if (Log.verbose) Log.log(this, "send:\n" + request);
+            InputStream is = http.POST("text/xml", request);
+            BufferedReader br = new BufferedReader(new InputStreamReader(is));
+            if (Log.verbose) br = Log.loggedReader(br);
+            if (tracker == null) tracker = new Hash();
+            if (objects == null) objects = new Vec();
+            try {
+                new Helper().parse(br);
+                final Object result = fault ? new JSExn(objects.elementAt(0)) : objects.size() == 0 ? null : objects.elementAt(0);
+                Scheduler.add(new Scheduler.Task() { public void perform() {
+                    try {
+                        callback.unpause(result);
+                    } catch (Exception e) {
+                        // FIXME
+                        Log.log(this, e);
+                    }
+                }});
+            } finally {
+                tracker.clear();
+                objects.setSize(0);
+            }
+        } catch (Exception e) {
+            // FIXME
+            Log.log(this, e);
+        }
     }
-
 }
index 00d15d1..5d18134 100644 (file)
@@ -122,14 +122,16 @@ public final class XWT extends JS {
         else if (nargs == 2) {
             //#switch(name)
             case "res.watch": return new Res.ProgressWatcher((Res)a, (JSFunction)b);
-            case "soap": return new SOAP((String)a, "", (String)b, null);
+            case "soap": /* return new SOAP((String)a, "", (String)b, null); */
             case "apply":
                 Template.getTemplate((Res)b).apply((Box)a, XWT.this);
                 return a;
             //#end
         } else if (nargs == 3 && name.equals("soap")) {
+            /*
             if (name.equals("soap"))
                 return new SOAP((String)a, "", (String)b, (String)c);
+            */
 
         } else if (nargs == 1) {
             //#switch(name)
@@ -151,7 +153,7 @@ public final class XWT extends JS {
             case "log.dump": Log.recursiveLog("","",a); return null;
             case "regexp": return new JSRegexp(a, null);
             case "rpc.xml": return new XMLRPC((String)a, "");
-            case "rpc.soap": return new SOAP((String)a, "", null, null);
+            case "rpc.soap": /* return new SOAP((String)a, "", null, null); */
             case "crypto.rsa": /* FEATURE */ break;
             case "crypto.md5": /* FEATURE */ break;
             case "crypto.sha1": /* FEATURE */ break;
@@ -173,11 +175,7 @@ public final class XWT extends JS {
                     try { Thread.sleep(i); } catch (InterruptedException e) { }
                     Scheduler.add(new Scheduler.Task() {
                             public void perform() throws JSExn {
-                                try {
-                                    callback.unpause(null);
-                                } catch (JS.PausedException pe) {
-                                    // okay
-                                }
+                                callback.unpause(null);
                             }
                         });
                 }
index f2a7c20..de0c5fc 100644 (file)
@@ -43,7 +43,8 @@ public class JS extends org.xwt.util.BalancedTree {
     public static class UnpauseCallback {
         Interpreter i;
         UnpauseCallback(Interpreter i) { this.i = i; }
-        public void unpause(Object o) throws PausedException, JSExn {
+        public void unpause(Object o) throws JSExn {
+            // FIXME: if o instanceof JSExn, throw it into the JSworld
             i.stack.push(o);
             i.resume();
         }
diff --git a/src/org/xwt/util/AccessibleCharArrayWriter.java b/src/org/xwt/util/AccessibleCharArrayWriter.java
new file mode 100644 (file)
index 0000000..211dfd1
--- /dev/null
@@ -0,0 +1,9 @@
+package org.xwt.util;
+
+import java.io.*;
+
+public class AccessibleCharArrayWriter extends CharArrayWriter {
+    public char[] getBuf() { return buf; }
+    public AccessibleCharArrayWriter(int i) { super(i); }
+}
+
index beb59b0..1eb61ef 100644 (file)
@@ -25,6 +25,28 @@ public class Log {
     public static void logJS(Object o, Object message) { logJS(message); }
     public static void logJS(Object message) { log(JS.getSourceName() + ":" + JS.getLine(), message); }
 
+    public static BufferedReader loggedReader(Reader r) {
+        // FIXME
+        return new BufferedReader(r);
+        /*
+            new BufferedReader(new FilterReader(new InputStreamReader(is)) {
+                    public int read() throws IOException {
+                        int i = super.read();
+                        if (Log.on) Log.log(this, "recv: " + ((char)i));
+                        return i;
+                    }
+                    public int read(char[] c, int off, int len) throws IOException {
+                        int ret = super.read(c, off, len);
+                        if (ret == -1) return ret;
+                        String s;
+                        BufferedReader br2 = new BufferedReader(new StringReader(new String(c, off, ret)));
+                        while ((s = br2.readLine()) != null) Log.log(this, "recv: " + s);
+                        return ret;
+                    }
+                });
+        */
+    }
+
     /** message can be a String or a Throwable */
     public static synchronized void log(Object o, Object message) {
         if (firstMessage && !logDates) {
@@ -55,23 +77,30 @@ public class Log {
             lastDate = d;
         }
 
-        if (!(message instanceof Throwable)) System.err.println(classname + message);
-        else {
+
+        if (message instanceof Throwable) {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ((Throwable)message).printStackTrace(new PrintStream(baos));
             byte[] b = baos.toByteArray();
             BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
             String s = null;
             try {
-                while((s = br.readLine()) != null) {
-                    System.err.print(classname);
-                    for(int i=0; i<s.length(); i++)
-                        System.err.print(s.charAt(i) == '\t' ? "    " : ("" + s.charAt(i)));
-                    System.err.println();
-                }
-            } catch (Exception e) { }
+                while((s = br.readLine()) != null) log(o, s);
+            } catch (IOException e) {
+                System.err.println("Logger: exception thrown by ByteArrayInputStream -- this should not happen");
+            }
+            return;
+        }
+
+        String str = message.toString();
+        while(str.indexOf('\t') != -1)
+            str = str.substring(0, str.indexOf('\t')) + "    " + str.substring(str.indexOf('\t') + 1);
 
+        while(str.indexOf('\n') != -1) {
+            System.err.println(classname + str.substring(0, str.indexOf('\n')));
+            str = str.substring(str.indexOf('\n') + 1);
         }
+        System.err.println(classname + str);
     }
 
     public static void recursiveLog(String indent, String name, Object o) throws JSExn {