private XComponentContext connect()
{
try
{
// create default local component context
XComponentContext xLocalContext =
com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
// initial serviceManager
XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
// create a urlresolver
Object urlResolver = xLocalServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xLocalContext );
// query for the XUnoUrlResolver interface
XUnoUrlResolver xUrlResolver =
(XUnoUrlResolver) UnoRuntime.queryInterface( XUnoUrlResolver.class, urlResolver );
// try to connect to soffice
Object aInitialObject = null;
try
{
aInitialObject = xUrlResolver.resolve( mURL );
}
catch( com.sun.star.connection.NoConnectException e )
{
// launch soffice
OfficeService aSOffice = new OfficeService();
aSOffice.startupService();
// wait until soffice is started
long nMaxMillis = System.currentTimeMillis() + 1000*aSOffice.getStartupTime();
while ( aInitialObject == null )
{
try
{
// try to connect to soffice
Thread.currentThread().sleep( 500 );
aInitialObject = xUrlResolver.resolve( mURL );
}
catch( com.sun.star.connection.NoConnectException aEx )
{
// soffice did not start in time
if ( System.currentTimeMillis() > nMaxMillis )
throw aEx;
}
}
}
finally
{
}
// XComponentContext
if( null != aInitialObject )
{
XPropertySet xPropertySet = (XPropertySet)
UnoRuntime.queryInterface( XPropertySet.class, aInitialObject);
Object xContext = xPropertySet.getPropertyValue("DefaultContext");
XComponentContext xComponentContext = (XComponentContext) UnoRuntime.queryInterface(
XComponentContext.class, xContext);
return xComponentContext;
}
}
catch( com.sun.star.connection.NoConnectException e )