OO-Snippets: Connect to Listening OpenOffice Variant 2 (BSF4Rexx)

Commons

Keywordsconnect, bootstrap, BSF4Rexx
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.

   A version utilizing the special ooRexx support for UNO can be found here: ./Office.ConnectToListeningOpenOfficeVariant2_UNO_CLS_.snip

*/


/* initialize connection to server, get its Desktop-service and XComponentLoader interface */
call bsf.cls         /* get full access to Java using BSF4Rexx    */

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

unoRuntime = bsf.loadClass("com.sun.star.uno.UnoRuntime")         /* import UnoRuntime Java class     */
bootstrap  = bsf.loadClass("com.sun.star.comp.helper.Bootstrap")  /* import Bootstrap Java class      */
localCC    = 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)
interfaceXUUR = bsf.loadClass("com.sun.star.bridge.XUnoUrlResolver")
localXUUR     = unoRuntime~queryInterface(interfaceXUUR, localUUR)

                     /* 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 */
remoteNS      = localXUUR~resolve(unoUrl)
interfaceXNS  = bsf.loadClass("com.sun.star.uno.XNamingService")
remoteXNS     = unoRuntime~queryInterface(interfaceXNS, remoteNS)

                     /* query the XMultiServiceFactory interface from the remote service object */
remoteSM      = remoteXNS~getRegisteredObject("StarOffice.ServiceManager")  /* get remote ServiceManager     */
interfaceXMSF = bsf.loadClass("com.sun.star.lang.XMultiServiceFactory")
remoteXMSF    = unoRuntime~queryInterface(interfaceXMSF, remoteSM)

                     /* query the XDesktop interface from the remote service object          */
remoteD       = remoteXMSF~createInstance("com.sun.star.frame.Desktop")
interfaceXD   = bsf.loadClass("com.sun.star.frame.XDesktop")
remoteXD      = unoRuntime~queryInterface(interfaceXD, remoteD)

                     /* query the XComponentLoader interface from the remote service object  */
interfaceXCL  = bsf.loadClass("com.sun.star.frame.XComponentLoader")
remoteXCL     = unoRuntime~queryInterface(interfaceXCL, remoteXD)

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

                     /* create an empty Java array of component type "PropertyValue"         */
noProps = bsf.createArray(bsf.loadClass("com.sun.star.beans.PropertyValue"), 0) /* 0=no elements, i.e. empty Java array   */

                     /* load an empty text document into a (local) new window                */
writerComponent = remoteXCL~loadComponentFromURL("private:factory/swriter", "_blank", 0, noProps)

Changelog

DateUserModification
2006-07-20RGFInitial version

and