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 com.sun.star.bridge.UnoUrlResolver. 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 com.sun.star.frame.Desktop is acquired:

  import com.sun.star.uno.XComponentContext;
  import com.sun.star.comp.helper.Bootstrap;
  import com.sun.star.lang.XMultiComponentFactory;
  import com.sun.star.bridge.UnoUrlResolver;
  import com.sun.star.bridge.XUnoUrlResolver;
  import com.sun.star.beans.XPropertySet
  import com.sun.star.uno.UnoRuntime;
 
  XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null);
 
  // create a connector, so that it can contact the office
  XUnoUrlResolver urlResolver = UnoUrlResolver.create(xcomponentcontext);
 
  Object initialObject = urlResolver.resolve(
      "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
 
  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(
       "com.sun.star.frame.Desktop", xOfficeComponentContext);

As the example shows, a local component context is created through the com.sun.star.comp.helper.Bootstrap 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.

Template:Documentation/Tip

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