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.
This commit is contained in:
Stephan Bergmann
2012-04-20 22:46:42 +02:00
parent b2fdaed465
commit c2fd9b533c
15 changed files with 2446 additions and 811 deletions

View File

@@ -26,9 +26,9 @@
*
************************************************************************/
#include "sal/config.h"
#include <string.h>
#include <vector>
#include <cstring>
#include "rtl/process.h"
#include "rtl/bootstrap.hxx"
@@ -38,27 +38,14 @@
#include "rtl/uri.hxx"
#include "osl/diagnose.h"
#include "osl/file.hxx"
#include "osl/module.hxx"
#include "osl/security.hxx"
#include "osl/thread.hxx"
#include "cppuhelper/shlib.hxx"
#include "cppuhelper/bootstrap.hxx"
#include "cppuhelper/component_context.hxx"
#include "cppuhelper/access_control.hxx"
#include "cppuhelper/findsofficepath.h"
#include "com/sun/star/uno/XComponentContext.hpp"
#include "com/sun/star/uno/XCurrentContext.hpp"
#include "com/sun/star/lang/XSingleServiceFactory.hpp"
#include "com/sun/star/lang/XSingleComponentFactory.hpp"
#include "com/sun/star/lang/XInitialization.hpp"
#include "com/sun/star/lang/XServiceInfo.hpp"
#include "com/sun/star/registry/XSimpleRegistry.hpp"
#include "com/sun/star/container/XSet.hpp"
#include "com/sun/star/beans/PropertyValue.hpp"
#include "com/sun/star/io/IOException.hpp"
#include "com/sun/star/bridge/UnoUrlResolver.hpp"
#include "com/sun/star/bridge/XUnoUrlResolver.hpp"
@@ -73,458 +60,9 @@ using namespace ::osl;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
namespace css = com::sun::star;
namespace cppu
{
OUString const & get_this_libpath()
{
static OUString s_path;
if (s_path.isEmpty())
{
OUString path;
Module::getUrlFromAddress( reinterpret_cast<oslGenericFunction>(get_this_libpath), path );
path = path.copy( 0, path.lastIndexOf( '/' ) );
MutexGuard guard( Mutex::getGlobalMutex() );
if (s_path.isEmpty())
s_path = path;
}
return s_path;
}
Bootstrap const & get_unorc() SAL_THROW(())
{
static rtlBootstrapHandle s_bstrap = 0;
if (! s_bstrap)
{
#ifdef ANDROID
// Wouldn't it be lovely to avoid this fugly hard-coding.
// The problem is that the 'create_bootstrap_macro_expander_factory()'
// required for bootstrapping services, calls cppu::get_unorc directly
// instead of re-using the BoostrapHandle from:
// defaultBootstrap_InitialComponentContext
// and since rtlBootstrapHandle is not ref-counted doing anything
// clean here is hardish.
OUString iniName( RTL_CONSTASCII_USTRINGPARAM( "file:///assets/program/unorc" ) );
#else
OUString iniName(
get_this_libpath() + OUSTR("/" SAL_CONFIGFILE("uno")) );
#endif
rtlBootstrapHandle bstrap = rtl_bootstrap_args_open( iniName.pData );
ClearableMutexGuard guard( Mutex::getGlobalMutex() );
if (s_bstrap)
{
guard.clear();
rtl_bootstrap_args_close( bstrap );
}
else
{
s_bstrap = bstrap;
}
}
return *(Bootstrap const *)&s_bstrap;
}
// private forward decl
Reference< lang::XMultiComponentFactory > bootstrapInitialSF(
OUString const & rBootstrapPath )
SAL_THROW( (Exception) );
Reference< XComponentContext > bootstrapInitialContext(
Reference< lang::XMultiComponentFactory > const & xSF,
Reference< registry::XSimpleRegistry > const & types_xRegistry,
Reference< registry::XSimpleRegistry > const & services_xRegistry,
OUString const & rBootstrapPath, Bootstrap const & bootstrap )
SAL_THROW( (Exception) );
Reference< XComponentContext > SAL_CALL createInitialCfgComponentContext(
ContextEntry_Init const * pEntries, sal_Int32 nEntries,
Reference< XComponentContext > const & xDelegate )
SAL_THROW(());
Reference< registry::XSimpleRegistry > SAL_CALL createRegistryWrapper(
const Reference< XComponentContext >& xContext );
namespace {
template< class T >
inline beans::PropertyValue createPropertyValue(
OUString const & name, T const & value )
SAL_THROW(())
{
return beans::PropertyValue(
name, -1, makeAny( value ), beans::PropertyState_DIRECT_VALUE );
}
OUString findBootstrapArgument(
const Bootstrap & bootstrap,
const OUString & arg_name,
sal_Bool * pFallenBack )
SAL_THROW(())
{
OUString result;
OUString prefixed_arg_name = OUSTR("UNO_");
prefixed_arg_name += arg_name.toAsciiUpperCase();
// environment not set -> try relative to executable
if(!bootstrap.getFrom(prefixed_arg_name, result))
{
if(pFallenBack)
*pFallenBack = sal_True;
OUString fileName;
bootstrap.getIniName(fileName);
// cut the rc extension
OUStringBuffer result_buf( 64 );
result_buf.append(
fileName.copy(
0, fileName.getLength() - strlen(SAL_CONFIGFILE(""))) );
result_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("_") );
result_buf.append( arg_name.toAsciiLowerCase() );
result_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(".rdb") );
result = result_buf.makeStringAndClear();
OSL_TRACE(
(OSL_LOG_PREFIX "findBootstrapArgument, setting \"%s\" relative to"
" executable \"%s\""),
OUStringToOString(arg_name, RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(result, RTL_TEXTENCODING_UTF8).getStr());
}
else
{
if(pFallenBack)
*pFallenBack = sal_False;
OSL_TRACE(
OSL_LOG_PREFIX "findBootstrapArgument, found \"%s\" in env \"%s\"",
(OUStringToOString(prefixed_arg_name, RTL_TEXTENCODING_UTF8).
getStr()),
OUStringToOString(result, RTL_TEXTENCODING_UTF8).getStr());
}
return result;
}
css::uno::Reference< css::registry::XSimpleRegistry > readRdbFile(
rtl::OUString const & url, bool fatalErrors,
css::uno::Reference< css::registry::XSimpleRegistry > const & lastRegistry,
css::uno::Reference< css::lang::XSingleServiceFactory > const &
simpleRegistryFactory,
css::uno::Reference< css::lang::XSingleServiceFactory > const &
nestedRegistryFactory)
{
OSL_ASSERT(simpleRegistryFactory.is() && nestedRegistryFactory.is());
try {
css::uno::Reference< css::registry::XSimpleRegistry > simple(
simpleRegistryFactory->createInstance(), css::uno::UNO_QUERY_THROW);
simple->open(url, true, false);
if (lastRegistry.is()) {
css::uno::Reference< css::registry::XSimpleRegistry > nested(
nestedRegistryFactory->createInstance(),
css::uno::UNO_QUERY_THROW);
css::uno::Sequence< css::uno::Any > args(2);
args[0] <<= lastRegistry;
args[1] <<= simple;
css::uno::Reference< css::lang::XInitialization >(
nested, css::uno::UNO_QUERY_THROW)->
initialize(args);
return nested;
} else {
return simple;
}
} catch (css::registry::InvalidRegistryException & e) {
(void) e; // avoid warnings
OSL_TRACE(
OSL_LOG_PREFIX "warning, could not open \"%s\": \"%s\"",
rtl::OUStringToOString(url, RTL_TEXTENCODING_UTF8).getStr(),
rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
if (fatalErrors) {
throw;
}
return lastRegistry;
}
}
Reference< registry::XSimpleRegistry > readRdbDirectory(
rtl::OUString const & url, bool fatalErrors,
css::uno::Reference< css::registry::XSimpleRegistry > const & lastRegistry,
css::uno::Reference< css::lang::XSingleServiceFactory > const &
simpleRegistryFactory,
css::uno::Reference< css::lang::XSingleServiceFactory > const &
nestedRegistryFactory)
{
OSL_ASSERT(simpleRegistryFactory.is() && nestedRegistryFactory.is());
osl::Directory dir(url);
switch (dir.open()) {
case osl::FileBase::E_None:
break;
case osl::FileBase::E_NOENT:
if (!fatalErrors) {
return lastRegistry;
}
// fall through
default:
throw css::uno::RuntimeException(
(rtl::OUString(
RTL_CONSTASCII_USTRINGPARAM("cannot open directory ")) +
url),
css::uno::Reference< css::uno::XInterface >());
}
std::vector<rtl::OUString> aURLs;
css::uno::Reference< css::registry::XSimpleRegistry > last(lastRegistry);
for (;;)
{
osl::DirectoryItem i;
osl::FileBase::RC eResult;
eResult = dir.getNextItem(i, SAL_MAX_UINT32);
if (eResult == osl::FileBase::E_NOENT)
break;
if (eResult != osl::FileBase::E_None)
{
throw css::uno::RuntimeException(
(rtl::OUString(
RTL_CONSTASCII_USTRINGPARAM("cannot iterate directory ")) +
url),
css::uno::Reference< css::uno::XInterface >());
}
osl::FileStatus stat(
osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName |
osl_FileStatus_Mask_FileURL);
if (i.getFileStatus(stat) != osl::FileBase::E_None) {
throw css::uno::RuntimeException(
(rtl::OUString(
RTL_CONSTASCII_USTRINGPARAM("cannot stat in directory ")) +
url),
css::uno::Reference< css::uno::XInterface >());
}
rtl::OUString aName = stat.getFileName();
// Ignore backup files - to allow people to edit their
// services/ without extremely confusing behaviour
if (aName.toChar() == '.' || aName.endsWithAsciiL("~", 1))
continue;
if (stat.getFileType() != osl::FileStatus::Directory) //TODO: symlinks
aURLs.push_back(stat.getFileURL());
}
size_t nXML = 0;
for (std::vector<rtl::OUString>::iterator it = aURLs.begin(); it != aURLs.end(); it++)
{
// Read / sniff the nasty files ...
osl::File aIn( *it );
if (aIn.open(osl_File_OpenFlag_Read) != osl::FileBase::E_None)
continue;
sal_uInt64 nRead = 0;
char buffer[6];
bool bIsXML = aIn.read(buffer, 6, nRead) == osl::FileBase::E_None &&
nRead == 6 && !strncmp(buffer, "<?xml ", 6);
aIn.close();
if (!bIsXML)
{
OSL_TRACE (OSL_LOG_PREFIX "rdb '%s' is a legacy format\n",
rtl::OUStringToOString( *it, RTL_TEXTENCODING_UTF8 ).getStr());
break;
}
nXML++;
}
if (nXML > 0 && nXML == aURLs.size())
{
OSL_TRACE (OSL_LOG_PREFIX "no legacy rdbs in directory '%s'\n",
rtl::OUStringToOString( url, RTL_TEXTENCODING_UTF8 ).getStr());
// read whole directory...
last = readRdbFile( url, fatalErrors, last,
simpleRegistryFactory, nestedRegistryFactory);
}
else
{
for (std::vector<rtl::OUString>::iterator it = aURLs.begin(); it != aURLs.end(); it++)
{
// Read / sniff the nasty files ...
last = readRdbFile(*it, fatalErrors, last,
simpleRegistryFactory, nestedRegistryFactory);
}
}
return last;
}
Reference< registry::XSimpleRegistry > nestRegistries(
const OUString &baseDir,
const Reference< lang::XSingleServiceFactory > & xSimRegFac,
const Reference< lang::XSingleServiceFactory > & xNesRegFac,
OUString csl_rdbs,
const OUString & write_rdb,
sal_Bool forceWrite_rdb,
sal_Bool bFallenBack )
SAL_THROW((Exception))
{
sal_Int32 index;
Reference< registry::XSimpleRegistry > lastRegistry;
if(!write_rdb.isEmpty()) // is there a write registry given?
{
lastRegistry.set(xSimRegFac->createInstance(), UNO_QUERY);
try
{
lastRegistry->open(write_rdb, sal_False, forceWrite_rdb);
}
catch (registry::InvalidRegistryException & e)
{
(void) e; // avoid warnings
OSL_TRACE(
OSL_LOG_PREFIX "warning, could not open \"%s\": \"%s\"",
OUStringToOString(write_rdb, RTL_TEXTENCODING_UTF8).getStr(),
OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
}
if(!lastRegistry->isValid())
lastRegistry.clear();
}
do
{
index = csl_rdbs.indexOf((sal_Unicode)' ');
OUString rdb_name = (index == -1) ? csl_rdbs : csl_rdbs.copy(0, index);
csl_rdbs = (index == -1) ? OUString() : csl_rdbs.copy(index + 1);
if (rdb_name.isEmpty()) {
continue;
}
bool fatalErrors = !bFallenBack;
if (rdb_name[0] == '?') {
rdb_name = rdb_name.copy(1);
fatalErrors = false;
}
bool directory = rdb_name.getLength() >= 3 && rdb_name[0] == '<' &&
rdb_name[rdb_name.getLength() - 2] == '>' &&
rdb_name[rdb_name.getLength() -1] == '*';
if (directory) {
rdb_name = rdb_name.copy(1, rdb_name.getLength() - 3);
}
osl::FileBase::getAbsoluteFileURL(baseDir, rdb_name, rdb_name);
lastRegistry = directory
? readRdbDirectory(
rdb_name, fatalErrors, lastRegistry, xSimRegFac, xNesRegFac)
: readRdbFile(
rdb_name, fatalErrors, lastRegistry, xSimRegFac, xNesRegFac);
}
while(index != -1 && csl_rdbs.getLength()); // are there more rdbs in list?
return lastRegistry;
}
Reference< XComponentContext >
SAL_CALL defaultBootstrap_InitialComponentContext(
Bootstrap const & bootstrap )
SAL_THROW( (Exception) )
{
OUString bootstrapPath;
if (!bootstrap.getFrom(
rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("URE_INTERNAL_LIB_DIR")),
bootstrapPath))
{
bootstrapPath = get_this_libpath();
}
OUString iniDir;
osl_getProcessWorkingDir(&iniDir.pData);
Reference<lang::XMultiComponentFactory> smgr_XMultiComponentFactory(
bootstrapInitialSF(bootstrapPath) );
Reference<lang::XMultiServiceFactory> smgr_XMultiServiceFactory(
smgr_XMultiComponentFactory, UNO_QUERY );
Reference<registry::XRegistryKey> xEmptyKey;
Reference<lang::XSingleServiceFactory> xSimRegFac(
loadSharedLibComponentFactory(
OUSTR("bootstrap.uno" SAL_DLLEXTENSION), bootstrapPath,
OUSTR("com.sun.star.comp.stoc.SimpleRegistry"),
smgr_XMultiServiceFactory,
xEmptyKey),
UNO_QUERY);
Reference<lang::XSingleServiceFactory> xNesRegFac(
loadSharedLibComponentFactory(
OUSTR("bootstrap.uno" SAL_DLLEXTENSION), bootstrapPath,
OUSTR("com.sun.star.comp.stoc.NestedRegistry"),
smgr_XMultiServiceFactory,
xEmptyKey),
UNO_QUERY);
sal_Bool bFallenback_types;
OUString cls_uno_types =
findBootstrapArgument( bootstrap, OUSTR("TYPES"), &bFallenback_types );
Reference<registry::XSimpleRegistry> types_xRegistry =
nestRegistries(
iniDir, xSimRegFac, xNesRegFac, cls_uno_types,
OUString(), sal_False, bFallenback_types );
// ==== bootstrap from services registry ====
sal_Bool bFallenback_services;
OUString cls_uno_services = findBootstrapArgument(
bootstrap, OUSTR("SERVICES"), &bFallenback_services );
sal_Bool fallenBackWriteRegistry;
OUString write_rdb = findBootstrapArgument(
bootstrap, OUSTR("WRITERDB"), &fallenBackWriteRegistry );
if (fallenBackWriteRegistry)
{
// no standard write rdb anymore
write_rdb = OUString();
}
Reference<registry::XSimpleRegistry> services_xRegistry = nestRegistries(
iniDir, xSimRegFac, xNesRegFac, cls_uno_services, write_rdb,
!fallenBackWriteRegistry, bFallenback_services );
Reference< XComponentContext > xContext(
bootstrapInitialContext(
smgr_XMultiComponentFactory, types_xRegistry, services_xRegistry,
bootstrapPath, bootstrap ) );
// initialize sf
Reference< lang::XInitialization > xInit(
smgr_XMultiComponentFactory, UNO_QUERY );
OSL_ASSERT( xInit.is() );
Sequence< Any > aSFInit( 1 );
aSFInit[ 0 ] <<= services_xRegistry;
xInit->initialize( aSFInit );
return xContext;
}
}
Reference< XComponentContext >
SAL_CALL defaultBootstrap_InitialComponentContext(
OUString const & iniFile )
SAL_THROW( (Exception) )
{
Bootstrap bootstrap( iniFile );
if (bootstrap.getHandle() == 0)
throw io::IOException(OUSTR("Cannot open for reading: ") + iniFile, 0);
return defaultBootstrap_InitialComponentContext( bootstrap );
}
Reference< XComponentContext >
SAL_CALL defaultBootstrap_InitialComponentContext()
SAL_THROW( (Exception) )
{
return defaultBootstrap_InitialComponentContext( get_unorc() );
}
BootstrapException::BootstrapException()
{
}
@@ -567,7 +105,7 @@ Reference< XComponentContext > SAL_CALL bootstrap()
}
rtl::OUString p2;
if (!rtl_convertStringToUString(
&p2.pData, p1, strlen(p1), osl_getThreadTextEncoding(),
&p2.pData, p1, std::strlen(p1), osl_getThreadTextEncoding(),
(RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))