2002/07/19 05:16:31
[org.ibex.core.git] / src / org / xwt / XMLRPC.java
index efe4959..7f5355b 100644 (file)
@@ -74,6 +74,7 @@ class XMLRPC extends XML implements Function {
     // Methods to Recieve and parse XML-RPC Response ////////////////////////////////////////////////////
 
     public void startElement(String name, String[] keys, Object[] vals, int line, int col) {
+        content.reset();
         if (name.equals("fault")) fault = true;
         else if (name.equals("struct")) objects.setElementAt(new JSObject(false), objects.size() - 1);
         else if (name.equals("array")) objects.setElementAt(null, objects.size() - 1);
@@ -184,11 +185,11 @@ class XMLRPC extends XML implements Function {
 
         } else if (o instanceof ByteStream) {
             try {
-                sb.append("                <value><base64>");
+                sb.append("                <value><base64>\n");
                 InputStream is = ((ByteStream)o).getInputStream();
-                byte[] buf = new byte[96];
+                byte[] buf = new byte[54];
                 while(true) {
-                    int numread = is.read(buf, 0, 96);
+                    int numread = is.read(buf, 0, 54);
                     if (numread == -1) break;
                     byte[] writebuf = buf;
                     if (numread < buf.length) {
@@ -196,10 +197,9 @@ class XMLRPC extends XML implements Function {
                         System.arraycopy(buf, 0, writebuf, 0, numread);
                     }
                     sb.append("              ");
-                    sb.append(Base64.encode(writebuf));
+                    sb.append(new String(Base64.encode(writebuf)));
                     sb.append("\n");
                 }
-                sb.append(((Boolean)o).booleanValue() ? "1" : "0");
                 sb.append("\n              </base64></value>\n");
             } catch (IOException e) {
                 if (Log.on) Log.log(this, "caught IOException while attempting to send a ByteStream via XML-RPC");
@@ -227,6 +227,26 @@ class XMLRPC extends XML implements Function {
             }
             sb.append("</string></value>\n");
 
+        } else if (o instanceof NativeDate) {
+            sb.append("                <value><dateTime.iso8601>");
+            NativeDate nd = (NativeDate)o;
+            Date d = new Date(nd.getRawTime());
+            sb.append(d.getYear() + 1900);
+            if (d.getMonth() + 1 < 10) sb.append('0');
+            sb.append(d.getMonth() + 1);
+            if (d.getDate() < 10) sb.append('0');
+            sb.append(d.getDate());
+            sb.append('T');
+            if (d.getHours() < 10) sb.append('0');
+            sb.append(d.getHours());
+            sb.append(':');
+            if (d.getMinutes() < 10) sb.append('0');
+            sb.append(d.getMinutes());
+            sb.append(':');
+            if (d.getSeconds() < 10) sb.append('0');
+            sb.append(d.getSeconds());
+            sb.append("</dateTime.iso8601></value>\n");
+
         } else if (o instanceof NativeArray) {
             if (tracker.get(o) != null) throw new JavaScriptException("attempted to send multi-ref data structure via XML-RPC");
             tracker.put(o, Boolean.TRUE);
@@ -293,13 +313,11 @@ class XMLRPC extends XML implements Function {
             new BufferedReader(new FilterReader(new InputStreamReader(new Filter(http.getInputStream()))) {
                 public int read() throws IOException {
                     int i = super.read();
-                    System.out.println("X " + i);
                     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);
-                    System.out.println("Y " + ret);
                     if (ret == -1) return ret;
                     if (Log.on) Log.log(this, "recv: " + new String(c, off, ret));
                     return ret;
@@ -315,15 +333,13 @@ class XMLRPC extends XML implements Function {
         content.append("        <methodName>");
         content.append(methodname);
         content.append("</methodName>\n");
-        if (args.length > 0) {
-            content.append("        <params>\n");
-            for(int i=0; i<args.length; i++) {
-                content.append("            <param>\n");
-                appendObject(args[i], content);
-                content.append("            </param>\n");
-            }
-            content.append("        </params>\n");
+        content.append("        <params>\n");
+        for(int i=0; i<args.length; i++) {
+            content.append("            <param>\n");
+            appendObject(args[i], content);
+            content.append("            </param>\n");
         }
+        content.append("        </params>\n");
         content.append("    </methodCall>");
         return content.toString();
     }
@@ -344,19 +360,10 @@ class XMLRPC extends XML implements Function {
 
     public final Object call(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) throws JavaScriptException {
 
-        // put ourselves in the background
-        Thread thread = Thread.currentThread();
-        if (!(thread instanceof ThreadMessage)) {
-            if (Log.on) Log.log(this, "RPC calls may only be made from background threads");
-            return null;
-        }
-        ThreadMessage mythread = (ThreadMessage)thread;
-        mythread.setPriority(Thread.MIN_PRIORITY);
-        mythread.done.release();
+        if (!ThreadMessage.suspendThread()) return null;
 
         try {
             return call(args);
-
         } catch (IOException se) {
             if (Log.on) Log.log(this, se);
             if (Log.on) Log.log(this, " at " + cx.interpreterSourceFile + ":" + cx.interpreterLine);
@@ -369,12 +376,8 @@ class XMLRPC extends XML implements Function {
                 if (Log.on) Log.log(this, " at " + cx.interpreterSourceFile + ":" + cx.interpreterLine);
             }
             throw jse;
-
         } finally {
-            // okay, let ourselves be brought to the foreground
-            MessageQueue.add(mythread);
-            mythread.setPriority(Thread.NORM_PRIORITY);
-            mythread.go.block();
+            ThreadMessage.resumeThread();
         }
 
     }
@@ -403,17 +406,17 @@ class XMLRPC extends XML implements Function {
         public Filter(InputStream is) { super(is); }
         public int read() throws IOException {
             Thread.yield();
-            while(MessageQueue.working) try { Thread.sleep(100); } catch (Exception e) { };
+            while(MessageQueue.nonThreadEventsInQueue > 0) try { Thread.sleep(100); } catch (Exception e) { };
             return super.read();
         }
         public int read(byte[] b) throws IOException {
             Thread.yield();
-            while(MessageQueue.working) try { Thread.sleep(100); } catch (Exception e) { };
+            while(MessageQueue.nonThreadEventsInQueue > 0) try { Thread.sleep(100); } catch (Exception e) { };
             return super.read(b);
         }
         public int read(byte[] b, int i, int j) throws IOException {
             Thread.yield();
-            while(MessageQueue.working) try { Thread.sleep(100); } catch (Exception e) { };
+            while(MessageQueue.nonThreadEventsInQueue > 0) try { Thread.sleep(100); } catch (Exception e) { };
             return super.read(b, i, j);
         }
     }