Language

The Free and Open Productivity Suite
Released: Apache OpenOffice 4.1.15

OpenOfficeHow to access OpenOffice or Sun ONE Webtop from remote


Contents

Introduction
Using the component UnoUrlResolver
Create an interprocess bridge
Summary

Introduction

If you want to access an Office service from a program written in the programming language Java, first both environments should be connected with each other. Java and Office act as a client-server environment. Office plays the role of the server while the Java program figures as the client.

Because you don't want to distinguish between local and remote UNO calls, you have to create an interprocess bridge.

Get a remote object

Using the component UnoUrlResolver

At first, a simple service manager will be created by calling the static method createSimpleServiceManager() from the class com.sun.star.comp.helper.Bootstrap.

XMultiServiceFactory xmultiservicefactory =
com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();

The created service manager is able to create the service UnoUrlResolver, if this service was registered before.

Object objectUrlResolver = xmultiservicefactory.createInstance(
"com.sun.star.bridge.UnoUrlResolver" );

Comparable to casting in C++ the class UnoRuntime provides a method queryInterface() for getting an object that has all methods from the interface XUnoUrlResolver.

XUnoUrlResolver xurlresolver = ( XUnoUrlResolver )
UnoRuntime.queryInterface( XUnoUrlResolver.class, objectUrlResolver );

Now, the object xurlresolver resolves the object that is specified as follow:
uno:<connection description>;<protocol description>;<initial object name>

At this point the Java program (client) will connect to the office (server) that was run with the command:
soffice -accept=socket,host=localhost,port=8100;urp

Object objectInitial = xurlresolver.resolve(
"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" );

Finally a XMultiServiceFactory will be created, which allows the acces to the running office.

xmultiservicefactory = ( XMultiServiceFactory )
UnoRuntime.queryInterface( XMultiServiceFactory.class, objectInitial );

All program fragments described above are summarized in the following.

XMultiServiceFactory xmultiservicefactory =
com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();

Object objectUrlResolver = xmultiservicefactory.createInstance(
"com.sun.star.bridge.UnoUrlResolver" );

XUnoUrlResolver xurlresolver = ( XUnoUrlResolver )
UnoRuntime.queryInterface( XUnoUrlResolver.class, objectUrlResolver );

Object objectInitial = xurlresolver.resolve(
"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" );

xmultiservicefactory = ( XMultiServiceFactory )
UnoRuntime.queryInterface( XMultiServiceFactory.class, objectInitial );

Creating an interprocess bridge

First of all the servicemanager is needed because the servicemanager allows to instantiate the service Connector. The class Bootstrap provides a method that will create a simple servicemanager.

XMultiServiceFactory xmultiservicefactoryServiceManager = 
  com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();

The servicemanager creates a new instance of the service Connector, which allows to establish a connection to another process (in that case: Office).

Object objectConnector  = xmultiservicefactoryServiceManager.createInstance( 
  "com.sun.star.connection.Connector" );

The method queryInterface queries for the new interface XConnector to the existing UNO object objectConnector.

XConnector xconnector = ( XConnector ) UnoRuntime.queryInterface( 
  XConnector.class, objectConnector );

The client (Java code) will contact the server (running office) if the office is launched for example with the following option: "soffice -accept=socket,host=localhost,port=8100;urp". Here, the UNO Remote Protocol is used to transmit UNO calls via process-boundaries ( comparable to iiop in CORBA). The connection description contains the kind of the connection plus a comma seperated list of attributes. In this case a TCP/IP connection is specified.

XConnection xconnection = xconnector.connect( "socket,host=localhost,port=8100" );

The service NamingService provides a collection of global reachable objects. Normally an UNO application exposes its external reachable objects through this service. The StarOffice naming service can be addressed as "StarOffice.NamingService".

String stringRootOid = "StarOffice.NamingService";

In order to create a new bridge between the environment Java and the environment Office the method "getBridgeByName" of the class UnoRuntime should be called. That method needs five arguments:

  • the name of the source environment,
  • the context of the source environment,
  • the name of the target environment,
  • the context of the target environment and
  • the initial arguments for the bridge (using the UNO Remote Protocol and the given connection).
com.sun.star.uno.IBridge ibridge = UnoRuntime.getBridgeByName( 
  "java", 
  null, 
  "remote",
  null, 
  new Object[]{ "urp", xconnection, null } );

The interface IBridge maps an object from the destination environment (Office) to the source environment (Java). The name of the object, which should be mapped, and the interface, under which the object should be mapped, are passed on to the bridge.

Object objectInitial = ibridge.mapInterfaceFrom( stringRootOid, 
  new com.sun.star.uno.Type( XInterface.class ) );

The method queryInterface queries for the new interface XNamingService to the existing UNO object objectInitial.

XNamingService xnamingservice = ( XNamingService ) UnoRuntime.queryInterface(
  XNamingService.class, objectInitial );

The method getRegisteredObject of the interface XNamingService provides a previous registered object: The service manager of the currrent office. Furthermore, there is a query for the interface XMultiServiceFactory to the existing service manager.

if( xnamingservice != null ) {
Object objectServiceManager = xnamingservice.getRegisteredObject("StarOffice.ServiceManager" );
  xmultiservicefactory = ( XMultiServiceFactory )
    UnoRuntime.queryInterface( XMultiServiceFactory.class, objectServiceManager );
}

Summary

All program fragments described above are summarized in the following.

XMultiServiceFactory xmultiservicefactoryServiceManager = 
  com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();

Object objectConnector = xmultiservicefactoryServiceManager.createInstance( "com.sun.star.connection.Connector" );
XConnector xconnector = ( XConnector ) UnoRuntime.queryInterface( XConnector.class, objectConnector );
XConnection xconnection = xconnector.connect( "socket, host=localhost,port=8100" );
String stringRootOid = "StarOffice.NamingService";
com.sun.star.uno.IBridge ibridge = UnoRuntime.getBridgeByName( "java", null, "remote",
  null, new Object[]{ "urp", xconnection, null } );
Object objectInitial = ibridge.mapInterfaceFrom( stringRootOid, 
  new com.sun.star.uno.Type( XInterface.class ) );
XNamingService xnamingservice = ( XNamingService ) UnoRuntime.queryInterface(
  XNamingService.class, objectInitial );
if( xnamingservice != null ) {
Object objectServiceManager = xnamingservice.getRegisteredObject("StarOffice.ServiceManager" );
  xmultiservicefactory = ( XMultiServiceFactory )
    UnoRuntime.queryInterface( XMultiServiceFactory.class, objectServiceManager );
}

 


Author: Bertram Nolte (Fri 3 Aug 2001 15:46:08)
Copyright 2001 Sun Microsystems, Inc., 901 San Antonio Road, Palo Alto, CA 94303 USA.



Apache Software Foundation

Copyright & License | Privacy | Contact Us | Donate | Thanks

Apache, OpenOffice, OpenOffice.org and the seagull logo are registered trademarks of The Apache Software Foundation. The Apache feather logo is a trademark of The Apache Software Foundation. Other names appearing on the site may be trademarks of their respective owners.