office-gobmx/cppuhelper/source/bootstrap.cxx
Stephan Bergmann c2fd9b533c New cppu::defaultBootstrap_InitialComponentContext implementation
...that no longer uses XSimpleRegistry structures for the service data and thus
is potentially more performant.

* Registry-based functions from cppuhelper/bootstrap are deprecated now, client
  code should always use defaultBootstrap_InitialComponentContext.

* References to the obsolete UNO_WRITERDB have been removed.

* Some of the functions in cppuhelper/source that are used from multiple .cxx
  but had not been properly placed into .hxx have been cleaned up.

* css.lang.ServiceManager XSet insert/remove now support special
  sequence<NamedValue> to improve live deployment/removal of XML-based extension
  components data.

* 09524d410b "stoc: accelerate opening of multiple
  XML .rdb files in a directory" and its follow-up
  cb5c881a7f "avoid using the new rdb reading
  logic for empty/non-existent directories" have been obsoleted by this change
  and have been reverted again.
2012-04-20 22:57:32 +02:00

258 lines
8.3 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include "sal/config.h"
#include <cstring>
#include "rtl/process.h"
#include "rtl/bootstrap.hxx"
#include "rtl/random.h"
#include "rtl/string.hxx"
#include "rtl/ustrbuf.hxx"
#include "rtl/uri.hxx"
#include "osl/diagnose.h"
#include "osl/file.hxx"
#include "osl/security.hxx"
#include "osl/thread.hxx"
#include "cppuhelper/bootstrap.hxx"
#include "cppuhelper/findsofficepath.h"
#include "com/sun/star/uno/XComponentContext.hpp"
#include "com/sun/star/bridge/UnoUrlResolver.hpp"
#include "com/sun/star/bridge/XUnoUrlResolver.hpp"
#include "macro_expander.hxx"
#define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
#define ARLEN(x) sizeof (x) / sizeof *(x)
using namespace ::rtl;
using namespace ::osl;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
namespace cppu
{
BootstrapException::BootstrapException()
{
}
BootstrapException::BootstrapException( const ::rtl::OUString & rMessage )
:m_aMessage( rMessage )
{
}
BootstrapException::BootstrapException( const BootstrapException & e )
{
m_aMessage = e.m_aMessage;
}
BootstrapException::~BootstrapException()
{
}
BootstrapException & BootstrapException::operator=( const BootstrapException & e )
{
m_aMessage = e.m_aMessage;
return *this;
}
const ::rtl::OUString & BootstrapException::getMessage() const
{
return m_aMessage;
}
Reference< XComponentContext > SAL_CALL bootstrap()
{
Reference< XComponentContext > xRemoteContext;
try
{
char const * p1 = cppuhelper_detail_findSofficePath();
if (p1 == NULL) {
throw BootstrapException(
OUSTR("no soffice installation found!"));
}
rtl::OUString p2;
if (!rtl_convertStringToUString(
&p2.pData, p1, std::strlen(p1), osl_getThreadTextEncoding(),
(RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
{
throw BootstrapException(
OUSTR("bad characters in soffice installation path!"));
}
OUString path;
if (osl::FileBase::getFileURLFromSystemPath(p2, path) !=
osl::FileBase::E_None)
{
throw BootstrapException(
OUSTR("cannot convert soffice installation path to URL!"));
}
if (!path.isEmpty() && path[path.getLength() - 1] != '/') {
path += OUSTR("/");
}
OUString uri;
if (!Bootstrap::get(OUSTR("URE_BOOTSTRAP"), uri)) {
Bootstrap::set(
OUSTR("URE_BOOTSTRAP"),
Bootstrap::encode(path + OUSTR(SAL_CONFIGFILE("fundamental"))));
}
// create default local component context
Reference< XComponentContext > xLocalContext(
defaultBootstrap_InitialComponentContext() );
if ( !xLocalContext.is() )
throw BootstrapException( OUSTR( "no local component context!" ) );
// create a random pipe name
rtlRandomPool hPool = rtl_random_createPool();
if ( hPool == 0 )
throw BootstrapException( OUSTR( "cannot create random pool!" ) );
sal_uInt8 bytes[ 16 ];
if ( rtl_random_getBytes( hPool, bytes, ARLEN( bytes ) )
!= rtl_Random_E_None )
throw BootstrapException( OUSTR( "random pool error!" ) );
rtl_random_destroyPool( hPool );
::rtl::OUStringBuffer buf;
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "uno" ) );
for ( sal_uInt32 i = 0; i < ARLEN( bytes ); ++i )
buf.append( static_cast< sal_Int32 >( bytes[ i ] ) );
OUString sPipeName( buf.makeStringAndClear() );
// accept string
OSL_ASSERT( buf.getLength() == 0 );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "--accept=pipe,name=" ) );
buf.append( sPipeName );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( ";urp;" ) );
// arguments
OUString args [] = {
OUSTR( "--nologo" ),
OUSTR( "--nodefault" ),
OUSTR( "--norestore" ),
OUSTR( "--nocrashreport" ),
OUSTR( "--nolockcheck" ),
buf.makeStringAndClear()
};
rtl_uString * ar_args [] = {
args[ 0 ].pData,
args[ 1 ].pData,
args[ 2 ].pData,
args[ 3 ].pData,
args[ 4 ].pData,
args[ 5 ].pData
};
::osl::Security sec;
// start office process
oslProcess hProcess = 0;
oslProcessError rc = osl_executeProcess(
(path + OUSTR("soffice")).pData, ar_args, ARLEN( ar_args ),
osl_Process_DETACHED,
sec.getHandle(),
0, // => current working dir
0, 0, // => no env vars
&hProcess );
switch ( rc )
{
case osl_Process_E_None:
osl_freeProcessHandle( hProcess );
break;
case osl_Process_E_NotFound:
throw BootstrapException( OUSTR( "image not found!" ) );
case osl_Process_E_TimedOut:
throw BootstrapException( OUSTR( "timout occurred!" ) );
case osl_Process_E_NoPermission:
throw BootstrapException( OUSTR( "permission denied!" ) );
case osl_Process_E_Unknown:
throw BootstrapException( OUSTR( "unknown error!" ) );
case osl_Process_E_InvalidError:
default:
throw BootstrapException( OUSTR( "unmapped error!" ) );
}
// create a URL resolver
Reference< bridge::XUnoUrlResolver > xUrlResolver(
bridge::UnoUrlResolver::create( xLocalContext ) );
// connection string
OSL_ASSERT( buf.getLength() == 0 );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "uno:pipe,name=" ) );
buf.append( sPipeName );
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
";urp;StarOffice.ComponentContext" ) );
OUString sConnectString( buf.makeStringAndClear() );
// wait until office is started
for ( ; ; )
{
try
{
// try to connect to office
xRemoteContext.set(
xUrlResolver->resolve( sConnectString ), UNO_QUERY_THROW );
break;
}
catch ( connection::NoConnectException & )
{
// wait 500 ms, then try to connect again
TimeValue tv = { 0 /* secs */, 500000000 /* nanosecs */ };
::osl::Thread::wait( tv );
}
}
}
catch ( Exception & e )
{
throw BootstrapException(
OUSTR( "unexpected UNO exception caught: " ) + e.Message );
}
return xRemoteContext;
}
OUString bootstrap_expandUri(OUString const & uri) {
static char const PREFIX[] = "vnd.sun.star.expand:";
return uri.matchAsciiL(RTL_CONSTASCII_STRINGPARAM(PREFIX))
? cppuhelper::detail::expandMacros(
rtl::Uri::decode(
uri.copy(RTL_CONSTASCII_LENGTH(PREFIX)),
rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8))
: uri;
}
} // namespace cppu
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */