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 com.sun.star.io.Pipe service is used which is already implemented by a component coming with the office. It exports an com.sun.star.io.XOutputStream and a com.sun.star.io.XInputStream 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 com.sun.star.io.Pipe -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:

  import com.sun.star.lang.XServiceInfo;
  import com.sun.star.uno.XComponentContext;
  import com.sun.star.bridge.XUnoUrlResolver;
  import com.sun.star.io.XOutputStream;
  import com.sun.star.io.XInputStream;
  import com.sun.star.uno.UnoRuntime;
 
  // 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 com.sun.star.io.Pipe service");
              return;
          }
 
          XComponentContext ctx =
              com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
 
          // get the UnoUrlResolver service
          Object o = ctx.getServiceManager().createInstanceWithContext(
              "com.sun.star.bridge.UnoUrlResolver" , 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 com.sun.star.uno.RuntimeException(
              "error: The object imported with " + args[0] + " did not support XServiceInfo", null);
          }
 
          if (!serviceInfo.supportsService("com.sun.star.io.Pipe")) {
              throw new com.sun.star.uno.RuntimeException(
                  "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
          output.writeBytes(array);
          output.closeOutput();
 
          // 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());
          input.readBytes(read,3);
          System.out.println("read " + read[0].length + ":" + read[0][0] +
              "," + read[0][1] + "," + read[0][2]);
 
          System.out.println("Terminating client");
          System.exit(0);
      }
  }

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 Apache OpenOffice - 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