OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Solution to close OOo connection (MessageDispatcher Thread)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
starm
General User
General User


Joined: 24 Apr 2006
Posts: 8

PostPosted: Mon Apr 24, 2006 10:29 am    Post subject: Solution to close OOo connection (MessageDispatcher Thread) Reply with quote

Hello,

Many people close the jvm with system.exit but it is not the good solution. So I post this solution to close correctly the connection with OOo without system.exit or xDesktop.Termante().

The solution is in the XBridge service.
See the code to open or close a connection to OOo :

Code:

/*
 * Created on 23 avr. 2006
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package fr.sigma.openoffice.connection;

import com.sun.star.beans.XPropertySet;
import com.sun.star.bridge.XBridge;
import com.sun.star.bridge.XBridgeFactory;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.connection.XConnection;
import com.sun.star.connection.XConnector;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

/**
 * @author vince
 * Création d'une connexion à OOo afin d'avoir le composant
 *         XBridge et de pouvoir le fermer une fois les traitements effectués.
 *
 * Sans ceci, la connexion ne se libère pas et OOo prend plus de ressources que
 * nécessaire.
 *
 */
public class OOoConnectionOpenOffice
{

    private XMultiComponentFactory mxMCF;
    private XComponentContext mxComponentContext;
    private XMultiServiceFactory mxMSF;
    private XBridge bridge = null;
    private XComponentContext xOfficeComponentContext;
    private XComponentLoader officeComponentLoader;

    public static final String DEFAULT_CONNECTION_STRING = "socket,host=localhost,port=8200";

    /**
     * Ouvre la connexion avec OOo en construisant les composants suivants :
     * XMultiComponentFactory mxMCF; XComponentContext mxComponentContext;
     * XMultiServiceFactory mxMSF; XBridge bridge; qui serve au reste de
     * l'application à interagir avec OOo.
     *
     */
    public void openConnectionOpenOffice()
    {
        try
        {

            // Création du context initial avec OOo
            mxComponentContext = Bootstrap.createInitialComponentContext(null);
            if (mxComponentContext == null)
                System.out.println("mxComponentContext is null");

            // Création de l'objet de type Connector permettant de créer une
            // connection entre java et OOo
            Object x = mxComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.connection.Connector", mxComponentContext);
            XConnector xConnector = (XConnector) UnoRuntime.queryInterface(XConnector.class, x);

            // Création de la connection entre java et OOo
            XConnection connection = xConnector.connect(DEFAULT_CONNECTION_STRING);
            if (connection == null)
                System.out.println("Connection is null");

            // Création du bridge, étape très importante pour pouvoir
            // fermer OOo sans avoir à faire un system.exit();
            // Le bridge est créé suivant la connection connection et le
            // protocol de discussion urp
            x = mxComponentContext.getServiceManager().createInstanceWithContext("com.sun.star.bridge.BridgeFactory", mxComponentContext);

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

            // Création de la XMultiComponentFactory qui permettra d'avoir
            // aux services tels que le mailmerge
            XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge);
            if (bridge == null)
                System.out.println("Bridge null");
            // get the remote instance
            x = bridge.getInstance("StarOffice.ServiceManager");
            // Query the initial object for its main factory interface
            mxMCF = (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, mxMCF);

            // Get the default context from the office server.
            Object oDefaultContext = xProperySet.getPropertyValue("DefaultContext");

            // Query for the interface XComponentContext.
            mxComponentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, oDefaultContext);

           mxMCF = mxComponentContext.getServiceManager();
           
            // now create the desktop service
            // NOTE: use the office component context here !
            Object oDesktop = mxMCF.createInstanceWithContext(
             "com.sun.star.frame.Desktop", mxComponentContext);

             officeComponentLoader = ( XComponentLoader )
                     UnoRuntime.queryInterface( XComponentLoader.class, oDesktop );

            // Query for an XMultiServiceFactory instance from the global
            // service manager
            if (mxMSF == null)
            {
                mxMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, mxMCF);
            }

        }
        catch (Exception exception)
        {
            System.err.println(exception);
        }
    }

    /**
     * Ferme la connection à OOo en créant un composant à partir du bridge et en
     * appelant la méthode dispose() dessus. Une fois ce composant fermé, OOo
     * peut se fermer normalement et la jvm se termine.
     */
    public void closeConnectionOpenOffice()
    {
        XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge);
        // Closing the bridge
        xcomponent.dispose();
    }
}


I find this solution in the snippets of openoffice.org :
http://codesnippets.services.openoffice.org/

Starm
Back to top
View user's profile Send private message
manutudescends
General User
General User


Joined: 21 Nov 2005
Posts: 25
Location: Toulouse, France

PostPosted: Tue Apr 25, 2006 12:20 am    Post subject: Reply with quote

thanks for this code.
Back to top
View user's profile Send private message
pvdev
General User
General User


Joined: 20 Mar 2006
Posts: 12
Location: Germany

PostPosted: Thu Apr 27, 2006 7:40 am    Post subject: System.exit is correct Reply with quote

You do know about shutdown hooks in the Java runtime right?

http://java.sun.com/developer/TechTips/2000/tt0711.html

This is the preferred way to clean up open resources.
Back to top
View user's profile Send private message Visit poster's website
crfonti
General User
General User


Joined: 10 Mar 2005
Posts: 24

PostPosted: Mon May 08, 2006 2:15 am    Post subject: Reply with quote

I have a problem using this code...the OO don't close correctly..the window with the document remains "up"...
_________________
Dott. Cristian Fonti
SCM Group S.p.a. http://www.scmgroup.com
Back to top
View user's profile Send private message
starm
General User
General User


Joined: 24 Apr 2006
Posts: 8

PostPosted: Tue May 09, 2006 5:04 am    Post subject: Reply with quote

Quote:

I have a problem using this code...the OO don't close correctly..the window with the document remains "up"...


Try with the document hidden, if it's not hidden, OOo wait the user close the window and so the application is not closed. Hidden is a property of the documùent when you open it.

Quote:

You do know about shutdown hooks in the Java runtime right?

http://java.sun.com/developer/TechTips/2000/tt0711.html

This is the preferred way to clean up open resources.


I don't know but I think it's not the solution because OOo is running in server mod and so it must not be closed. If you open a connection like this, the jvm is closed normally and open office stay open. The only problem is OOo don't clean up open ressources but when it is launch in server mod, it is not possible to close it to clean ressources.
It's the better solution I find.

Starm
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group