01: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
02: *
03: * ***** BEGIN LICENSE BLOCK *****
04: * Version: MPL 1.1/GPL 2.0
05: *
06: * The contents of this file are subject to the Mozilla Public License Version
07: * 1.1 (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: * http://www.mozilla.org/MPL/
10: *
11: * Software distributed under the License is distributed on an "AS IS" basis,
12: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13: * for the specific language governing rights and limitations under the
14: * License.
15: *
16: * The Original Code is Delegator.java, released
17: * Sep 27, 2000.
18: *
19: * The Initial Developer of the Original Code is
20: * Matthias Radestock. <matthias@sorted.org>.
21: * Portions created by the Initial Developer are Copyright (C) 2000
22: * the Initial Developer. All Rights Reserved.
23: *
24: * Contributor(s):
25: *
26: * Alternatively, the contents of this file may be used under the terms of
27: * the GNU General Public License Version 2 or later (the "GPL"), in which
28: * case the provisions of the GPL are applicable instead of those above. If
29: * you wish to allow use of your version of this file only under the terms of
30: * the GPL and not to allow others to use your version of this file under the
31: * MPL, indicate your decision by deleting the provisions above and replacing
32: * them with the notice and other provisions required by the GPL. If you do
33: * not delete the provisions above, a recipient may use your version of this
34: * file under either the MPL or the GPL.
35: *
36: * ***** END LICENSE BLOCK ***** */
37:
38: // API class
39: package org.mozilla.javascript;
40:
41: /**
42: * This class provides support for implementing Java-style synchronized
43: * methods in Javascript.
44: *
45: * Synchronized functions are created from ordinary Javascript
46: * functions by the <code>Synchronizer</code> constructor, e.g.
47: * <code>new Packages.org.mozilla.javascript.Synchronizer(fun)</code>.
48: * The resulting object is a function that establishes an exclusive
49: * lock on the <code>this</code> object of its invocation.
50: *
51: * The Rhino shell provides a short-cut for the creation of
52: * synchronized methods: <code>sync(fun)</code> has the same effect as
53: * calling the above constructor.
54: *
55: * @see org.mozilla.javascript.Delegator
56: * @author Matthias Radestock
57: */
58:
59: public class Synchronizer extends Delegator {
60:
61: /**
62: * Create a new synchronized function from an existing one.
63: *
64: * @param obj the existing function
65: */
66: public Synchronizer(Scriptable obj) {
67: super (obj);
68: }
69:
70: /**
71: * @see org.mozilla.javascript.Function#call
72: */
73: public Object call(Context cx, Scriptable scope,
74: Scriptable this Obj, Object[] args) {
75: synchronized (this Obj instanceof Wrapper ? ((Wrapper) this Obj)
76: .unwrap() : this Obj) {
77: return ((Function) obj).call(cx, scope, thisObj, args);
78: }
79: }
80: }
|