diff options
Diffstat (limited to 'cpputools/source')
-rw-r--r-- | cpputools/source/registercomponent/makefile.mk | 75 | ||||
-rw-r--r-- | cpputools/source/registercomponent/registercomponent.cxx | 846 | ||||
-rw-r--r-- | cpputools/source/regsingleton/makefile.mk | 79 | ||||
-rw-r--r-- | cpputools/source/regsingleton/regsingleton.cxx | 173 | ||||
-rw-r--r-- | cpputools/source/sp2bv/makefile.mk | 56 | ||||
-rw-r--r-- | cpputools/source/sp2bv/readme.txt | 10 | ||||
-rw-r--r-- | cpputools/source/sp2bv/sp2bv.cxx | 141 | ||||
-rw-r--r-- | cpputools/source/unoexe/makefile.mk | 91 | ||||
-rw-r--r-- | cpputools/source/unoexe/unoexe.cxx | 890 |
9 files changed, 2361 insertions, 0 deletions
diff --git a/cpputools/source/registercomponent/makefile.mk b/cpputools/source/registercomponent/makefile.mk new file mode 100644 index 000000000000..6808e0b90b4a --- /dev/null +++ b/cpputools/source/registercomponent/makefile.mk @@ -0,0 +1,75 @@ +#************************************************************************* +# +# 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. +# +#************************************************************************* +PRJ=..$/.. + +PRJNAME=cpputools +TARGET=regcomp +LIBTARGET=NO + +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- +.INCLUDE : settings.mk + +UNOUCRDEP=$(SOLARBINDIR)$/udkapi.rdb +UNOUCRRDB=$(SOLARBINDIR)$/udkapi.rdb + +NO_OFFUH=TRUE +CPPUMAKERFLAGS += -C + +UNOTYPES=\ + com.sun.star.uno.TypeClass \ + com.sun.star.lang.XMultiServiceFactory \ + com.sun.star.lang.XSingleServiceFactory \ + com.sun.star.lang.XMultiComponentFactory \ + com.sun.star.lang.XSingleComponentFactory \ + com.sun.star.lang.XComponent \ + com.sun.star.container.XContentEnumerationAccess \ + com.sun.star.container.XSet \ + com.sun.star.loader.CannotActivateFactoryException \ + com.sun.star.registry.XImplementationRegistration2 + +# --- Files -------------------------------------------------------- + +DEPOBJFILES= $(OBJ)$/registercomponent.obj + +APP1TARGET= $(TARGET) +APP1OBJS=$(DEPOBJFILES) +APP1RPATH=UREBIN + +APP1STDLIBS=\ + $(SALLIB) \ + $(CPPULIB) \ + $(CPPUHELPERLIB) + +.IF "$(GUI)"=="WNT" +APP1STDLIBS+= \ + $(LIBCMT) +.ENDIF + + +.INCLUDE : target.mk diff --git a/cpputools/source/registercomponent/registercomponent.cxx b/cpputools/source/registercomponent/registercomponent.cxx new file mode 100644 index 000000000000..c487d95c59a5 --- /dev/null +++ b/cpputools/source/registercomponent/registercomponent.cxx @@ -0,0 +1,846 @@ +/************************************************************************* + * + * 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 <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include <vector> + +#include "sal/main.h" +#include <rtl/strbuf.hxx> +#include <rtl/ustrbuf.hxx> + +#include <cppuhelper/servicefactory.hxx> +#include <cppuhelper/shlib.hxx> + +#include <com/sun/star/container/XSet.hpp> +#include <com/sun/star/container/XContentEnumerationAccess.hpp> +#include <com/sun/star/registry/XImplementationRegistration2.hpp> +#include <com/sun/star/registry/XSimpleRegistry.hpp> +#include <com/sun/star/lang/XComponent.hpp> + +#include <algorithm> +#include <osl/process.h> +#include <osl/diagnose.h> +#include <osl/thread.h> +#include <osl/file.hxx> + +#ifdef SAL_UNX +#define SEPARATOR '/' +#else +#define SEPARATOR '\\' +#endif + +using namespace ::rtl; +using namespace ::osl; +using namespace ::cppu; +using namespace ::std; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::registry; +using com::sun::star::container::XSet; +using com::sun::star::container::XContentEnumerationAccess; +using com::sun::star::container::XEnumeration; + +namespace { + +OUString replacePrefix(OUString const & url, OUString const & prefix) { + sal_Int32 i = url.lastIndexOf('/'); + // Backward compatibility with stoc/source/implementationregistration/ + // implreg.cxx:1.27 l. 1892: + if (i == -1) { + i = url.lastIndexOf('\\'); + } + return prefix + url.copy(i + 1); +} + +} + +sal_Bool isFileUrl(const OUString& fileName) +{ + if (fileName.indexOf(OUString::createFromAscii("file://")) == 0 ) + return sal_True; + return sal_False; +} + +OUString convertToFileUrl(const OUString& fileName) +{ + if ( isFileUrl(fileName) ) + { + return fileName; + } + + OUString uUrlFileName; + if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 ) + { + OUString uWorkingDir; + if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) { + OSL_ASSERT(false); + } + if (FileBase::getAbsoluteFileURL(uWorkingDir, fileName, uUrlFileName) + != FileBase::E_None) + { + OSL_ASSERT(false); + } + } else + { + if (FileBase::getFileURLFromSystemPath(fileName, uUrlFileName) + != FileBase::E_None) + { + OSL_ASSERT(false); + } + } + + return uUrlFileName; +} +static void usingRegisterImpl() +{ + fprintf(stderr, "usage: regcomp -register|revoke -r registryfile -c locationUrl [-br registryfile] [-l componentLoaderUrl] [-s] [-classpath path]\n"); + fprintf(stderr, " Parameters:\n"); + fprintf(stderr, " -register\n" + " register a new component.\n"); + fprintf(stderr, " -revoke\n" + " revoke a component.\n"); + fprintf(stderr, " -br registryfile\n" + " the name of the registry used for bootstrapping\n" + " regcomp. The option can be given twice, each\n" + " one followed by exactly one registry file.\n" + " The registries are used to access both types and\n" + " registered components.\n"); + fprintf(stderr, " -r registryfile\n" + " the name of the target registry (will be created\n" + " if it does not exists). The file name may match\n" + " with one of the filenames given with the -br option.\n"); + fprintf(stderr, " -c locationUrls\n" + " the location of a component (a url to a shared\n" + " library or a absolute url to a .jar file) or a\n" + " list of urls seperated by ';' or ' '. Note if a\n" + " list of urls is specified, the components must\n" + " all need the same loader (quoting is possible with\n" + " \\ or \"\").\n"); + fprintf(stderr, " -l componentLoaderUrl\n" + " the name of the needed loader. If no loader is\n" + " specified and the components have a .jar suffix,\n" + " the default is com.sun.star.loader.Java2.\n" + " Otherwise, the default is\n" + " com.sun.star.loader.SharedLibrary\n" + " -s\n" + " silent, regcomp prints messages only on error.\n" + " -wop\n" + " register the component name only without path\n" + " -wop=prefix\n" + " register the component name with path replaced\n" + " by given prefix\n" + " -classpath path\n" + " sets the java classpath to path (overwriting the\n" + " current classpath environment variable). Note that\n" + " in case you start regcomp e.g. within an office\n" + " environment, the classpath entries in the\n" + " configuration still have precedence over this\n" + " option.\n"); +} + +class IllegalArgument +{ +public: + IllegalArgument(const OString& rMessage) + : m_message(rMessage) + {} + + OString m_message; +}; + +struct Options +{ + Options() + : bRegister(sal_False) + , bRevoke(sal_False) + , bSilent( sal_False ) + , bPrefix( sal_False ) + {} + + sal_Bool bRegister; + sal_Bool bRevoke; + sal_Bool bSilent; + sal_Bool bPrefix; + OUString sPrefix; + OUString sProgramName; + OUString sBootRegName; + OUString sBootRegName2; + OUString sRegName; + OUString sComponentUrls; + OUString sLoaderName; +}; + +sal_Bool parseOptions(int ac, char* av[], Options& rOptions, sal_Bool bCmdFile) + throw( IllegalArgument ) +{ + sal_Bool ret = sal_True; + sal_uInt16 i=0; + sal_Bool bLoaderExplicitlyGiven = sal_False; + + rOptions.sProgramName = OUString::createFromAscii(av[i++]); + + if (!bCmdFile) + { + bCmdFile = sal_True; + + if (ac < 2) + { + usingRegisterImpl(); + ret = sal_False; + } + } + + for (; i < ac; i++) + { + if (av[i][0] == '-') + { + switch (av[i][1]) + { + case 'r': + if (strcmp(av[i], "-register") == 0) + { + rOptions.bRegister = sal_True; + } else + if (strcmp(av[i], "-revoke") == 0) + { + rOptions.bRevoke = sal_True; + } else + if (av[i][2] == '\0') + { + if (i < ac - 1 && av[i+1][0] != '-') + { + i++; + rOptions.sRegName = OStringToOUString(av[i], osl_getThreadTextEncoding()); + } else + { + OString tmp("'-r', please check"); + if (i <= ac - 1) + { + tmp += " your input '" + OString(av[i+1]) + "'"; + } + throw IllegalArgument(tmp); + } + } else + { + rOptions.sRegName = OStringToOUString(av[i]+2, osl_getThreadTextEncoding()); + } + break; + case 'b': + if (av[i][2] != 'r') + { + OString tmp("'-b', invalid option!"); + throw IllegalArgument(tmp); + } + if (av[i][3] == '\0') + { + if (i < ac - 1 && av[i+1][0] != '-') + { + i++; + OUString regName = OStringToOUString(av[i], osl_getThreadTextEncoding()); + if( ! rOptions.sBootRegName.getLength() ) + { + rOptions.sBootRegName = regName; + } + else + { + rOptions.sBootRegName2 = regName; + } + } else + { + OString tmp("'-br', please check"); + if (i <= ac - 1) + { + tmp += " your input '" + OString(av[i+1]) + "'"; + } + throw IllegalArgument(tmp); + } + } else + { + rOptions.sBootRegName = OStringToOUString(av[i]+3, osl_getThreadTextEncoding()); + } + break; + case 'c': + { + OUString sUrls; + if (av[i][2] == '\0') + { + if (i < ac - 1 && av[i+1][0] != '-') + { + i++; + sUrls = OStringToOUString(av[i], osl_getThreadTextEncoding()); + } else + { + OString tmp("'-c', please check"); + if (i <= ac - 1) + { + tmp += " your input '" + OString(av[i+1]) + "'"; + } + throw IllegalArgument(tmp); + } + } + else if( 0 == strncmp( av[i] , "-classpath" ,10 ) ) + { + i++; + if( i < ac ) + { + rtl::OUString envVar(RTL_CONSTASCII_USTRINGPARAM("CLASSPATH")); + rtl::OUString envValue(av[i], strlen(av[i]), osl_getThreadTextEncoding()); + osl_setEnvironment(envVar.pData, envValue.pData); + } + break; + } + else + { + sUrls = OStringToOUString(av[i]+2, osl_getThreadTextEncoding()); + } + + if (rOptions.sComponentUrls.getLength()) + { + OUString tmp(rOptions.sComponentUrls + OUString(";", 1, osl_getThreadTextEncoding()) + sUrls); + rOptions.sComponentUrls = tmp; + } else + { + rOptions.sComponentUrls = sUrls; + } + break; + } + case 'l': + { + if (av[i][2] == '\0') + { + if (i < ac - 1 && av[i+1][0] != '-') + { + i++; + rOptions.sLoaderName = OUString::createFromAscii(av[i]); + bLoaderExplicitlyGiven = sal_True; + } else + { + OString tmp("'-l', please check"); + if (i <= ac - 1) + { + tmp += " your input '" + OString(av[i+1]) + "'"; + } + throw IllegalArgument(tmp); + } + } else + { + bLoaderExplicitlyGiven = sal_True; + rOptions.sLoaderName = OUString::createFromAscii(av[i]+2); + } + break; + } + case 's': + { + if( av[i][2] == 0 ) + { + rOptions.bSilent = sal_True; + } + else + { + rtl::OStringBuffer buf; + buf.append( "Unknown error " ); + buf.append( av[i] ); + throw IllegalArgument( av[i] ); + } + break; + } + case 'e': + { + if( av[i][2] == 'n' && av[i][3] == 'v' && av[i][4] == ':' ) + { + // bootstrap variable, ignore it + break; + } + } + case 'w': + { + if (strcmp(av[i], "-wop") == 0) + { + rOptions.bPrefix = sal_True; + rOptions.sPrefix = OUString(); + // in case there are multiple -wops + break; + } + else if ( + strncmp(av[i], "-wop=", RTL_CONSTASCII_LENGTH("-wop=")) + == 0) + { + rOptions.bPrefix = sal_True; + rOptions.sPrefix = OStringToOUString( + av[i] + RTL_CONSTASCII_LENGTH("-wop="), + osl_getThreadTextEncoding()); + break; + } + } + default: + { + OString tmp( "unknown option " ); + tmp += av[i]; + throw IllegalArgument( tmp ); + } + } + } else + { + if (av[i][0] == '@') + { + FILE* cmdFile = fopen(av[i]+1, "r"); + if( cmdFile == NULL ) + { + usingRegisterImpl(); + ret = sal_False; + } else + { + fseek( cmdFile , 0 , SEEK_END ); + sal_Int32 nLen = ftell( cmdFile); + fseek( cmdFile, 0, SEEK_SET ); + + // 2 chars per string is a upper limit for the number of + // substrings ( at least one separator char needed for fscanf). + char ** rargv = (char **)rtl_allocateMemory( nLen * sizeof( char* ) /2); + if( ! rargv ) + { + OStringBuffer buf; + buf.append( "Not enough memory for reading command file " ); + buf.append( av[i] +1 ); + buf.append( " with length " ); + buf.append( nLen ); + buf.append( "." ); + throw IllegalArgument( buf.makeStringAndClear() ); + } + char *buffer = ( char * )rtl_allocateMemory( nLen +1 ); + if( ! buffer ) + { + OStringBuffer buf; + buf.append( "Not enough memory for reading command file " ); + buf.append( av[i] +1 ); + buf.append( " with length " ); + buf.append( nLen ); + buf.append( "." ); + throw IllegalArgument( buf.makeStringAndClear() ); + } + + // we start at one to omit argv[0] + sal_Int32 rargc = 1; + rargv[0] = av[0]; + while ( fscanf(cmdFile, "%s", buffer) != EOF ) + { + rargv[rargc]= (char * )rtl_allocateMemory( strlen( buffer ) +1 ); + if( ! rargv[rargc] ) + { + OStringBuffer buf; + buf.append( "Not enough memory for reading command file " ); + buf.append( av[i] +1 ); + buf.append( " with length " ); + buf.append( nLen ); + buf.append( "." ); + throw IllegalArgument( buf.makeStringAndClear() ); + } + strcpy( rargv[rargc] , buffer ); // #100211# - checked + rargc++; + } + fclose(cmdFile); + + parseOptions(rargc, rargv, rOptions, bCmdFile); + + for (long j=1; j < rargc; j++) + { + rtl_freeMemory(rargv[j]); + } + rtl_freeMemory( buffer ); + rtl_freeMemory( rargv ); + } + } else + { + usingRegisterImpl(); + ret = sal_False; + } + } + } + + if( ! bLoaderExplicitlyGiven ) + { + if ( rOptions.sComponentUrls.getLength() > 4 && + rOptions.sComponentUrls.matchAsciiL( + ".jar" , 4 , rOptions.sComponentUrls.getLength() - 4 ) ) + { + if( ! rOptions.bSilent ) + { + printf( "using loader com.sun.star.loader.Java2\n" ); + } + rOptions.sLoaderName = OUString( + RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.Java2")); + } + else + { + rOptions.sLoaderName = OUString( + RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary") ); + } + } + + return ret; +} + + +struct DoIt +{ + sal_Bool _bRegister; + sal_Bool _bRevoke; + sal_Bool _bSilent; + sal_Bool _bPrefix; + OUString _sPrefix; + OString _sRegName; + OUString _sLoaderName; + Reference<XImplementationRegistration2> _xImplRegistration; + Reference<XSimpleRegistry> _xReg; + sal_uInt32 * _exitCode; + + DoIt(sal_Bool bRegister, + sal_Bool bRevoke, + sal_Bool bSilent, + sal_Bool bPrefix, + const OUString & sPrefix, + const Reference<XSimpleRegistry> & xReg, + const OString & sRegName, + const Reference<XImplementationRegistration2> & xImplRegistration, + const OUString & sLoaderName, + sal_uInt32 * exitCode) + throw(); + + void operator()(const OUString & url) throw(); +}; + +DoIt::DoIt(sal_Bool bRegister, + sal_Bool bRevoke, + sal_Bool bSilent, + sal_Bool bPrefix, + const OUString & sPrefix, + const Reference<XSimpleRegistry> & xReg, + const OString & sRegName, + const Reference<XImplementationRegistration2> & xImplRegistration, + const OUString & sLoaderName, + sal_uInt32 * exitCode) throw() + : _bRegister(bRegister), + _bRevoke(bRevoke), + _bSilent( bSilent ), + _bPrefix( bPrefix ), + _sPrefix( sPrefix ), + _sRegName(sRegName), + _sLoaderName(sLoaderName), + _xImplRegistration(xImplRegistration), + _xReg(xReg), + _exitCode(exitCode) +{} + +void DoIt::operator() (const OUString & url) throw() +{ + OString sUrl = OUStringToOString(url, osl_getThreadTextEncoding()); + + if (_bRegister) + { + try + { + Reference<XImplementationRegistration2> _xImplRegistration2(_xImplRegistration, UNO_QUERY); + if ( _bPrefix ) { + _xImplRegistration->registerImplementationWithLocation( + _sLoaderName, url, replacePrefix(url, _sPrefix), _xReg); + } else { + _xImplRegistration->registerImplementation(_sLoaderName, url, _xReg); + } + + if ( ! _bSilent ) + { + fprintf(stderr, "register component '%s' in registry '%s' succesful!\n", sUrl.getStr(), _sRegName.getStr()); + } + + } + catch(CannotRegisterImplementationException & cannotRegisterImplementationException) { + OString aMessage(OUStringToOString(cannotRegisterImplementationException.Message, RTL_TEXTENCODING_ASCII_US)); + fprintf(stderr, "register component '%s' in registry '%s' failed!\n", sUrl.getStr(), _sRegName.getStr()); + fprintf(stderr, "error (CannotRegisterImplementationException): %s\n", aMessage.getStr()); + + ++ (*_exitCode); + } + catch( RuntimeException & e ) + { + OString aMessage(OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US)); + fprintf(stderr, "register component '%s' in registry '%s' failed!\n", sUrl.getStr(), _sRegName.getStr()); + fprintf(stderr, "error (RuntimeException): %s\n", aMessage.getStr()); + + ++ (*_exitCode); + } + } + else if(_bRevoke) + { + try + { + sal_Bool bRet = _xImplRegistration->revokeImplementation(url, _xReg); + + if (bRet) + { + if ( ! _bSilent ) + fprintf(stderr, "revoke component '%s' from registry '%s' succesful!\n", sUrl.getStr(), _sRegName.getStr()); + } + else + { + fprintf(stderr, "revoke component '%s' from registry '%s' failed!\n", sUrl.getStr(), _sRegName.getStr()); + ++ (*_exitCode); + } + } + catch( RuntimeException & e ) + { + OString aMessage(OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US)); + fprintf( stderr, + "revoke component '%s' from registry '%s' failed!\n", + sUrl.getStr(), + _sRegName.getStr() ); + fprintf( stderr, "RuntimeException: %s\n" , aMessage.getStr()); + ++ (*_exitCode); + } + } +} + +static bool hasService( + const Reference< XMultiServiceFactory > &xSMgr, + const sal_Char * service ) +{ + bool ret = false; + + Reference< XContentEnumerationAccess > access( xSMgr, UNO_QUERY ); + if( access.is( )) + { + Reference< XEnumeration > enumeration = access->createContentEnumeration( + OUString::createFromAscii( service ) ); + + if( enumeration.is() && enumeration->hasMoreElements() ) + { + ret = true; + } + } + return ret; +} + +static void bootstrap( + Options & opt , + Reference< XMultiServiceFactory > &xSMgr, + Reference< XSimpleRegistry > & reg ) throw ( Exception ) +{ + if( opt.sRegName.equals( opt.sBootRegName2 ) ) + { + OUString tmp2 = opt.sBootRegName; + opt.sBootRegName = opt.sBootRegName2; + opt.sBootRegName2 = tmp2; + } + + if ( opt.sRegName.equals(opt.sBootRegName) ) + { + if( opt.sBootRegName2.getLength() ) + { + xSMgr = createRegistryServiceFactory( + convertToFileUrl(opt.sRegName), + convertToFileUrl(opt.sBootRegName2), + sal_False ); + } + else + { + xSMgr = createRegistryServiceFactory( + convertToFileUrl(opt.sRegName) , sal_False ); + } + } + else + { + if( opt.sBootRegName2.getLength() ) + { + xSMgr = createRegistryServiceFactory( + convertToFileUrl( opt.sBootRegName2 ), + convertToFileUrl( opt.sBootRegName ), + sal_True ); + } + else if ( opt.sBootRegName.getLength() ) + { + xSMgr = createRegistryServiceFactory( + convertToFileUrl( opt.sBootRegName ), + sal_True ); + } + else + { + xSMgr = createServiceFactory(); + } + reg = Reference< XSimpleRegistry >( + xSMgr->createInstance( + rtl::OUString::createFromAscii("com.sun.star.registry.SimpleRegistry")), UNO_QUERY); + + if (reg.is()) + { + try + { + reg->open( convertToFileUrl(opt.sRegName), sal_False, sal_True); + if (!reg->isValid()) + { + fprintf(stderr, "ERROR: open|create registry '%s' failed!\n", + OUStringToOString(opt.sRegName, osl_getThreadTextEncoding() ).getStr()); + exit(1); + } + } + catch( InvalidRegistryException & e) + { + OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ); + fprintf(stderr, + "ERROR: create registry '%s' failed!\n" + "InvalidRegistryException: %s\n", + OUStringToOString( opt.sRegName, osl_getThreadTextEncoding()).getStr(), + o.getStr() ); + exit(1); + } + } + } + + if( ! opt.sLoaderName.compareToAscii( "com.sun.star.loader.Java2" ) && + ! hasService( xSMgr, "com.sun.star.loader.Java2" ) ) + { + // we know our java loader, so we check, whether a java-loader is + // registered + Reference< XInterface > r = loadSharedLibComponentFactory( + OUString::createFromAscii( "javavm.uno" SAL_DLLEXTENSION ), + OUString(), + OUString::createFromAscii( "com.sun.star.comp.stoc.JavaVirtualMachine" ), + xSMgr, + Reference< XRegistryKey > () ); + Reference< XInterface > r2 = loadSharedLibComponentFactory( + OUString::createFromAscii( "javaloader.uno" SAL_DLLEXTENSION ), + OUString(), + OUString::createFromAscii(( "com.sun.star.comp.stoc.JavaComponentLoader" ) ), + xSMgr, + Reference< XRegistryKey > () ); + Reference <XSet> xSet( xSMgr, UNO_QUERY ); + if( r.is() && r2.is() && xSet.is() ) + { + xSet->insert( makeAny( r ) ); + xSet->insert( makeAny( r2 ) ); + } + } +} + +SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv) +{ + sal_Bool bRet = sal_False; + sal_uInt32 exitCode = 0; + Options aOptions; + + try + { + if ( !parseOptions(argc, argv, aOptions, sal_False) ) + { + exit(1); + } + } + catch ( IllegalArgument& e) + { + fprintf(stderr, "ERROR: %s\n", e.m_message.getStr()); + exit(1); + } + + if( ! aOptions.sRegName.getLength() ) + { + fprintf( stderr, "ERROR: target registry missing (-r option)\n" ); + exit( 1 ); + } + if ( aOptions.sComponentUrls.getLength() == 0 ) + { + fprintf(stderr, "ERROR: no component url is specified!\n"); + exit(1); + } + + Reference< XMultiServiceFactory > xSMgr; + Reference< XSimpleRegistry > xReg; + try + { + bootstrap( aOptions, xSMgr ,xReg ); + } + catch( Exception& e ) + { + fprintf(stderr, "ERROR: create ServiceManager failed!\n"); + if ( e.Message.getLength() ) + { + fprintf(stderr, "ERROR description: %s\n", + OUStringToOString(e.Message, osl_getThreadTextEncoding()).getStr()); + } + exit(1); + } + + Reference<XImplementationRegistration2> xImplRegistration( + xSMgr->createInstance( + OUString(RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.registry.ImplementationRegistration"))), + UNO_QUERY); + + if (xImplRegistration.is()) + { + sal_Int32 index = 0; + vector<OUString> urls; + + OUString urlListWithSemikolon = aOptions.sComponentUrls; + do { + OUString aToken = urlListWithSemikolon.getToken( 0, ';', index); + fprintf(stderr, "%s\n", OUStringToOString(aToken, osl_getThreadTextEncoding()).getStr()); + urls.push_back(aToken); + } while ( index >= 0 ); + + + OString sRegName = OUStringToOString( aOptions.sRegName, osl_getThreadTextEncoding() ); + if(aOptions.bRegister || aOptions.bRevoke) + { + for_each(urls.begin(), urls.end(), + DoIt(aOptions.bRegister, aOptions.bRevoke, aOptions.bSilent, + aOptions.bPrefix, aOptions.sPrefix, + xReg, sRegName, xImplRegistration, + aOptions.sLoaderName, &exitCode)); + } + else + { + ++ exitCode; + usingRegisterImpl(); + } + } + else + { + fprintf(stderr, "Component registration service could not be loaded!\n"); + exitCode++; + } + + if (!bRet && xReg.is() && xReg->isValid()) + xReg->close(); + + Reference< XComponent > xComponent( xSMgr, UNO_QUERY ); + if ( xComponent.is() ) + xComponent->dispose(); + + return exitCode; +} + + diff --git a/cpputools/source/regsingleton/makefile.mk b/cpputools/source/regsingleton/makefile.mk new file mode 100644 index 000000000000..e90b0c8c483f --- /dev/null +++ b/cpputools/source/regsingleton/makefile.mk @@ -0,0 +1,79 @@ +#************************************************************************* +# +# 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. +# +#************************************************************************* +PRJ=..$/.. + +PRJNAME=cpputools +TARGET=regsingleton +LIBTARGET=NO +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +UNOUCRDEP=$(SOLARBINDIR)$/udkapi.rdb +UNOUCRRDB=$(SOLARBINDIR)$/udkapi.rdb + +NO_OFFUH=TRUE +CPPUMAKERFLAGS+= -C + +UNOTYPES= \ + com.sun.star.uno.TypeClass \ + com.sun.star.uno.XAggregation \ + com.sun.star.uno.XWeak \ + com.sun.star.uno.XComponentContext \ + com.sun.star.lang.XTypeProvider \ + com.sun.star.lang.XComponent \ + com.sun.star.lang.XSingleServiceFactory \ + com.sun.star.lang.XSingleComponentFactory \ + com.sun.star.lang.XMultiServiceFactory \ + com.sun.star.lang.XMultiComponentFactory \ + com.sun.star.container.XHierarchicalNameAccess \ + com.sun.star.registry.XSimpleRegistry \ + com.sun.star.registry.XRegistryKey + +# --- Files -------------------------------------------------------- + +DEPOBJFILES= \ + $(OBJ)$/regsingleton.obj + +APP1TARGET=$(TARGET) +APP1OBJS=$(DEPOBJFILES) + +APP1STDLIBS= \ + $(SALLIB) \ + $(CPPULIB) \ + $(CPPUHELPERLIB) + +.IF "$(debug)" != "" +.IF "$(COM)" == "MSC" +CFLAGS += -Ob0 +.ENDIF +.ENDIF + +.INCLUDE : target.mk + diff --git a/cpputools/source/regsingleton/regsingleton.cxx b/cpputools/source/regsingleton/regsingleton.cxx new file mode 100644 index 000000000000..688bdc6d40b8 --- /dev/null +++ b/cpputools/source/regsingleton/regsingleton.cxx @@ -0,0 +1,173 @@ +/************************************************************************* + * + * 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 <stdio.h> + +#include "sal/main.h" +#include <osl/diagnose.h> +#include <osl/file.h> + +#include <cppuhelper/bootstrap.hxx> + +#include <com/sun/star/registry/XSimpleRegistry.hpp> + +#define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) ) + + +using namespace ::rtl; +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; + +static void print_options() SAL_THROW( () ) +{ + printf( + "\nusage: regsingleton [-r|-ra] registry_file singleton_name[=service_name] ...\n\n" + "Inserts a singleton entry into rdb.\n" + "Option -r revokes given entries, -ra revokes all entries.\n" ); +} + +//================================================================================================== +SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv) +{ + if (argc < 3) + { + print_options(); + return 1; + } + + bool insert_entry = true; + bool remove_all = false; + int nPos = 1; + if ('-' == argv[ nPos ][ 0 ] && 'r' == argv[ nPos ][ 1 ]) + { + if ('a' == argv[ nPos ][ 2 ] && '\0' == argv[ nPos ][ 3 ]) + { + remove_all = true; + } + else if ('\0' != argv[ nPos ][ 2 ]) + { + print_options(); + return 1; + } + insert_entry = false; + ++nPos; + } + + OUString sys_path( OUString::createFromAscii( argv[ nPos ] ) ); + OUString file_url; + oslFileError rc = osl_getFileURLFromSystemPath( sys_path.pData, &file_url.pData ); + if (osl_File_E_None != rc) + { + fprintf( stderr, "\nerror: cannot make file url out of %s\n", argv[ nPos ] ); + return 1; + } + ++nPos; + + try + { + Reference< registry::XSimpleRegistry > xSimReg( ::cppu::createSimpleRegistry() ); + xSimReg->open( file_url, sal_False, sal_True ); + Reference< registry::XRegistryKey > xRoot( xSimReg->getRootKey() ); + + if (remove_all) + { + try + { + xRoot->deleteKey( OUSTR("SINGLETONS") ); + } + catch (registry::InvalidRegistryException & exc) + { + OString cstr_msg( + OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) ); + fprintf( + stderr, "\nwarning: removing all singletons failed: %s\n", + cstr_msg.getStr() ); + } + } + else + { + Reference< registry::XRegistryKey > xKey( xRoot->openKey( OUSTR("SINGLETONS") ) ); + if (! xKey.is()) + xKey = xRoot->createKey( OUSTR("SINGLETONS") ); + + for ( ; nPos < argc; ++nPos ) + { + OUString singleton( OUString::createFromAscii( argv[ nPos ] ) ); + OUString service; + sal_Int32 eq = singleton.indexOf( '=' ); + if (eq >= 0) + { + service = singleton.copy( eq +1 ); + singleton = singleton.copy( 0, eq ); + } + + if (insert_entry) + { + if (service.getLength()) + { + Reference< registry::XRegistryKey > xEntry( xKey->openKey( singleton ) ); + if (! xEntry.is()) + xEntry = xKey->createKey( singleton ); + xEntry->setStringValue( service ); + } + else + { + OString entry( OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) ); + fprintf( + stderr, "\nwarning: no service name given for singleton %s!\n", + entry.getStr() ); + } + } + else + { + try + { + xKey->deleteKey( singleton ); + } + catch (registry::InvalidRegistryException & exc) + { + OString cstr_singleton( + OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) ); + OString cstr_msg( + OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) ); + fprintf( + stderr, "\nwarning: singleton %s is not registered: %s\n", + cstr_singleton.getStr(), cstr_msg.getStr() ); + } + } + } + } + + return 0; + } + catch (Exception & rExc) + { + OString msg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) ); + fprintf( stderr, "\nerror: %s\n", msg.getStr() ); + return 1; + } +} diff --git a/cpputools/source/sp2bv/makefile.mk b/cpputools/source/sp2bv/makefile.mk new file mode 100644 index 000000000000..a8040a15d1dd --- /dev/null +++ b/cpputools/source/sp2bv/makefile.mk @@ -0,0 +1,56 @@ +#************************************************************************* +# +# 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. +# +#************************************************************************* + +PRJ=..$/.. + +PRJNAME=cpputools +TARGET=sp2bv +TARGETTYPE=CUI +.IF "$(OS)" != "MACOSX" +# hack to get stdc++ linked +NO_DEFAULT_STL=TRUE +.ENDIF +LIBTARGET=NO +ENABLE_EXCEPTIONS=true + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + + +# --- Files -------------------------------------------------------- + +OBJFILES=$(OBJ)$/sp2bv.obj + +APP1TARGET=sp2bv +APP1OBJS=$(OBJFILES) +APP1STDLIBS=$(SALLIB) + + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk diff --git a/cpputools/source/sp2bv/readme.txt b/cpputools/source/sp2bv/readme.txt new file mode 100644 index 000000000000..257fe28856c3 --- /dev/null +++ b/cpputools/source/sp2bv/readme.txt @@ -0,0 +1,10 @@ +This tool converts system paths into file urls and escapes it for use as +bootstrap variable. For example + +c:\program files\App$ +-> +file:///c:/program%20files/App$ +-> +file:///c:/program%20files/App\$ + +
\ No newline at end of file diff --git a/cpputools/source/sp2bv/sp2bv.cxx b/cpputools/source/sp2bv/sp2bv.cxx new file mode 100644 index 000000000000..afacab909545 --- /dev/null +++ b/cpputools/source/sp2bv/sp2bv.cxx @@ -0,0 +1,141 @@ +/************************************************************************* + * + * 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "osl/thread.h" +#include "osl/file.h" +#include "rtl/ustring.hxx" +#include "rtl/ustrbuf.h" + + + +using namespace rtl; + +static sal_Bool hasOption(char const * szOption, int argc, char** argv); + + +#define HELP_TEXT \ +"SYNOPSIS \n\n" \ +"\tsp2bv [-h] [-?] string \n\n" \ +"DESCRIPTION\n\n" \ +"\tsp2bv stands for \"system path to bootstrap variable\"." \ +" First the system path is converted into a file URL. Then all " \ +"characters which have a special meaning in bootstrap variables, " \ +"such as \'$\' are escaped. The resulting string is written to " \ +"stdout an can be assigned to a bootstrap variable.\n" \ +"\n\n" \ +"OPTIONS \n\n" \ +"\tThe following options are supported: \n" \ +"-?\n " \ +"--help" \ +" Display help information.\n" + + + + +int main(int argc, char **argv) +{ + if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv)) + { + fprintf(stdout, HELP_TEXT);// default + return 0; + } + + if (argc != 2) + { + fprintf(stdout, HELP_TEXT); + return -1; + } + + rtl_uString* pPath = NULL; + rtl_string2UString( &pPath, argv[1], strlen(argv[1]), + osl_getThreadTextEncoding(),OSTRING_TO_OUSTRING_CVTFLAGS ); + + rtl_uString* pUrl = NULL; + if (osl_getFileURLFromSystemPath(pPath, &pUrl) != osl_File_E_None) + return -1; +//escape the special characters + + sal_Unicode cEscapeChar = 0x5c; + rtl_uString* pBuffer = NULL; + sal_Int32 nCapacity = 255; + rtl_uString_new_WithLength( &pBuffer, nCapacity ); + + const sal_Unicode* pCur = pUrl->buffer; + for (int i = 0; i != pUrl->length; i++) + { + switch( *pCur) + { + case '$': + rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, &cEscapeChar, 1); + rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1); + break; + case '{': + case '}': + case '\\': fprintf(stderr, "sp2vb: file URL contains invalid characters!\n"); + return -1; + default: + rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1); + } + pCur ++; + } +//convert back to byte string so that we can print it. + rtl_String* pBootVar = NULL; + rtl_uString2String( &pBootVar, pBuffer->buffer, pBuffer->length, + osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS); + + fprintf(stdout, "%s", pBootVar->buffer); + fflush(stdout); + + rtl_uString_release(pBuffer); + rtl_uString_release(pPath); + rtl_uString_release(pUrl); + rtl_string_release(pBootVar); + return 0; +} + + + +static sal_Bool hasOption(char const * szOption, int argc, char** argv) +{ + sal_Bool retVal= sal_False; + for(sal_Int16 i= 1; i < argc; i++) + { + if( ! strcmp(argv[i], szOption)) + { + retVal= sal_True; + break; + } + } + return retVal; +} + + + + + diff --git a/cpputools/source/unoexe/makefile.mk b/cpputools/source/unoexe/makefile.mk new file mode 100644 index 000000000000..c17fe9001cde --- /dev/null +++ b/cpputools/source/unoexe/makefile.mk @@ -0,0 +1,91 @@ +#************************************************************************* +# +# 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. +# +#************************************************************************* +PRJ=..$/.. + +PRJNAME=cpputools +TARGET=uno +LIBTARGET=NO +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +UNOUCRDEP=$(SOLARBINDIR)$/udkapi.rdb +UNOUCRRDB=$(SOLARBINDIR)$/udkapi.rdb + +NO_OFFUH=TRUE +CPPUMAKERFLAGS+= -C + +UNOTYPES= \ + com.sun.star.uno.TypeClass \ + com.sun.star.uno.XAggregation \ + com.sun.star.uno.XWeak \ + com.sun.star.uno.XComponentContext \ + com.sun.star.lang.XTypeProvider \ + com.sun.star.lang.XMain \ + com.sun.star.lang.XInitialization \ + com.sun.star.lang.XComponent \ + com.sun.star.lang.XSingleServiceFactory \ + com.sun.star.lang.XSingleComponentFactory \ + com.sun.star.lang.XMultiServiceFactory \ + com.sun.star.lang.XMultiComponentFactory \ + com.sun.star.container.XSet \ + com.sun.star.container.XHierarchicalNameAccess \ + com.sun.star.loader.XImplementationLoader \ + com.sun.star.registry.XSimpleRegistry \ + com.sun.star.registry.XRegistryKey \ + com.sun.star.connection.XAcceptor \ + com.sun.star.connection.XConnection \ + com.sun.star.bridge.XBridgeFactory \ + com.sun.star.bridge.XBridge + +# --- Files -------------------------------------------------------- + +DEPOBJFILES=$(OBJ)$/unoexe.obj + +APP1TARGET=$(TARGET) +APP1OBJS=$(DEPOBJFILES) +APP1RPATH=UREBIN + +# Include all relevant (see ure/source/README) dynamic libraries, so that C++ +# UNO components running in the uno executable have a defined environment +# (stlport, unxlngi6 libstdc++.so.6, and wntmsci10 uwinapi.dll are already +# included via APP1STDLIB, unxlngi6 libgcc_s.so.1 and wntmsci10 msvcr71.dll and +# msvcp71.dll are magic---TODO): +APP1STDLIBS= \ + $(SALLIB) \ + $(SALHELPERLIB) \ + $(CPPULIB) \ + $(CPPUHELPERLIB)\ + $(LIBXML2LIB) +.IF "$(OS)" == "WNT" +APP1STDLIBS += $(UNICOWSLIB) +.ENDIF + +.INCLUDE : target.mk + diff --git a/cpputools/source/unoexe/unoexe.cxx b/cpputools/source/unoexe/unoexe.cxx new file mode 100644 index 000000000000..c546cdadedfc --- /dev/null +++ b/cpputools/source/unoexe/unoexe.cxx @@ -0,0 +1,890 @@ +/************************************************************************* + * + * 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 <stdio.h> +#include <vector> + +#include "sal/main.h" +#include <osl/diagnose.h> +#include <osl/mutex.hxx> +#include <osl/conditn.hxx> +#include <osl/module.h> + +#include <rtl/process.h> +#include <rtl/string.h> +#include <rtl/strbuf.hxx> +#include <rtl/ustrbuf.hxx> + +#include <uno/environment.h> +#include <uno/mapping.hxx> + +#include <cppuhelper/factory.hxx> +#include <cppuhelper/bootstrap.hxx> +#include <cppuhelper/servicefactory.hxx> +#include <cppuhelper/shlib.hxx> +#include <cppuhelper/implbase1.hxx> + +#include <com/sun/star/lang/XMain.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/lang/XEventListener.hpp> +#include <com/sun/star/container/XSet.hpp> +#include <com/sun/star/loader/XImplementationLoader.hpp> +#include <com/sun/star/registry/XSimpleRegistry.hpp> +#include <com/sun/star/registry/XRegistryKey.hpp> +#include <com/sun/star/connection/XAcceptor.hpp> +#include <com/sun/star/connection/XConnection.hpp> +#include <com/sun/star/bridge/XBridgeFactory.hpp> +#include <com/sun/star/bridge/XBridge.hpp> +#include <osl/process.h> +#include <osl/thread.h> +#include <osl/file.hxx> + +#ifdef SAL_UNX +#define SEPARATOR '/' +#else +#define SEPARATOR '\\' +#endif + +using namespace std; +using namespace rtl; +using namespace osl; +using namespace cppu; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::loader; +using namespace com::sun::star::registry; +using namespace com::sun::star::connection; +using namespace com::sun::star::bridge; +using namespace com::sun::star::container; + +namespace unoexe +{ + +static sal_Bool isFileUrl(const OUString& fileName) +{ + if (fileName.indexOf(OUString::createFromAscii("file://")) == 0 ) + return sal_True; + return sal_False; +} + +static OUString convertToFileUrl(const OUString& fileName) +{ + if ( isFileUrl(fileName) ) + { + return fileName; + } + + OUString uUrlFileName; + if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 ) + { + OUString uWorkingDir; + if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) { + OSL_ASSERT(false); + } + if (FileBase::getAbsoluteFileURL(uWorkingDir, fileName, uUrlFileName) + != FileBase::E_None) + { + OSL_ASSERT(false); + } + } else + { + if (FileBase::getFileURLFromSystemPath(fileName, uUrlFileName) + != FileBase::E_None) + { + OSL_ASSERT(false); + } + } + + return uUrlFileName; +} + +static sal_Bool s_quiet = false; + +//-------------------------------------------------------------------------------------------------- +static inline void out( const sal_Char * pText ) +{ + if (! s_quiet) + fprintf( stderr, pText ); +} +//-------------------------------------------------------------------------------------------------- +static inline void out( const OUString & rText ) +{ + if (! s_quiet) + { + OString aText( OUStringToOString( rText, RTL_TEXTENCODING_ASCII_US ) ); + fprintf( stderr, aText.getStr() ); + } +} + +//-------------------------------------------------------------------------------------------------- +static const char arUsingText[] = +"\nusing:\n\n" +"uno [-c ComponentImplementationName -l LocationUrl | -s ServiceName]\n" +" [-ro ReadOnlyRegistry1] [-ro ReadOnlyRegistry2] ... [-rw ReadWriteRegistry]\n" +" [-u uno:(socket[,host=HostName][,port=nnn]|pipe[,name=PipeName]);<protocol>;Name\n" +" [--singleaccept] [--singleinstance]]\n" +" [--quiet]\n" +" [-- Argument1 Argument2 ...]\n"; + +//-------------------------------------------------------------------------------------------------- +static sal_Bool readOption( OUString * pValue, const sal_Char * pOpt, + sal_Int32 * pnIndex, const OUString & aArg) + throw (RuntimeException) +{ + const OUString dash = OUString(RTL_CONSTASCII_USTRINGPARAM("-")); + if(aArg.indexOf(dash) != 0) + return sal_False; + + OUString aOpt = OUString::createFromAscii( pOpt ); + + if (aArg.getLength() < aOpt.getLength()) + return sal_False; + + if (aOpt.equalsIgnoreAsciiCase( aArg.copy(1) )) + { + // take next argument + ++(*pnIndex); + + rtl_getAppCommandArg(*pnIndex, &pValue->pData); + if (*pnIndex >= (sal_Int32)rtl_getAppCommandArgCount() || pValue->copy(1).equals(dash)) + { + OUStringBuffer buf( 32 ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("incomplete option \"-") ); + buf.appendAscii( pOpt ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" given!") ); + throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() ); + } + else + { +#if OSL_DEBUG_LEVEL > 1 + out( "\n> identified option -" ); + out( pOpt ); + out( " = " ); + OString tmp = OUStringToOString(aArg, RTL_TEXTENCODING_ASCII_US); + out( tmp.getStr() ); +#endif + ++(*pnIndex); + return sal_True; + } + } + else if (aArg.indexOf(aOpt) == 1) + { + *pValue = aArg.copy(1 + aOpt.getLength()); +#if OSL_DEBUG_LEVEL > 1 + out( "\n> identified option -" ); + out( pOpt ); + out( " = " ); + OString tmp = OUStringToOString(aArg.copy(aOpt.getLength()), RTL_TEXTENCODING_ASCII_US); + out( tmp.getStr() ); +#endif + ++(*pnIndex); + + return sal_True; + } + return sal_False; +} +//-------------------------------------------------------------------------------------------------- +static sal_Bool readOption( sal_Bool * pbOpt, const sal_Char * pOpt, + sal_Int32 * pnIndex, const OUString & aArg) +{ + const OUString dashdash(RTL_CONSTASCII_USTRINGPARAM("--")); + OUString aOpt = OUString::createFromAscii(pOpt); + + if(aArg.indexOf(dashdash) == 0 && aOpt.equals(aArg.copy(2))) + { + ++(*pnIndex); + *pbOpt = sal_True; +#if OSL_DEBUG_LEVEL > 1 + out( "\n> identified option --" ); + out( pOpt ); +#endif + return sal_True; + } + return sal_False; +} + + +//################################################################################################## +//################################################################################################## +//################################################################################################## + + +//-------------------------------------------------------------------------------------------------- +template< class T > +void createInstance( + Reference< T > & rxOut, + const Reference< XComponentContext > & xContext, + const OUString & rServiceName ) + throw (Exception) +{ + Reference< XMultiComponentFactory > xMgr( xContext->getServiceManager() ); + Reference< XInterface > x( xMgr->createInstanceWithContext( rServiceName, xContext ) ); + + if (! x.is()) + { + static sal_Bool s_bSet = sal_False; + if (! s_bSet) + { + MutexGuard aGuard( Mutex::getGlobalMutex() ); + if (! s_bSet) + { + Reference< XSet > xSet( xMgr, UNO_QUERY ); + if (xSet.is()) + { + Reference< XMultiServiceFactory > xSF( xMgr, UNO_QUERY ); + // acceptor + xSet->insert( makeAny( loadSharedLibComponentFactory( + OUString( RTL_CONSTASCII_USTRINGPARAM( + "acceptor.uno" SAL_DLLEXTENSION) ), + OUString(), + OUString( RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.comp.io.Acceptor") ), + xSF, Reference< XRegistryKey >() ) ) ); + // connector + xSet->insert( makeAny( loadSharedLibComponentFactory( + OUString( RTL_CONSTASCII_USTRINGPARAM( + "connector.uno" SAL_DLLEXTENSION) ), + OUString(), + OUString( RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.comp.io.Connector") ), + xSF, Reference< XRegistryKey >() ) ) ); + // bridge factory + xSet->insert( makeAny( loadSharedLibComponentFactory( + OUString( RTL_CONSTASCII_USTRINGPARAM( + "binaryurp.uno" SAL_DLLEXTENSION) ), + OUString(), + OUString( + RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.comp.bridge.BridgeFactory") ), + xSF, Reference< XRegistryKey >() ) ) ); + } + s_bSet = sal_True; + } + } + x = xMgr->createInstanceWithContext( rServiceName, xContext ); + } + + if (! x.is()) + { + OUStringBuffer buf( 64 ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("cannot get service instance \"") ); + buf.append( rServiceName ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") ); + throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() ); + } + + rxOut = Reference< T >::query( x ); + if (! rxOut.is()) + { + OUStringBuffer buf( 64 ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("service instance \"") ); + buf.append( rServiceName ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" does not support demanded interface \"") ); + const Type & rType = ::getCppuType( (const Reference< T > *)0 ); + buf.append( rType.getTypeName() ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") ); + throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() ); + } +} +//-------------------------------------------------------------------------------------------------- +static Reference< XSimpleRegistry > nestRegistries( + const Reference< XSimpleRegistry > & xReadWrite, + const Reference< XSimpleRegistry > & xReadOnly ) + throw (Exception) +{ + Reference< XSimpleRegistry > xReg( createNestedRegistry() ); + if (! xReg.is()) + { + throw RuntimeException( + OUString( RTL_CONSTASCII_USTRINGPARAM("no nested registry service!" ) ), + Reference< XInterface >() ); + } + + Reference< XInitialization > xInit( xReg, UNO_QUERY ); + if (! xInit.is()) + throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("nested registry does not export interface \"com.sun.star.lang.XInitialization\"!" ) ), Reference< XInterface >() ); + + Sequence< Any > aArgs( 2 ); + aArgs[0] <<= xReadWrite; + aArgs[1] <<= xReadOnly; + xInit->initialize( aArgs ); + + return xReg; +} +//-------------------------------------------------------------------------------------------------- +static Reference< XSimpleRegistry > openRegistry( + const OUString & rURL, + sal_Bool bReadOnly, sal_Bool bCreate ) + throw (Exception) +{ + Reference< XSimpleRegistry > xNewReg( createSimpleRegistry() ); + if (! xNewReg.is()) + { + throw RuntimeException( + OUString( RTL_CONSTASCII_USTRINGPARAM("no simple registry service!" ) ), + Reference< XInterface >() ); + } + + try + { + xNewReg->open( convertToFileUrl(rURL), bReadOnly, bCreate ); + if (xNewReg->isValid()) + return xNewReg; + else + xNewReg->close(); + } + catch (Exception &) + { + } + + out( "\n> warning: cannot open registry \"" ); + out( rURL ); + if (bReadOnly) + out( "\" for reading, ignoring!" ); + else + out( "\" for reading and writing, ignoring!" ); + return Reference< XSimpleRegistry >(); +} +//-------------------------------------------------------------------------------------------------- +static Reference< XInterface > loadComponent( + const Reference< XComponentContext > & xContext, + const OUString & rImplName, const OUString & rLocation ) + throw (Exception) +{ + // determine loader to be used + sal_Int32 nDot = rLocation.lastIndexOf( '.' ); + if (nDot > 0 && nDot < rLocation.getLength()) + { + Reference< XImplementationLoader > xLoader; + + OUString aExt( rLocation.copy( nDot +1 ) ); + + if (aExt.compareToAscii( "dll" ) == 0 || + aExt.compareToAscii( "exe" ) == 0 || + aExt.compareToAscii( "dylib" ) == 0 || + aExt.compareToAscii( "so" ) == 0) + { + createInstance( + xLoader, xContext, OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary") ) ); + } + else if (aExt.compareToAscii( "jar" ) == 0 || + aExt.compareToAscii( "class" ) == 0) + { + createInstance( + xLoader, xContext, OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.Java") ) ); + } + else + { + OUStringBuffer buf( 64 ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("unknown extension of \"") ); + buf.append( rLocation ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"! No loader available!") ); + throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() ); + } + + Reference< XInterface > xInstance; + + // activate + Reference< XInterface > xFactory( xLoader->activate( + rImplName, OUString(), rLocation, Reference< XRegistryKey >() ) ); + if (xFactory.is()) + { + Reference< XSingleComponentFactory > xCFac( xFactory, UNO_QUERY ); + if (xCFac.is()) + { + xInstance = xCFac->createInstanceWithContext( xContext ); + } + else + { + Reference< XSingleServiceFactory > xSFac( xFactory, UNO_QUERY ); + if (xSFac.is()) + { + out( "\n> warning: ignroing context for implementation \"" ); + out( rImplName ); + out( "\"!" ); + xInstance = xSFac->createInstance(); + } + } + } + + if (! xInstance.is()) + { + OUStringBuffer buf( 64 ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("activating component \"") ); + buf.append( rImplName ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" from location \"") ); + buf.append( rLocation ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" failed!") ); + throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() ); + } + + return xInstance; + } + else + { + OUStringBuffer buf( 64 ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("location \"") ); + buf.append( rLocation ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" has no extension! Cannot determine loader to be used!") ); + throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() ); + } +} + + +//################################################################################################## +//################################################################################################## +//################################################################################################## + + +//================================================================================================== +class OInstanceProvider + : public WeakImplHelper1< XInstanceProvider > +{ + Reference< XComponentContext > _xContext; + + Mutex _aSingleInstanceMutex; + Reference< XInterface > _xSingleInstance; + sal_Bool _bSingleInstance; + + OUString _aImplName; + OUString _aLocation; + OUString _aServiceName; + Sequence< Any > _aInitParams; + + OUString _aInstanceName; + + inline Reference< XInterface > createInstance() throw (Exception); + +public: + OInstanceProvider( const Reference< XComponentContext > & xContext, + const OUString & rImplName, const OUString & rLocation, + const OUString & rServiceName, const Sequence< Any > & rInitParams, + sal_Bool bSingleInstance, const OUString & rInstanceName ) + : _xContext( xContext ) + , _bSingleInstance( bSingleInstance ) + , _aImplName( rImplName ) + , _aLocation( rLocation ) + , _aServiceName( rServiceName ) + , _aInitParams( rInitParams ) + , _aInstanceName( rInstanceName ) + {} + + // XInstanceProvider + virtual Reference< XInterface > SAL_CALL getInstance( const OUString & rName ) + throw (NoSuchElementException, RuntimeException); +}; +//__________________________________________________________________________________________________ +inline Reference< XInterface > OInstanceProvider::createInstance() + throw (Exception) +{ + Reference< XInterface > xRet; + if (_aImplName.getLength()) // manually via loader + xRet = loadComponent( _xContext, _aImplName, _aLocation ); + else // via service manager + unoexe::createInstance( xRet, _xContext, _aServiceName ); + + // opt XInit + Reference< XInitialization > xInit( xRet, UNO_QUERY ); + if (xInit.is()) + xInit->initialize( _aInitParams ); + + return xRet; +} +//__________________________________________________________________________________________________ +Reference< XInterface > OInstanceProvider::getInstance( const OUString & rName ) + throw (NoSuchElementException, RuntimeException) +{ + try + { + if (_aInstanceName == rName) + { + Reference< XInterface > xRet; + + if (_aImplName.getLength() == 0 && _aServiceName.getLength() == 0) + { + OSL_ASSERT( + rName.equalsAsciiL( + RTL_CONSTASCII_STRINGPARAM("uno.ComponentContext") ) ); + xRet = _xContext; + } + else if (_bSingleInstance) + { + if (! _xSingleInstance.is()) + { + MutexGuard aGuard( _aSingleInstanceMutex ); + if (! _xSingleInstance.is()) + { + _xSingleInstance = createInstance(); + } + } + xRet = _xSingleInstance; + } + else + { + xRet = createInstance(); + } + + return xRet; + } + } + catch (Exception & rExc) + { + out( "\n> error: " ); + out( rExc.Message ); + } + OUStringBuffer buf( 64 ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("no such element \"") ); + buf.append( rName ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") ); + throw NoSuchElementException( buf.makeStringAndClear(), Reference< XInterface >() ); +} + +//================================================================================================== +struct ODisposingListener : public WeakImplHelper1< XEventListener > +{ + Condition cDisposed; + + // XEventListener + virtual void SAL_CALL disposing( const EventObject & rEvt ) + throw (RuntimeException); + + //---------------------------------------------------------------------------------------------- + static void waitFor( const Reference< XComponent > & xComp ); +}; +//__________________________________________________________________________________________________ +void ODisposingListener::disposing( const EventObject & ) + throw (RuntimeException) +{ + cDisposed.set(); +} +//-------------------------------------------------------------------------------------------------- +void ODisposingListener::waitFor( const Reference< XComponent > & xComp ) +{ + ODisposingListener * pListener = new ODisposingListener(); + Reference< XEventListener > xListener( pListener ); + + xComp->addEventListener( xListener ); + pListener->cDisposed.wait(); +} + + +//################################################################################################## +//################################################################################################## +//################################################################################################## + + +//################################################################################################## +} // namespace unoexe + +using namespace unoexe; + +SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,) +{ + if (argc <= 1) + { + out( arUsingText ); + return 0; + } + + sal_Int32 nRet = 0; + Reference< XComponentContext > xContext; + + + try + { + OUString aImplName, aLocation, aServiceName, aUnoUrl; + vector< OUString > aReadOnlyRegistries; + Sequence< OUString > aParams; + sal_Bool bSingleAccept = sal_False; + sal_Bool bSingleInstance = sal_False; + + //#### read command line arguments ######################################################### + + bool bOldRegistryMimic = false; + bool bNewRegistryMimic = false; + OUString aReadWriteRegistry; + + sal_Int32 nPos = 0; + sal_Int32 nCount = (sal_Int32)rtl_getAppCommandArgCount(); + // read up to arguments + while (nPos < nCount) + { + OUString arg; + + rtl_getAppCommandArg(nPos, &arg.pData); + + const OUString dashdash = OUString(RTL_CONSTASCII_USTRINGPARAM("--")); + if (dashdash == arg) + { + ++nPos; + break; + } + + if (readOption( &aImplName, "c", &nPos, arg) || + readOption( &aLocation, "l", &nPos, arg) || + readOption( &aServiceName, "s", &nPos, arg) || + readOption( &aUnoUrl, "u", &nPos, arg) || + readOption( &s_quiet, "quiet", &nPos, arg) || + readOption( &bSingleAccept, "singleaccept", &nPos, arg) || + readOption( &bSingleInstance, "singleinstance", &nPos, arg)) + { + continue; + } + OUString aRegistry; + if (readOption( &aRegistry, "ro", &nPos, arg)) + { + aReadOnlyRegistries.push_back( aRegistry ); + bNewRegistryMimic = true; + continue; + } + if (readOption( &aReadWriteRegistry, "rw", &nPos, arg)) + { + bNewRegistryMimic = true; + continue; + } + if (readOption( &aRegistry, "r", &nPos, arg)) + { + aReadOnlyRegistries.push_back( aRegistry ); + aReadWriteRegistry = aRegistry; + out( "\n> warning: DEPRECATED use of option -r, use -ro or -rw!" ); + bOldRegistryMimic = true; + continue; + } + + // else illegal argument + OUStringBuffer buf( 64 ); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("unexpected parameter \"") ); + buf.append(arg); + buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") ); + throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() ); + } + + if (bOldRegistryMimic) // last one was set to be read-write + { + aReadOnlyRegistries.pop_back(); + if (bOldRegistryMimic && bNewRegistryMimic) + { + throw RuntimeException( + OUString( RTL_CONSTASCII_USTRINGPARAM("mixing with DEPRECATED registry options!") ), + Reference< XInterface >() ); + } + } + + if ((aImplName.getLength() != 0) && (aServiceName.getLength() != 0)) + throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("give component exOR service name!" ) ), Reference< XInterface >() ); + if (aImplName.getLength() == 0 && aServiceName.getLength() == 0) + { + if (! aUnoUrl.endsWithIgnoreAsciiCaseAsciiL( + RTL_CONSTASCII_STRINGPARAM(";uno.ComponentContext") )) + throw RuntimeException( + OUString( RTL_CONSTASCII_USTRINGPARAM( + "expected UNO-URL with instance name " + "uno.ComponentContext!") ), + Reference<XInterface>() ); + if (bSingleInstance) + throw RuntimeException( + OUString( RTL_CONSTASCII_USTRINGPARAM( + "unexpected option --singleinstance!") ), + Reference<XInterface>() ); + } + if (aImplName.getLength() && !aLocation.getLength()) + throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("give component location!" ) ), Reference< XInterface >() ); + if (aServiceName.getLength() && aLocation.getLength()) + out( "\n> warning: service name given, will ignore location!" ); + + // read component params + aParams.realloc( nCount - nPos ); + OUString * pParams = aParams.getArray(); + + sal_Int32 nOffset = nPos; + for ( ; nPos < nCount; ++nPos ) + { + if (rtl_getAppCommandArg( nPos, &pParams[nPos -nOffset].pData ) + != osl_Process_E_None) + { + OSL_ASSERT(false); + } + } + + if (aReadOnlyRegistries.size() > 0 || + aReadWriteRegistry.getLength() > 0) + { + //#### create registry ############################################# + + Reference< XSimpleRegistry > xRegistry; + + // ReadOnly registries + for ( size_t nReg = 0; nReg < aReadOnlyRegistries.size(); ++nReg ) + { +#if OSL_DEBUG_LEVEL > 1 + out( "\n> trying to open ro registry: " ); + out( OUStringToOString( + aReadOnlyRegistries[ nReg ], + RTL_TEXTENCODING_ASCII_US ).getStr() ); +#endif + Reference< XSimpleRegistry > xNewReg( + openRegistry( + aReadOnlyRegistries[ nReg ], sal_True, sal_False ) ); + if (xNewReg.is()) + xRegistry = (xRegistry.is() ? nestRegistries( + xNewReg, xRegistry ) : xNewReg); + } + if (aReadWriteRegistry.getLength()) + { +#if OSL_DEBUG_LEVEL > 1 + out( "\n> trying to open rw registry: " ); + out( OUStringToOString( + aReadWriteRegistry, + RTL_TEXTENCODING_ASCII_US ).getStr() ); +#endif + // ReadWrite registry + Reference< XSimpleRegistry > xNewReg( + openRegistry( aReadWriteRegistry, sal_False, sal_True ) ); + if (xNewReg.is()) + xRegistry = (xRegistry.is() + ? nestRegistries( xNewReg, xRegistry ) + : xNewReg); + } + + OSL_ASSERT( xRegistry.is() ); + xContext = bootstrap_InitialComponentContext( xRegistry ); + } + else // defaulting + { + xContext = defaultBootstrap_InitialComponentContext(); + } + + //#### accept, instanciate, etc. ########################################################### + + if (aUnoUrl.getLength()) // accepting connections + { + sal_Int32 nIndex = 0, nTokens = 0; + do { aUnoUrl.getToken( 0, ';', nIndex ); nTokens++; } while( nIndex != -1 ); + if (nTokens != 3 || aUnoUrl.getLength() < 10 || + !aUnoUrl.copy( 0, 4 ).equalsIgnoreAsciiCase( OUString( RTL_CONSTASCII_USTRINGPARAM("uno:") ) )) + { + throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("illegal uno url given!" ) ), Reference< XInterface >() ); + } + nIndex = 0; + OUString aConnectDescr( aUnoUrl.getToken( 0, ';', nIndex ).copy( 4 ) ); // uno:CONNECTDESCR;iiop;InstanceName + OUString aInstanceName( aUnoUrl.getToken( 1, ';', nIndex ) ); + + Reference< XAcceptor > xAcceptor; + createInstance( + xAcceptor, xContext, + OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor") ) ); + + // init params + Sequence< Any > aInitParams( aParams.getLength() ); + const OUString * p = aParams.getConstArray(); + Any * pInitParams = aInitParams.getArray(); + for ( sal_Int32 i = aParams.getLength(); i--; ) + { + pInitParams[i] = makeAny( p[i] ); + } + + // instance provider + Reference< XInstanceProvider > xInstanceProvider( new OInstanceProvider( + xContext, aImplName, aLocation, aServiceName, aInitParams, + bSingleInstance, aInstanceName ) ); + + nIndex = 0; + OUString aUnoUrlToken( aUnoUrl.getToken( 1, ';', nIndex ) ); + for (;;) + { + // accepting + out( "\n> accepting " ); + out( aConnectDescr ); + out( "..." ); + Reference< XConnection > xConnection( xAcceptor->accept( aConnectDescr ) ); + out( "connection established." ); + + Reference< XBridgeFactory > xBridgeFactory; + createInstance( + xBridgeFactory, xContext, + OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory") ) ); + + // bridge + Reference< XBridge > xBridge( xBridgeFactory->createBridge( + OUString(), aUnoUrlToken, + xConnection, xInstanceProvider ) ); + + if (bSingleAccept) + { + Reference< XComponent > xComp( xBridge, UNO_QUERY ); + if (! xComp.is()) + throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("bridge factory does not export interface \"com.sun.star.lang.XComponent\"!" ) ), Reference< XInterface >() ); + ODisposingListener::waitFor( xComp ); + break; + } + } + } + else // no uno url + { + Reference< XInterface > xInstance; + if (aImplName.getLength()) // manually via loader + xInstance = loadComponent( xContext, aImplName, aLocation ); + else // via service manager + createInstance( xInstance, xContext, aServiceName ); + + // execution + Reference< XMain > xMain( xInstance, UNO_QUERY ); + if (xMain.is()) + { + nRet = xMain->run( aParams ); + } + else + { + Reference< XComponent > xComp( xInstance, UNO_QUERY ); + if (xComp.is()) + xComp->dispose(); + throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("component does not export interface interface \"com.sun.star.lang.XMain\"!" ) ), Reference< XInterface >() ); + } + } + } + catch (Exception & rExc) + { + out( "\n> error: " ); + out( rExc.Message ); + out( "\n> dying..." ); + nRet = 1; + } + + // cleanup + Reference< XComponent > xComp( xContext, UNO_QUERY ); + if (xComp.is()) + xComp->dispose(); + + out( "\n" ); + return nRet; +} + + + |