Service Manager

From Apache OpenOffice Wiki
Jump to: navigation, search



The com.sun.star.lang.ServiceManager is the main factory in every UNO application. It instantiates services by their service name, to enumerate all implementations of a certain service, and to add or remove factories for a certain service at runtime. The service manager is passed to every UNO component during instantiation.

XMultiServiceFactory Interface

The main interface of the service manager is the com.sun.star.lang.XMultiServiceFactory interface. It offers three methods: createInstance(), createInstanceWithArguments() and getAvailableServiceNames().

  interface XMultiServiceFactory: com::sun::star::uno::XInterface
  { 
      com::sun::star::uno::XInterface createInstance( [in] string aServiceSpecifier ) 
          raises( com::sun::star::uno::Exception ); 
 
      com::sun::star::uno::XInterface createInstanceWithArguments(
              [in] string ServiceSpecifier, 
              [in] sequence<any> Arguments ) 
          raises( com::sun::star::uno::Exception ); 
 
      sequence<string> getAvailableServiceNames();
  };
  • createInstance() returns a default constructed service instance. The returned service is guaranteed to support at least all interfaces, which were specified for the requested servicename. The returned XInterface reference can now be queried for the interfaces specified at the service description.
When using the service name, the caller does not have any influence on which concrete implementation is instantiated. If multiple implementations for a service exist, the service manager is free to decide which one to employ. This in general does not make a difference to the caller because every implementation does fulfill the service contract. Performance or other details may make a difference. So it is also possible to pass the implementation name instead of the service name, but it is not advised to do so as the implementation name may change.
In case the service manager does not provide an implementation for a request, a null reference is returned, so it is mandatory to check. Every UNO exception may be thrown during instantiation. Some may be described in the specification of the service that is to be instantiated, for instance, because of a misconfiguration of the concrete implementation. Another reason may be the lack of a certain bridge, for instance the Java-C++ bridge, in case a Java component shall be instantiated from C++ code.
  • createInstanceWithArguments() instantiates the service with additional parameters. A service signals that it expects parameters during instantiation by supporting the com.sun.star.lang.XInitialization interface. The service definition should describe the meaning of each element of the sequence. There maybe services which can only be instantiated with parameters.
  • getAvailableServiceNames() returns every servicename the service manager does support.

XContentEnumerationAccess Interface

The com.sun.star.container.XContentEnumerationAccess interface allows the creation of an enumeration of all implementations of a concrete servicename.

  interface XContentEnumerationAccess: com::sun::star::uno::XInterface
  { 
      com::sun::star::container::XEnumeration createContentEnumeration( [in] string aServiceName ); 
 
      sequence<string> getAvailableServiceNames(); 
 
  };

The createContentEnumeration() method returns a com.sun.star.container.XEnumeration interface. Note that it may return an empty reference in case the enumeration is empty.

  interface XEnumeration: com::sun::star::uno::XInterface
  { 
      boolean hasMoreElements(); 
 
      any nextElement() 
          raises( com::sun::star::container::NoSuchElementException, 
                  com::sun::star::lang::WrappedTargetException ); 
 
  };

In the above case, the returned any of the method Xenumeration.nextElement() contains a com.sun.star.lang.XSingleServiceFactory interface for each implementation of this specific service. You can, for instance, iterate over all implementations of a certain service and check each one for additional implemented services. The XSingleServiceFactory interface provides such a method. With this method, you can instantiate a feature rich implementation of a service.

XSet Interface

The com.sun.star.container.XSet interface allows the insertion or removal of com.sun.star.lang.XSingleServiceFactory or com.sun.star.lang.XSingleComponentFactory implementations to the service manager at runtime without making the changes permanent. When the office application terminates, all the changes are lost. The object must also support the com.sun.star.lang.XServiceInfo interface that provides information about the implementation name and supported services of the component implementation.

This feature may be of particular interest during the development phase. For instance, you can connect to a running office, insert a new factory into the service manager and directly instantiate the new service without having it registered before.

The chapter Special Service Manager Configurations shows an example that demonstrates how a factory is inserted into the service manager.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools