KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > modules > synchronize


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.modules;
3 import org.python.core.*;
4
5 class SynchronizedCallable extends PyObject
6 {
7     PyObject callable;
8
9     public SynchronizedCallable(PyObject callable) {
10         this.callable = callable;
11     }
12
13     public PyObject _doget(PyObject container) {
14         // TBD: third arg == null? Hmm...
15
return new PyMethod(container, this, null);
16     }
17
18     public PyObject __call__() {
19         throw Py.TypeError("synchronized callable called with 0 args");
20     }
21
22     public PyObject __call__(PyObject arg) {
23         synchronized(synchronize._getSync(arg)) {
24             return callable.__call__(arg);
25         }
26     }
27
28     public PyObject __call__(PyObject arg1, PyObject arg2) {
29         synchronized(synchronize._getSync(arg1)) {
30             return callable.__call__(arg1, arg2);
31         }
32     }
33
34     public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) {
35         synchronized(synchronize._getSync(arg1)) {
36             return callable.__call__(arg1, arg2, arg3);
37         }
38     }
39
40     public PyObject __call__(PyObject[] args, String JavaDoc[] keywords) {
41         if (args.length == 0) {
42             throw Py.TypeError("synchronized callable called with 0 args");
43         }
44         synchronized(synchronize._getSync(args[0])) {
45             return callable.__call__(args, keywords);
46         }
47     }
48
49     public PyObject __call__(PyObject arg1, PyObject[] args,
50                              String JavaDoc[] keywords)
51     {
52         synchronized(synchronize._getSync(arg1)) {
53             return callable.__call__(arg1, args, keywords);
54         }
55     }
56
57
58 }
59
60 public class synchronize
61 {
62     public static Object JavaDoc _getSync(PyObject obj) {
63         return Py.tojava(obj, Object JavaDoc.class);
64     }
65
66     public static PyObject apply_synchronized(PyObject sync_object,
67                                               PyObject callable,
68                                               PyObject args)
69     {
70         synchronized (_getSync(sync_object)) {
71             return __builtin__.apply(callable, args);
72         }
73     }
74     public static PyObject apply_synchronized(PyObject sync_object,
75                                               PyObject callable,
76                                               PyObject args,
77                                               PyDictionary kws)
78     {
79         synchronized (_getSync(sync_object)) {
80             return __builtin__.apply(callable, args, kws);
81         }
82     }
83
84     public static PyObject make_synchronized(PyObject callable) {
85         return new SynchronizedCallable(callable);
86     }
87 }
88
Popular Tags