OO-Snippets: Connect to Listening OpenOffice Variant 2 (UNO.CLS)

Commons

Keywordsconnect, bootstrap, UNO.CLS
LanguageooRexx
ApplicationOffice
AuthorsRony G. Flatscher (initial)
Supported Versions2.0.x  
Supported OSAll  
QuestionHow to connect to a already listening OpenOffice?

You can start OpenOffice in listening mode with e.g. this command:

soffice -accept=socket,host=localhost,port=8100;urp;

This commandline starts OpenOffice listening on port 8100.

Answer

This snippet connects to a listening OpenOffice and creates a wordprocessor document.

Code-Snippet-Listing (snippet-source)

/* Demonstrate how to connect to a remote OOo server and use it via ooRexx (http://www.ooRexx.org)

   Assumes that an instance of OOo got started that listens on port 8100; one can
   create such an instance via the commandline, using the following command:

         soffice -accept=socket,host=localhost,port=8100;urp;

   to start an instance without a visible window on the server, you may use the command:

         soffice -invisible -accept=socket,host=localhost,port=8100;urp;

   or

         soffice -headless -accept=socket,host=localhost,port=8100;urp;


   OOo can be configured to listen on a specific port, cf. "Developer's Guide" for
   further information on this.

   The code in this snippet could be transcribed to Java in a 1:1 mapping.

   This version is based upon the pure BSF4Rexx version that can be found here: ./Office.ConnectToListeningOpenOfficeVariant2_BSF4Rexx_.snip
*/


/* initialize connection to server, get its Desktop-service and XComponentLoader interface */
call UNO.CLS         /* get special UNO ooRexx support, which itself calls the module BSF.CLS,
                        thereby establishing full access to Java using BSF4Rexx     */

      /****************************/
      /* ===> Bootstrapping <===  */
      /****************************/

   /* note: the class "com.sun.star.comp.helper.Bootstrap" is pre-registered under the name
            "bootstrap" in a directory which can be addressed using the environment symbol ".uno" */
localCC    = .uno~bootstrap~createInitialComponentContext(.nil)   /* create a local ComponentContext  */
localSM    = localCC~getServiceManager                            /* create its ServiceManager        */

                     /* query the XUrlResolver interface from the local UnoUrlResolver object   */
                           /* create the URL resolver service object    */
localUUR   = localSM~createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localCC)
localXUUR  = localUUR~XUnoUrlResolver

                     /* query the XNamingService interface from the remote service object      */
hostname   = "localhost"   /* can be any host on the Internet           */
hostport   = 8100          /* port on which OOo listens for clients     */
   /* URL to use for connecting to server    */
unoUrl     = "uno:socket,host="hostname",port="hostport";urp;StarOffice.NamingService"
say "connecting using the URL:" pp(unoURL)"..."

                        /* resolve URL, thereby retrieving the remote NamingService service object */
remoteXNS  = localXUUR~resolve(unoUrl)~XNamingService

                     /* query the XMultiServiceFactory interface from the remote service object */
remoteXMSF = remoteXNS~getRegisteredObject("StarOffice.ServiceManager")~XMultiServiceFactory

                     /* query the XDesktop interface from the remote service object          */
remoteXD   = remoteXMSF~createInstance("com.sun.star.frame.Desktop")~XDesktop

                     /* query the XComponentLoader interface from the remote service object  */
remoteXCL  = remoteXD~XComponentLoader

      /*********************************************************************/
      /* ===> Now we are ready to load/open and/or create documents  <===  */
      /*********************************************************************/

                     /* load an empty text document into a (local) new window                */
   /* note: an instance of an empty array of type "com.sun.star.beans.PropertyValue" is
            pre-registered under the name "noProps" in a directory which can be addressed
            using the environment symbol ".uno" */
writerComponent = remoteXCL~loadComponentFromURL("private:factory/swriter", "_blank", 0, .uno~noProps)

Changelog

DateUserModification
2006-07-20RGFInitial version

and