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.servicemanager; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 27cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 28cdf0e10cSrcweir 29cdf0e10cSrcweir import com.sun.star.container.XSet; 30cdf0e10cSrcweir import com.sun.star.container.XContentEnumerationAccess; 31cdf0e10cSrcweir import com.sun.star.container.XEnumeration; 32cdf0e10cSrcweir 33cdf0e10cSrcweir import com.sun.star.lang.XComponent; 34cdf0e10cSrcweir import com.sun.star.lang.XEventListener; 35cdf0e10cSrcweir import com.sun.star.lang.XInitialization; 36cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 37cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 38cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 39cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory; 40cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 41cdf0e10cSrcweir 42cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey; 43cdf0e10cSrcweir import com.sun.star.registry.XSimpleRegistry; 44cdf0e10cSrcweir 45cdf0e10cSrcweir import com.sun.star.loader.XImplementationLoader; 46cdf0e10cSrcweir 47cdf0e10cSrcweir import java.lang.reflect.InvocationTargetException; 48cdf0e10cSrcweir 49cdf0e10cSrcweir /** 50cdf0e10cSrcweir * The <code>ServiceManager</code> class is an implmentation of the <code>ServiceManager</code>the central class needed for 51cdf0e10cSrcweir * implementing or using UNO components in Java. 52cdf0e10cSrcweir * <p> 53cdf0e10cSrcweir * The Methods <code>queryInterface</code> and <code>isSame</code> delegate 54cdf0e10cSrcweir * calls to the implementing objects and are used instead of casts 55cdf0e10cSrcweir * and identity comparisons. 56cdf0e10cSrcweir * <p> 57cdf0e10cSrcweir * @version $Revision: 1.10 $ $ $Date: 2008-04-11 11:11:46 $ 58cdf0e10cSrcweir * @author Markus Herzog 59cdf0e10cSrcweir * @see com.sun.star.lang.XMultiServiceFactory 60cdf0e10cSrcweir * @see com.sun.star.container.XSet 61cdf0e10cSrcweir * @see com.sun.star.container.XContentEnumerationAccess 62cdf0e10cSrcweir * @see com.sun.star.lang.XComponent 63cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 64cdf0e10cSrcweir * @see com.sun.star.lang.XInitialization 65cdf0e10cSrcweir * @since UDK1.0 66cdf0e10cSrcweir */ 67cdf0e10cSrcweir public class ServiceManager implements XMultiServiceFactory, 68cdf0e10cSrcweir XMultiComponentFactory, 69cdf0e10cSrcweir XSet, 70cdf0e10cSrcweir XContentEnumerationAccess, 71cdf0e10cSrcweir XComponent, 72cdf0e10cSrcweir XServiceInfo, 73cdf0e10cSrcweir XInitialization 74cdf0e10cSrcweir { 75cdf0e10cSrcweir private static final boolean DEBUG = false; 76cdf0e10cSrcweir DEBUG(String dbg)77cdf0e10cSrcweir private static final void DEBUG (String dbg) { 78cdf0e10cSrcweir if (DEBUG) System.err.println( dbg ); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir 81cdf0e10cSrcweir private static com.sun.star.uno.Type UNO_TYPE = null; 82cdf0e10cSrcweir 83cdf0e10cSrcweir XImplementationLoader loader = null; 84cdf0e10cSrcweir 85cdf0e10cSrcweir static String[] supportedServiceNames = { 86cdf0e10cSrcweir "com.sun.star.lang.MultiServiceFactory", 87cdf0e10cSrcweir "com.sun.star.lang.ServiceManager" 88cdf0e10cSrcweir }; 89cdf0e10cSrcweir 90cdf0e10cSrcweir java.util.Vector eventListener; 91cdf0e10cSrcweir java.util.Hashtable factoriesByImplNames; 92cdf0e10cSrcweir java.util.Hashtable factoriesByServiceNames; // keys: 93cdf0e10cSrcweir 94cdf0e10cSrcweir private com.sun.star.uno.XComponentContext m_xDefaultContext; 95cdf0e10cSrcweir 96cdf0e10cSrcweir /** 97cdf0e10cSrcweir * Creates a new instance of the <code>ServiceManager</code>. 98cdf0e10cSrcweir */ ServiceManager()99cdf0e10cSrcweir public ServiceManager() { 100cdf0e10cSrcweir eventListener = new java.util.Vector(); 101cdf0e10cSrcweir factoriesByImplNames = new java.util.Hashtable(); 102cdf0e10cSrcweir factoriesByServiceNames = new java.util.Hashtable(); 103cdf0e10cSrcweir m_xDefaultContext = null; 104cdf0e10cSrcweir } 105cdf0e10cSrcweir /** 106cdf0e10cSrcweir * Creates a new instance of the <code>ServiceManager</code>. 107cdf0e10cSrcweir */ ServiceManager( XComponentContext xContext )108cdf0e10cSrcweir public ServiceManager( XComponentContext xContext ) { 109cdf0e10cSrcweir eventListener = new java.util.Vector(); 110cdf0e10cSrcweir factoriesByImplNames = new java.util.Hashtable(); 111cdf0e10cSrcweir factoriesByServiceNames = new java.util.Hashtable(); 112cdf0e10cSrcweir m_xDefaultContext = xContext; 113cdf0e10cSrcweir } 114cdf0e10cSrcweir 115cdf0e10cSrcweir /** 116cdf0e10cSrcweir * Returns the service factory for the <code>ServiceManager</code>. If the given implementation name 117cdf0e10cSrcweir * does not equal to the <code>ServiceManagers</code> class name null will be returned. 118cdf0e10cSrcweir * <p> 119cdf0e10cSrcweir * @return the factory for the <code>ServiceManager</code>. 120cdf0e10cSrcweir * @param implName the implementation name of the of the service. 121cdf0e10cSrcweir * Must be equal to <code>com.sun.star.comp.servicemanager.ServicManager</code> 122cdf0e10cSrcweir * @param multiFactory refernce of the <code>MultiServiceFactory</code>. This parameter will be ignored. 123cdf0e10cSrcweir * @param regKey the root key of the registry. This parameter will be ignored. 124cdf0e10cSrcweir */ getServiceFactory( String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)125cdf0e10cSrcweir public static XSingleServiceFactory getServiceFactory( String implName, 126cdf0e10cSrcweir XMultiServiceFactory multiFactory, 127cdf0e10cSrcweir XRegistryKey regKey) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir if ( implName.equals(ServiceManager.class.getName()) ) 130cdf0e10cSrcweir return new ServiceManagerFactory(); 131cdf0e10cSrcweir 132cdf0e10cSrcweir return null; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir 136cdf0e10cSrcweir /** 137cdf0e10cSrcweir * Supplies a Java component loader. The loader component must be enlisted at the <code>ServiceManager</code> before. 138cdf0e10cSrcweir * <p> 139cdf0e10cSrcweir * @return a new instance of the Java component loader 140cdf0e10cSrcweir * @see com.sun.star.loader.Java 141cdf0e10cSrcweir */ getLoader()142cdf0e10cSrcweir private XImplementationLoader getLoader() 143cdf0e10cSrcweir throws com.sun.star.uno.Exception, 144cdf0e10cSrcweir com.sun.star.uno.RuntimeException 145cdf0e10cSrcweir { 146cdf0e10cSrcweir Object[] param = { this }; 147cdf0e10cSrcweir DEBUG("make loader"); 148cdf0e10cSrcweir Object loaderObj = createInstanceWithArgumentsAndContext( 149cdf0e10cSrcweir "com.sun.star.loader.Java", param, m_xDefaultContext ); 150cdf0e10cSrcweir 151cdf0e10cSrcweir if (loaderObj == null) 152cdf0e10cSrcweir throw new com.sun.star.uno.Exception("Can get an instance of com.sun.star.loader.Java"); 153cdf0e10cSrcweir 154cdf0e10cSrcweir return UnoRuntime.queryInterface( XImplementationLoader.class, loaderObj ); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir /** 158cdf0e10cSrcweir * Registers a list of components given by their class names. 159cdf0e10cSrcweir * <p> 160cdf0e10cSrcweir * @param newImpls list of the components that should be registered, given by their class names. 161cdf0e10cSrcweir * If any exception occured during the registration, the process will be canceled. 162cdf0e10cSrcweir * @see com.sun.star.container.XSet 163cdf0e10cSrcweir */ xaddFactories( String[] newImpls )164cdf0e10cSrcweir private void xaddFactories( String[] newImpls ) 165cdf0e10cSrcweir throws com.sun.star.uno.Exception 166cdf0e10cSrcweir { 167cdf0e10cSrcweir for (int i=0; i<newImpls.length; i++) { 168cdf0e10cSrcweir DEBUG ("try to add " + newImpls[i] ); 169cdf0e10cSrcweir Object newFactory = null; 170cdf0e10cSrcweir 171cdf0e10cSrcweir try { 172cdf0e10cSrcweir if (loader == null) 173cdf0e10cSrcweir loader = getLoader(); 174cdf0e10cSrcweir 175cdf0e10cSrcweir newFactory = loader.activate( newImpls[i], null, null, null ); 176cdf0e10cSrcweir } 177cdf0e10cSrcweir catch (com.sun.star.uno.Exception e) { 178cdf0e10cSrcweir 179cdf0e10cSrcweir //****************************** BEGIN DEPRECATED ****************************************** 180cdf0e10cSrcweir 181cdf0e10cSrcweir try { 182cdf0e10cSrcweir // try to get the class of the implementation 183cdf0e10cSrcweir Class clazz = Class.forName( newImpls[i] ); 184cdf0e10cSrcweir 185cdf0e10cSrcweir Class[] methodClassParam = { String.class, XMultiServiceFactory.class, XRegistryKey.class }; 186cdf0e10cSrcweir java.lang.reflect.Method getFactoryMeth ; 187cdf0e10cSrcweir try { 188cdf0e10cSrcweir getFactoryMeth = clazz.getMethod("__getServiceFactory", methodClassParam); 189cdf0e10cSrcweir } 190cdf0e10cSrcweir catch (NoSuchMethodException noSuchMethodEx) { 191cdf0e10cSrcweir getFactoryMeth = null; 192cdf0e10cSrcweir } 193cdf0e10cSrcweir catch (SecurityException securityExc) { 194cdf0e10cSrcweir getFactoryMeth = null; 195cdf0e10cSrcweir } 196cdf0e10cSrcweir 197cdf0e10cSrcweir if (getFactoryMeth == null) 198cdf0e10cSrcweir getFactoryMeth = clazz.getMethod("getServiceFactory", methodClassParam); 199cdf0e10cSrcweir 200cdf0e10cSrcweir Object[] methodParams = { newImpls[i], this, null }; 201cdf0e10cSrcweir newFactory = getFactoryMeth.invoke( clazz, methodParams ); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir catch (NoSuchMethodException ex) {} 204cdf0e10cSrcweir catch (SecurityException ex) {} 205cdf0e10cSrcweir catch (ClassNotFoundException ex) {} 206cdf0e10cSrcweir catch (IllegalAccessException ex) {} 207cdf0e10cSrcweir catch (IllegalArgumentException ex) {} 208cdf0e10cSrcweir catch (InvocationTargetException ex) {} 209cdf0e10cSrcweir 210cdf0e10cSrcweir //****************************** END DEPRECATED ****************************************** 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir if ( newFactory == null ) 214cdf0e10cSrcweir throw new com.sun.star.loader.CannotActivateFactoryException("Can not get factory for " + newImpls[i]); 215cdf0e10cSrcweir 216cdf0e10cSrcweir insert( newFactory ); 217cdf0e10cSrcweir } // end of for ... 218cdf0e10cSrcweir } 219cdf0e10cSrcweir 220cdf0e10cSrcweir /** 221cdf0e10cSrcweir * The method is used to add components to the <code>ServiceManager</code>. The first argument indicates a <code>SimpleRegistry</code>. 222cdf0e10cSrcweir * The components which should be added will be searched under the <i>Implementations</i> key in the registry. 223cdf0e10cSrcweir * <p> 224cdf0e10cSrcweir * @param args the first argument ( args[0] ) specifices the SimpleRegistry object 225cdf0e10cSrcweir * @see com.sun.star.lang.XInitialization 226cdf0e10cSrcweir * @see com.sun.star.lang.RegistryServiceManager 227cdf0e10cSrcweir * @see com.sun.star.lang.XSimpleRegistry 228cdf0e10cSrcweir */ initialize( Object args[] )229cdf0e10cSrcweir public void initialize( Object args[] ) 230cdf0e10cSrcweir throws com.sun.star.uno.Exception, 231cdf0e10cSrcweir com.sun.star.uno.RuntimeException { 232cdf0e10cSrcweir XSimpleRegistry xSimpleRegistry ; 233cdf0e10cSrcweir try { 234cdf0e10cSrcweir xSimpleRegistry = (XSimpleRegistry) args[0]; 235cdf0e10cSrcweir if (xSimpleRegistry != null) 236cdf0e10cSrcweir { 237cdf0e10cSrcweir XRegistryKey rootkey = xSimpleRegistry.getRootKey(); 238cdf0e10cSrcweir 239cdf0e10cSrcweir XRegistryKey implkey_xRegistryKey = rootkey.openKey("Implementations"); 240cdf0e10cSrcweir if(implkey_xRegistryKey != null) { 241cdf0e10cSrcweir XRegistryKey xRegistryKeys[] = implkey_xRegistryKey.openKeys(); 242cdf0e10cSrcweir 243cdf0e10cSrcweir for(int i = 0; i < xRegistryKeys.length; ++ i) { 244cdf0e10cSrcweir xaddFactories(new String[]{xRegistryKeys[i].getStringValue()}); 245cdf0e10cSrcweir } 246cdf0e10cSrcweir } 247cdf0e10cSrcweir } 248cdf0e10cSrcweir 249cdf0e10cSrcweir if (args.length > 1) 250cdf0e10cSrcweir { 251cdf0e10cSrcweir m_xDefaultContext = (XComponentContext)args[ 1 ]; 252cdf0e10cSrcweir } 253cdf0e10cSrcweir } 254cdf0e10cSrcweir catch (ArrayIndexOutOfBoundsException e) 255cdf0e10cSrcweir { 256cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException("Argument must not be null."); 257cdf0e10cSrcweir } 258cdf0e10cSrcweir } 259cdf0e10cSrcweir 260cdf0e10cSrcweir /** 261cdf0e10cSrcweir * Creates a new instance of a specified service. Therefor the associated factory of the service is 262cdf0e10cSrcweir * looked up and used to instanciate a new component. 263cdf0e10cSrcweir * <p> 264cdf0e10cSrcweir * @return newly created component 265cdf0e10cSrcweir * @param serviceSpecifier indicates the service or component name 266cdf0e10cSrcweir * @see com.sun.star.lang.XMultiServiceFactory 267cdf0e10cSrcweir */ createInstance( String serviceSpecifier )268cdf0e10cSrcweir public java.lang.Object createInstance( String serviceSpecifier ) 269cdf0e10cSrcweir throws com.sun.star.uno.Exception, 270cdf0e10cSrcweir com.sun.star.uno.RuntimeException 271cdf0e10cSrcweir { 272cdf0e10cSrcweir return createInstanceWithContext( serviceSpecifier, m_xDefaultContext ); 273cdf0e10cSrcweir } 274cdf0e10cSrcweir 275cdf0e10cSrcweir /** 276cdf0e10cSrcweir * Creates a new instance of a specified service with the given parameters. 277cdf0e10cSrcweir * Therefor the associated factory of the service is looked up and used to instanciate a new component. 278cdf0e10cSrcweir * <p> 279cdf0e10cSrcweir * @return newly created component 280cdf0e10cSrcweir * @param serviceSpecifier indicates the service or component name 281cdf0e10cSrcweir * @see com.sun.star.lang.XMultiServiceFactory 282cdf0e10cSrcweir */ createInstanceWithArguments( String serviceSpecifier, Object[] args )283cdf0e10cSrcweir public java.lang.Object createInstanceWithArguments( 284cdf0e10cSrcweir String serviceSpecifier, Object[] args ) 285cdf0e10cSrcweir throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException 286cdf0e10cSrcweir { 287cdf0e10cSrcweir if (DEBUG) { 288cdf0e10cSrcweir System.err.println("createInstanceWithArguments:" ); 289cdf0e10cSrcweir 290cdf0e10cSrcweir for (int i=0; i<args.length; i++) 291cdf0e10cSrcweir System.err.print(" "+ args[i]); 292cdf0e10cSrcweir 293cdf0e10cSrcweir System.err.println(); 294cdf0e10cSrcweir } 295cdf0e10cSrcweir 296cdf0e10cSrcweir return createInstanceWithArgumentsAndContext( serviceSpecifier, args, m_xDefaultContext ); 297cdf0e10cSrcweir } 298cdf0e10cSrcweir 299cdf0e10cSrcweir /** 300cdf0e10cSrcweir * Look up the factory for a given service or implementation name. 301cdf0e10cSrcweir * First the requested service name is search in the list of avaible services. If it can not be found 302cdf0e10cSrcweir * the name is looked up in the the implementation list. 303cdf0e10cSrcweir * <p> 304cdf0e10cSrcweir * @return the factory of the service / implementation 305cdf0e10cSrcweir * @param serviceSpecifier indicates the service or implementation name 306cdf0e10cSrcweir * @see com.sun.star.lang.XMultiServiceFactory 307cdf0e10cSrcweir */ queryServiceFactory(String serviceName)308cdf0e10cSrcweir private Object queryServiceFactory(String serviceName) 309cdf0e10cSrcweir throws com.sun.star.uno.Exception, 310cdf0e10cSrcweir com.sun.star.uno.RuntimeException 311cdf0e10cSrcweir { 312cdf0e10cSrcweir DEBUG("queryServiceFactory for name " + serviceName ); 313cdf0e10cSrcweir Object factory = null; 314cdf0e10cSrcweir 315cdf0e10cSrcweir if ( factoriesByServiceNames.containsKey( serviceName ) ) { 316cdf0e10cSrcweir java.util.Vector aviableFact = (java.util.Vector) factoriesByServiceNames.get( serviceName ); 317cdf0e10cSrcweir 318cdf0e10cSrcweir DEBUG(""); 319cdf0e10cSrcweir DEBUG("aviable factories for " + serviceName +" "+ aviableFact); 320cdf0e10cSrcweir DEBUG(""); 321cdf0e10cSrcweir 322cdf0e10cSrcweir if ( !aviableFact.isEmpty() ) 323cdf0e10cSrcweir factory = aviableFact.lastElement(); 324cdf0e10cSrcweir 325cdf0e10cSrcweir } else // not found in list of services - now try the implementations 326cdf0e10cSrcweir factory = factoriesByImplNames.get( serviceName ); // return null if none is aviable 327cdf0e10cSrcweir 328cdf0e10cSrcweir if (DEBUG) { 329cdf0e10cSrcweir if (factory == null) System.err.println("service not registered"); 330cdf0e10cSrcweir else 331cdf0e10cSrcweir System.err.println("service found:" + factory + " " + UnoRuntime.queryInterface(XSingleServiceFactory.class, factory)); 332cdf0e10cSrcweir } 333cdf0e10cSrcweir 334cdf0e10cSrcweir if (factory == null) 335cdf0e10cSrcweir throw new com.sun.star.uno.Exception("Query for service factory for " + serviceName + " failed."); 336cdf0e10cSrcweir 337cdf0e10cSrcweir return factory; 338cdf0e10cSrcweir } 339cdf0e10cSrcweir 340cdf0e10cSrcweir /** 341cdf0e10cSrcweir * Supplies a list of all avialable services names. 342cdf0e10cSrcweir * <p> 343cdf0e10cSrcweir * @return list of Strings of all service names 344cdf0e10cSrcweir * @see com.sun.star.container.XContentEnumerationAccess 345cdf0e10cSrcweir */ getAvailableServiceNames()346cdf0e10cSrcweir public String[] getAvailableServiceNames() 347cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 348cdf0e10cSrcweir { 349cdf0e10cSrcweir int i = 0; 350cdf0e10cSrcweir String[] availableServiceNames = new String[factoriesByServiceNames.size()]; 351cdf0e10cSrcweir 352cdf0e10cSrcweir java.util.Enumeration keys = factoriesByServiceNames.keys(); 353cdf0e10cSrcweir 354cdf0e10cSrcweir while (keys.hasMoreElements()) 355cdf0e10cSrcweir availableServiceNames[i++] = (String) keys.nextElement(); 356cdf0e10cSrcweir 357cdf0e10cSrcweir return availableServiceNames; 358cdf0e10cSrcweir } 359cdf0e10cSrcweir 360cdf0e10cSrcweir // XMultiComponentFactory implementation 361cdf0e10cSrcweir 362cdf0e10cSrcweir /** Create a service instance with given context. 363cdf0e10cSrcweir 364cdf0e10cSrcweir @param rServiceSpecifier service name 365cdf0e10cSrcweir @param xContext context 366cdf0e10cSrcweir @return service instance 367cdf0e10cSrcweir */ createInstanceWithContext( String rServiceSpecifier, com.sun.star.uno.XComponentContext xContext )368cdf0e10cSrcweir public java.lang.Object createInstanceWithContext( 369cdf0e10cSrcweir String rServiceSpecifier, 370cdf0e10cSrcweir com.sun.star.uno.XComponentContext xContext ) 371cdf0e10cSrcweir throws com.sun.star.uno.Exception 372cdf0e10cSrcweir { 373cdf0e10cSrcweir Object fac = queryServiceFactory( rServiceSpecifier ); 374cdf0e10cSrcweir if (fac != null) 375cdf0e10cSrcweir { 376cdf0e10cSrcweir XSingleComponentFactory xCompFac = UnoRuntime.queryInterface( 377cdf0e10cSrcweir XSingleComponentFactory.class, fac ); 378cdf0e10cSrcweir if (xCompFac != null) 379cdf0e10cSrcweir { 380cdf0e10cSrcweir return xCompFac.createInstanceWithContext( xContext ); 381cdf0e10cSrcweir } 382cdf0e10cSrcweir else 383cdf0e10cSrcweir { 384cdf0e10cSrcweir XSingleServiceFactory xServiceFac = UnoRuntime.queryInterface( 385cdf0e10cSrcweir XSingleServiceFactory.class, fac ); 386cdf0e10cSrcweir if (xServiceFac != null) 387cdf0e10cSrcweir { 388cdf0e10cSrcweir if (DEBUG) 389cdf0e10cSrcweir System.err.println( "### ignoring context raising service \"" + rServiceSpecifier + "\"!" ); 390cdf0e10cSrcweir return xServiceFac.createInstance(); 391cdf0e10cSrcweir } 392cdf0e10cSrcweir else 393cdf0e10cSrcweir { 394cdf0e10cSrcweir throw new com.sun.star.uno.Exception( 395cdf0e10cSrcweir "retrieved service factory object for \"" + rServiceSpecifier + 396cdf0e10cSrcweir "\" does not export XSingleComponentFactory nor XSingleServiceFactory!" ); 397cdf0e10cSrcweir } 398cdf0e10cSrcweir } 399cdf0e10cSrcweir } 400cdf0e10cSrcweir return null; 401cdf0e10cSrcweir } 402cdf0e10cSrcweir /** Create a service instance with given context and arguments. 403cdf0e10cSrcweir 404cdf0e10cSrcweir @param rServiceSpecifier service name 405cdf0e10cSrcweir @param rArguments arguments 406cdf0e10cSrcweir @param xContext context 407cdf0e10cSrcweir @return service instance 408cdf0e10cSrcweir */ createInstanceWithArgumentsAndContext( String rServiceSpecifier, java.lang.Object[] rArguments, com.sun.star.uno.XComponentContext xContext )409cdf0e10cSrcweir public java.lang.Object createInstanceWithArgumentsAndContext( 410cdf0e10cSrcweir String rServiceSpecifier, 411cdf0e10cSrcweir java.lang.Object[] rArguments, 412cdf0e10cSrcweir com.sun.star.uno.XComponentContext xContext ) 413cdf0e10cSrcweir throws com.sun.star.uno.Exception 414cdf0e10cSrcweir { 415cdf0e10cSrcweir Object fac = queryServiceFactory( rServiceSpecifier ); 416cdf0e10cSrcweir if (fac != null) 417cdf0e10cSrcweir { 418cdf0e10cSrcweir XSingleComponentFactory xCompFac = UnoRuntime.queryInterface( 419cdf0e10cSrcweir XSingleComponentFactory.class, fac ); 420cdf0e10cSrcweir if (xCompFac != null) 421cdf0e10cSrcweir { 422cdf0e10cSrcweir return xCompFac.createInstanceWithArgumentsAndContext( rArguments, xContext ); 423cdf0e10cSrcweir } 424cdf0e10cSrcweir else 425cdf0e10cSrcweir { 426cdf0e10cSrcweir XSingleServiceFactory xServiceFac = UnoRuntime.queryInterface( 427cdf0e10cSrcweir XSingleServiceFactory.class, fac ); 428cdf0e10cSrcweir if (xServiceFac != null) 429cdf0e10cSrcweir { 430cdf0e10cSrcweir if (DEBUG) 431cdf0e10cSrcweir System.err.println( "### ignoring context raising service \"" + rServiceSpecifier + "\"!" ); 432cdf0e10cSrcweir return xServiceFac.createInstanceWithArguments( rArguments ); 433cdf0e10cSrcweir } 434cdf0e10cSrcweir else 435cdf0e10cSrcweir { 436cdf0e10cSrcweir throw new com.sun.star.uno.Exception( 437cdf0e10cSrcweir "retrieved service factory object for \"" + rServiceSpecifier + 438cdf0e10cSrcweir "\" does not export XSingleComponentFactory nor XSingleServiceFactory!" ); 439cdf0e10cSrcweir } 440cdf0e10cSrcweir } 441cdf0e10cSrcweir } 442cdf0e10cSrcweir return null; 443cdf0e10cSrcweir } 444cdf0e10cSrcweir // public String[] getAvailableServiceNames(); 445cdf0e10cSrcweir 446cdf0e10cSrcweir /** 447cdf0e10cSrcweir * Removes all listeners from the <code>ServiceManager</code> and clears the list of the services. 448cdf0e10cSrcweir * <p> 449cdf0e10cSrcweir * @see com.sun.star.lang.XComponent 450cdf0e10cSrcweir */ dispose()451cdf0e10cSrcweir public void dispose() 452cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 453cdf0e10cSrcweir { 454cdf0e10cSrcweir if (eventListener != null) { 455cdf0e10cSrcweir java.util.Enumeration enumer = eventListener.elements(); 456cdf0e10cSrcweir 457cdf0e10cSrcweir while (enumer.hasMoreElements()) { 458cdf0e10cSrcweir XEventListener listener = (XEventListener) enumer.nextElement(); 459cdf0e10cSrcweir listener.disposing(new com.sun.star.lang.EventObject(this)); 460cdf0e10cSrcweir } 461cdf0e10cSrcweir eventListener.removeAllElements(); 462cdf0e10cSrcweir } 463cdf0e10cSrcweir 464cdf0e10cSrcweir factoriesByServiceNames.clear(); 465cdf0e10cSrcweir factoriesByImplNames.clear(); 466cdf0e10cSrcweir } 467cdf0e10cSrcweir 468cdf0e10cSrcweir /** 469cdf0e10cSrcweir * Adds a new <code>EventListener</code>. The listener is notified when a 470cdf0e10cSrcweir * service is added (removed) to (from) the <code>ServiceManager</code>. 471cdf0e10cSrcweir * If the listener is already registred a 472cdf0e10cSrcweir * <code>com.sun.star.uno.RuntimeException</code> will be thrown. 473cdf0e10cSrcweir * <p> 474cdf0e10cSrcweir * @param xListener the new listener which should been added. 475cdf0e10cSrcweir * @see com.sun.star.lang.XComponent 476cdf0e10cSrcweir */ addEventListener( XEventListener xListener )477cdf0e10cSrcweir public void addEventListener( XEventListener xListener ) 478cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 479cdf0e10cSrcweir { 480cdf0e10cSrcweir if (xListener == null) 481cdf0e10cSrcweir throw new com.sun.star.uno.RuntimeException("Listener must not be null"); 482cdf0e10cSrcweir 483cdf0e10cSrcweir if ( eventListener.contains(xListener) ) 484cdf0e10cSrcweir throw new com.sun.star.uno.RuntimeException("Listener already registred."); 485cdf0e10cSrcweir 486cdf0e10cSrcweir eventListener.addElement(xListener); 487cdf0e10cSrcweir } 488cdf0e10cSrcweir 489cdf0e10cSrcweir /** 490cdf0e10cSrcweir * Removes a <code>EventListener</code> from the <code>ServiceManager</code>. 491cdf0e10cSrcweir * If the listener is not registered a <code>com.sun.star.uno.RuntimeException</code> 492cdf0e10cSrcweir * will be thrown. 493cdf0e10cSrcweir * <p> 494cdf0e10cSrcweir * @param xListener the new listener which should been removed. 495cdf0e10cSrcweir * @see com.sun.star.lang.XComponent 496cdf0e10cSrcweir */ removeEventListener( XEventListener xListener )497cdf0e10cSrcweir public void removeEventListener( XEventListener xListener ) 498cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 499cdf0e10cSrcweir { 500cdf0e10cSrcweir if (xListener == null) 501cdf0e10cSrcweir throw new com.sun.star.uno.RuntimeException("Listener must not be null"); 502cdf0e10cSrcweir 503cdf0e10cSrcweir if ( !eventListener.contains(xListener) ) 504cdf0e10cSrcweir throw new com.sun.star.uno.RuntimeException("Listener is not registered."); 505cdf0e10cSrcweir 506cdf0e10cSrcweir eventListener.removeElement(xListener); 507cdf0e10cSrcweir } 508cdf0e10cSrcweir 509cdf0e10cSrcweir /** 510cdf0e10cSrcweir * Checks if a component is registered at the <code>ServiceManager</code>. The given object argument must 511cdf0e10cSrcweir * provide a <code>XServiceInfo</code> interface. 512cdf0e10cSrcweir * <p> 513cdf0e10cSrcweir * @return true if the component is registred otherwise false. 514cdf0e10cSrcweir * @param object object which provides a <code>XServiceInfo</code> interface. 515cdf0e10cSrcweir * @see com.sun.star.container.XSet 516cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 517cdf0e10cSrcweir */ has( Object object )518cdf0e10cSrcweir public boolean has( Object object ) 519cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 520cdf0e10cSrcweir { 521cdf0e10cSrcweir if (object == null) 522cdf0e10cSrcweir throw new com.sun.star.uno.RuntimeException("The parameter must not been null"); 523cdf0e10cSrcweir 524cdf0e10cSrcweir XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, object); 525cdf0e10cSrcweir 526cdf0e10cSrcweir return xServiceInfo != null && UnoRuntime.areSame(factoriesByImplNames.get(xServiceInfo.getImplementationName()), object); 527cdf0e10cSrcweir 528cdf0e10cSrcweir } 529cdf0e10cSrcweir 530cdf0e10cSrcweir /** 531cdf0e10cSrcweir * Adds a <code>SingleServiceFactory</code> to the <code>ServiceManager</code>. 532cdf0e10cSrcweir * <p> 533cdf0e10cSrcweir * @param object factory which should be added. 534cdf0e10cSrcweir * @see com.sun.star.container.XSet 535cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 536cdf0e10cSrcweir */ insert( Object object )537cdf0e10cSrcweir public void insert( Object object ) 538cdf0e10cSrcweir throws com.sun.star.lang.IllegalArgumentException, 539cdf0e10cSrcweir com.sun.star.container.ElementExistException, 540cdf0e10cSrcweir com.sun.star.uno.RuntimeException 541cdf0e10cSrcweir { 542cdf0e10cSrcweir if (object == null) throw new com.sun.star.lang.IllegalArgumentException(); 543cdf0e10cSrcweir 544cdf0e10cSrcweir XServiceInfo xServiceInfo = 545cdf0e10cSrcweir UnoRuntime.queryInterface(XServiceInfo.class, object); 546cdf0e10cSrcweir 547cdf0e10cSrcweir if (xServiceInfo == null) 548cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException( 549cdf0e10cSrcweir "The given object does not implement the XServiceInfo interface." 550cdf0e10cSrcweir ); 551cdf0e10cSrcweir 552cdf0e10cSrcweir if ( factoriesByImplNames.containsKey( xServiceInfo.getImplementationName() ) ) { 553cdf0e10cSrcweir throw new com.sun.star.container.ElementExistException( 554cdf0e10cSrcweir xServiceInfo.getImplementationName() + " already registred" 555cdf0e10cSrcweir ); 556cdf0e10cSrcweir } 557cdf0e10cSrcweir 558cdf0e10cSrcweir DEBUG("add factory " + object.toString() + " for " + xServiceInfo.getImplementationName()); 559cdf0e10cSrcweir factoriesByImplNames.put( xServiceInfo.getImplementationName(), object ); 560cdf0e10cSrcweir 561cdf0e10cSrcweir 562cdf0e10cSrcweir String[] serviceNames = xServiceInfo.getSupportedServiceNames(); 563cdf0e10cSrcweir java.util.Vector vec ; 564cdf0e10cSrcweir 565cdf0e10cSrcweir for (int i=0; i<serviceNames.length; i++) { 566cdf0e10cSrcweir if ( !factoriesByServiceNames.containsKey( serviceNames[i] ) ) { 567cdf0e10cSrcweir DEBUG("> no registered services found under " + serviceNames[i] + ": adding..." ); 568cdf0e10cSrcweir factoriesByServiceNames.put(serviceNames[i], new java.util.Vector()); 569cdf0e10cSrcweir } 570cdf0e10cSrcweir 571cdf0e10cSrcweir vec = (java.util.Vector) factoriesByServiceNames.get( serviceNames[i] ); 572cdf0e10cSrcweir 573cdf0e10cSrcweir if ( vec.contains( object ) ) 574cdf0e10cSrcweir System.err.println("The implementation " + xServiceInfo.getImplementationName() + 575cdf0e10cSrcweir " already registered for the service " + serviceNames[i] + " - ignoring!"); 576cdf0e10cSrcweir else 577cdf0e10cSrcweir vec.addElement(object); 578cdf0e10cSrcweir } 579cdf0e10cSrcweir } 580cdf0e10cSrcweir 581cdf0e10cSrcweir /** 582cdf0e10cSrcweir * Removes a <code>SingleServiceFactory</code> from the <code>ServiceManager</code>. 583cdf0e10cSrcweir * <p> 584cdf0e10cSrcweir * @param object factory which should be removed. 585cdf0e10cSrcweir * @see com.sun.star.container.XSet 586cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 587cdf0e10cSrcweir */ remove( Object object )588cdf0e10cSrcweir public void remove( Object object ) 589cdf0e10cSrcweir throws com.sun.star.lang.IllegalArgumentException, 590cdf0e10cSrcweir com.sun.star.container.NoSuchElementException, 591cdf0e10cSrcweir com.sun.star.uno.RuntimeException 592cdf0e10cSrcweir { 593cdf0e10cSrcweir if (object == null) 594cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException( 595cdf0e10cSrcweir "The given object must not be null." 596cdf0e10cSrcweir ); 597cdf0e10cSrcweir 598cdf0e10cSrcweir XServiceInfo xServiceInfo = 599cdf0e10cSrcweir UnoRuntime.queryInterface(XServiceInfo.class, object); 600cdf0e10cSrcweir 601cdf0e10cSrcweir if (xServiceInfo == null) 602cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException( 603cdf0e10cSrcweir "The given object does not implement the XServiceInfo interface." 604cdf0e10cSrcweir ); 605cdf0e10cSrcweir 606cdf0e10cSrcweir XSingleServiceFactory xSingleServiceFactory = 607cdf0e10cSrcweir UnoRuntime.queryInterface(XSingleServiceFactory.class, object); 608cdf0e10cSrcweir 609cdf0e10cSrcweir if (xSingleServiceFactory == null) 610cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException( 611cdf0e10cSrcweir "The given object does not implement the XSingleServiceFactory interface." 612cdf0e10cSrcweir ); 613cdf0e10cSrcweir 614cdf0e10cSrcweir if ( factoriesByImplNames.remove( xServiceInfo.getImplementationName() ) == null ) 615cdf0e10cSrcweir throw new com.sun.star.container.NoSuchElementException( 616cdf0e10cSrcweir xServiceInfo.getImplementationName() + 617cdf0e10cSrcweir " is not registered as an implementation." 618cdf0e10cSrcweir ); 619cdf0e10cSrcweir 620cdf0e10cSrcweir String[] serviceNames = xServiceInfo.getSupportedServiceNames(); 621cdf0e10cSrcweir 622cdf0e10cSrcweir for ( int i=0; i<serviceNames.length; i++ ) { 623cdf0e10cSrcweir if ( factoriesByServiceNames.containsKey( serviceNames[i] ) ) { 624cdf0e10cSrcweir java.util.Vector vec = (java.util.Vector) factoriesByServiceNames.get(serviceNames[i]); 625cdf0e10cSrcweir 626cdf0e10cSrcweir if ( !vec.removeElement(object) ) 627cdf0e10cSrcweir System.err.println("The implementation " + xServiceInfo.getImplementationName() + 628cdf0e10cSrcweir " is not registered for the service " + serviceNames[i] + " - ignoring!"); 629cdf0e10cSrcweir 630cdf0e10cSrcweir if ( vec.isEmpty() ) // remove the vector if no implementations aviable for the service 631cdf0e10cSrcweir factoriesByServiceNames.remove( serviceNames[i] ); 632cdf0e10cSrcweir } 633cdf0e10cSrcweir } 634cdf0e10cSrcweir } 635cdf0e10cSrcweir 636cdf0e10cSrcweir /** 637cdf0e10cSrcweir * Provides an enumeration of all registred services. 638cdf0e10cSrcweir * <p> 639cdf0e10cSrcweir * @return an enumeration of all avialable services. 640cdf0e10cSrcweir * @see com.sun.star.conatiner.XEnumerationAccess 641cdf0e10cSrcweir */ createEnumeration()642cdf0e10cSrcweir public XEnumeration createEnumeration() 643cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 644cdf0e10cSrcweir { 645cdf0e10cSrcweir return new ServiceEnumerationImpl( factoriesByImplNames.elements() ); 646cdf0e10cSrcweir } 647cdf0e10cSrcweir 648cdf0e10cSrcweir /** 649cdf0e10cSrcweir * Provides the UNO type of the <code>ServiceManager</code> 650cdf0e10cSrcweir * <p> 651cdf0e10cSrcweir * @return the UNO type of the <code>ServiceManager</code>. 652cdf0e10cSrcweir * @see com.sun.star.container.XElementAccess 653cdf0e10cSrcweir * @see com.sun.star.uno.TypeClass 654cdf0e10cSrcweir */ getElementType()655cdf0e10cSrcweir public com.sun.star.uno.Type getElementType() 656cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 657cdf0e10cSrcweir { 658cdf0e10cSrcweir if ( UNO_TYPE == null ) 659cdf0e10cSrcweir UNO_TYPE = new com.sun.star.uno.Type(ServiceManager.class); 660cdf0e10cSrcweir 661cdf0e10cSrcweir return UNO_TYPE; 662cdf0e10cSrcweir } 663cdf0e10cSrcweir 664cdf0e10cSrcweir /** 665cdf0e10cSrcweir * Checks if the any componets are registered. 666cdf0e10cSrcweir * <p> 667cdf0e10cSrcweir * @return true - if the list of the registred components is not empty - otherwise false. 668cdf0e10cSrcweir * @see com.sun.star.container.XElementAccess 669cdf0e10cSrcweir */ hasElements()670cdf0e10cSrcweir public boolean hasElements() { 671cdf0e10cSrcweir return ! factoriesByImplNames.isEmpty(); 672cdf0e10cSrcweir } 673cdf0e10cSrcweir 674cdf0e10cSrcweir /** 675cdf0e10cSrcweir * Provides an enumeration of of all factorys for a specified service. 676cdf0e10cSrcweir * <p> 677cdf0e10cSrcweir * @return an enumeration for service name. 678cdf0e10cSrcweir * @param serviceName name of the requested service 679cdf0e10cSrcweir * @see com.sun.star.container.XContentEnumerationAccess 680cdf0e10cSrcweir */ createContentEnumeration( String serviceName )681cdf0e10cSrcweir public XEnumeration createContentEnumeration( String serviceName ) 682cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 683cdf0e10cSrcweir { 684cdf0e10cSrcweir XEnumeration enumer ; 685cdf0e10cSrcweir 686cdf0e10cSrcweir java.util.Vector serviceList = (java.util.Vector) factoriesByServiceNames.get(serviceName); 687cdf0e10cSrcweir 688cdf0e10cSrcweir if (serviceList != null) 689cdf0e10cSrcweir enumer = new ServiceEnumerationImpl( serviceList.elements() ); 690cdf0e10cSrcweir else 691cdf0e10cSrcweir enumer = new ServiceEnumerationImpl(); 692cdf0e10cSrcweir 693cdf0e10cSrcweir return enumer; 694cdf0e10cSrcweir } 695cdf0e10cSrcweir 696cdf0e10cSrcweir /** 697cdf0e10cSrcweir * Returns the implementation name of the <code>ServiceManager</code> component. 698cdf0e10cSrcweir * <p> 699cdf0e10cSrcweir * @return the class name of the <code>ServiceManager</code>. 700cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 701cdf0e10cSrcweir */ getImplementationName()702cdf0e10cSrcweir public String getImplementationName() 703cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 704cdf0e10cSrcweir { 705cdf0e10cSrcweir return getClass().getName(); 706cdf0e10cSrcweir } 707cdf0e10cSrcweir 708cdf0e10cSrcweir /** 709cdf0e10cSrcweir * Checks if the <code>ServiceManager</code> supports a service. 710cdf0e10cSrcweir * <p> 711cdf0e10cSrcweir * @return true if the service is supported - otherwise false. 712cdf0e10cSrcweir * @param serviceName service name which should be checked. 713cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 714cdf0e10cSrcweir */ supportsService( String serviceName )715cdf0e10cSrcweir public boolean supportsService( String serviceName ) 716cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 717cdf0e10cSrcweir { 718cdf0e10cSrcweir for (int i=0; i<supportedServiceNames.length; i++) 719cdf0e10cSrcweir if (supportedServiceNames[i].equals( serviceName )) return true; 720cdf0e10cSrcweir 721cdf0e10cSrcweir return getImplementationName().equals(serviceName); 722cdf0e10cSrcweir 723cdf0e10cSrcweir } 724cdf0e10cSrcweir 725cdf0e10cSrcweir /** 726cdf0e10cSrcweir * Supplies list of all supported services. 727cdf0e10cSrcweir * <p> 728cdf0e10cSrcweir * @return a list of all supported service names. 729cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 730cdf0e10cSrcweir */ getSupportedServiceNames()731cdf0e10cSrcweir public String[] getSupportedServiceNames() 732cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 733cdf0e10cSrcweir { 734cdf0e10cSrcweir return supportedServiceNames; 735cdf0e10cSrcweir } 736cdf0e10cSrcweir 737cdf0e10cSrcweir /** 738cdf0e10cSrcweir * The <code>ServiceEnumerationImpl</code> class provides an 739cdf0e10cSrcweir * implementation of the @see com.sun.star.container.XEnumeration interface. 740cdf0e10cSrcweir * It is a inner wrapper for a java.util.Enumeration object. 741cdf0e10cSrcweir * <p> 742cdf0e10cSrcweir * @version $Revision: 1.10 $ $ $Date: 2008-04-11 11:11:46 $ 743cdf0e10cSrcweir * @author Markus Herzog 744cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 745cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 746cdf0e10cSrcweir * @since UDK1.0 747cdf0e10cSrcweir */ 748cdf0e10cSrcweir class ServiceEnumerationImpl implements XEnumeration { 749cdf0e10cSrcweir java.util.Enumeration enumeration = null; 750cdf0e10cSrcweir 751cdf0e10cSrcweir /** 752cdf0e10cSrcweir * Constructs a new empty instance. 753cdf0e10cSrcweir */ ServiceEnumerationImpl()754cdf0e10cSrcweir public ServiceEnumerationImpl() { 755cdf0e10cSrcweir } 756cdf0e10cSrcweir 757cdf0e10cSrcweir /** 758cdf0e10cSrcweir * Constructs a new instance with a given enumeration. 759cdf0e10cSrcweir * <p> 760cdf0e10cSrcweir * @param enumer is the enumeration which should been wrapped. 761cdf0e10cSrcweir * @see com.sun.star.container.XEnumeration 762cdf0e10cSrcweir */ ServiceEnumerationImpl(java.util.Enumeration enumer)763cdf0e10cSrcweir public ServiceEnumerationImpl(java.util.Enumeration enumer) { 764cdf0e10cSrcweir enumeration = enumer; 765cdf0e10cSrcweir } 766cdf0e10cSrcweir 767cdf0e10cSrcweir /** 768cdf0e10cSrcweir * Checks if the enumeration contains more elements. 769cdf0e10cSrcweir * <p> 770cdf0e10cSrcweir * @return true if more elements are available - otherwise false. 771cdf0e10cSrcweir * @see com.sun.star.container.XEnumeration 772cdf0e10cSrcweir */ hasMoreElements()773cdf0e10cSrcweir public boolean hasMoreElements() 774cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 775cdf0e10cSrcweir { 776cdf0e10cSrcweir return enumeration != null && enumeration.hasMoreElements(); 777cdf0e10cSrcweir 778cdf0e10cSrcweir } 779cdf0e10cSrcweir 780cdf0e10cSrcweir /** 781cdf0e10cSrcweir * Returns the next element of the enumeration. If no further elements 782cdf0e10cSrcweir * available a com.sun.star.container.NoSuchElementException exception will be thrown. 783cdf0e10cSrcweir * <p> 784cdf0e10cSrcweir * @return the next element. 785cdf0e10cSrcweir * @see com.sun.star.container.XEnumeration 786cdf0e10cSrcweir */ nextElement()787cdf0e10cSrcweir public Object nextElement() 788cdf0e10cSrcweir throws com.sun.star.container.NoSuchElementException, 789cdf0e10cSrcweir com.sun.star.lang.WrappedTargetException, 790cdf0e10cSrcweir com.sun.star.uno.RuntimeException 791cdf0e10cSrcweir { 792cdf0e10cSrcweir if (enumeration == null) 793cdf0e10cSrcweir throw new com.sun.star.container.NoSuchElementException(); 794cdf0e10cSrcweir 795cdf0e10cSrcweir try { 796cdf0e10cSrcweir return enumeration.nextElement(); 797cdf0e10cSrcweir } catch (java.util.NoSuchElementException e) { 798cdf0e10cSrcweir com.sun.star.container.NoSuchElementException ex = 799cdf0e10cSrcweir new com.sun.star.container.NoSuchElementException(); 800cdf0e10cSrcweir ex.fillInStackTrace(); 801cdf0e10cSrcweir 802cdf0e10cSrcweir throw ex; 803cdf0e10cSrcweir } 804cdf0e10cSrcweir } 805cdf0e10cSrcweir } 806cdf0e10cSrcweir } 807cdf0e10cSrcweir /** 808cdf0e10cSrcweir * The <code>ServiceManagerFactory</code> is the factory class for the 809cdf0e10cSrcweir * <code>ServiceManager</code>. As all factories it implments the 810cdf0e10cSrcweir * com.sun.star.lang.XSingleServiceFactory and the com.sun.star.lang.XServiceInfo 811cdf0e10cSrcweir * interfaces. 812cdf0e10cSrcweir * <p> 813cdf0e10cSrcweir * @version $Revision: 1.10 $ $ $Date: 2008-04-11 11:11:46 $ 814cdf0e10cSrcweir * @author Markus Herzog 815cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 816cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 817cdf0e10cSrcweir * @since UDK1.0 818cdf0e10cSrcweir */ 819cdf0e10cSrcweir class ServiceManagerFactory implements XServiceInfo, XSingleComponentFactory, XSingleServiceFactory 820cdf0e10cSrcweir { 821cdf0e10cSrcweir /** 822cdf0e10cSrcweir * Creates a new instance of the <code>ServiceManagerFactory</code>. 823cdf0e10cSrcweir */ ServiceManagerFactory()824cdf0e10cSrcweir public ServiceManagerFactory() { 825cdf0e10cSrcweir } 826cdf0e10cSrcweir 827cdf0e10cSrcweir /** 828cdf0e10cSrcweir * Supplies the implementation name of the <code>ServiceManager</code>. 829cdf0e10cSrcweir * <p> 830cdf0e10cSrcweir * @return <code>ServiceManager</code> class name. 831cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 832cdf0e10cSrcweir */ getImplementationName()833cdf0e10cSrcweir public String getImplementationName() 834cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 835cdf0e10cSrcweir { 836cdf0e10cSrcweir return ServiceManager.class.getName(); 837cdf0e10cSrcweir } 838cdf0e10cSrcweir 839cdf0e10cSrcweir /** 840cdf0e10cSrcweir * Checks wether or not a service is supported. 841cdf0e10cSrcweir * <p> 842cdf0e10cSrcweir * @return true - if the service is supported, otherwise false. 843cdf0e10cSrcweir * @param serviceName the name of the service that should be checked. 844cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 845cdf0e10cSrcweir */ supportsService( String serviceName )846cdf0e10cSrcweir public boolean supportsService( String serviceName ) 847cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 848cdf0e10cSrcweir { 849cdf0e10cSrcweir for ( int i=0; i<ServiceManager.supportedServiceNames.length; i++ ) 850cdf0e10cSrcweir if ( ServiceManager.supportedServiceNames[i].equals(serviceName) ) return true; 851cdf0e10cSrcweir 852cdf0e10cSrcweir return getImplementationName().equals(serviceName); 853cdf0e10cSrcweir 854cdf0e10cSrcweir } 855cdf0e10cSrcweir 856cdf0e10cSrcweir /** 857cdf0e10cSrcweir * Returns all service names which are supported by <code>ServiceManager</code>. 858cdf0e10cSrcweir * <p> 859cdf0e10cSrcweir * @return a list aof all supported service names. 860cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 861cdf0e10cSrcweir */ getSupportedServiceNames()862cdf0e10cSrcweir public String[] getSupportedServiceNames() 863cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 864cdf0e10cSrcweir { 865cdf0e10cSrcweir return ServiceManager.supportedServiceNames; 866cdf0e10cSrcweir } 867cdf0e10cSrcweir 868cdf0e10cSrcweir /** 869cdf0e10cSrcweir * Creates a new instance of the <code>ServiceManager</code>. 870cdf0e10cSrcweir * <p> 871cdf0e10cSrcweir * @return newly created <code>ServiceManager</code> object. 872cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 873cdf0e10cSrcweir */ createInstance()874cdf0e10cSrcweir public java.lang.Object createInstance() 875cdf0e10cSrcweir throws com.sun.star.uno.Exception, 876cdf0e10cSrcweir com.sun.star.uno.RuntimeException 877cdf0e10cSrcweir { 878cdf0e10cSrcweir return new ServiceManager(); 879cdf0e10cSrcweir } 880cdf0e10cSrcweir 881cdf0e10cSrcweir /** 882cdf0e10cSrcweir * Creates a new instance of the <code>ServiceManager</code> with arguments. 883cdf0e10cSrcweir * At this time it always throws a com.sun.star.lang.NoSuchMethodException 884cdf0e10cSrcweir * because there is no the <code>ServiceManager</code> has no constructor with 885cdf0e10cSrcweir * arguments. 886cdf0e10cSrcweir * <p> 887cdf0e10cSrcweir * @return null - allways throws an exception 888cdf0e10cSrcweir * @param aArguments arguments for new instance. 889cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 890cdf0e10cSrcweir */ createInstanceWithArguments( java.lang.Object[] aArguments )891cdf0e10cSrcweir public java.lang.Object createInstanceWithArguments( java.lang.Object[] aArguments ) 892cdf0e10cSrcweir throws com.sun.star.uno.Exception, 893cdf0e10cSrcweir com.sun.star.uno.RuntimeException 894cdf0e10cSrcweir { 895cdf0e10cSrcweir throw new com.sun.star.lang.NoSuchMethodException("Constructor with arguments is not supported."); 896cdf0e10cSrcweir } 897cdf0e10cSrcweir 898cdf0e10cSrcweir // XSingleComponentFactory impl 899cdf0e10cSrcweir //______________________________________________________________________________________________ createInstanceWithContext( XComponentContext xContext )900cdf0e10cSrcweir public Object createInstanceWithContext( XComponentContext xContext ) 901cdf0e10cSrcweir throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException 902cdf0e10cSrcweir { 903cdf0e10cSrcweir return new ServiceManager( xContext ); 904cdf0e10cSrcweir } 905cdf0e10cSrcweir //______________________________________________________________________________________________ createInstanceWithArgumentsAndContext( Object aArguments [], XComponentContext xContext )906cdf0e10cSrcweir public Object createInstanceWithArgumentsAndContext( 907cdf0e10cSrcweir Object aArguments [], XComponentContext xContext ) 908cdf0e10cSrcweir throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException 909cdf0e10cSrcweir { 910cdf0e10cSrcweir throw new com.sun.star.lang.NoSuchMethodException( 911cdf0e10cSrcweir "ServiceManagerFactory.createInstanceWithArgumentsAndContext() not impl!" ); 912cdf0e10cSrcweir } 913cdf0e10cSrcweir } 914cdf0e10cSrcweir 915cdf0e10cSrcweir 916