2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com
.sun
.star
.comp
.servicemanager
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Collections
;
24 import com
.sun
.star
.container
.XContentEnumerationAccess
;
25 import com
.sun
.star
.container
.XEnumeration
;
26 import com
.sun
.star
.container
.XSet
;
27 import com
.sun
.star
.lang
.XComponent
;
28 import com
.sun
.star
.lang
.XEventListener
;
29 import com
.sun
.star
.lang
.XMultiComponentFactory
;
30 import com
.sun
.star
.lang
.XMultiServiceFactory
;
31 import com
.sun
.star
.lang
.XServiceInfo
;
32 import com
.sun
.star
.lang
.XSingleComponentFactory
;
33 import com
.sun
.star
.lang
.XSingleServiceFactory
;
34 import com
.sun
.star
.uno
.UnoRuntime
;
35 import com
.sun
.star
.uno
.XComponentContext
;
38 * The <code>ServiceManager</code> class is an implmentation of the
39 * <code>ServiceManager</code>the central class needed for implementing or using
40 * UNO components in Java.
42 * <p>The Methods <code>queryInterface</code> and <code>isSame</code> delegate
43 * calls to the implementing objects and are used instead of casts and identity
46 * @see com.sun.star.lang.XMultiServiceFactory
47 * @see com.sun.star.container.XSet
48 * @see com.sun.star.container.XContentEnumerationAccess
49 * @see com.sun.star.lang.XComponent
50 * @see com.sun.star.lang.XServiceInfo
53 public class ServiceManager
implements XMultiServiceFactory
,
54 XMultiComponentFactory
,
56 XContentEnumerationAccess
,
60 private static final boolean DEBUG
= false;
62 private static final void DEBUG (String dbg
) {
63 if (DEBUG
) System
.err
.println( dbg
);
66 private static com
.sun
.star
.uno
.Type UNO_TYPE
= null;
68 static String
[] supportedServiceNames
= {
69 "com.sun.star.lang.MultiServiceFactory",
70 "com.sun.star.lang.ServiceManager"
73 ArrayList
<XEventListener
> eventListener
;
74 java
.util
.HashMap
<String
, Object
> factoriesByImplNames
;
75 java
.util
.HashMap
<String
, ArrayList
<Object
>> factoriesByServiceNames
; // keys:
77 private com
.sun
.star
.uno
.XComponentContext m_xDefaultContext
;
80 * Creates a new instance of the <code>ServiceManager</code>.
82 public ServiceManager() {
83 eventListener
= new ArrayList
<XEventListener
>();
84 factoriesByImplNames
= new java
.util
.HashMap
<String
, Object
>();
85 factoriesByServiceNames
= new java
.util
.HashMap
<String
, ArrayList
<Object
>>();
86 m_xDefaultContext
= null;
89 public void setDefaultContext(XComponentContext context
) {
90 m_xDefaultContext
= context
;
94 * Creates a new instance of a specified service.
96 * <p>Therefore the associated factory of the service is looked up and used
97 * to instanciate a new component. </p>
99 * @param serviceSpecifier indicates the service or component name.
100 * @return newly created component.
102 * @see com.sun.star.lang.XMultiServiceFactory
104 public java
.lang
.Object
createInstance( String serviceSpecifier
)
105 throws com
.sun
.star
.uno
.Exception
,
106 com
.sun
.star
.uno
.RuntimeException
108 return createInstanceWithContext( serviceSpecifier
, m_xDefaultContext
);
112 * Creates a new instance of a specified service with the given parameters.
114 * <p>Therefore the associated factory of the service is looked up and used
115 * to instanciate a new component.</p>
117 * @return newly created component.
118 * @param serviceSpecifier indicates the service or component name.
119 * @see com.sun.star.lang.XMultiServiceFactory
121 public java
.lang
.Object
createInstanceWithArguments(
122 String serviceSpecifier
, Object
[] args
)
123 throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.uno
.RuntimeException
126 System
.err
.println("createInstanceWithArguments:" );
128 for (Object arg
: args
) {
129 System
.err
.print(" " + arg
);
132 System
.err
.println();
135 return createInstanceWithArgumentsAndContext( serviceSpecifier
, args
, m_xDefaultContext
);
139 * Look up the factory for a given service or implementation name.
141 * <p>First the requested service name is search in the list of available
142 * services. If it can not be found the name is looked up in the implementation
145 * @param serviceName indicates the service or implementation name.
146 * @return the factory of the service / implementation.
148 * @see com.sun.star.lang.XMultiServiceFactory
150 private Object
queryServiceFactory(String serviceName
)
151 throws com
.sun
.star
.uno
.Exception
,
152 com
.sun
.star
.uno
.RuntimeException
154 DEBUG("queryServiceFactory for name " + serviceName
);
155 Object factory
= null;
157 if ( factoriesByServiceNames
.containsKey( serviceName
) ) {
158 ArrayList
<Object
> availableFact
= factoriesByServiceNames
.get( serviceName
);
161 DEBUG("aviable factories for " + serviceName
+" "+ availableFact
);
164 if ( !availableFact
.isEmpty() )
165 factory
= availableFact
.get(availableFact
.size()-1);
167 } else // not found in list of services - now try the implementations
168 factory
= factoriesByImplNames
.get( serviceName
); // return null if none is aviable
171 if (factory
== null) System
.err
.println("service not registered");
173 System
.err
.println("service found:" + factory
+ " " + UnoRuntime
.queryInterface(XSingleServiceFactory
.class, factory
));
177 throw new com
.sun
.star
.uno
.Exception("Query for service factory for " + serviceName
+ " failed.");
183 * Supplies a list of all avialable services names.
185 * @return list of Strings of all service names.
186 * @see com.sun.star.container.XContentEnumerationAccess
188 public String
[] getAvailableServiceNames()
189 throws com
.sun
.star
.uno
.RuntimeException
192 return factoriesByServiceNames
.keySet().toArray(
193 new String
[ factoriesByServiceNames
.size() ] );
194 } catch(Exception ex
) {
195 throw new com
.sun
.star
.uno
.RuntimeException(ex
);
199 // XMultiComponentFactory implementation
201 /** Create a service instance with given context.
203 * @param rServiceSpecifier service name.
204 * @param xContext context.
205 * @return service instance.
207 public java
.lang
.Object
createInstanceWithContext(
208 String rServiceSpecifier
,
209 com
.sun
.star
.uno
.XComponentContext xContext
)
210 throws com
.sun
.star
.uno
.Exception
212 Object fac
= queryServiceFactory( rServiceSpecifier
);
215 XSingleComponentFactory xCompFac
= UnoRuntime
.queryInterface(
216 XSingleComponentFactory
.class, fac
);
217 if (xCompFac
!= null)
219 return xCompFac
.createInstanceWithContext( xContext
);
223 XSingleServiceFactory xServiceFac
= UnoRuntime
.queryInterface(
224 XSingleServiceFactory
.class, fac
);
225 if (xServiceFac
!= null)
228 System
.err
.println( "### ignoring context raising service \"" + rServiceSpecifier
+ "\"!" );
229 return xServiceFac
.createInstance();
233 throw new com
.sun
.star
.uno
.Exception(
234 "retrieved service factory object for \"" + rServiceSpecifier
+
235 "\" does not export XSingleComponentFactory nor XSingleServiceFactory!" );
242 * Create a service instance with given context and arguments.
244 * @param rServiceSpecifier service name.
245 * @param rArguments arguments.
246 * @param xContext context.
247 * @return service instance.
249 public java
.lang
.Object
createInstanceWithArgumentsAndContext(
250 String rServiceSpecifier
,
251 java
.lang
.Object
[] rArguments
,
252 com
.sun
.star
.uno
.XComponentContext xContext
)
253 throws com
.sun
.star
.uno
.Exception
255 Object fac
= queryServiceFactory( rServiceSpecifier
);
258 XSingleComponentFactory xCompFac
= UnoRuntime
.queryInterface(
259 XSingleComponentFactory
.class, fac
);
260 if (xCompFac
!= null)
262 return xCompFac
.createInstanceWithArgumentsAndContext( rArguments
, xContext
);
266 XSingleServiceFactory xServiceFac
= UnoRuntime
.queryInterface(
267 XSingleServiceFactory
.class, fac
);
268 if (xServiceFac
!= null)
271 System
.err
.println( "### ignoring context raising service \"" + rServiceSpecifier
+ "\"!" );
272 return xServiceFac
.createInstanceWithArguments( rArguments
);
276 throw new com
.sun
.star
.uno
.Exception(
277 "retrieved service factory object for \"" + rServiceSpecifier
+
278 "\" does not export XSingleComponentFactory nor XSingleServiceFactory!" );
286 * Removes all listeners from the <code>ServiceManager</code> and clears the
287 * list of the services.
289 * @see com.sun.star.lang.XComponent
291 public void dispose()
292 throws com
.sun
.star
.uno
.RuntimeException
294 if (eventListener
!= null) {
295 for (XEventListener listener
: eventListener
) {
296 listener
.disposing(new com
.sun
.star
.lang
.EventObject(this));
298 eventListener
.clear();
301 factoriesByServiceNames
.clear();
302 factoriesByImplNames
.clear();
306 * Adds a new <code>EventListener</code>.
308 * <p>The listener is notified when a service is added (removed) to (from)
309 * the <code>ServiceManager</code>.</p>
311 * <p>If the listener is already registred a
312 * <code>com.sun.star.uno.RuntimeException</code> will be thrown.</p>
314 * @param xListener the new listener which should been added.
315 * @see com.sun.star.lang.XComponent
317 public void addEventListener( XEventListener xListener
)
318 throws com
.sun
.star
.uno
.RuntimeException
320 if (xListener
== null)
321 throw new com
.sun
.star
.uno
.RuntimeException("Listener must not be null");
323 if ( eventListener
.contains(xListener
) )
324 throw new com
.sun
.star
.uno
.RuntimeException("Listener already registred.");
326 eventListener
.add(xListener
);
330 * Removes a <code>EventListener</code> from the <code>ServiceManager</code>.
332 * <p>If the listener is not registered a <code>com.sun.star.uno.RuntimeException</code>
333 * will be thrown.</p>
335 * @param xListener the new listener which should been removed.
336 * @see com.sun.star.lang.XComponent
338 public void removeEventListener( XEventListener xListener
)
339 throws com
.sun
.star
.uno
.RuntimeException
341 if (xListener
== null)
342 throw new com
.sun
.star
.uno
.RuntimeException("Listener must not be null");
344 if ( !eventListener
.contains(xListener
) )
345 throw new com
.sun
.star
.uno
.RuntimeException("Listener is not registered.");
347 eventListener
.remove(xListener
);
351 * Checks if a component is registered at the <code>ServiceManager</code>.
353 * <p>The given object argument must provide a <code>XServiceInfo</code>
356 * @param object object which provides a <code>XServiceInfo</code> interface.
357 * @return true if the component is registred otherwise false.
359 * @see com.sun.star.container.XSet
360 * @see com.sun.star.lang.XServiceInfo
362 public boolean has( Object object
)
363 throws com
.sun
.star
.uno
.RuntimeException
366 throw new com
.sun
.star
.uno
.RuntimeException("The parameter must not been null");
368 XServiceInfo xServiceInfo
= UnoRuntime
.queryInterface(XServiceInfo
.class, object
);
370 return xServiceInfo
!= null && UnoRuntime
.areSame(factoriesByImplNames
.get(xServiceInfo
.getImplementationName()), object
);
374 * Adds a <code>SingleServiceFactory</code> to the <code>ServiceManager</code>.
376 * @param object factory which should be added.
377 * @see com.sun.star.container.XSet
378 * @see com.sun.star.lang.XSingleServiceFactory
380 public void insert( Object object
)
381 throws com
.sun
.star
.lang
.IllegalArgumentException
,
382 com
.sun
.star
.container
.ElementExistException
,
383 com
.sun
.star
.uno
.RuntimeException
385 if (object
== null) throw new com
.sun
.star
.lang
.IllegalArgumentException();
387 XServiceInfo xServiceInfo
=
388 UnoRuntime
.queryInterface(XServiceInfo
.class, object
);
390 if (xServiceInfo
== null)
391 throw new com
.sun
.star
.lang
.IllegalArgumentException(
392 "The given object does not implement the XServiceInfo interface."
395 if ( factoriesByImplNames
.containsKey( xServiceInfo
.getImplementationName() ) ) {
396 throw new com
.sun
.star
.container
.ElementExistException(
397 xServiceInfo
.getImplementationName() + " already registred"
401 DEBUG("add factory " + object
.toString() + " for " + xServiceInfo
.getImplementationName());
402 factoriesByImplNames
.put( xServiceInfo
.getImplementationName(), object
);
405 String
[] serviceNames
= xServiceInfo
.getSupportedServiceNames();
406 ArrayList
<Object
> vec
;
408 for (String serviceName
: serviceNames
) {
409 if (!factoriesByServiceNames
.containsKey(serviceName
)) {
410 DEBUG("> no registered services found under " + serviceName
+ ": adding...");
411 factoriesByServiceNames
.put(serviceName
, new ArrayList
<Object
>());
413 vec
= factoriesByServiceNames
.get(serviceName
);
414 if (vec
.contains( object
)) {
415 System
.err
.println("The implementation " + xServiceInfo
.getImplementationName() +
416 " already registered for the service " + serviceName
+ " - ignoring!");
424 * Removes a <code>SingleServiceFactory</code> from the <code>ServiceManager</code>.
426 * @param object factory which should be removed.
427 * @see com.sun.star.container.XSet
428 * @see com.sun.star.lang.XSingleServiceFactory
430 public void remove( Object object
)
431 throws com
.sun
.star
.lang
.IllegalArgumentException
,
432 com
.sun
.star
.container
.NoSuchElementException
,
433 com
.sun
.star
.uno
.RuntimeException
436 throw new com
.sun
.star
.lang
.IllegalArgumentException(
437 "The given object must not be null."
440 XServiceInfo xServiceInfo
=
441 UnoRuntime
.queryInterface(XServiceInfo
.class, object
);
443 if (xServiceInfo
== null)
444 throw new com
.sun
.star
.lang
.IllegalArgumentException(
445 "The given object does not implement the XServiceInfo interface."
448 XSingleServiceFactory xSingleServiceFactory
=
449 UnoRuntime
.queryInterface(XSingleServiceFactory
.class, object
);
451 if (xSingleServiceFactory
== null)
452 throw new com
.sun
.star
.lang
.IllegalArgumentException(
453 "The given object does not implement the XSingleServiceFactory interface."
456 if ( factoriesByImplNames
.remove( xServiceInfo
.getImplementationName() ) == null )
457 throw new com
.sun
.star
.container
.NoSuchElementException(
458 xServiceInfo
.getImplementationName() +
459 " is not registered as an implementation."
462 String
[] serviceNames
= xServiceInfo
.getSupportedServiceNames();
464 for (String serviceName
: serviceNames
) {
465 if (factoriesByServiceNames
.containsKey(serviceName
)) {
466 ArrayList
<Object
> vec
= factoriesByServiceNames
.get(serviceName
);
467 if (!vec
.remove(object
)) {
468 System
.err
.println("The implementation " + xServiceInfo
.getImplementationName() +
469 " is not registered for the service " + serviceName
+ " - ignoring!");
471 // remove the vector if no implementations aviable for the service
473 factoriesByServiceNames
.remove(serviceName
);
480 * Provides an enumeration of all registered services.
482 * @return an enumeration of all available services.
483 * @see com.sun.star.container.XEnumerationAccess
485 public XEnumeration
createEnumeration()
486 throws com
.sun
.star
.uno
.RuntimeException
488 return new ServiceEnumerationImpl( factoriesByImplNames
.values().iterator() );
492 * Provides the UNO type of the <code>ServiceManager</code>
494 * @return the UNO type of the <code>ServiceManager</code>.
495 * @see com.sun.star.container.XElementAccess
496 * @see com.sun.star.uno.TypeClass
498 public com
.sun
.star
.uno
.Type
getElementType()
499 throws com
.sun
.star
.uno
.RuntimeException
501 if ( UNO_TYPE
== null )
502 UNO_TYPE
= new com
.sun
.star
.uno
.Type(ServiceManager
.class);
508 * Checks if the any componets are registered.
510 * @return true - if the list of the registred components is not empty - otherwise false.
511 * @see com.sun.star.container.XElementAccess
513 public boolean hasElements() {
514 return ! factoriesByImplNames
.isEmpty();
518 * Provides an enumeration of all factories for a specified service.
520 * @param serviceName name of the requested service.
521 * @return an enumeration for service name.
522 * @see com.sun.star.container.XContentEnumerationAccess
524 public XEnumeration
createContentEnumeration( String serviceName
)
525 throws com
.sun
.star
.uno
.RuntimeException
527 XEnumeration enumer
;
529 ArrayList
<Object
> serviceList
= factoriesByServiceNames
.get(serviceName
);
531 if (serviceList
!= null)
532 enumer
= new ServiceEnumerationImpl( serviceList
.iterator() );
534 enumer
= new ServiceEnumerationImpl();
540 * Returns the implementation name of the <code>ServiceManager</code> component.
542 * @return the class name of the <code>ServiceManager</code>.
543 * @see com.sun.star.lang.XServiceInfo
545 public String
getImplementationName()
546 throws com
.sun
.star
.uno
.RuntimeException
548 return getClass().getName();
552 * Checks if the <code>ServiceManager</code> supports a service.
554 * @param serviceName service name which should be checked.
555 * @return true if the service is supported - otherwise false.
557 * @see com.sun.star.lang.XServiceInfo
559 public boolean supportsService( String serviceName
)
560 throws com
.sun
.star
.uno
.RuntimeException
562 for (String supportedServiceName
: supportedServiceNames
) {
563 if (supportedServiceName
.equals(serviceName
)) {
568 return getImplementationName().equals(serviceName
);
572 * Supplies list of all supported services.
574 * @return a list of all supported service names.
575 * @see com.sun.star.lang.XServiceInfo
577 public String
[] getSupportedServiceNames()
578 throws com
.sun
.star
.uno
.RuntimeException
580 return supportedServiceNames
;
584 * The <code>ServiceEnumerationImpl</code> class provides an
585 * implementation of the @see com.sun.star.container.XEnumeration interface.
587 * <p>It is a inner wrapper for a java.util.Enumeration object.</p>
589 * @see com.sun.star.lang.XSingleServiceFactory
590 * @see com.sun.star.lang.XServiceInfo
593 class ServiceEnumerationImpl
implements XEnumeration
{
594 java
.util
.Iterator
<Object
> enumeration
= null;
597 * Constructs a new empty instance.
599 public ServiceEnumerationImpl() {
603 * Constructs a new instance with a given enumeration.
605 * @param enumer is the enumeration which should been wrapped.
606 * @see com.sun.star.container.XEnumeration
608 public ServiceEnumerationImpl(java
.util
.Enumeration
<Object
> enumer
) {
609 enumeration
= Collections
.list(enumer
).iterator();
613 * Constructs a new instance with a given enumeration.
615 * @param enumer is the enumeration which should been wrapped.
616 * @see com.sun.star.container.XEnumeration
618 public ServiceEnumerationImpl(java
.util
.Iterator
<Object
> enumer
) {
619 enumeration
= enumer
;
623 * Checks if the enumeration contains more elements.
625 * @return true if more elements are available - otherwise false.
626 * @see com.sun.star.container.XEnumeration
628 public boolean hasMoreElements()
629 throws com
.sun
.star
.uno
.RuntimeException
631 return enumeration
!= null && enumeration
.hasNext();
636 * Returns the next element of the enumeration.
638 * <p>If no further elements available a com.sun.star.container.NoSuchElementException
639 * exception will be thrown.</p>
641 * @return the next element.
642 * @see com.sun.star.container.XEnumeration
644 public Object
nextElement()
645 throws com
.sun
.star
.container
.NoSuchElementException
,
646 com
.sun
.star
.lang
.WrappedTargetException
,
647 com
.sun
.star
.uno
.RuntimeException
649 if (enumeration
== null)
650 throw new com
.sun
.star
.container
.NoSuchElementException();
653 return enumeration
.next();
654 } catch (java
.util
.NoSuchElementException e
) {
655 throw new com
.sun
.star
.container
.NoSuchElementException(e
, e
.toString());