Server Use Case

From Apache OpenOffice Wiki
Jump to: navigation, search

This use case enables the export of any arbitrary UNO component as a remote server. As an example, the service is used which is already implemented by a component coming with the office. It exports an and a interface. The data is written through the output stream into the pipe and the same data from the input stream is read again. To export this component as a remote server, switch to the <OfficePath>/program directory and issue the following command line.

 i:\o641l\program>uno -s -ro types.rdb -ro services.rdb -u uno:socket,host=0,port=2002;urp;test
 > accepting socket,host=0,port=2083...

Now a client program can connect to the server. A client may look like the following:

  // Note: This example does not do anything meaningful, it shall just show,
  //       how to import an arbitrary UNO object from a remote process.
  class UnoExeClient {
      public static void main(String [] args) throws java.lang.Exception {
          if (args.length != 1) {
              System.out.println("Usage : java UnoExeClient uno-url");
              System.out.println(" The imported object must support the service");
          XComponentContext ctx =
          // get the UnoUrlResolver service
          Object o = ctx.getServiceManager().createInstanceWithContext(
              "" , ctx);
          XUnoUrlResolver resolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
              XUnoUrlResolver.class, o);
          // connect to the remote server and retrieve the appropriate object
          o = resolver.resolve(args[0]);
          // Check if we got what we expected
          // Note: This is not really necessary, you can also use the try and error approach 
          XServiceInfo serviceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class,o);
          if (serviceInfo == null) {
          throw new
              "error: The object imported with " + args[0] + " did not support XServiceInfo", null);
          if (!serviceInfo.supportsService("")) {
              throw new
                  "error: The object imported with "+args[0]+" does not support the pipe service", null);
          XOutputStream output = (XOutputStream) UnoRuntime.queryInterface(XOutputStream.class,o);
          XInputStream input = (XInputStream) UnoRuntime.queryInterface(XInputStream.class,o);
          // construct an array.
          byte[] array = new byte[]{1,2,3,4,5};
          // send it to the remote object
          // now read it again in two blocks
          byte [][] read = new byte[1][0];
          System.out.println("Available bytes : " + input.available());
          input.readBytes( read,2 );
          System.out.println("read " + read[0].length + ":" + read[0][0] + "," + read[0][1]);
          System.out.println("Available bytes : " + input.available());
          System.out.println("read " + read[0].length + ":" + read[0][0] +
              "," + read[0][1] + "," + read[0][2]);
          System.out.println("Terminating client");

After bootstrapping the component context, the UnoUrlResolver service is instantiated to access remote objects. After resolving the remote object, check whether it really supports the Pipe service. For instance, try to connect this client to a running - this check will fail. A byte array with five elements is written to the remote server and read again with two readBytes() calls. Starting the client with the following command line connects to the server started above. You should get the following output:

 I:\tmp>java UnoExeClient uno:socket,host=localhost,port=2083;urp;test
 Available bytes : 5
 read 2:1,2
 Available bytes : 3
 read 3:3,4,5
 Terminating client

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