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.comp.loader; 25cdf0e10cSrcweir 26cdf0e10cSrcweir 27cdf0e10cSrcweir import java.lang.reflect.Constructor; 28cdf0e10cSrcweir import java.lang.reflect.Field; 29cdf0e10cSrcweir import java.lang.reflect.InvocationTargetException; 30cdf0e10cSrcweir 31cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 32cdf0e10cSrcweir import com.sun.star.lang.XInitialization; 33cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 34cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 35cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 36cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory; 37cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider; 38cdf0e10cSrcweir 39cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey; 40cdf0e10cSrcweir 41cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 42cdf0e10cSrcweir import com.sun.star.uno.Type; 43cdf0e10cSrcweir 44cdf0e10cSrcweir 45cdf0e10cSrcweir /** 46cdf0e10cSrcweir * The purpose of this class to help component implementation. 47cdf0e10cSrcweir * This class has default implementations for <code>getServiceFactory</code> 48cdf0e10cSrcweir * and <code>writeRegistryServiceInfo</code>. 49cdf0e10cSrcweir * <p> 50cdf0e10cSrcweir * @version $Revision: 1.9 $ $ $Date: 2008-04-11 11:10:09 $ 51cdf0e10cSrcweir * @author Kay Ramme 52cdf0e10cSrcweir * @see com.sun.star.lang.XMultiServiceFactory 53cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 54cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 55cdf0e10cSrcweir * @see com.sun.star.registry.XRegistryKey 56cdf0e10cSrcweir * @since UDK1.0 57cdf0e10cSrcweir */ 58cdf0e10cSrcweir public class FactoryHelper { 59cdf0e10cSrcweir 60cdf0e10cSrcweir private static final boolean DEBUG = false; 61cdf0e10cSrcweir // the factory 62cdf0e10cSrcweir static protected class Factory 63cdf0e10cSrcweir implements XSingleServiceFactory, XSingleComponentFactory, XServiceInfo, 64cdf0e10cSrcweir XTypeProvider { 65cdf0e10cSrcweir protected static Class __objectArray; 66cdf0e10cSrcweir 67cdf0e10cSrcweir static { 68cdf0e10cSrcweir try { 69cdf0e10cSrcweir __objectArray = Class.forName("[Ljava.lang.Object;"); 70cdf0e10cSrcweir } 71cdf0e10cSrcweir catch(ClassNotFoundException classNotFoundException) { 72cdf0e10cSrcweir System.err.println(FactoryHelper.class.getName() + " exception occurred - " + classNotFoundException); 73cdf0e10cSrcweir } 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir // private static final boolean DEBUG = false; 77cdf0e10cSrcweir 78cdf0e10cSrcweir protected XMultiServiceFactory _xMultiServiceFactory; 79cdf0e10cSrcweir protected XRegistryKey _xRegistryKey; 80cdf0e10cSrcweir protected int _nCode; 81cdf0e10cSrcweir protected Constructor _constructor; 82cdf0e10cSrcweir protected String _implName; 83cdf0e10cSrcweir protected String _serviceName; 84cdf0e10cSrcweir // keeps the Id for XTypeProvider 85cdf0e10cSrcweir protected static Object _mutex= new Object(); 86cdf0e10cSrcweir private static byte[] _implementationId; 87cdf0e10cSrcweir Factory(Class implClass, String serviceName, XMultiServiceFactory xMultiServiceFactory, XRegistryKey xRegistryKey)88cdf0e10cSrcweir protected Factory(Class implClass, 89cdf0e10cSrcweir String serviceName, 90cdf0e10cSrcweir XMultiServiceFactory xMultiServiceFactory, 91cdf0e10cSrcweir XRegistryKey xRegistryKey) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir _xMultiServiceFactory = xMultiServiceFactory; 94cdf0e10cSrcweir _xRegistryKey = xRegistryKey; 95cdf0e10cSrcweir _implName = implClass.getName(); 96cdf0e10cSrcweir _serviceName = serviceName; 97cdf0e10cSrcweir 98cdf0e10cSrcweir Constructor constructors[] = implClass.getConstructors(); 99cdf0e10cSrcweir for(int i = 0; i < constructors.length && _constructor == null; ++i) { 100cdf0e10cSrcweir Class parameters[] = constructors[i].getParameterTypes(); 101cdf0e10cSrcweir 102cdf0e10cSrcweir if(parameters.length == 3 103cdf0e10cSrcweir && parameters[0].equals(XComponentContext.class) 104cdf0e10cSrcweir && parameters[1].equals(XRegistryKey.class) 105cdf0e10cSrcweir && parameters[2].equals(__objectArray)) { 106cdf0e10cSrcweir _nCode = 0; 107cdf0e10cSrcweir _constructor = constructors[i]; 108cdf0e10cSrcweir } 109cdf0e10cSrcweir else if(parameters.length == 2 110cdf0e10cSrcweir && parameters[0].equals(XComponentContext.class) 111cdf0e10cSrcweir && parameters[1].equals(XRegistryKey.class)) { 112cdf0e10cSrcweir _nCode = 1; 113cdf0e10cSrcweir _constructor = constructors[i]; 114cdf0e10cSrcweir } 115cdf0e10cSrcweir else if(parameters.length == 2 116cdf0e10cSrcweir && parameters[0].equals(XComponentContext.class) 117cdf0e10cSrcweir && parameters[1].equals(__objectArray)) { 118cdf0e10cSrcweir _nCode = 2; 119cdf0e10cSrcweir _constructor = constructors[i]; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir else if(parameters.length == 1 122cdf0e10cSrcweir && parameters[0].equals(XComponentContext.class)) { 123cdf0e10cSrcweir _nCode = 3; 124cdf0e10cSrcweir _constructor = constructors[i]; 125cdf0e10cSrcweir } 126cdf0e10cSrcweir // depr 127cdf0e10cSrcweir else if(parameters.length == 3 128cdf0e10cSrcweir && parameters[0].equals(XMultiServiceFactory.class) 129cdf0e10cSrcweir && parameters[1].equals(XRegistryKey.class) 130cdf0e10cSrcweir && parameters[2].equals(__objectArray)) { 131cdf0e10cSrcweir _nCode = 4; 132cdf0e10cSrcweir _constructor = constructors[i]; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir else if(parameters.length == 2 135cdf0e10cSrcweir && parameters[0].equals(XMultiServiceFactory.class) 136cdf0e10cSrcweir && parameters[1].equals(XRegistryKey.class)) { 137cdf0e10cSrcweir _nCode = 5; 138cdf0e10cSrcweir _constructor = constructors[i]; 139cdf0e10cSrcweir } 140cdf0e10cSrcweir else if(parameters.length == 2 141cdf0e10cSrcweir && parameters[0].equals(XMultiServiceFactory.class) 142cdf0e10cSrcweir && parameters[1].equals(__objectArray)) { 143cdf0e10cSrcweir _nCode = 6; 144cdf0e10cSrcweir _constructor = constructors[i]; 145cdf0e10cSrcweir } 146cdf0e10cSrcweir else if(parameters.length == 1 147cdf0e10cSrcweir && parameters[0].equals(XMultiServiceFactory.class)) { 148cdf0e10cSrcweir _nCode = 7; 149cdf0e10cSrcweir _constructor = constructors[i]; 150cdf0e10cSrcweir } 151cdf0e10cSrcweir else if(parameters.length == 1 152cdf0e10cSrcweir && parameters[0].equals(__objectArray)) { 153cdf0e10cSrcweir _nCode = 8; 154cdf0e10cSrcweir _constructor = constructors[i]; 155cdf0e10cSrcweir } 156cdf0e10cSrcweir else if(parameters.length == 0) { 157cdf0e10cSrcweir _nCode = 9; 158cdf0e10cSrcweir _constructor = constructors[i]; 159cdf0e10cSrcweir } 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir if(_constructor == null) // have not found a useable constructor 163cdf0e10cSrcweir throw new com.sun.star.uno.RuntimeException(getClass().getName() + " can not find a useable constructor"); 164cdf0e10cSrcweir } 165cdf0e10cSrcweir getSMgr( XComponentContext xContext )166cdf0e10cSrcweir private final XMultiServiceFactory getSMgr( XComponentContext xContext ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir if (xContext != null) 169cdf0e10cSrcweir { 170cdf0e10cSrcweir return UnoRuntime.queryInterface( 171cdf0e10cSrcweir XMultiServiceFactory.class, xContext.getServiceManager() ); 172cdf0e10cSrcweir } 173cdf0e10cSrcweir else 174cdf0e10cSrcweir { 175cdf0e10cSrcweir return _xMultiServiceFactory; 176cdf0e10cSrcweir } 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179cdf0e10cSrcweir // XComponentContext impl 180cdf0e10cSrcweir //______________________________________________________________________________________________ createInstanceWithContext( XComponentContext xContext )181cdf0e10cSrcweir public Object createInstanceWithContext( 182cdf0e10cSrcweir XComponentContext xContext ) 183cdf0e10cSrcweir throws com.sun.star.uno.Exception 184cdf0e10cSrcweir { 185cdf0e10cSrcweir Object args[]; 186cdf0e10cSrcweir switch (_nCode) 187cdf0e10cSrcweir { 188cdf0e10cSrcweir case 0: 189cdf0e10cSrcweir args = new Object [] { xContext, _xRegistryKey, new Object[ 0 ] }; 190cdf0e10cSrcweir break; 191cdf0e10cSrcweir case 1: 192cdf0e10cSrcweir args = new Object [] { xContext, _xRegistryKey }; 193cdf0e10cSrcweir break; 194cdf0e10cSrcweir case 2: 195cdf0e10cSrcweir args = new Object [] { xContext, new Object[ 0 ] }; 196cdf0e10cSrcweir break; 197cdf0e10cSrcweir case 3: 198cdf0e10cSrcweir args = new Object [] { xContext }; 199cdf0e10cSrcweir break; 200cdf0e10cSrcweir case 4: 201cdf0e10cSrcweir args = new Object [] { getSMgr( xContext ), _xRegistryKey, new Object[ 0 ] }; 202cdf0e10cSrcweir break; 203cdf0e10cSrcweir case 5: 204cdf0e10cSrcweir args = new Object [] { getSMgr( xContext ), _xRegistryKey }; 205cdf0e10cSrcweir break; 206cdf0e10cSrcweir case 6: 207cdf0e10cSrcweir args = new Object [] { getSMgr( xContext ), new Object[ 0 ] }; 208cdf0e10cSrcweir break; 209cdf0e10cSrcweir case 7: 210cdf0e10cSrcweir args = new Object [] { getSMgr( xContext ) }; 211cdf0e10cSrcweir break; 212cdf0e10cSrcweir case 8: 213cdf0e10cSrcweir args = new Object [] { new Object[ 0 ] }; 214cdf0e10cSrcweir break; 215cdf0e10cSrcweir default: 216cdf0e10cSrcweir args = new Object [ 0 ]; 217cdf0e10cSrcweir break; 218cdf0e10cSrcweir } 219cdf0e10cSrcweir 220cdf0e10cSrcweir try 221cdf0e10cSrcweir { 222cdf0e10cSrcweir return _constructor.newInstance( args ); 223cdf0e10cSrcweir } 224cdf0e10cSrcweir catch (InvocationTargetException invocationTargetException) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir Throwable targetException = invocationTargetException.getTargetException(); 227cdf0e10cSrcweir 228cdf0e10cSrcweir if (targetException instanceof java.lang.RuntimeException) 229cdf0e10cSrcweir throw (java.lang.RuntimeException)targetException; 230cdf0e10cSrcweir else if (targetException instanceof com.sun.star.uno.Exception) 231cdf0e10cSrcweir throw (com.sun.star.uno.Exception)targetException; 232cdf0e10cSrcweir else if (targetException instanceof com.sun.star.uno.RuntimeException) 233cdf0e10cSrcweir throw (com.sun.star.uno.RuntimeException)targetException; 234cdf0e10cSrcweir else 235cdf0e10cSrcweir throw new com.sun.star.uno.Exception( targetException.toString() ); 236cdf0e10cSrcweir } 237cdf0e10cSrcweir catch (IllegalAccessException illegalAccessException) 238cdf0e10cSrcweir { 239cdf0e10cSrcweir throw new com.sun.star.uno.Exception( illegalAccessException.toString() ); 240cdf0e10cSrcweir } 241cdf0e10cSrcweir catch (InstantiationException instantiationException) 242cdf0e10cSrcweir { 243cdf0e10cSrcweir throw new com.sun.star.uno.Exception( instantiationException.toString() ); 244cdf0e10cSrcweir } 245cdf0e10cSrcweir } 246cdf0e10cSrcweir //______________________________________________________________________________________________ createInstanceWithArgumentsAndContext( Object rArguments[], XComponentContext xContext )247cdf0e10cSrcweir public Object createInstanceWithArgumentsAndContext( 248cdf0e10cSrcweir Object rArguments[], XComponentContext xContext ) 249cdf0e10cSrcweir throws com.sun.star.uno.Exception 250cdf0e10cSrcweir { 251cdf0e10cSrcweir Object args[]; 252cdf0e10cSrcweir 253cdf0e10cSrcweir boolean bInitCall = true; 254cdf0e10cSrcweir switch (_nCode) 255cdf0e10cSrcweir { 256cdf0e10cSrcweir case 0: 257cdf0e10cSrcweir args = new Object [] { xContext, _xRegistryKey, rArguments }; 258cdf0e10cSrcweir bInitCall = false; 259cdf0e10cSrcweir break; 260cdf0e10cSrcweir case 1: 261cdf0e10cSrcweir args = new Object [] { xContext, _xRegistryKey }; 262cdf0e10cSrcweir break; 263cdf0e10cSrcweir case 2: 264cdf0e10cSrcweir args = new Object [] { xContext, rArguments }; 265cdf0e10cSrcweir bInitCall = false; 266cdf0e10cSrcweir break; 267cdf0e10cSrcweir case 3: 268cdf0e10cSrcweir args = new Object [] { xContext }; 269cdf0e10cSrcweir break; 270cdf0e10cSrcweir case 4: 271cdf0e10cSrcweir args = new Object [] { getSMgr( xContext ), _xRegistryKey, rArguments }; 272cdf0e10cSrcweir bInitCall = false; 273cdf0e10cSrcweir break; 274cdf0e10cSrcweir case 5: 275cdf0e10cSrcweir args = new Object [] { getSMgr( xContext ), _xRegistryKey }; 276cdf0e10cSrcweir break; 277cdf0e10cSrcweir case 6: 278cdf0e10cSrcweir args = new Object [] { getSMgr( xContext ), rArguments }; 279cdf0e10cSrcweir bInitCall = false; 280cdf0e10cSrcweir break; 281cdf0e10cSrcweir case 7: 282cdf0e10cSrcweir args = new Object [] { getSMgr( xContext ) }; 283cdf0e10cSrcweir break; 284cdf0e10cSrcweir case 8: 285cdf0e10cSrcweir args = new Object [] { rArguments }; 286cdf0e10cSrcweir bInitCall = false; 287cdf0e10cSrcweir break; 288cdf0e10cSrcweir default: 289cdf0e10cSrcweir args = new Object [ 0 ]; 290cdf0e10cSrcweir break; 291cdf0e10cSrcweir } 292cdf0e10cSrcweir 293cdf0e10cSrcweir try 294cdf0e10cSrcweir { 295cdf0e10cSrcweir Object instance = _constructor.newInstance( args ); 296cdf0e10cSrcweir if (bInitCall) 297cdf0e10cSrcweir { 298cdf0e10cSrcweir XInitialization xInitialization = UnoRuntime.queryInterface( 299cdf0e10cSrcweir XInitialization.class, instance ); 300cdf0e10cSrcweir if (xInitialization != null) 301cdf0e10cSrcweir { 302cdf0e10cSrcweir xInitialization.initialize( rArguments ); 303cdf0e10cSrcweir } 304cdf0e10cSrcweir } 305cdf0e10cSrcweir return instance; 306cdf0e10cSrcweir } 307cdf0e10cSrcweir catch (InvocationTargetException invocationTargetException) 308cdf0e10cSrcweir { 309cdf0e10cSrcweir Throwable targetException = invocationTargetException.getTargetException(); 310cdf0e10cSrcweir 311cdf0e10cSrcweir if (targetException instanceof java.lang.RuntimeException) 312cdf0e10cSrcweir throw (java.lang.RuntimeException)targetException; 313cdf0e10cSrcweir else if (targetException instanceof com.sun.star.uno.Exception) 314cdf0e10cSrcweir throw (com.sun.star.uno.Exception)targetException; 315cdf0e10cSrcweir else if (targetException instanceof com.sun.star.uno.RuntimeException) 316cdf0e10cSrcweir throw (com.sun.star.uno.RuntimeException)targetException; 317cdf0e10cSrcweir else 318cdf0e10cSrcweir throw new com.sun.star.uno.Exception( targetException.toString() ); 319cdf0e10cSrcweir } 320cdf0e10cSrcweir catch (IllegalAccessException illegalAccessException) 321cdf0e10cSrcweir { 322cdf0e10cSrcweir throw new com.sun.star.uno.Exception( illegalAccessException.toString() ); 323cdf0e10cSrcweir } 324cdf0e10cSrcweir catch (InstantiationException instantiationException) 325cdf0e10cSrcweir { 326cdf0e10cSrcweir throw new com.sun.star.uno.Exception( instantiationException.toString() ); 327cdf0e10cSrcweir } 328cdf0e10cSrcweir } 329cdf0e10cSrcweir 330cdf0e10cSrcweir /** 331cdf0e10cSrcweir * Creates an instance of the desired service. 332cdf0e10cSrcweir * <p> 333cdf0e10cSrcweir * @return returns an instance of the desired service 334cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 335cdf0e10cSrcweir */ createInstance()336cdf0e10cSrcweir public Object createInstance() 337cdf0e10cSrcweir throws com.sun.star.uno.Exception, 338cdf0e10cSrcweir com.sun.star.uno.RuntimeException 339cdf0e10cSrcweir { 340cdf0e10cSrcweir return createInstanceWithContext( null ); 341cdf0e10cSrcweir } 342cdf0e10cSrcweir 343cdf0e10cSrcweir /** 344cdf0e10cSrcweir * Creates an instance of the desired service. 345cdf0e10cSrcweir * <p> 346cdf0e10cSrcweir * @return returns an instance of the desired service 347cdf0e10cSrcweir * @param args the args given to the constructor of the service 348cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 349cdf0e10cSrcweir */ createInstanceWithArguments(Object[] args)350cdf0e10cSrcweir public Object createInstanceWithArguments(Object[] args) 351cdf0e10cSrcweir throws com.sun.star.uno.Exception, 352cdf0e10cSrcweir com.sun.star.uno.RuntimeException 353cdf0e10cSrcweir { 354cdf0e10cSrcweir return createInstanceWithArgumentsAndContext( args, null ); 355cdf0e10cSrcweir } 356cdf0e10cSrcweir 357cdf0e10cSrcweir /** 358cdf0e10cSrcweir * Gives the supported services 359cdf0e10cSrcweir * <p> 360cdf0e10cSrcweir * @return returns an array of supported services 361cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 362cdf0e10cSrcweir */ getSupportedServiceNames()363cdf0e10cSrcweir public String[] getSupportedServiceNames() throws com.sun.star.uno.RuntimeException { 364cdf0e10cSrcweir return new String[]{_serviceName}; 365cdf0e10cSrcweir } 366cdf0e10cSrcweir 367cdf0e10cSrcweir /** 368cdf0e10cSrcweir * Gives the implementation name 369cdf0e10cSrcweir * <p> 370cdf0e10cSrcweir * @return returns the implementation name 371cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 372cdf0e10cSrcweir */ getImplementationName()373cdf0e10cSrcweir public String getImplementationName() throws com.sun.star.uno.RuntimeException { 374cdf0e10cSrcweir return _implName; 375cdf0e10cSrcweir } 376cdf0e10cSrcweir 377cdf0e10cSrcweir /** 378cdf0e10cSrcweir * Indicates if the given service is supported. 379cdf0e10cSrcweir * <p> 380cdf0e10cSrcweir * @return returns true if the given service is supported 381cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 382cdf0e10cSrcweir */ supportsService(String serviceName)383cdf0e10cSrcweir public boolean supportsService(String serviceName) throws com.sun.star.uno.RuntimeException { 384cdf0e10cSrcweir String services[] = getSupportedServiceNames(); 385cdf0e10cSrcweir 386cdf0e10cSrcweir boolean found = false; 387cdf0e10cSrcweir 388cdf0e10cSrcweir for(int i = 0; i < services.length && !found; ++i) 389cdf0e10cSrcweir found = services[i].equals(serviceName); 390cdf0e10cSrcweir 391cdf0e10cSrcweir return found; 392cdf0e10cSrcweir } 393cdf0e10cSrcweir 394cdf0e10cSrcweir //XTypeProvider getImplementationId()395cdf0e10cSrcweir public byte[] getImplementationId() 396cdf0e10cSrcweir { 397cdf0e10cSrcweir synchronized (_mutex) 398cdf0e10cSrcweir { 399cdf0e10cSrcweir if (_implementationId == null) 400cdf0e10cSrcweir { 401cdf0e10cSrcweir int hash = hashCode(); 402cdf0e10cSrcweir String sName= getClass().getName(); 403cdf0e10cSrcweir byte[] arName= sName.getBytes(); 404cdf0e10cSrcweir int nNameLength= arName.length; 405cdf0e10cSrcweir 406cdf0e10cSrcweir _implementationId= new byte[ 4 + nNameLength]; 407cdf0e10cSrcweir _implementationId[0]= (byte)(hash & 0xff); 408cdf0e10cSrcweir _implementationId[1]= (byte)((hash >>> 8) & 0xff); 409cdf0e10cSrcweir _implementationId[2]= (byte)((hash >>> 16) & 0xff); 410cdf0e10cSrcweir _implementationId[3]= (byte)((hash >>>24) & 0xff); 411cdf0e10cSrcweir 412cdf0e10cSrcweir System.arraycopy(arName, 0, _implementationId, 4, nNameLength); 413cdf0e10cSrcweir } 414cdf0e10cSrcweir } 415cdf0e10cSrcweir return _implementationId; 416cdf0e10cSrcweir } 417cdf0e10cSrcweir //XTypeProvider getTypes()418cdf0e10cSrcweir public com.sun.star.uno.Type[] getTypes() 419cdf0e10cSrcweir { 420cdf0e10cSrcweir Type[] t = new Type[] { 421cdf0e10cSrcweir new Type(XSingleServiceFactory.class), 422cdf0e10cSrcweir new Type(XSingleComponentFactory.class), 423cdf0e10cSrcweir new Type(XServiceInfo.class), 424cdf0e10cSrcweir new Type(XTypeProvider.class) 425cdf0e10cSrcweir }; 426cdf0e10cSrcweir return t; 427cdf0e10cSrcweir } 428cdf0e10cSrcweir 429cdf0e10cSrcweir } 430cdf0e10cSrcweir 431cdf0e10cSrcweir /** 432cdf0e10cSrcweir * Creates a factory for the given class. 433cdf0e10cSrcweir * <p> 434cdf0e10cSrcweir * @deprecated as of UDK 1.0 435cdf0e10cSrcweir * <p> 436cdf0e10cSrcweir * @return returns a factory 437cdf0e10cSrcweir * @param implClass the implementing class 438cdf0e10cSrcweir * @param multiFactory the given multi service factory (service manager) 439cdf0e10cSrcweir * @param regKey the given registry key 440cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 441cdf0e10cSrcweir */ getServiceFactory(Class implClass, XMultiServiceFactory multiFactory, XRegistryKey regKey)442cdf0e10cSrcweir static public XSingleServiceFactory getServiceFactory(Class implClass, 443cdf0e10cSrcweir XMultiServiceFactory multiFactory, 444cdf0e10cSrcweir XRegistryKey regKey) 445cdf0e10cSrcweir { 446cdf0e10cSrcweir XSingleServiceFactory xSingleServiceFactory = null; 447cdf0e10cSrcweir 448cdf0e10cSrcweir try { 449cdf0e10cSrcweir Field serviceName ; 450cdf0e10cSrcweir 451cdf0e10cSrcweir try { 452cdf0e10cSrcweir serviceName = implClass.getField("__serviceName"); 453cdf0e10cSrcweir } 454cdf0e10cSrcweir catch(NoSuchFieldException noSuchFieldExceptio) { 455cdf0e10cSrcweir serviceName = implClass.getField("serviceName"); // old style 456cdf0e10cSrcweir } 457cdf0e10cSrcweir 458cdf0e10cSrcweir xSingleServiceFactory = new Factory(implClass, (String)serviceName.get(null), multiFactory, regKey); 459cdf0e10cSrcweir } 460cdf0e10cSrcweir catch(NoSuchFieldException noSuchFieldException) { 461cdf0e10cSrcweir System.err.println("##### FactoryHelper.getServiceFactory - exception:" + noSuchFieldException); 462cdf0e10cSrcweir } 463cdf0e10cSrcweir catch(IllegalAccessException illegalAccessException) { 464cdf0e10cSrcweir System.err.println("##### FactoryHelper.getServiceFactory - exception:" + illegalAccessException); 465cdf0e10cSrcweir } 466cdf0e10cSrcweir 467cdf0e10cSrcweir return xSingleServiceFactory; 468cdf0e10cSrcweir } 469cdf0e10cSrcweir 470cdf0e10cSrcweir /** 471cdf0e10cSrcweir * Creates a factory for the given class. 472cdf0e10cSrcweir * <p> 473cdf0e10cSrcweir * @return returns a factory 474cdf0e10cSrcweir * @param implClass the implementing class 475cdf0e10cSrcweir * @param serviceName the service name of the implementing class 476cdf0e10cSrcweir * @param multiFactory the given multi service factory (service manager) 477cdf0e10cSrcweir * @param regKey the given registry key 478cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 479cdf0e10cSrcweir */ getServiceFactory(Class implClass, String serviceName, XMultiServiceFactory multiFactory, XRegistryKey regKey)480cdf0e10cSrcweir static public XSingleServiceFactory getServiceFactory(Class implClass, 481cdf0e10cSrcweir String serviceName, 482cdf0e10cSrcweir XMultiServiceFactory multiFactory, 483cdf0e10cSrcweir XRegistryKey regKey) 484cdf0e10cSrcweir { 485cdf0e10cSrcweir return new Factory(implClass, serviceName, multiFactory, regKey); 486cdf0e10cSrcweir } 487cdf0e10cSrcweir 488cdf0e10cSrcweir /** Creates a factory for the given class. 489cdf0e10cSrcweir 490cdf0e10cSrcweir @return returns a factory object 491cdf0e10cSrcweir @param implClass the implementing class 492cdf0e10cSrcweir */ createComponentFactory( Class implClass, String serviceName )493cdf0e10cSrcweir static public Object createComponentFactory( Class implClass, String serviceName ) 494cdf0e10cSrcweir { 495cdf0e10cSrcweir return new Factory( implClass, serviceName, null, null ); 496cdf0e10cSrcweir } 497cdf0e10cSrcweir 498cdf0e10cSrcweir /** 499cdf0e10cSrcweir * Writes the registration data into the registry key 500cdf0e10cSrcweir * <p> 501cdf0e10cSrcweir * @return success 502cdf0e10cSrcweir * @param implName the name of the implementing class 503cdf0e10cSrcweir * @param serviceName the service name 504cdf0e10cSrcweir * @param regKey the given registry key 505cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 506cdf0e10cSrcweir */ writeRegistryServiceInfo(String implName, String serviceName, XRegistryKey regKey)507cdf0e10cSrcweir static public boolean writeRegistryServiceInfo(String implName, String serviceName, XRegistryKey regKey) { 508cdf0e10cSrcweir boolean result = false; 509cdf0e10cSrcweir 510cdf0e10cSrcweir try { 511cdf0e10cSrcweir XRegistryKey newKey = regKey.createKey("/" + implName + "/UNO/SERVICES"); 512cdf0e10cSrcweir 513cdf0e10cSrcweir newKey.createKey(serviceName); 514cdf0e10cSrcweir 515cdf0e10cSrcweir result = true; 516cdf0e10cSrcweir } 517cdf0e10cSrcweir catch (Exception ex) { 518cdf0e10cSrcweir System.err.println(">>>Connection_Impl.writeRegistryServiceInfo " + ex); 519cdf0e10cSrcweir } 520cdf0e10cSrcweir 521cdf0e10cSrcweir return result; 522cdf0e10cSrcweir } 523cdf0e10cSrcweir 524cdf0e10cSrcweir /** Writes the registration data into the registry key. 525cdf0e10cSrcweir * Several services are supported. 526cdf0e10cSrcweir * 527cdf0e10cSrcweir * @param impl_name name of implementation 528cdf0e10cSrcweir * @param supported_services supported services of implementation 529cdf0e10cSrcweir * @param xKey registry key to write to 530cdf0e10cSrcweir * @return success 531cdf0e10cSrcweir */ writeRegistryServiceInfo( String impl_name, String supported_services [], XRegistryKey xKey )532cdf0e10cSrcweir public static boolean writeRegistryServiceInfo( 533cdf0e10cSrcweir String impl_name, String supported_services [], XRegistryKey xKey ) 534cdf0e10cSrcweir { 535cdf0e10cSrcweir try 536cdf0e10cSrcweir { 537cdf0e10cSrcweir XRegistryKey xNewKey = xKey.createKey( "/" + impl_name + "/UNO/SERVICES" ); 538cdf0e10cSrcweir for ( int nPos = 0; nPos < supported_services.length; ++nPos ) 539cdf0e10cSrcweir { 540cdf0e10cSrcweir xNewKey.createKey( supported_services[ nPos ] ); 541cdf0e10cSrcweir } 542cdf0e10cSrcweir return true; 543cdf0e10cSrcweir } 544cdf0e10cSrcweir catch (com.sun.star.registry.InvalidRegistryException exc) 545cdf0e10cSrcweir { 546cdf0e10cSrcweir if (DEBUG) 547cdf0e10cSrcweir { 548cdf0e10cSrcweir System.err.println( 549cdf0e10cSrcweir "##### " + Factory.class.getName() + ".writeRegistryServiceInfo -- exc: " + 550cdf0e10cSrcweir exc.toString() ); 551cdf0e10cSrcweir } 552cdf0e10cSrcweir } 553cdf0e10cSrcweir return false; 554cdf0e10cSrcweir } 555cdf0e10cSrcweir 556cdf0e10cSrcweir } 557cdf0e10cSrcweir 558