1a5b190bfSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3a5b190bfSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a5b190bfSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a5b190bfSAndrew Rist  * distributed with this work for additional information
6a5b190bfSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a5b190bfSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a5b190bfSAndrew Rist  * "License"); you may not use this file except in compliance
9a5b190bfSAndrew Rist  * with the License.  You may obtain a copy of the License at
10a5b190bfSAndrew Rist  *
11a5b190bfSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12a5b190bfSAndrew Rist  *
13a5b190bfSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a5b190bfSAndrew Rist  * software distributed under the License is distributed on an
15a5b190bfSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a5b190bfSAndrew Rist  * KIND, either express or implied.  See the License for the
17a5b190bfSAndrew Rist  * specific language governing permissions and limitations
18a5b190bfSAndrew Rist  * under the License.
19a5b190bfSAndrew Rist  *
20a5b190bfSAndrew Rist  *************************************************************/
21a5b190bfSAndrew Rist 
22a5b190bfSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.lib.uno.helper;
25cdf0e10cSrcweir import java.lang.ref.WeakReference;
26cdf0e10cSrcweir import com.sun.star.uno.XAdapter;
27cdf0e10cSrcweir import com.sun.star.uno.XReference;
28cdf0e10cSrcweir import java.util.List;
29cdf0e10cSrcweir import java.util.Collections;
30cdf0e10cSrcweir import java.util.LinkedList;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /** An XAdapter implementation that holds a weak reference (java.lang.ref.WeakReference)
33cdf0e10cSrcweir  *  to an object. Clients can register listener (com.sun.star.lang.XReference) which
34cdf0e10cSrcweir  *  are notified when the the object (the one which is kept weak) is being finalized. That
35cdf0e10cSrcweir  *  is, that object is being destroyed because there are not any hard references
36cdf0e10cSrcweir  *  to it.
37cdf0e10cSrcweir  */
38cdf0e10cSrcweir public class WeakAdapter implements XAdapter
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     private final boolean DEBUG= false;
41cdf0e10cSrcweir     // references the XWeak implementation
42cdf0e10cSrcweir     private WeakReference m_weakRef;
43cdf0e10cSrcweir     // contains XReference objects registered by addReference
44cdf0e10cSrcweir     private List m_xreferenceList;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     /**
47cdf0e10cSrcweir      *@param component the object that is to be held weak
48cdf0e10cSrcweir      */
WeakAdapter(Object component)49cdf0e10cSrcweir     public WeakAdapter(Object component)
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir         m_weakRef= new WeakReference(component);
52cdf0e10cSrcweir         m_xreferenceList= Collections.synchronizedList( new LinkedList());
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     /** Called by the XWeak implementation (WeakBase) when it is being finalized.
56cdf0e10cSrcweir      *  It is only being called once.
57cdf0e10cSrcweir      *  The registererd XReference listeners are notified. On notification they are
58cdf0e10cSrcweir      *  to unregister themselves. The notification is thread-safe. However, it is possible
59cdf0e10cSrcweir      *  to add a listener during the notification process, which will never receive a
60cdf0e10cSrcweir      *  notification. To prevent this, one would have to synchronize this method with
61cdf0e10cSrcweir      *  the addReference method. But this can result in deadlocks in a multithreaded
62cdf0e10cSrcweir      *  environment.
63cdf0e10cSrcweir      */
referentDying()64cdf0e10cSrcweir     void referentDying()
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir         //synchronized call
67cdf0e10cSrcweir         Object[] references= m_xreferenceList.toArray();
68cdf0e10cSrcweir         for (int i= references.length; i > 0; i--)
69cdf0e10cSrcweir         {
70cdf0e10cSrcweir             ((XReference) references[i-1]).dispose();
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     /** Method  of com.sun.star.uno.XAdapter. It is called to obtain a hard reference
75cdf0e10cSrcweir      *  to the object which is kept weak by this instance.
76cdf0e10cSrcweir      *  @return hard reference to the object
77cdf0e10cSrcweir      */
queryAdapted()78cdf0e10cSrcweir     public Object queryAdapted()
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         return m_weakRef.get();
81cdf0e10cSrcweir     }
82*e6b649b5SPedro Giffuni 
83cdf0e10cSrcweir     /** Method of com.sun.star.uno.XAdapter. Called by clients to register listener which
84cdf0e10cSrcweir      * are notified when the weak object is dying.
85cdf0e10cSrcweir      *@param xReference a listener
86cdf0e10cSrcweir      */
removeReference(XReference xReference)87cdf0e10cSrcweir     public void removeReference(XReference xReference)
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         m_xreferenceList.remove(xReference);
90cdf0e10cSrcweir     }
91*e6b649b5SPedro Giffuni 
92cdf0e10cSrcweir     /** Method of com.sun.star.uno.XAdapter. Called by clients to unregister listeners.
93*e6b649b5SPedro Giffuni      *@param xReference listener
94cdf0e10cSrcweir      */
addReference(XReference xReference)95cdf0e10cSrcweir     public void addReference(XReference xReference)
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         m_xreferenceList.add(xReference);
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101