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