1*34dd1e25SAndrew Rist /************************************************************** 2*34dd1e25SAndrew Rist * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10*34dd1e25SAndrew Rist * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*34dd1e25SAndrew Rist * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19*34dd1e25SAndrew Rist * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 25cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 26cdf0e10cSrcweir import com.sun.star.lang.XInitialization; 27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 28cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 29cdf0e10cSrcweir import com.sun.star.uno.XInterface; 30cdf0e10cSrcweir import com.sun.star.uno.Any; 31cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 32cdf0e10cSrcweir 33cdf0e10cSrcweir import java.lang.reflect.Constructor; 34cdf0e10cSrcweir 35cdf0e10cSrcweir // 36cdf0e10cSrcweir // purpose of this class is to provide a service factory that instantiates 37cdf0e10cSrcweir // the services only once (as long as this factory itself exists) 38cdf0e10cSrcweir // and returns only reference to that instance. 39cdf0e10cSrcweir // 40cdf0e10cSrcweir 41cdf0e10cSrcweir public class OneInstanceFactory implements 42cdf0e10cSrcweir XSingleServiceFactory, 43cdf0e10cSrcweir XServiceInfo 44cdf0e10cSrcweir { 45cdf0e10cSrcweir Class aMyClass; 46cdf0e10cSrcweir String aSvcImplName; 47cdf0e10cSrcweir String[] aSupportedSvcNames; 48cdf0e10cSrcweir XInterface xInstantiatedService; 49cdf0e10cSrcweir XMultiServiceFactory xMultiFactory; 50cdf0e10cSrcweir OneInstanceFactory( Class aMyClass, String aSvcImplName, String[] aSupportedSvcNames, XMultiServiceFactory xMultiFactory )51cdf0e10cSrcweir public OneInstanceFactory( 52cdf0e10cSrcweir Class aMyClass, 53cdf0e10cSrcweir String aSvcImplName, 54cdf0e10cSrcweir String[] aSupportedSvcNames, 55cdf0e10cSrcweir XMultiServiceFactory xMultiFactory ) 56cdf0e10cSrcweir { 57cdf0e10cSrcweir this.aMyClass = aMyClass; 58cdf0e10cSrcweir this.aSvcImplName = aSvcImplName; 59cdf0e10cSrcweir this.aSupportedSvcNames = aSupportedSvcNames; 60cdf0e10cSrcweir this.xMultiFactory = xMultiFactory; 61cdf0e10cSrcweir xInstantiatedService = null; 62cdf0e10cSrcweir } 63cdf0e10cSrcweir 64cdf0e10cSrcweir //********************** 65cdf0e10cSrcweir // XSingleServiceFactory 66cdf0e10cSrcweir //********************** createInstance()67cdf0e10cSrcweir public Object createInstance() 68cdf0e10cSrcweir throws com.sun.star.uno.Exception, 69cdf0e10cSrcweir com.sun.star.uno.RuntimeException 70cdf0e10cSrcweir { 71cdf0e10cSrcweir if (xInstantiatedService == null) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir //!! the here used services all have exact one constructor!! 74cdf0e10cSrcweir Constructor [] aCtor = aMyClass.getConstructors(); 75cdf0e10cSrcweir try { 76cdf0e10cSrcweir xInstantiatedService = (XInterface) aCtor[0].newInstance( (Object[])null ); 77cdf0e10cSrcweir } 78cdf0e10cSrcweir catch( Exception e ) { 79cdf0e10cSrcweir } 80cdf0e10cSrcweir 81cdf0e10cSrcweir //!! workaround for services not always being created 82cdf0e10cSrcweir //!! via 'createInstanceWithArguments' 83cdf0e10cSrcweir XInitialization xIni = (XInitialization) UnoRuntime.queryInterface( 84cdf0e10cSrcweir XInitialization.class, createInstance()); 85cdf0e10cSrcweir if (xIni != null) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir Object[] aArguments = new Object[]{ null, null }; 88cdf0e10cSrcweir if (xMultiFactory != null) 89cdf0e10cSrcweir { 90cdf0e10cSrcweir XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( 91cdf0e10cSrcweir XPropertySet.class , xMultiFactory.createInstance( 92cdf0e10cSrcweir "com.sun.star.linguistic2.LinguProperties" ) ); 93cdf0e10cSrcweir aArguments[0] = xPropSet; 94cdf0e10cSrcweir } 95cdf0e10cSrcweir xIni.initialize( aArguments ); 96cdf0e10cSrcweir } 97cdf0e10cSrcweir } 98cdf0e10cSrcweir return xInstantiatedService; 99cdf0e10cSrcweir } 100cdf0e10cSrcweir createInstanceWithArguments( Object[] aArguments )101cdf0e10cSrcweir public Object createInstanceWithArguments( Object[] aArguments ) 102cdf0e10cSrcweir throws com.sun.star.uno.Exception, 103cdf0e10cSrcweir com.sun.star.uno.RuntimeException 104cdf0e10cSrcweir { 105cdf0e10cSrcweir if (xInstantiatedService == null) 106cdf0e10cSrcweir { 107cdf0e10cSrcweir XInitialization xIni = (XInitialization) UnoRuntime.queryInterface( 108cdf0e10cSrcweir XInitialization.class, createInstance()); 109cdf0e10cSrcweir if (xIni != null) 110cdf0e10cSrcweir xIni.initialize( aArguments ); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir return xInstantiatedService; 113cdf0e10cSrcweir } 114cdf0e10cSrcweir 115cdf0e10cSrcweir 116cdf0e10cSrcweir //************* 117cdf0e10cSrcweir // XServiceInfo 118cdf0e10cSrcweir //************* supportsService( String aServiceName )119cdf0e10cSrcweir public boolean supportsService( String aServiceName ) 120cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 121cdf0e10cSrcweir { 122cdf0e10cSrcweir boolean bFound = false; 123cdf0e10cSrcweir int nCnt = aSupportedSvcNames.length; 124cdf0e10cSrcweir for (int i = 0; i < nCnt && !bFound; ++i) 125cdf0e10cSrcweir { 126cdf0e10cSrcweir if (aServiceName.equals( aSupportedSvcNames[i] )) 127cdf0e10cSrcweir bFound = true; 128cdf0e10cSrcweir } 129cdf0e10cSrcweir return bFound; 130cdf0e10cSrcweir } 131cdf0e10cSrcweir getImplementationName()132cdf0e10cSrcweir public String getImplementationName() 133cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 134cdf0e10cSrcweir { 135cdf0e10cSrcweir return aSvcImplName; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir getSupportedServiceNames()138cdf0e10cSrcweir public String[] getSupportedServiceNames() 139cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 140cdf0e10cSrcweir { 141cdf0e10cSrcweir return aSupportedSvcNames; 142cdf0e10cSrcweir } 143cdf0e10cSrcweir }; 144cdf0e10cSrcweir 145