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