1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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
;
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
42 public class WeakAdapter
implements XAdapter
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
;
51 *@param component the object that is to be held weak
53 public WeakAdapter(Object component
)
55 m_weakRef
= new WeakReference(component
);
56 m_xreferenceList
= Collections
.synchronizedList( new LinkedList());
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
71 Object
[] references
= m_xreferenceList
.toArray();
72 for (int i
= references
.length
; i
> 0; i
--)
74 ((XReference
) references
[i
-1]).dispose();
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
82 public Object
queryAdapted()
84 return m_weakRef
.get();
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
90 public void removeReference(XReference xReference
)
92 m_xreferenceList
.remove(xReference
);
94 /** Method of com.sun.star.uno.XAdapter. Called by clients to unregister listeners.
97 public void addReference(XReference xReference
)
99 m_xreferenceList
.add(xReference
);