OO-Snippets: Open connection and close without System.exit(0)

Commons

KeywordsxBridge, System.exit
LanguageJava
ApplicationOffice
AuthorsDavid Dankwerth
Guy Schohet
Supported Versions1.1.x  
Supported OSAll  
QuestionHow to open connection to xBride that i can dispose

I open connection to openoffice using the UNO api, but dont want to run

System.exit(0) to exit the program ( because it runs in a servlet for example)

Answer

In order for the program to exit, the connection must be disposed,

in order to dispose the connection you must have a direct reference

to the Bridge.

using :

XComponentContext xComponentContext =

com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );

as most the examples do, does not give you direct reference to the bridge instance

Code-Snippet-Listing (snippet-source)

import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;

import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;

import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;

import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.PropertyValue;

import com.sun.star.bridge.XBridgeFactory;
import com.sun.star.bridge.XBridge;

import com.sun.star.connection.XConnector;
import com.sun.star.connection.XConnection;


   public class sample
   {
           private XBridge bridge = null ;
           private XComponentContext xRemoteContext = null;
           private com.sun.star.frame.XComponentLoader officeComponentLoader;
           private  XMultiComponentFactory xRemoteServiceManager = null;
           private String con ;
           /** 
             * main
             */
           public static void main(String[] args)
                throws Exception
           {
                   sample s = new sample(args) ;
                   s.useConnection() ;
                   // here you do your thing
                   s.release();
                   
           }
           /* constructor */
           public sample(String[] args)
           {
                   if (args.length != 2)
                   {
                           System.out.println("Give me java sample host port");
                           System.exit(-1);
                   }
                   else
                           con = "socket,host="+args[0]+",port="+args[1];
                   
           }
           /**
           * open connection to openoffice
           */
            public  void useConnection() throws java.lang.Exception {
                try {
                        XComponentContext _ctx =
                                com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
                        
                        xRemoteContext = _ctx ;
                                
                        Object x = xRemoteContext.getServiceManager().createInstanceWithContext(
                                "com.sun.star.connection.Connector", xRemoteContext );
        
                        XConnector xConnector = (XConnector )
                                UnoRuntime.queryInterface(XConnector.class, x);
                        XConnection connection = xConnector.connect( con);
                        if (connection == null)
                                System.out.println("Connection is null");
                        x = xRemoteContext.getServiceManager().createInstanceWithContext(
                                "com.sun.star.bridge.BridgeFactory", xRemoteContext);
                        XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(
                                XBridgeFactory.class , x );
                        if (xBridgeFactory== null)
                                System.out.println("bridge factoriy is null");
                        // this is the bridge that you will dispose        
                        bridge = xBridgeFactory.createBridge( "" , "urp", connection , null );
                        XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
                                XComponent.class, bridge );
                        // get the remote instance                         
                        x = bridge.getInstance( "StarOffice.ServiceManager");
                        // Query the initial object for its main factory interface
                        xRemoteServiceManager = ( XMultiComponentFactory )
                                UnoRuntime.queryInterface( XMultiComponentFactory.class, x );
                       // retrieve the component context (it's not yet exported from the office)
                       // Query for the XPropertySet interface.
                       XPropertySet xProperySet = ( XPropertySet )
                                UnoRuntime.queryInterface( XPropertySet.class, xRemoteServiceManager );
                            
                       // Get the default context from the office server.
                       Object oDefaultContext =
                                xProperySet.getPropertyValue( "DefaultContext" );
                            
                       // Query for the interface XComponentContext.
                       XComponentContext xOfficeComponentContext =
                                ( XComponentContext ) UnoRuntime.queryInterface(
                                    XComponentContext.class, oDefaultContext );
                       // now create the desktop service
                       // NOTE: use the office component context here !
                       Object oDesktop = xRemoteServiceManager.createInstanceWithContext(
                        "com.sun.star.frame.Desktop", xOfficeComponentContext );
        
                        officeComponentLoader = ( XComponentLoader )
                                UnoRuntime.queryInterface( XComponentLoader.class, oDesktop );
        
                        
        
                    String available = (null !=officeComponentLoader ? "available" : "not available");
                    System.out.println( "remote ServiceManager is " + available );
                }
                catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
                    xRemoteContext = null;
                    throw e;
                }          
            }
        
               /**
                * when all the work is done, call release to dispose of the bridge
                * and your program can exit normaly, without forcing a System.exit
                */
                public void release()
                throws Exception
               {
                    XComponent xcomponent =
                    ( XComponent ) UnoRuntime.queryInterface( XComponent.class,
                    bridge );
                    // Closing the bridge
                    xcomponent.dispose();
                    
                }
   }

Changelog

DateUserModification
2005-06-08changed the title and the fact that it wasnt a full sample, now it compiles and allows you to "add your thing"

and