| View previous topic :: View next topic |
| Author |
Message |
starm General User

Joined: 24 Apr 2006 Posts: 8
|
Posted: Mon Apr 24, 2006 10:29 am Post subject: Solution to close OOo connection (MessageDispatcher Thread) |
|
|
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 |
|
 |
manutudescends General User

Joined: 21 Nov 2005 Posts: 25 Location: Toulouse, France
|
Posted: Tue Apr 25, 2006 12:20 am Post subject: |
|
|
| thanks for this code. |
|
| Back to top |
|
 |
pvdev General User


Joined: 20 Mar 2006 Posts: 12 Location: Germany
|
|
| Back to top |
|
 |
crfonti General User

Joined: 10 Mar 2005 Posts: 24
|
Posted: Mon May 08, 2006 2:15 am Post subject: |
|
|
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 |
|
 |
starm General User

Joined: 24 Apr 2006 Posts: 8
|
Posted: Tue May 09, 2006 5:04 am Post subject: |
|
|
| 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.
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 |
|
 |
|
|
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
|