服务器使用实例

From Apache OpenOffice Wiki
Jump to: navigation, search


此使用实例可以将任意 UNO 组件导出为远程服务器。例如,使用 com.sun.star.io.Pipe 服务,它已由办公软件附带的组件实现。该服务导出 com.sun.star.io.XOutputStreamcom.sun.star.io.XInputStream 接口。数据通过输出流写入管道,并且再次从输入流读取相同的数据。要将此组件导出为远程服务器,请切换到 <OfficePath>/program 目录并执行以下命令行。

 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...


现在客户端程序可以连接到服务器。客户端可能类似如下所示:

  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);
      }
  }


引导组件上下文后,UnoUrlResolver 服务被实例化以访问远程对象。解析远程对象后,请检查其是否真正支持 Pipe 服务。例如,尝试将此客户端连接到运行的 OpenOffice.org — 此检查将会失败。具有五个元素的 byte array 将被写入远程服务器,并使用两个 readBytes() 调用再次读取。使用以下命令行启动客户端可以连接到上面启动的服务器。您应获得以下输出:

 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