 | OO-Snippets: Open connection and close without System.exit(0)Commons| Keywords | xBridge, System.exit |
|---|
| Language | Java |
|---|
| Application | Office |
|---|
| Authors | David Dankwerth Guy Schohet
|
|---|
| Supported Versions | 1.1.x |
|---|
| Supported OS | All |
|---|
| Question | How 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 |
|---|
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() ;
s.release();
}
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");
bridge = xBridgeFactory.createBridge( "" , "urp", connection , null );
XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
XComponent.class, bridge );
x = bridge.getInstance( "StarOffice.ServiceManager");
xRemoteServiceManager = ( XMultiComponentFactory )
UnoRuntime.queryInterface( XMultiComponentFactory.class, x );
XPropertySet xProperySet = ( XPropertySet )
UnoRuntime.queryInterface( XPropertySet.class, xRemoteServiceManager );
Object oDefaultContext =
xProperySet.getPropertyValue( "DefaultContext" );
XComponentContext xOfficeComponentContext =
( XComponentContext ) UnoRuntime.queryInterface(
XComponentContext.class, oDefaultContext );
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 ) {
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 );
xcomponent.dispose();
}
}
|
Changelog| Date | User | Modification |
|---|
| 2005-06-08 | | changed the title and the fact that it wasnt a full sample, now it compiles and allows you to "add your thing" |
|