summaryrefslogtreecommitdiffstats
path: root/khtml/java/org/kde/kjas/server/KJASAppletStub.java
blob: c84013e7cbb9e2cc8bdd2660eccd2a482445b8be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
package org.kde.kjas.server;

import java.applet.*;
import java.util.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.security.PrivilegedAction;
import java.security.AccessController;
import java.security.AccessControlContext;
import java.security.ProtectionDomain;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * The stub used by Applets to communicate with their environment.
 *
 */
public final class KJASAppletStub
    implements AppletStub
{
    private KJASAppletContext context;    // The containing context.
    private Hashtable         params;     // Maps parameter names to values
    private URL               codeBase;   // The URL directory where files are
    private URL               docBase;    // The document that referenced the applet
    private boolean           active;     // Is the applet active?
    private String            appletName; // The name of this applet instance
    private String            appletID;   // The id of this applet- for use in callbacks
    private Dimension         appletSize;
    private String            windowName;
    private String            className;
    private Class             appletClass;
    private JFrame            frame;

    /**
    * out of bounds applet state :-), perform an action
    */
    public static final int ACTION = -1;
    /**
    * applet state unknown
    */
    public static final int UNKNOWN = 0;
    /**
    * the applet class has been loaded 
    */
    public static final int CLASS_LOADED = 1;
    /**
    * the applet has been instanciated 
    */
    public static final int INSTANCIATED = 2;
    /**
    * the applet has been initialized 
    */
    public static final int INITIALIZED = 3;
    /**
    * the applet has been started 
    */
    public static final int STARTED = 4;
    /**
    * the applet has been stopped 
    */
    public static final int STOPPED = 5;
    /**
    * the applet has been destroyed 
    */
    public static final int DESTROYED = 6;
    /**
    * request for termination of the applet thread 
    */
    private static final int TERMINATE = 7;
    /**
    * like TERMINATE, an end-point state 
    */
    private static final int FAILED = 8;
   
    
    //private KJASAppletClassLoader loader;
    KJASAppletClassLoader loader;
    private KJASAppletPanel       panel;
    private Applet                app;
    KJASAppletStub                me;

    /**
     * Interface for so called LiveConnect actions, put-, get- and callMember
     */
    // keep this in sync with KParts::LiveConnectExtension::Type
    private final static int JError    = -1;
    private final static int JVoid     = 0;
    private final static int JBoolean  = 1;
    private final static int JFunction = 2;
    private final static int JNumber   = 3;
    private final static int JObject   = 4;
    final static int         JString   = 5;

    interface AppletAction {
        void apply();
        void fail();
    }

    private class RunThread extends Thread {
        private int request_state = CLASS_LOADED;
        private int current_state = UNKNOWN;
        private Vector actions = new Vector();
        private AccessControlContext acc = null;

        RunThread() {
            super("KJAS-AppletStub-" + appletID + "-" + appletName);
            setContextClassLoader(loader);
        }
        /**
         * Ask applet to go to the next state
         */
        synchronized void requestState(int nstate) {
            if (nstate > current_state) {
                request_state = nstate;
                notifyAll();
            }
        }
        synchronized void requestAction(AppletAction action) {
            actions.add(action);
            notifyAll();
        }
        /**
         * Get the asked state
         */
        synchronized private int getRequestState() {
            while (request_state == current_state) {
                if (!actions.isEmpty()) {
                    if (current_state >= INITIALIZED && current_state < STOPPED)
                        return ACTION;
                    else {
                        AppletAction action = (AppletAction) actions.remove(0);
                        action.fail();
                    }
                } else {
                    try {
                        wait ();
                    } catch(InterruptedException ie) {
                    }
                }
            }
            if (request_state == DESTROYED && current_state == STARTED)
                return current_state + 1; // make sure we don't skip stop()
            return request_state;
        }
        /**
         * Get the current state
         */
        synchronized int getAppletState() {
            return current_state;
        }
        /**
         * Set the current state
         */
        synchronized private void setState(int nstate) {
            current_state = nstate;
        }
        /**
         * Put applet in asked state
         * Note, kjavaapletviewer asks for create/start/stop/destroy, the
         * missing states instance/init/terminate, we do automatically
         */
        private void doState(int nstate) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
            switch (nstate) {
                case CLASS_LOADED:
                    appletClass = loader.loadClass( className );
                    requestState(INSTANCIATED);
                    break;
                case INSTANCIATED: {
                    Object object = null;
                    try {
                        object = appletClass.newInstance();
                        app = (Applet) object;
                    }
                    catch ( ClassCastException e ) {
                        if ( object != null && object instanceof java.awt.Component) {
                            app = new Applet();
                            app.setLayout(new BorderLayout());
                            app.add( (Component) object, BorderLayout.CENTER);
                        } else
                            throw e;
                    }
                    acc = new AccessControlContext(new ProtectionDomain[] {app.getClass().getProtectionDomain()});
                    requestState(INITIALIZED);
                    break;
                }
                case INITIALIZED:
                    app.setStub( me );
                    app.setVisible(false);
                    panel.setApplet( app );
                    if (appletSize.getWidth() > 0)
                        app.setBounds( 0, 0, appletSize.width, appletSize.height );
                    else
                        app.setBounds( 0, 0, panel.getSize().width, panel.getSize().height );
                    app.init();
                    loader.removetqStatusListener(panel);
                    // stop the loading... animation 
                    panel.stopAnimation();
                    app.setVisible(true);
                    break;
                case STARTED:
                    active = true;
                    app.start();
                    frame.validate();
                    app.tqrepaint();
                    break;
                case STOPPED:
                    active = false;
                    app.stop();
                    if (Main.java_version > 1.399) {
                        // kill the windowClosing listener(s)
                        WindowListener[] l = frame.getWindowListeners();
                        for (int i = 0; l != null && i < l.length; i++)
                            frame.removeWindowListener(l[i]);
                    }
                    frame.setVisible(false);
                    break;
                case DESTROYED:
                    if (app != null)
                        app.destroy();
                    frame.dispose();
                    app = null;
                    requestState(TERMINATE);
                    break;
                default:
                    return;
                }
        }
        /**
         * RunThread run(), loop until state is TERMINATE
         */
        public void run() {
            while (true) {
                int nstate = getRequestState();
                if (nstate >= TERMINATE)
                    return;
                if (nstate == ACTION) {
                    AccessController.doPrivileged(
                            new PrivilegedAction() {
                                public Object run() {
                                    AppletAction action = (AppletAction) actions.remove(0);
                                    try {
                                        action.apply();
                                    } catch (Exception ex) {
                                        Main.debug("Error during action " + ex);
                                        action.fail();
                                    }
                                    return null;
                                }
                            },
                            acc);
                } else { // move to nstate
                    try {
                        doState(nstate);
                    } catch (Exception ex) {
                        Main.kjas_err("Error during state " + nstate, ex);
                        if (nstate < INITIALIZED) {
                            setState(FAILED);
                            setFailed(ex.toString());
                            return;
                        }
                    } catch (Throwable tr) {
                        setState(FAILED);
                        setFailed(tr.toString());
                        return;
                    }
                    setState(nstate);
                    stateChange(nstate);
                }
            }
        }
    }
    private RunThread                runThread = null;

    /**
     * Create an AppletStub for the specified applet. The stub will be in
     * the specified context and will automatically attach itself to the
     * passed applet.
     */
    public KJASAppletStub( KJASAppletContext _context, String _appletID,
                           URL _codeBase, URL _docBase,
                           String _appletName, String _className,
                           Dimension _appletSize, Hashtable _params,
                           String _windowName, KJASAppletClassLoader _loader )
    {
        context    = _context;
        appletID   = _appletID;
        codeBase   = _codeBase;
        docBase    = _docBase;
        active     = false;
        appletName = _appletName;
        className  = _className.tqreplace( '/', '.' );
        appletSize = _appletSize;
        params     = _params;
        windowName = _windowName;
        loader     = _loader;
 
        String fixedClassName = _className;
        if (_className.endsWith(".class") || _className.endsWith(".CLASS"))
        {
            fixedClassName = _className.substring(0, _className.length()-6);   
        }
        else if (_className.endsWith(".java")|| _className.endsWith(".JAVA"))
        {
            fixedClassName = _className.substring(0, _className.length()-5);   
        }
        className = fixedClassName.tqreplace('/', '.');
            
        appletClass = null;
        me = this;
        
        
    }

    private void stateChange(int newState) {
        Main.protocol.sendAppletStateNotification(
            context.getID(),
            appletID,
            newState);
    }
    
    private void setFailed(String why) {
        loader.removetqStatusListener(panel);
        panel.stopAnimation();
        panel.showFailed();
        Main.protocol.sendAppletFailed(context.getID(), appletID, why); 
    }
    
    void createApplet() {
        panel = new KJASAppletPanel();
        frame = new JFrame(windowName);
        // under certain circumstances, it may happen that the
        // applet is not embedded but shown in a separate window.
        // think of konqueror running under fvwm or gnome.
        // than, the user should have the ability to close the window.
        
        frame.addWindowListener
        (
            new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    me.destroyApplet();
                }
            }
        );
        frame.getContentPane().add( panel, BorderLayout.CENTER );
	try {
            if (Main.java_version > 1.399)
                frame.setUndecorated(true);
	} catch(java.awt.IllegalComponentStateException e) {
            // This happens with gcj 4.0.1, ignore for now...
	}
        frame.setLocation( 0, 0 );
        frame.pack();
        // resize frame for j2sdk1.5beta1..
        if (appletSize.getWidth() > 0)
            frame.setBounds( 0, 0, appletSize.width, appletSize.height );
        else
            frame.setBounds( 0, 0, 50, 50 );
        frame.setVisible(true);
        loader.addtqStatusListener(panel);
        runThread = new RunThread();
        runThread.start();
    }

    /**
    * starts the applet managed by this stub by calling the applets start() method.
    * Also marks this stub as active.
    * @see java.applet.Applet#start()
    * @see java.applet.AppletStub#isActive()
    * 
    */
    void startApplet()
    {
        runThread.requestState(STARTED);
    }

    /**
    * stops the applet managed by this stub by calling the applets stop() method.
    * Also marks this stub as inactive.
    * @see java.applet.Applet#stop()
    * @see java.applet.AppletStub#isActive()
    * 
    */
    void stopApplet()
    {
        runThread.requestState(STOPPED);
    }

    /**
    * initialize the applet managed by this stub by calling the applets init() method.
    * @see java.applet.Applet#init()
    */
    void initApplet()
    {
        runThread.requestState(INITIALIZED);
   }

    /**
    * destroys the applet managed by this stub by calling the applets destroy() method.
    * Also marks the the applet as inactive.
    * @see java.applet.Applet#init()
    */
    synchronized void destroyApplet()
    {
        runThread.requestState(DESTROYED);
    }

    static void waitForAppletThreads()
    {
        Thread [] ts = new Thread[Thread.activeCount() + 5];
        int len = Thread.enumerate(ts);
        for (int i = 0; i < len; i++) {
            try {
                if (ts[i].getName() != null && 
                        ts[i].getName().startsWith("KJAS-AppletStub-")) {
                    try {
                        ((RunThread) ts[i]).requestState(TERMINATE);
                        ts[i].join(10000);
                    } catch (InterruptedException ie) {}
                }
            } catch (Exception e) {}
        }
    }

    /**
    * get the Applet managed by this stub.
    * @return the Applet or null if the applet could not be loaded
    * or instanciated.
    */
    Applet getApplet()
    {
        if (runThread != null && runThread.getAppletState() > CLASS_LOADED)
            return app;
        return null;
    }

    /**
    * get a parameter value given in the &lt;APPLET&gt; tag 
    * @param name the name of the parameter
    * @return the value  or null if no parameter with this name exists.
    */
    
    public String getParameter( String name )
    {
        return (String) params.get( name.toUpperCase() );
    }

    /**
    * implements the isActive method of the AppletStub interface.
    * @return if the applet managed by this stub is currently active. 
    * @see java.applet.AppletStub#isActive()
    */
    public boolean isActive()
    {
        return active;
    }

    /**
    * determines if the applet has been loaded and instanciated
    * and can hence be used.
    * @return true if the applet has been completely loaded.
    */
    boolean isLoaded() {
        if (runThread == null)
            return false;
        int state = runThread.getAppletState();
        return (state >= INSTANCIATED && state < DESTROYED);
    }
    
    public void appletResize( int width, int height )
    {
        if( active )
        {
            if ( (width >= 0) && (height >= 0))
            {
                Main.debug( "Applet #" + appletID + ": appletResize to : (" + width + ", " + height + ")" );
                Main.protocol.sendResizeAppletCmd( context.getID(), appletID, width, height );
                appletSize = new Dimension( width, height );
                //pack();
            }
        }
    }

    /**
    * converts Object <b>arg</b> into an object of class <b>cl</b>.
    * @param arg Object to convert
    * @param cl Destination class
    * @return An Object of the specified class with the value specified
    *  in <b>arg</b>
    */
    private static final Object cast(Object arg, Class cl) throws NumberFormatException {
        Object ret = arg;
        if (arg == null) {
            ret = null;
        }
        else if (cl.isAssignableFrom(arg.getClass())) {
            return arg;
        }
        else if (arg instanceof String) {
            String s = (String)arg;
            Main.debug("Argument String: \"" + s + "\"");
            if (cl == Boolean.TYPE || cl == Boolean.class) {
                ret = new Boolean(s);
            } else if (cl == Integer.TYPE || cl == Integer.class) {
                ret = new Integer(s);
            } else if (cl == Long.TYPE || cl == Long.class) {
                ret = new Long(s);
            } else if (cl == Float.TYPE || cl == Float.class) {
                ret = new Float(s);
            } else if (cl == Double.TYPE || cl == Double.class) {
                ret = new Double(s);
            } else if (cl == Short.TYPE || cl == Short.class) {
                ret = new Short(s);
            } else if (cl  == Byte.TYPE || cl == Byte.class) {
                ret = new Byte(s);
            } else if (cl == Character.TYPE || cl == Character.class) {
                ret = new Character(s.charAt(0));
            }
        }
        return ret;
    }
    private Method tqfindMethod(Class c, String name, Class [] argcls) {
        try {
            Method[] methods = c.getMethods();
            for (int i = 0; i < methods.length; i++) {
                Method m = methods[i];
                if (m.getName().equals(name)) {
                    Main.debug("Candidate: " + m);
                    Class [] parameterTypes = m.getParameterTypes();
                    if (argcls == null) {
                        if (parameterTypes.length == 0) {
                           return m;
                        } 
                    } else {
                        if (argcls.length == parameterTypes.length) {
                          for (int j = 0; j < argcls.length; j++) {
                            // Main.debug("Parameter " + j + " " + parameterTypes[j]);
                            argcls[j] = parameterTypes[j];
                          }
                          return m;                        
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private int[] getJSTypeValue(Hashtable jsRefs, Object obj, int objid, StringBuffer value) {
        String val = obj.toString();
        int[] rettype = { JError, objid };
        String type = obj.getClass().getName();
        if (type.equals("boolean") || type.equals("java.lang.Boolean"))
            rettype[0] = JBoolean;
        else if (type.equals("int") || type.equals("long") || 
                type.equals("float") || type.equals("double") ||
                type.equals("byte") || obj instanceof java.lang.Number)
            rettype[0] = JNumber;
        else if (type.equals("java.lang.String"))
            rettype[0] = JString;
        else if (!type.startsWith("org.kde.kjas.server") &&
                 !(obj instanceof java.lang.Class &&
                   ((Class)obj).getName().startsWith("org.kde.kjas.server"))) {
            rettype[0] = JObject;
            rettype[1] = obj.hashCode();
            jsRefs.put(new Integer(rettype[1]), obj);
        }
        value.insert(0, val);
        return rettype;
    }
    private class PutAction implements AppletAction {
        int call_id;
        int objid;
        String name;
        String value;
        PutAction(int cid, int oid, String n, String v) {
            call_id = cid;
            objid = oid;
            name = n;
            value = v;
        }
        public void apply() {
            Hashtable jsRefs = loader.getJSReferencedObjects();
            Object o = objid==0 ? getApplet() : jsRefs.get(new Integer(objid));
            if (o == null) {
                Main.debug("Error in putValue: object " + objid + " not found");
                fail();
                return;
            }
            Field f;
            try {
                f = o.getClass().getField(name);
            } catch (Exception e) {
                fail();
                return;
            }
            if (f == null) {
                Main.debug("Error in putValue: " + name + " not found");
                fail();
                return;
            }
            try {
                String type = f.getType().getName();
                Main.debug("putValue: (" + type + ")" + name + "=" + value);
                if (type.equals("boolean"))
                    f.setBoolean(o, Boolean.getBoolean(value));
                else if (type.equals("java.lang.Boolean"))
                    f.set(o, Boolean.valueOf(value));
                else if (type.equals("int"))
                    f.setInt(o, Integer.parseInt(value));
                else if (type.equals("java.lang.Integer"))
                    f.set(o, Integer.valueOf(value));
                else if (type.equals("byte"))
                    f.setByte(o, Byte.parseByte(value));
                else if (type.equals("java.lang.Byte"))
                    f.set(o, Byte.valueOf(value));
                else if (type.equals("char"))
                    f.setChar(o, value.charAt(0));
                else if (type.equals("java.lang.Character"))
                    f.set(o, new Character(value.charAt(0)));
                else if (type.equals("double"))
                    f.setDouble(o, Double.parseDouble(value));
                else if (type.equals("java.lang.Double"))
                    f.set(o, Double.valueOf(value));
                else if (type.equals("float"))
                    f.setFloat(o, Float.parseFloat(value));
                else if (type.equals("java.lang.Float"))
                    f.set(o, Float.valueOf(value));
                else if (type.equals("long"))
                    f.setLong(o, Long.parseLong(value));
                else if (type.equals("java.lang.Long"))
                    f.set(o, Long.valueOf(value));
                else if (type.equals("short"))
                    f.setShort(o, Short.parseShort(value));
                else if (type.equals("java.lang.Short"))
                    f.set(o, Short.valueOf(value));
                else if (type.equals("java.lang.String"))
                    f.set(o, value);
                else {
                    Main.debug("Error putValue: unsupported type: " + type);
                    fail();
                    return;
                }
            } catch (Exception e) {
                Main.debug("Exception in putValue: " + e.getMessage());
                fail();
                return;
            }
            Main.protocol.sendPutMember( context.getID(), call_id, true ); 
        }
        public void fail() {
            Main.protocol.sendPutMember( context.getID(), call_id, false ); 
        }
    }
    private class GetAction implements AppletAction {
        int call_id;
        int objid;
        String name;
        GetAction(int cid, int oid, String n) {
            call_id = cid;
            objid = oid;
            name = n;
        }
        public void apply() {
            Main.debug("getMember: " + name);
            StringBuffer value = new StringBuffer();
            int ret[] = { JError, objid };
            Hashtable jsRefs = loader.getJSReferencedObjects();
            Object o = objid==0 ? getApplet() : jsRefs.get(new Integer(objid));
            if (o == null) {
                fail();
                return;
            }
            Class c = o.getClass();
            try {
                Field field = c.getField(name);
                ret = getJSTypeValue(jsRefs, field.get(o), objid, value);
            } catch (Exception ex) {
                Method [] m = c.getMethods();
                for (int i = 0; i < m.length; i++)
                    if (m[i].getName().equals(name)) {
                        ret[0] = JFunction;
                        break;
                    }
            }
            Main.protocol.sendMemberValue(context.getID(), KJASProtocolHandler.GetMember, call_id, ret[0], ret[1], value.toString()); 
        }
        public void fail() {
            Main.protocol.sendMemberValue(context.getID(), KJASProtocolHandler.GetMember, call_id, -1, 0, ""); 
        }
    }
    private class CallAction implements AppletAction {
        int call_id;
        int objid;
        String name;
        java.util.List args;
        CallAction(int cid, int oid, String n, java.util.List a) {
            call_id = cid;
            objid = oid;
            name = n;
            args = a;
        }
        public void apply() {
            StringBuffer value = new StringBuffer();
            Hashtable jsRefs = loader.getJSReferencedObjects();
            int [] ret = { JError, objid };
            Object o = objid==0 ? getApplet() : jsRefs.get(new Integer(objid));
            if (o == null) {
                fail();
                return;
            }

            try {
                Main.debug("callMember: " + name);
                Object obj;
                Class c = o.getClass();
                String type;
                Class [] argcls = new Class[args.size()];
                for (int i = 0; i < args.size(); i++)
                    argcls[i] = name.getClass(); // String for now, will be updated by tqfindMethod
                Method m = tqfindMethod(c, (String) name, argcls);
                Main.debug("Found Method: " + m);
                if (m != null) {
                    Object [] argobj = new Object[args.size()];
                    for (int i = 0; i < args.size(); i++) {
                        argobj[i] = cast(args.get(i), argcls[i]);
                    }
                    Object retval =  m.invoke(o, argobj);
                    if (retval == null)
                        ret[0] = JVoid;
                    else
                        ret = getJSTypeValue(jsRefs, retval, objid, value);
                }
            } catch (Exception e) {
                Main.debug("callMember threw exception: " + e.toString());
            }
            Main.protocol.sendMemberValue(context.getID(), KJASProtocolHandler.CallMember, call_id, ret[0], ret[1], value.toString()); 
        }
        public void fail() {
            Main.protocol.sendMemberValue(context.getID(), KJASProtocolHandler.CallMember, call_id, -1, 0, ""); 
        }
    }
    boolean putMember(int callid, int objid, String name, String val) {
        if (runThread == null)
            return false;
        runThread.requestAction( new PutAction( callid, objid, name, val) );
        return true;
    }
    boolean getMember(int cid, int oid, String name) {
        if (runThread == null)
            return false;
        runThread.requestAction( new GetAction( cid, oid, name) );
        return true;
    }
    boolean callMember(int cid, int oid, String name, java.util.List args) {
        if (runThread == null)
            return false;
        runThread.requestAction( new CallAction( cid, oid, name, args) );
        return true;
    }
    /*************************************************************************
     ********************** AppletStub Interface *****************************
     *************************************************************************/
    /**
    * implements the getAppletContext method of the AppletStub interface.
    * @return the AppletContext to which this stub belongs.
    * @see java.applet.AppletStub#getAppletContext()
    */
    public AppletContext getAppletContext()
    {
        return context;
    }

    /**
    * implements the getCodeBase method of the AppletStub interface.
    * @return the code base of the applet as given in the &lt;APPLET&gt; tag.
    * @see java.applet.AppletStub#getCodeBase()
    */
    public URL getCodeBase()
    {
        return codeBase;
    }

    /**
    * implements the getDocumentBase method of the AppletStub interface.
    * @return the code base of the applet as given in the 
    * &lt;APPLET&gt; tag or determined by the containing page.
    * @see java.applet.AppletStub#getDocumentBase()
    */
    public URL getDocumentBase()
    {
        return docBase;
    }

    /**
    * get the applet's name
    * @return the name of the applet as given in the 
    * &lt;APPLET&gt; tag or determined by the <em>code</em> parameter.
    */
    public String getAppletName()
    {
        return appletName;
    }

}