1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.lib.uno.helper;
29 import java.lang.ref.WeakReference;
30 import com.sun.star.uno.XAdapter;
31 import com.sun.star.uno.XReference;
32 import java.util.List;
33 import java.util.Collections;
34 import java.util.LinkedList;
35 
36 /** An XAdapter implementation that holds a weak reference (java.lang.ref.WeakReference)
37  *  to an object. Clients can register listener (com.sun.star.lang.XReference) which
38  *  are notified when the the object (the one which is kept weak) is being finalized. That
39  *  is, that object is being destroyed because there are not any hard references
40  *  to it.
41  */
42 public class WeakAdapter implements XAdapter
43 {
44     private final boolean DEBUG= false;
45     // references the XWeak implementation
46     private WeakReference m_weakRef;
47     // contains XReference objects registered by addReference
48     private List m_xreferenceList;
49 
50     /**
51      *@param component the object that is to be held weak
52      */
53     public WeakAdapter(Object component)
54     {
55         m_weakRef= new WeakReference(component);
56         m_xreferenceList= Collections.synchronizedList( new LinkedList());
57     }
58 
59     /** Called by the XWeak implementation (WeakBase) when it is being finalized.
60      *  It is only being called once.
61      *  The registererd XReference listeners are notified. On notification they are
62      *  to unregister themselves. The notification is thread-safe. However, it is possible
63      *  to add a listener during the notification process, which will never receive a
64      *  notification. To prevent this, one would have to synchronize this method with
65      *  the addReference method. But this can result in deadlocks in a multithreaded
66      *  environment.
67      */
68     void referentDying()
69     {
70         //synchronized call
71         Object[] references= m_xreferenceList.toArray();
72         for (int i= references.length; i > 0; i--)
73         {
74             ((XReference) references[i-1]).dispose();
75         }
76     }
77 
78     /** Method  of com.sun.star.uno.XAdapter. It is called to obtain a hard reference
79      *  to the object which is kept weak by this instance.
80      *  @return hard reference to the object
81      */
82     public Object queryAdapted()
83     {
84         return m_weakRef.get();
85     }
86     /** Method of com.sun.star.uno.XAdapter. Called by clients to register listener which
87      * are notified when the weak object is dying.
88      *@param xReference a listener
89      */
90     public void removeReference(XReference xReference)
91     {
92         m_xreferenceList.remove(xReference);
93     }
94     /** Method of com.sun.star.uno.XAdapter. Called by clients to unregister listeners.
95      *@param a listener
96      */
97     public void addReference(XReference xReference)
98     {
99         m_xreferenceList.add(xReference);
100     }
101 }
102 
103