1*2be43276SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*2be43276SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*2be43276SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*2be43276SAndrew Rist * distributed with this work for additional information 6*2be43276SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*2be43276SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*2be43276SAndrew Rist * "License"); you may not use this file except in compliance 9*2be43276SAndrew Rist * with the License. You may obtain a copy of the License at 10*2be43276SAndrew Rist * 11*2be43276SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*2be43276SAndrew Rist * 13*2be43276SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*2be43276SAndrew Rist * software distributed under the License is distributed on an 15*2be43276SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*2be43276SAndrew Rist * KIND, either express or implied. See the License for the 17*2be43276SAndrew Rist * specific language governing permissions and limitations 18*2be43276SAndrew Rist * under the License. 19*2be43276SAndrew Rist * 20*2be43276SAndrew Rist *************************************************************/ 21*2be43276SAndrew Rist 22*2be43276SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.uno; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.uno.XWeak; 27cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 28cdf0e10cSrcweir import com.sun.star.uno.XAdapter; 29cdf0e10cSrcweir import com.sun.star.uno.XReference; 30cdf0e10cSrcweir 31cdf0e10cSrcweir /** This class holds weak reference to an object. It actually holds a reference to a 32cdf0e10cSrcweir com.sun.star.XAdapter implementation and obtains a hard reference if necessary. 33cdf0e10cSrcweir */ 34cdf0e10cSrcweir public class WeakReference 35cdf0e10cSrcweir { 36cdf0e10cSrcweir private final boolean DEBUG= false; 37cdf0e10cSrcweir private OWeakRefListener m_listener; 38cdf0e10cSrcweir // There is no default constructor. Every instance must register itself with the 39cdf0e10cSrcweir // XAdapter interface, which is done in the constructors. Assume we have this code 40cdf0e10cSrcweir // WeakReference ref= new WeakReference(); 41cdf0e10cSrcweir // ref = someOtherWeakReference; 42cdf0e10cSrcweir // 43cdf0e10cSrcweir // ref would not be notified (XReference.dispose()) because it did not register 44cdf0e10cSrcweir // itself. Therefore the XAdapter would be kept aliver although this is not 45cdf0e10cSrcweir // necessary. 46cdf0e10cSrcweir 47cdf0e10cSrcweir /** Creates an instance of this class. 48cdf0e10cSrcweir *@param obj - another instance that is to be copied 49cdf0e10cSrcweir */ WeakReference(WeakReference obj)50cdf0e10cSrcweir public WeakReference(WeakReference obj) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir if (obj != null) 53cdf0e10cSrcweir { 54cdf0e10cSrcweir Object weakImpl= obj.get(); 55cdf0e10cSrcweir if (weakImpl != null) 56cdf0e10cSrcweir { 57cdf0e10cSrcweir XWeak weak= UnoRuntime.queryInterface(XWeak.class, weakImpl); 58cdf0e10cSrcweir if (weak != null) 59cdf0e10cSrcweir { 60cdf0e10cSrcweir XAdapter adapter= (XAdapter) weak.queryAdapter(); 61cdf0e10cSrcweir if (adapter != null) 62cdf0e10cSrcweir m_listener= new OWeakRefListener(adapter); 63cdf0e10cSrcweir } 64cdf0e10cSrcweir } 65cdf0e10cSrcweir } 66cdf0e10cSrcweir } 67cdf0e10cSrcweir 68cdf0e10cSrcweir /** Creates an instance of this class. 69cdf0e10cSrcweir *@param obj XWeak implementation 70cdf0e10cSrcweir */ WeakReference(Object obj)71cdf0e10cSrcweir public WeakReference(Object obj) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir XWeak weak= UnoRuntime.queryInterface(XWeak.class, obj); 74cdf0e10cSrcweir if (weak != null) 75cdf0e10cSrcweir { 76cdf0e10cSrcweir XAdapter adapter= (XAdapter) weak.queryAdapter(); 77cdf0e10cSrcweir if (adapter != null) 78cdf0e10cSrcweir m_listener= new OWeakRefListener(adapter); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir } 81cdf0e10cSrcweir /** Returns a hard reference to the object that is kept weak by this class. 82cdf0e10cSrcweir *@return a hard reference to the XWeak implementation. 83cdf0e10cSrcweir */ get()84cdf0e10cSrcweir public Object get() 85cdf0e10cSrcweir { 86cdf0e10cSrcweir if (m_listener != null) 87cdf0e10cSrcweir return m_listener.get(); 88cdf0e10cSrcweir return null; 89cdf0e10cSrcweir } 90cdf0e10cSrcweir } 91cdf0e10cSrcweir 92cdf0e10cSrcweir /** Implementation of com.sun.star.uno.XReference for use with WeakReference. 93cdf0e10cSrcweir * It keeps the XAdapter implementation and registers always with it. Deregistering 94cdf0e10cSrcweir * occurs on notification by the adapter and the adapter is released. 95cdf0e10cSrcweir */ 96cdf0e10cSrcweir class OWeakRefListener implements XReference 97cdf0e10cSrcweir { 98cdf0e10cSrcweir private final boolean DEBUG= false; 99cdf0e10cSrcweir private XAdapter m_adapter; 100cdf0e10cSrcweir 101cdf0e10cSrcweir /** The constructor registered this object with adapter. 102cdf0e10cSrcweir *@param adapter the XAdapter implementation. 103cdf0e10cSrcweir */ OWeakRefListener( XAdapter adapter)104cdf0e10cSrcweir OWeakRefListener( XAdapter adapter) 105cdf0e10cSrcweir { 106cdf0e10cSrcweir m_adapter= adapter; 107cdf0e10cSrcweir m_adapter.addReference(this); 108cdf0e10cSrcweir } 109cdf0e10cSrcweir /** Method of com.sun.star.uno.XReference. When called, it deregisteres this 110cdf0e10cSrcweir * object with the adapter and releases the reference to it. 111cdf0e10cSrcweir */ dispose()112cdf0e10cSrcweir synchronized public void dispose() 113cdf0e10cSrcweir { 114cdf0e10cSrcweir if (m_adapter != null) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir m_adapter.removeReference(this); 117cdf0e10cSrcweir m_adapter= null; 118cdf0e10cSrcweir } 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir /** Obtains a hard reference to the object which is kept weak by the adapter 122cdf0e10cSrcweir * and returns it. 123cdf0e10cSrcweir * @return hard reference to the otherwise weakly kept object. 124cdf0e10cSrcweir */ get()125cdf0e10cSrcweir synchronized Object get() 126cdf0e10cSrcweir { 127cdf0e10cSrcweir Object retVal= null; 128cdf0e10cSrcweir if (m_adapter != null) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir retVal= m_adapter.queryAdapted(); 131cdf0e10cSrcweir if (retVal == null) 132cdf0e10cSrcweir { 133cdf0e10cSrcweir // If this object registered as listener with XAdapter while it was notifying 134cdf0e10cSrcweir // the listeners then this object might not have been notified. If queryAdapted 135cdf0e10cSrcweir // returned null then the weak kept object is dead and the listeners have already 136cdf0e10cSrcweir // been notified. And we missed it. 137cdf0e10cSrcweir m_adapter.removeReference(this); 138cdf0e10cSrcweir m_adapter= null; 139cdf0e10cSrcweir } 140cdf0e10cSrcweir } 141cdf0e10cSrcweir return retVal; 142cdf0e10cSrcweir } 143cdf0e10cSrcweir } 144