summaryrefslogtreecommitdiff
path: root/jurt/com/sun/star/lib/uno/environments/java/java_environment.java
blob: 36404f28d57e4170536a838ea3a07b3233eef977 (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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: java_environment.java,v $
 * $Revision: 1.17 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

package com.sun.star.lib.uno.environments.java;

import com.sun.star.uno.IEnvironment;
import com.sun.star.uno.Type;
import com.sun.star.uno.UnoRuntime;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * The java_environment is the environment where objects and
 * interfaces are registered, which are mapped out of java or
 * into java.
 *
 * <p>The java_environment implements the <code>IEnvironment</code> interface
 * defined in the uno runtime.</p>
 *
 * @see com.sun.star.uno.UnoRuntime
 * @see com.sun.star.uno.IEnvironment
 * @since UDK1.0
 */
public final class java_environment implements IEnvironment {
    public java_environment(Object context) {
        this.context = context;
    }

    // @see com.sun.star.uno.IEnvironment#getContext
    public Object getContext() {
        return context;
    }

    // @see com.sun.star.uno.IEnvironment#getName
    public String getName() {
        return "java";
    }

    // @see com.sun.star.uno.IEnvironment#registerInterface
    public Object registerInterface(Object object, String[] oid, Type type) {
        if (oid[0] == null) {
            oid[0] = UnoRuntime.generateOid(object);
        }
        return (isProxy(object) ? proxies : localObjects).register(
            object, oid[0], type);
    }

    /**
     * You have to revoke ANY interface that has been registered via this
     * method.
     *
     * @param oid object id of interface to be revoked
     * @param type the type description of the interface
     * @see com.sun.star.uno.IEnvironment#revokeInterface
     */
    public void revokeInterface(String oid, Type type) {
        if (!proxies.revoke(oid, type)) {
            localObjects.revoke(oid, type);
        }
    }

    /**
     * Retrieves an interface identified by its object id and type from this
     * environment.
     *
     * @param oid object id of interface to be retrieved
     * @param type the type description of the interface to be retrieved
     * @see com.sun.star.uno.IEnvironment#getRegisteredInterface
     */
    public Object getRegisteredInterface(String oid, Type type) {
        Object o = proxies.get(oid, type);
        if (o == null) {
            o = localObjects.get(oid, type);
        }
        return o;
    }

    /**
     * Retrieves the object identifier for a registered interface from this
     * environment.
     *
     * @param object a registered interface
     * @see com.sun.star.uno.IEnvironment#getRegisteredObjectIdentifier
     */
    public String getRegisteredObjectIdentifier(Object object) {
        return UnoRuntime.generateOid(object);
    }

    // @see com.sun.star.uno.IEnvironment#list
    public void list() {
// TODO???
//      synchronized (proxies) {
//          System.err.println("##### " + getClass().getName() + ".list: "
//                             + getName() + ", " + getContext());
//          for (Iterator it = proxies.values().iterator(); it.hasNext();) {
//              System.err.println("#### entry: " + it.next());
//          }
//      }
    }

    /**
     * Revokes all registered proxy interfaces.
     *
     * <p>This method should be part of <code>IEnvironment</code>.  It is called
     * from <code>com.sun.star.lib.uno.bridges.java_remote.<!--
     * -->java_remote_bridge.dispose</code>.</p>
     */
    public void revokeAllProxies() {
        proxies.clear();
    }

    // TODO  What's this???  java.lang.Object#equals requires reflexivity...
    //
    // Maybe this was hacked in so that different bridges use different
    // instances of java_environment.  That is desirable for the following
    // reason:  An OID is bridged in over bridge A, a proxy is created on the
    // Java side, and recorded in the java_environment.  The same OID is then
    // bridged in over another bridge B.  If there were only one
    // java_environment shared by both bridges, the proxy from bridge A would be
    // reused.  If now bridge A is taken down programatically (e.g., because
    // some controlling code somehow deduced that no objects are mapped over
    // that bridge any longer), but the proxy is still used by bridge B, using
    // the proxy would now result in errors.  The explicit API to control
    // bridges forbids to transparently share proxies between bridges, and using
    // different java_environment instances for different bridges is the way to
    // enforce this.
    public boolean equals(Object obj) {
        return false;
    }

    private static final class Registry {
        public Object register(Object object, String oid, Type type) {
            synchronized (map) {
                cleanUp();
                Level1Entry l1 = getLevel1Entry(oid);
                if (l1 != null) {
                    Level2Entry l2 = l1.get(type);
                    if (l2 != null) {
                        Object o = l2.get();
                        if (o != null) {
                            l2.acquire();
                            return o;
                        }
                    }
                }
                // TODO  If a holder references an unreachable object, but still
                // has a positive count, it is replaced with a new holder
                // (referencing a reachable object, and with a count of 1).  Any
                // later calls to revoke that should decrement the count of the
                // previous holder would now decrement the count of the new
                // holder, removing it prematurely.  This is a design flaw that
                // will be fixed when IEnvironment.revokeInterface is changed to
                // no longer use counting.  (And this problem is harmless, as
                // currently a holder either references a strongly held object
                // and uses register/revoke to control it, or references a
                // weakly held proxy and never revokes it.)
                if (l1 == null) {
                    l1 = new Level1Entry();
                    map.put(oid, l1);
                }
                l1.add(new Level2Entry(oid, type, object, queue));
            }
            return object;
        }

        public boolean revoke(String oid, Type type) {
            synchronized (map) {
                Level1Entry l1 = getLevel1Entry(oid);
                Level2Entry l2 = null;
                if (l1 != null) {
                    l2 = l1.get(type);
                    if (l2 != null && l2.release()) {
                        removeLevel2Entry(oid, l1, l2);
                    }
                }
                cleanUp();
                return l2 != null;
            }
        }

        public Object get(String oid, Type type) {
            synchronized (map) {
                Level1Entry l1 = getLevel1Entry(oid);
                return l1 == null ? null : l1.find(type);
            }
        }

        public void clear() {
            synchronized (map) {
                map.clear();
                cleanUp();
            }
        }

        // must only be called while synchronized on map:
        private void cleanUp() {
            for (;;) {
                Level2Entry l2 = (Level2Entry) queue.poll();
                if (l2 == null) {
                    break;
                }
                // It is possible that a Level2Entry e1 for the OID/type pair
                // (o,t) becomes weakly reachable, then another Level2Entry e2
                // is registered for the same pair (o,t) (a new Level2Entry is
                // created since now e1.get() == null), and only then e1 is
                // enqueued.  To not erroneously remove the new e2 in that case,
                // check whether the map still contains e1:
                String oid = l2.getOid();
                Level1Entry l1 = getLevel1Entry(oid);
                if (l1 != null && l1.get(l2.getType()) == l2) {
                    removeLevel2Entry(oid, l1, l2);
                }
            }
        }

        // must only be called while synchronized on map:
        private Level1Entry getLevel1Entry(String oid) {
            return (Level1Entry) map.get(oid);
        }

        // must only be called while synchronized on map:
        private void removeLevel2Entry(String oid, Level1Entry l1,
                                       Level2Entry l2) {
            if (l1.remove(l2)) {
                map.remove(oid);
            }
        }

        private static final class Level1Entry {
            // must only be called while synchronized on map:
            public Level2Entry get(Type type) {
                for (Iterator i = list.iterator(); i.hasNext();) {
                    Level2Entry l2 = (Level2Entry) i.next();
                    if (l2.getType().equals(type)) {
                        return l2;
                    }
                }
                return null;
            }

            // must only be called while synchronized on map:
            public Object find(Type type) {
                // First, look for an exactly matching entry; then, look for an
                // arbitrary entry for a subtype of the request type:
                for (Iterator i = list.iterator(); i.hasNext();) {
                    Level2Entry l2 = (Level2Entry) i.next();
                    if (l2.getType().equals(type)) {
                        Object o = l2.get();
                        if (o != null) {
                            return o;
                        }
                    }
                }
                for (Iterator i = list.iterator(); i.hasNext();) {
                    Level2Entry l2 = (Level2Entry) i.next();
                    if (type.isSupertypeOf(l2.getType())) {
                        Object o = l2.get();
                        if (o != null) {
                            return o;
                        }
                    }
                }
                return null;
            }

            // must only be called while synchronized on map:
            public void add(Level2Entry l2) {
                list.add(l2);
            }

            // must only be called while synchronized on map:
            public boolean remove(Level2Entry l2) {
                list.remove(l2);
                return list.isEmpty();
            }

            private final LinkedList list = new LinkedList(); // of Level2Entry
        }

        private static final class Level2Entry extends WeakReference {
            public Level2Entry(String oid, Type type, Object object,
                               ReferenceQueue queue) {
                super(object, queue);
                this.oid = oid;
                this.type = type;
            }

            public String getOid() {
                return oid;
            }

            public Type getType() {
                return type;
            }

            // must only be called while synchronized on map:
            public void acquire() {
                ++count;
            }

            // must only be called while synchronized on map:
            public boolean release() {
                return --count == 0;
            }

            private final String oid;
            private final Type type;
            private int count = 1;
        }

        private final HashMap map = new HashMap();
            // from OID (String) to Level1Entry
        private final ReferenceQueue queue = new ReferenceQueue();
    }

    private boolean isProxy(Object object) {
        return object instanceof com.sun.star.lib.uno.Proxy;
    }

    private static final Registry localObjects = new Registry();

    private final Object context;
    private final Registry proxies = new Registry();
}