Getting a Service Manager

From Apache OpenOffice Wiki
Jump to: navigation, search

Office objects that provide the desired functionality are required when writing a client application that accesses the office. The root of all these objects is the service manager component, therefore clients need to instantiate it. Service manager runs in the office process, therefore office must be running first when you use Java components that are instantiated by the office. In a client-server scenario, the office has to be launched directly. The interprocess communication uses a remote protocol that can be provided as a command-line argument to the office:

 soffice -accept=socket,host=localhost,port=8100;urp

The client obtains a reference to the global service manager of the office (the server) using a local The global service manager of the office is used to get objects from the other side of the bridge. In this case, an instance of the is acquired:

  XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null);
  // create a connector, so that it can contact the office
  XUnoUrlResolver urlResolver = UnoUrlResolver.create(xcomponentcontext);
  Object initialObject = urlResolver.resolve(
  XMultiComponentFactory xOfficeFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(
      XMultiComponentFactory.class, initialObject);
  // retrieve the component context as property (it is not yet exported from the office)
  // Query for the XPropertySet interface.
  XPropertySet xProperySet = (XPropertySet) UnoRuntime.queryInterface( 
      XPropertySet.class, xOfficeFactory);
  // Get the default context from the office server.
  Object oDefaultContext = xProperySet.getPropertyValue("DefaultContext");
  // Query for the interface XComponentContext.
  XComponentContext xOfficeComponentContext = (XComponentContext) UnoRuntime.queryInterface(
      XComponentContext.class, oDefaultContext);
  // now create the desktop service
  // NOTE: use the office component context here!
  Object oDesktop = xOfficeFactory.createInstanceWithContext(
       "", xOfficeComponentContext);

As the example shows, a local component context is created through the class (a Java UNO runtime class). Its implementation provides a service manager that is limited in the number of services it can create. The names of these services are:

They are sufficient to establish a remote connection and obtain the fully featured service manager provided by the office.

Tip.png The service manager of the local component context could create other components, but this is only possible if the service manager is provided with the respective factories during runtime. An example that shows how this works can be found in the implementation of the Bootstrap class in the project javaunohelper.

There is also a service manager that uses a registry database to locate services. It is implemented by the class in the project javaunohelper. However, the implementation uses a native registry service manager instead of providing a pure Java implementation.

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