예제 #1
0
  public static XMultiServiceFactory connect(String sConnectStr)
      throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException, Exception {
    // Get component context
    XComponentContext xComponentContext =
        com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);

    // initial serviceManager
    XMultiComponentFactory xLocalServiceManager = xComponentContext.getServiceManager();

    // create a connector, so that it can contact the office
    Object oUrlResolver =
        xLocalServiceManager.createInstanceWithContext(
            "com.sun.star.bridge.UnoUrlResolver", xComponentContext);
    XUnoUrlResolver xUrlResolver = UnoRuntime.queryInterface(XUnoUrlResolver.class, oUrlResolver);

    Object oInitialObject = xUrlResolver.resolve(sConnectStr);
    XNamingService xName = UnoRuntime.queryInterface(XNamingService.class, oInitialObject);

    XMultiServiceFactory xMSF = null;
    if (xName != null) {
      System.err.println("got the remote naming service !");
      Object oMSF = xName.getRegisteredObject("StarOffice.ServiceManager");

      xMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, oMSF);
    } else System.out.println("Error: Can't get XNamingService interface from url resolver!");

    return xMSF;
  }
  /**
   * @descr Connect to a already running LibreOffice application that has been started with a
   *     command line argument like "--accept=socket,host=localhost,port=5678;urp;"
   */
  private void connect(String hostname, int portnumber) {
    mbInitialized = true;
    //  Set up connection string.
    String sConnectString =
        "uno:socket,host=" + hostname + ",port=" + portnumber + ";urp;StarOffice.ServiceManager";

    // connect to a running office and get the ServiceManager
    try {
      //  Create a URL Resolver.
      XMultiServiceFactory aLocalServiceManager =
          com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();
      XUnoUrlResolver aURLResolver =
          UnoRuntime.queryInterface(
              XUnoUrlResolver.class,
              aLocalServiceManager.createInstance("com.sun.star.bridge.UnoUrlResolver"));

      maServiceManager =
          UnoRuntime.queryInterface(
              XMultiServiceFactory.class, aURLResolver.resolve(sConnectString));
    } catch (Exception e) {
      MessageArea.println("Could not connect with " + sConnectString + " : " + e);
      MessageArea.println(
          "Please start LibreOffice with " + "\"--accept=socket,host=localhost,port=5678;urp;\"");
    }
  }