Example: A Connection Aware Client

From Apache OpenOffice Wiki
Jump to: navigation, search

The following example shows an advanced client which can be informed about the status of the remote bridge. A complete example for a simple client is given in First Steps.

The following Java example opens a small awt window containing the buttons new writer and new calc that opens a new document and a status label. It connects to a running office when a button is clicked for the first time. Therefore it uses the connector/bridge factory combination, and registers itself as an event listener at the interprocess bridge.

When the office is terminated, the disposing event is terminated, and the Java program sets the text in the status label to 'disconnected' and clears the office desktop reference. The next time a button is pressed, the program knows that it has to re-establish the connection.

The method getComponentLoader() retrieves the XComponentLoader reference on demand:

  XComponentLoader _officeComponentLoader = null;
  // local component context
  XComponentContext _ctx; 
  protected com.sun.star.frame.XComponentLoader getComponentLoader()
          throws com.sun.star.uno.Exception {
      XComponentLoader officeComponentLoader = _officeComponentLoader;
      if (officeComponentLoader == null) {
          // instantiate connector service
          Object x = _ctx.getServiceManager().createInstanceWithContext(
              "com.sun.star.connection.Connector", _ctx);
          XConnector xConnector = (XConnector) UnoRuntime.queryInterface(XConnector.class, x);
          // helper function to parse the UNO URL into a string array
          String a[] = parseUnoUrl(_url);
          if (null == a) {
              throw new com.sun.star.uno.Exception("Couldn't parse UNO URL "+ _url);
          // connect using the connection string part of the UNO URL only.
          XConnection connection = xConnector.connect(a[0]);
          x = _ctx.getServiceManager().createInstanceWithContext(
          "com.sun.star.bridge.BridgeFactory", _ctx);
          XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(
              XBridgeFactory.class , x);
          // create a nameless bridge with no instance provider
          // using the middle part of the UNO URL
          XBridge bridge = xBridgeFactory.createBridge("" , a[1] , connection , null);
          // query for the XComponent interface and add this as event listener
          XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
              XComponent.class, bridge);
          // get the remote instance 
          x = bridge.getInstance(a[2]);
          // Did the remote server export this object ?
          if (null == x) {
              throw new com.sun.star.uno.Exception(
                  "Server didn't provide an instance for" + a[2], null);
          // Query the initial object for its main factory interface
          XMultiComponentFactory xOfficeMultiComponentFactory = (XMultiComponentFactory)
              UnoRuntime.queryInterface(XMultiComponentFactory.class, x);
          // retrieve the component context (it's not yet exported from the office)
          // Query for the XPropertySet interface.
          XPropertySet xProperySet = (XPropertySet)
              UnoRuntime.queryInterface(XPropertySet.class, xOfficeMultiComponentFactory);
          // Get the default context from the office server.
          Object oDefaultContext =
          // 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 = xOfficeMultiComponentFactory.createInstanceWithContext(
              "com.sun.star.frame.Desktop", xOfficeComponentContext);
          officeComponentLoader = (XComponentLoader)
              UnoRuntime.queryInterface( XComponentLoader.class, oDesktop);
          if (officeComponentLoader == null) {
              throw new com.sun.star.uno.Exception(
                  "Couldn't instantiate com.sun.star.frame.Desktop" , null);
          _officeComponentLoader = officeComponentLoader;
      return officeComponentLoader;

This is the button event handler:

  public void actionPerformed(ActionEvent event) {
      try {
          String sUrl;
          if (event.getSource() == _btnWriter) {
              sUrl = "private:factory/swriter";
          } else {
              sUrl = "private:factory/scalc";
              sUrl, "_blank", 0,new com.sun.star.beans.PropertyValue[0]);
      } catch (com.sun.star.connection.NoConnectException exc) {
      } catch (com.sun.star.uno.Exception exc) {
          throw new java.lang.RuntimeException(exc.getMessage());

And the disposing handler clears the _officeComponentLoader reference:

  public void disposing(com.sun.star.lang.EventObject event) {
      // remote bridge has gone down, because the office crashed or was terminated.
      _officeComponentLoader = null;
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages