1*a5b190bfSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*a5b190bfSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*a5b190bfSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*a5b190bfSAndrew Rist * distributed with this work for additional information 6*a5b190bfSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*a5b190bfSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*a5b190bfSAndrew Rist * "License"); you may not use this file except in compliance 9*a5b190bfSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*a5b190bfSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*a5b190bfSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*a5b190bfSAndrew Rist * software distributed under the License is distributed on an 15*a5b190bfSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*a5b190bfSAndrew Rist * KIND, either express or implied. See the License for the 17*a5b190bfSAndrew Rist * specific language governing permissions and limitations 18*a5b190bfSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*a5b190bfSAndrew Rist *************************************************************/ 21*a5b190bfSAndrew Rist 22*a5b190bfSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.lib.uno.helper; 25cdf0e10cSrcweir import com.sun.star.uno.XWeak; 26cdf0e10cSrcweir import com.sun.star.uno.XAdapter; 27cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider; 28cdf0e10cSrcweir import com.sun.star.uno.Type; 29cdf0e10cSrcweir import java.util.Vector; 30cdf0e10cSrcweir import java.util.Map; 31cdf0e10cSrcweir import java.util.Hashtable; 32cdf0e10cSrcweir 33cdf0e10cSrcweir 34cdf0e10cSrcweir /** This class can be used as the base class for UNO components. It implements the capability 35cdf0e10cSrcweir * to be kept weak (com.sun.star.uno.XWeak) and it implements com.sun.star.lang.XTypeProvider 36cdf0e10cSrcweir * which is necessary for using the component with StarBasic. 37cdf0e10cSrcweir */ 38cdf0e10cSrcweir public class WeakBase implements XWeak, XTypeProvider 39cdf0e10cSrcweir { 40cdf0e10cSrcweir private final boolean DEBUG= false; 41cdf0e10cSrcweir 42cdf0e10cSrcweir // Contains all WeakAdapter which have been created in this class 43cdf0e10cSrcweir // They have to be notified when this object dies 44cdf0e10cSrcweir private WeakAdapter m_adapter; 45cdf0e10cSrcweir 46cdf0e10cSrcweir protected static Map _mapImplementationIds= new Hashtable(); 47cdf0e10cSrcweir protected static Map _mapTypes= new Hashtable(); 48cdf0e10cSrcweir 49cdf0e10cSrcweir /** Method of XWeak. The returned XAdapter implementation can be used to keap 50cdf0e10cSrcweir * a weak reference to this object. 51cdf0e10cSrcweir * @return a com.sun.star.uno.XAdapter implementation. 52cdf0e10cSrcweir */ queryAdapter()53cdf0e10cSrcweir synchronized public XAdapter queryAdapter() 54cdf0e10cSrcweir { 55cdf0e10cSrcweir if (m_adapter == null) 56cdf0e10cSrcweir m_adapter= new WeakAdapter(this); 57cdf0e10cSrcweir return m_adapter; 58cdf0e10cSrcweir } 59cdf0e10cSrcweir 60cdf0e10cSrcweir /** Override of Object.finalize. When there are no references to this object anymore 61cdf0e10cSrcweir * then the garbage collector calls this method. Thereby causing the adapter object 62cdf0e10cSrcweir * to be notified. The adapter, in turn, notifies all listeners (com.sun.star.uno.XReference) 63cdf0e10cSrcweir */ finalize()64cdf0e10cSrcweir protected void finalize() throws java.lang.Throwable 65cdf0e10cSrcweir { 66cdf0e10cSrcweir if (m_adapter != null) 67cdf0e10cSrcweir m_adapter.referentDying(); 68cdf0e10cSrcweir super.finalize(); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir /** Method of XTypeProvider. It returns an array of Type objects which represent 72cdf0e10cSrcweir * all implemented UNO interfaces of this object. 73cdf0e10cSrcweir * @return Type objects of all implemented interfaces. 74cdf0e10cSrcweir */ getTypes()75cdf0e10cSrcweir public Type[] getTypes() 76cdf0e10cSrcweir { 77cdf0e10cSrcweir Type[] arTypes= (Type[]) _mapTypes.get( getClass()); 78cdf0e10cSrcweir if (arTypes == null) 79cdf0e10cSrcweir { 80cdf0e10cSrcweir Vector vec= new Vector(); 81cdf0e10cSrcweir Class currentClass= getClass(); 82cdf0e10cSrcweir do 83cdf0e10cSrcweir { 84cdf0e10cSrcweir Class interfaces[]= currentClass.getInterfaces(); 85cdf0e10cSrcweir for(int i = 0; i < interfaces.length; ++ i) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir // Test if it is a UNO interface 88cdf0e10cSrcweir if (com.sun.star.uno.XInterface.class.isAssignableFrom((interfaces[i]))) 89cdf0e10cSrcweir vec.add(new Type(interfaces[i])); 90cdf0e10cSrcweir } 91cdf0e10cSrcweir // get the superclass the currentClass inherits from 92cdf0e10cSrcweir currentClass= currentClass.getSuperclass(); 93cdf0e10cSrcweir } while (currentClass != null); 94cdf0e10cSrcweir 95cdf0e10cSrcweir Type types[]= new Type[vec.size()]; 96cdf0e10cSrcweir for( int i= 0; i < types.length; i++) 97cdf0e10cSrcweir types[i]= (Type) vec.elementAt(i); 98cdf0e10cSrcweir _mapTypes.put(getClass(), types); 99cdf0e10cSrcweir arTypes= types; 100cdf0e10cSrcweir } 101cdf0e10cSrcweir return arTypes; 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir /** Method of XTypeProvider. It provides an identifier that represents the set of UNO 105cdf0e10cSrcweir * interfaces implemented by this class. All instances of this class 106cdf0e10cSrcweir * which run in the same Java Virtual Machine return the same array. (This only works as long 107cdf0e10cSrcweir * the ClassLoader preserves the class even if no instance exist.) 108cdf0e10cSrcweir *@return identifier as array of bytes 109cdf0e10cSrcweir */ getImplementationId()110cdf0e10cSrcweir public byte[] getImplementationId() 111cdf0e10cSrcweir { 112cdf0e10cSrcweir byte[] id= null; 113cdf0e10cSrcweir synchronized (_mapImplementationIds) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir id= (byte[]) _mapImplementationIds.get(getClass()); 116cdf0e10cSrcweir 117cdf0e10cSrcweir if (id == null) 118cdf0e10cSrcweir { 119cdf0e10cSrcweir int hash = hashCode(); 120cdf0e10cSrcweir String sName= getClass().getName(); 121cdf0e10cSrcweir byte[] arName= sName.getBytes(); 122cdf0e10cSrcweir int nNameLength= arName.length; 123cdf0e10cSrcweir 124cdf0e10cSrcweir id= new byte[ 4 + nNameLength]; 125cdf0e10cSrcweir id[0]= (byte)(hash & 0xff); 126cdf0e10cSrcweir id[1]= (byte)((hash >>> 8) & 0xff); 127cdf0e10cSrcweir id[2]= (byte)((hash >>> 16) & 0xff); 128cdf0e10cSrcweir id[3]= (byte)((hash >>>24) & 0xff); 129cdf0e10cSrcweir 130cdf0e10cSrcweir for (int i= 0; i < nNameLength; i++) 131cdf0e10cSrcweir { 132cdf0e10cSrcweir id[4 + i]= arName[i]; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir _mapImplementationIds.put(getClass(), id); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir } 137cdf0e10cSrcweir return id; 138cdf0e10cSrcweir } 139cdf0e10cSrcweir } 140