merge the formfield patch from ooo-build
[ooovba.git] / cpputools / source / unoexe / unoexe.cxx
blobbc06031dd60065546cd9554e7eb1f30bb8a7cfa9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unoexe.cxx,v $
10 * $Revision: 1.22 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <stdio.h>
32 #include <vector>
34 #include "sal/main.h"
35 #include <osl/diagnose.h>
36 #include <osl/mutex.hxx>
37 #include <osl/conditn.hxx>
38 #include <osl/module.h>
40 #include <rtl/process.h>
41 #include <rtl/string.h>
42 #include <rtl/strbuf.hxx>
43 #include <rtl/ustrbuf.hxx>
45 #include <uno/environment.h>
46 #include <uno/mapping.hxx>
48 #include <cppuhelper/factory.hxx>
49 #include <cppuhelper/bootstrap.hxx>
50 #include <cppuhelper/servicefactory.hxx>
51 #include <cppuhelper/shlib.hxx>
52 #include <cppuhelper/implbase1.hxx>
54 #include <com/sun/star/lang/XMain.hpp>
55 #include <com/sun/star/lang/XInitialization.hpp>
56 #include <com/sun/star/lang/XComponent.hpp>
57 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
58 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
59 #include <com/sun/star/lang/XEventListener.hpp>
60 #include <com/sun/star/container/XSet.hpp>
61 #include <com/sun/star/loader/XImplementationLoader.hpp>
62 #include <com/sun/star/registry/XSimpleRegistry.hpp>
63 #include <com/sun/star/registry/XRegistryKey.hpp>
64 #include <com/sun/star/connection/XAcceptor.hpp>
65 #include <com/sun/star/connection/XConnection.hpp>
66 #include <com/sun/star/bridge/XBridgeFactory.hpp>
67 #include <com/sun/star/bridge/XBridge.hpp>
68 #include <osl/process.h>
69 #include <osl/thread.h>
70 #include <osl/file.hxx>
72 #ifdef SAL_UNX
73 #define SEPARATOR '/'
74 #else
75 #define SEPARATOR '\\'
76 #endif
78 using namespace std;
79 using namespace rtl;
80 using namespace osl;
81 using namespace cppu;
82 using namespace com::sun::star::uno;
83 using namespace com::sun::star::lang;
84 using namespace com::sun::star::loader;
85 using namespace com::sun::star::registry;
86 using namespace com::sun::star::connection;
87 using namespace com::sun::star::bridge;
88 using namespace com::sun::star::container;
90 namespace unoexe
93 static sal_Bool isFileUrl(const OUString& fileName)
95 if (fileName.indexOf(OUString::createFromAscii("file://")) == 0 )
96 return sal_True;
97 return sal_False;
100 static OUString convertToFileUrl(const OUString& fileName)
102 if ( isFileUrl(fileName) )
104 return fileName;
107 OUString uUrlFileName;
108 if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
110 OUString uWorkingDir;
111 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) {
112 OSL_ASSERT(false);
114 if (FileBase::getAbsoluteFileURL(uWorkingDir, fileName, uUrlFileName)
115 != FileBase::E_None)
117 OSL_ASSERT(false);
119 } else
121 if (FileBase::getFileURLFromSystemPath(fileName, uUrlFileName)
122 != FileBase::E_None)
124 OSL_ASSERT(false);
128 return uUrlFileName;
131 static sal_Bool s_quiet = false;
133 //--------------------------------------------------------------------------------------------------
134 static inline void out( const sal_Char * pText )
136 if (! s_quiet)
137 fprintf( stderr, "%s", pText );
139 //--------------------------------------------------------------------------------------------------
140 static inline void out( const OUString & rText )
142 if (! s_quiet)
144 OString aText( OUStringToOString( rText, RTL_TEXTENCODING_ASCII_US ) );
145 fprintf( stderr, "%s", aText.getStr() );
149 //--------------------------------------------------------------------------------------------------
150 static const char arUsingText[] =
151 "\nusing:\n\n"
152 "uno [-c ComponentImplementationName -l LocationUrl | -s ServiceName]\n"
153 " [-ro ReadOnlyRegistry1] [-ro ReadOnlyRegistry2] ... [-rw ReadWriteRegistry]\n"
154 " [-u uno:(socket[,host=HostName][,port=nnn]|pipe[,name=PipeName]);<protocol>;Name\n"
155 " [--singleaccept] [--singleinstance]]\n"
156 " [--quiet]\n"
157 " [-- Argument1 Argument2 ...]\n";
159 //--------------------------------------------------------------------------------------------------
160 static sal_Bool readOption( OUString * pValue, const sal_Char * pOpt,
161 sal_Int32 * pnIndex, const OUString & aArg)
162 throw (RuntimeException)
164 const OUString dash = OUString(RTL_CONSTASCII_USTRINGPARAM("-"));
165 if(aArg.indexOf(dash) != 0)
166 return sal_False;
168 OUString aOpt = OUString::createFromAscii( pOpt );
170 if (aArg.getLength() < aOpt.getLength())
171 return sal_False;
173 if (aOpt.equalsIgnoreAsciiCase( aArg.copy(1) ))
175 // take next argument
176 ++(*pnIndex);
178 rtl_getAppCommandArg(*pnIndex, &pValue->pData);
179 if (*pnIndex >= (sal_Int32)rtl_getAppCommandArgCount() || pValue->copy(1).equals(dash))
181 OUStringBuffer buf( 32 );
182 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("incomplete option \"-") );
183 buf.appendAscii( pOpt );
184 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" given!") );
185 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
187 else
189 #if OSL_DEBUG_LEVEL > 1
190 out( "\n> identified option -" );
191 out( pOpt );
192 out( " = " );
193 OString tmp = OUStringToOString(aArg, RTL_TEXTENCODING_ASCII_US);
194 out( tmp.getStr() );
195 #endif
196 ++(*pnIndex);
197 return sal_True;
200 else if (aArg.indexOf(aOpt) == 1)
202 *pValue = aArg.copy(1 + aOpt.getLength());
203 #if OSL_DEBUG_LEVEL > 1
204 out( "\n> identified option -" );
205 out( pOpt );
206 out( " = " );
207 OString tmp = OUStringToOString(aArg.copy(aOpt.getLength()), RTL_TEXTENCODING_ASCII_US);
208 out( tmp.getStr() );
209 #endif
210 ++(*pnIndex);
212 return sal_True;
214 return sal_False;
216 //--------------------------------------------------------------------------------------------------
217 static sal_Bool readOption( sal_Bool * pbOpt, const sal_Char * pOpt,
218 sal_Int32 * pnIndex, const OUString & aArg)
220 const OUString dashdash(RTL_CONSTASCII_USTRINGPARAM("--"));
221 OUString aOpt = OUString::createFromAscii(pOpt);
223 if(aArg.indexOf(dashdash) == 0 && aOpt.equals(aArg.copy(2)))
225 ++(*pnIndex);
226 *pbOpt = sal_True;
227 #if OSL_DEBUG_LEVEL > 1
228 out( "\n> identified option --" );
229 out( pOpt );
230 #endif
231 return sal_True;
233 return sal_False;
237 //##################################################################################################
238 //##################################################################################################
239 //##################################################################################################
242 //--------------------------------------------------------------------------------------------------
243 template< class T >
244 void createInstance(
245 Reference< T > & rxOut,
246 const Reference< XComponentContext > & xContext,
247 const OUString & rServiceName )
248 throw (Exception)
250 Reference< XMultiComponentFactory > xMgr( xContext->getServiceManager() );
251 Reference< XInterface > x( xMgr->createInstanceWithContext( rServiceName, xContext ) );
253 if (! x.is())
255 static sal_Bool s_bSet = sal_False;
256 if (! s_bSet)
258 MutexGuard aGuard( Mutex::getGlobalMutex() );
259 if (! s_bSet)
261 Reference< XSet > xSet( xMgr, UNO_QUERY );
262 if (xSet.is())
264 Reference< XMultiServiceFactory > xSF( xMgr, UNO_QUERY );
265 // acceptor
266 xSet->insert( makeAny( loadSharedLibComponentFactory(
267 OUString( RTL_CONSTASCII_USTRINGPARAM(
268 "acceptor.uno" SAL_DLLEXTENSION) ),
269 OUString(),
270 OUString( RTL_CONSTASCII_USTRINGPARAM(
271 "com.sun.star.comp.io.Acceptor") ),
272 xSF, Reference< XRegistryKey >() ) ) );
273 // connector
274 xSet->insert( makeAny( loadSharedLibComponentFactory(
275 OUString( RTL_CONSTASCII_USTRINGPARAM(
276 "connector.uno" SAL_DLLEXTENSION) ),
277 OUString(),
278 OUString( RTL_CONSTASCII_USTRINGPARAM(
279 "com.sun.star.comp.io.Connector") ),
280 xSF, Reference< XRegistryKey >() ) ) );
281 // iiop bridge
282 xSet->insert( makeAny( loadSharedLibComponentFactory(
283 OUString( RTL_CONSTASCII_USTRINGPARAM(
284 "remotebridge.uno" SAL_DLLEXTENSION) ),
285 OUString(),
286 OUString( RTL_CONSTASCII_USTRINGPARAM(
287 "com.sun.star.comp.remotebridges."
288 "Bridge.various") ),
289 xSF, Reference< XRegistryKey >() ) ) );
290 // bridge factory
291 xSet->insert( makeAny( loadSharedLibComponentFactory(
292 OUString( RTL_CONSTASCII_USTRINGPARAM(
293 "bridgefac.uno" SAL_DLLEXTENSION) ),
294 OUString(),
295 OUString( RTL_CONSTASCII_USTRINGPARAM(
296 "com.sun.star.comp.remotebridges."
297 "BridgeFactory") ),
298 xSF, Reference< XRegistryKey >() ) ) );
300 s_bSet = sal_True;
303 x = xMgr->createInstanceWithContext( rServiceName, xContext );
306 if (! x.is())
308 OUStringBuffer buf( 64 );
309 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("cannot get service instance \"") );
310 buf.append( rServiceName );
311 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
312 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
315 rxOut = Reference< T >::query( x );
316 if (! rxOut.is())
318 OUStringBuffer buf( 64 );
319 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("service instance \"") );
320 buf.append( rServiceName );
321 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" does not support demanded interface \"") );
322 const Type & rType = ::getCppuType( (const Reference< T > *)0 );
323 buf.append( rType.getTypeName() );
324 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
325 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
328 //--------------------------------------------------------------------------------------------------
329 static Reference< XSimpleRegistry > nestRegistries(
330 const Reference< XSimpleRegistry > & xReadWrite,
331 const Reference< XSimpleRegistry > & xReadOnly )
332 throw (Exception)
334 Reference< XSimpleRegistry > xReg( createNestedRegistry() );
335 if (! xReg.is())
337 throw RuntimeException(
338 OUString( RTL_CONSTASCII_USTRINGPARAM("no nested registry service!" ) ),
339 Reference< XInterface >() );
342 Reference< XInitialization > xInit( xReg, UNO_QUERY );
343 if (! xInit.is())
344 throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("nested registry does not export interface \"com.sun.star.lang.XInitialization\"!" ) ), Reference< XInterface >() );
346 Sequence< Any > aArgs( 2 );
347 aArgs[0] <<= xReadWrite;
348 aArgs[1] <<= xReadOnly;
349 xInit->initialize( aArgs );
351 return xReg;
353 //--------------------------------------------------------------------------------------------------
354 static Reference< XSimpleRegistry > openRegistry(
355 const OUString & rURL,
356 sal_Bool bReadOnly, sal_Bool bCreate )
357 throw (Exception)
359 Reference< XSimpleRegistry > xNewReg( createSimpleRegistry() );
360 if (! xNewReg.is())
362 throw RuntimeException(
363 OUString( RTL_CONSTASCII_USTRINGPARAM("no simple registry service!" ) ),
364 Reference< XInterface >() );
369 xNewReg->open( convertToFileUrl(rURL), bReadOnly, bCreate );
370 if (xNewReg->isValid())
371 return xNewReg;
372 else
373 xNewReg->close();
375 catch (Exception &)
379 out( "\n> warning: cannot open registry \"" );
380 out( rURL );
381 if (bReadOnly)
382 out( "\" for reading, ignoring!" );
383 else
384 out( "\" for reading and writing, ignoring!" );
385 return Reference< XSimpleRegistry >();
387 //--------------------------------------------------------------------------------------------------
388 static Reference< XInterface > loadComponent(
389 const Reference< XComponentContext > & xContext,
390 const OUString & rImplName, const OUString & rLocation )
391 throw (Exception)
393 // determine loader to be used
394 sal_Int32 nDot = rLocation.lastIndexOf( '.' );
395 if (nDot > 0 && nDot < rLocation.getLength())
397 Reference< XImplementationLoader > xLoader;
399 OUString aExt( rLocation.copy( nDot +1 ) );
401 if (aExt.compareToAscii( "dll" ) == 0 ||
402 aExt.compareToAscii( "exe" ) == 0 ||
403 aExt.compareToAscii( "dylib" ) == 0 ||
404 aExt.compareToAscii( "so" ) == 0)
406 createInstance(
407 xLoader, xContext, OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary") ) );
409 else if (aExt.compareToAscii( "jar" ) == 0 ||
410 aExt.compareToAscii( "class" ) == 0)
412 createInstance(
413 xLoader, xContext, OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.Java") ) );
415 else
417 OUStringBuffer buf( 64 );
418 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("unknown extension of \"") );
419 buf.append( rLocation );
420 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"! No loader available!") );
421 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
424 Reference< XInterface > xInstance;
426 // activate
427 Reference< XInterface > xFactory( xLoader->activate(
428 rImplName, OUString(), rLocation, Reference< XRegistryKey >() ) );
429 if (xFactory.is())
431 Reference< XSingleComponentFactory > xCFac( xFactory, UNO_QUERY );
432 if (xCFac.is())
434 xInstance = xCFac->createInstanceWithContext( xContext );
436 else
438 Reference< XSingleServiceFactory > xSFac( xFactory, UNO_QUERY );
439 if (xSFac.is())
441 out( "\n> warning: ignroing context for implementation \"" );
442 out( rImplName );
443 out( "\"!" );
444 xInstance = xSFac->createInstance();
449 if (! xInstance.is())
451 OUStringBuffer buf( 64 );
452 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("activating component \"") );
453 buf.append( rImplName );
454 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" from location \"") );
455 buf.append( rLocation );
456 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" failed!") );
457 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
460 return xInstance;
462 else
464 OUStringBuffer buf( 64 );
465 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("location \"") );
466 buf.append( rLocation );
467 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" has no extension! Cannot determine loader to be used!") );
468 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
473 //##################################################################################################
474 //##################################################################################################
475 //##################################################################################################
478 //==================================================================================================
479 class OInstanceProvider
480 : public WeakImplHelper1< XInstanceProvider >
482 Reference< XComponentContext > _xContext;
484 Mutex _aSingleInstanceMutex;
485 Reference< XInterface > _xSingleInstance;
486 sal_Bool _bSingleInstance;
488 OUString _aImplName;
489 OUString _aLocation;
490 OUString _aServiceName;
491 Sequence< Any > _aInitParams;
493 OUString _aInstanceName;
495 inline Reference< XInterface > createInstance() throw (Exception);
497 public:
498 OInstanceProvider( const Reference< XComponentContext > & xContext,
499 const OUString & rImplName, const OUString & rLocation,
500 const OUString & rServiceName, const Sequence< Any > & rInitParams,
501 sal_Bool bSingleInstance, const OUString & rInstanceName )
502 : _xContext( xContext )
503 , _bSingleInstance( bSingleInstance )
504 , _aImplName( rImplName )
505 , _aLocation( rLocation )
506 , _aServiceName( rServiceName )
507 , _aInitParams( rInitParams )
508 , _aInstanceName( rInstanceName )
511 // XInstanceProvider
512 virtual Reference< XInterface > SAL_CALL getInstance( const OUString & rName )
513 throw (NoSuchElementException, RuntimeException);
515 //__________________________________________________________________________________________________
516 inline Reference< XInterface > OInstanceProvider::createInstance()
517 throw (Exception)
519 Reference< XInterface > xRet;
520 if (_aImplName.getLength()) // manually via loader
521 xRet = loadComponent( _xContext, _aImplName, _aLocation );
522 else // via service manager
523 unoexe::createInstance( xRet, _xContext, _aServiceName );
525 // opt XInit
526 Reference< XInitialization > xInit( xRet, UNO_QUERY );
527 if (xInit.is())
528 xInit->initialize( _aInitParams );
530 return xRet;
532 //__________________________________________________________________________________________________
533 Reference< XInterface > OInstanceProvider::getInstance( const OUString & rName )
534 throw (NoSuchElementException, RuntimeException)
538 if (_aInstanceName == rName)
540 Reference< XInterface > xRet;
542 if (_aImplName.getLength() == 0 && _aServiceName.getLength() == 0)
544 OSL_ASSERT(
545 rName.equalsAsciiL(
546 RTL_CONSTASCII_STRINGPARAM("uno.ComponentContext") ) );
547 xRet = _xContext;
549 else if (_bSingleInstance)
551 if (! _xSingleInstance.is())
553 MutexGuard aGuard( _aSingleInstanceMutex );
554 if (! _xSingleInstance.is())
556 _xSingleInstance = createInstance();
559 xRet = _xSingleInstance;
561 else
563 xRet = createInstance();
566 return xRet;
569 catch (Exception & rExc)
571 out( "\n> error: " );
572 out( rExc.Message );
574 OUStringBuffer buf( 64 );
575 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("no such element \"") );
576 buf.append( rName );
577 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
578 throw NoSuchElementException( buf.makeStringAndClear(), Reference< XInterface >() );
581 //==================================================================================================
582 struct ODisposingListener : public WeakImplHelper1< XEventListener >
584 Condition cDisposed;
586 // XEventListener
587 virtual void SAL_CALL disposing( const EventObject & rEvt )
588 throw (RuntimeException);
590 //----------------------------------------------------------------------------------------------
591 static void waitFor( const Reference< XComponent > & xComp );
593 //__________________________________________________________________________________________________
594 void ODisposingListener::disposing( const EventObject & )
595 throw (RuntimeException)
597 cDisposed.set();
599 //--------------------------------------------------------------------------------------------------
600 void ODisposingListener::waitFor( const Reference< XComponent > & xComp )
602 ODisposingListener * pListener = new ODisposingListener();
603 Reference< XEventListener > xListener( pListener );
605 xComp->addEventListener( xListener );
606 pListener->cDisposed.wait();
610 //##################################################################################################
611 //##################################################################################################
612 //##################################################################################################
615 //##################################################################################################
616 } // namespace unoexe
618 using namespace unoexe;
620 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,)
622 if (argc <= 1)
624 out( arUsingText );
625 return 0;
628 sal_Int32 nRet = 0;
629 Reference< XComponentContext > xContext;
634 OUString aImplName, aLocation, aServiceName, aUnoUrl;
635 vector< OUString > aReadOnlyRegistries;
636 Sequence< OUString > aParams;
637 sal_Bool bSingleAccept = sal_False;
638 sal_Bool bSingleInstance = sal_False;
640 //#### read command line arguments #########################################################
642 bool bOldRegistryMimic = false;
643 bool bNewRegistryMimic = false;
644 OUString aReadWriteRegistry;
646 sal_Int32 nPos = 0;
647 sal_Int32 nCount = (sal_Int32)rtl_getAppCommandArgCount();
648 // read up to arguments
649 while (nPos < nCount)
651 OUString arg;
653 rtl_getAppCommandArg(nPos, &arg.pData);
655 const OUString dashdash = OUString(RTL_CONSTASCII_USTRINGPARAM("--"));
656 if (dashdash == arg)
658 ++nPos;
659 break;
662 if (readOption( &aImplName, "c", &nPos, arg) ||
663 readOption( &aLocation, "l", &nPos, arg) ||
664 readOption( &aServiceName, "s", &nPos, arg) ||
665 readOption( &aUnoUrl, "u", &nPos, arg) ||
666 readOption( &s_quiet, "quiet", &nPos, arg) ||
667 readOption( &bSingleAccept, "singleaccept", &nPos, arg) ||
668 readOption( &bSingleInstance, "singleinstance", &nPos, arg))
670 continue;
672 OUString aRegistry;
673 if (readOption( &aRegistry, "ro", &nPos, arg))
675 aReadOnlyRegistries.push_back( aRegistry );
676 bNewRegistryMimic = true;
677 continue;
679 if (readOption( &aReadWriteRegistry, "rw", &nPos, arg))
681 bNewRegistryMimic = true;
682 continue;
684 if (readOption( &aRegistry, "r", &nPos, arg))
686 aReadOnlyRegistries.push_back( aRegistry );
687 aReadWriteRegistry = aRegistry;
688 out( "\n> warning: DEPRECATED use of option -r, use -ro or -rw!" );
689 bOldRegistryMimic = true;
690 continue;
693 // else illegal argument
694 OUStringBuffer buf( 64 );
695 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("unexpected parameter \"") );
696 buf.append(arg);
697 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
698 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
701 if (bOldRegistryMimic) // last one was set to be read-write
703 aReadOnlyRegistries.pop_back();
704 if (bOldRegistryMimic && bNewRegistryMimic)
706 throw RuntimeException(
707 OUString( RTL_CONSTASCII_USTRINGPARAM("mixing with DEPRECATED registry options!") ),
708 Reference< XInterface >() );
712 if ((aImplName.getLength() != 0) && (aServiceName.getLength() != 0))
713 throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("give component exOR service name!" ) ), Reference< XInterface >() );
714 if (aImplName.getLength() == 0 && aServiceName.getLength() == 0)
716 if (! aUnoUrl.endsWithIgnoreAsciiCaseAsciiL(
717 RTL_CONSTASCII_STRINGPARAM(";uno.ComponentContext") ))
718 throw RuntimeException(
719 OUString( RTL_CONSTASCII_USTRINGPARAM(
720 "expected UNO-URL with instance name "
721 "uno.ComponentContext!") ),
722 Reference<XInterface>() );
723 if (bSingleInstance)
724 throw RuntimeException(
725 OUString( RTL_CONSTASCII_USTRINGPARAM(
726 "unexpected option --singleinstance!") ),
727 Reference<XInterface>() );
729 if (aImplName.getLength() && !aLocation.getLength())
730 throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("give component location!" ) ), Reference< XInterface >() );
731 if (aServiceName.getLength() && aLocation.getLength())
732 out( "\n> warning: service name given, will ignore location!" );
734 // read component params
735 aParams.realloc( nCount - nPos );
736 OUString * pParams = aParams.getArray();
738 sal_Int32 nOffset = nPos;
739 for ( ; nPos < nCount; ++nPos )
741 if (rtl_getAppCommandArg( nPos, &pParams[nPos -nOffset].pData )
742 != osl_Process_E_None)
744 OSL_ASSERT(false);
748 if (aReadOnlyRegistries.size() > 0 ||
749 aReadWriteRegistry.getLength() > 0)
751 //#### create registry #############################################
753 Reference< XSimpleRegistry > xRegistry;
755 // ReadOnly registries
756 for ( size_t nReg = 0; nReg < aReadOnlyRegistries.size(); ++nReg )
758 #if OSL_DEBUG_LEVEL > 1
759 out( "\n> trying to open ro registry: " );
760 out( OUStringToOString(
761 aReadOnlyRegistries[ nReg ],
762 RTL_TEXTENCODING_ASCII_US ).getStr() );
763 #endif
764 Reference< XSimpleRegistry > xNewReg(
765 openRegistry(
766 aReadOnlyRegistries[ nReg ], sal_True, sal_False ) );
767 if (xNewReg.is())
768 xRegistry = (xRegistry.is() ? nestRegistries(
769 xNewReg, xRegistry ) : xNewReg);
771 if (aReadWriteRegistry.getLength())
773 #if OSL_DEBUG_LEVEL > 1
774 out( "\n> trying to open rw registry: " );
775 out( OUStringToOString(
776 aReadWriteRegistry,
777 RTL_TEXTENCODING_ASCII_US ).getStr() );
778 #endif
779 // ReadWrite registry
780 Reference< XSimpleRegistry > xNewReg(
781 openRegistry( aReadWriteRegistry, sal_False, sal_True ) );
782 if (xNewReg.is())
783 xRegistry = (xRegistry.is()
784 ? nestRegistries( xNewReg, xRegistry )
785 : xNewReg);
788 OSL_ASSERT( xRegistry.is() );
789 xContext = bootstrap_InitialComponentContext( xRegistry );
791 else // defaulting
793 xContext = defaultBootstrap_InitialComponentContext();
796 //#### accept, instanciate, etc. ###########################################################
798 if (aUnoUrl.getLength()) // accepting connections
800 sal_Int32 nIndex = 0, nTokens = 0;
801 do { aUnoUrl.getToken( 0, ';', nIndex ); nTokens++; } while( nIndex != -1 );
802 if (nTokens != 3 || aUnoUrl.getLength() < 10 ||
803 !aUnoUrl.copy( 0, 4 ).equalsIgnoreAsciiCase( OUString( RTL_CONSTASCII_USTRINGPARAM("uno:") ) ))
805 throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("illegal uno url given!" ) ), Reference< XInterface >() );
807 nIndex = 0;
808 OUString aConnectDescr( aUnoUrl.getToken( 0, ';', nIndex ).copy( 4 ) ); // uno:CONNECTDESCR;iiop;InstanceName
809 OUString aInstanceName( aUnoUrl.getToken( 1, ';', nIndex ) );
811 Reference< XAcceptor > xAcceptor;
812 createInstance(
813 xAcceptor, xContext,
814 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor") ) );
816 // init params
817 Sequence< Any > aInitParams( aParams.getLength() );
818 const OUString * p = aParams.getConstArray();
819 Any * pInitParams = aInitParams.getArray();
820 for ( sal_Int32 i = aParams.getLength(); i--; )
822 pInitParams[i] = makeAny( p[i] );
825 // instance provider
826 Reference< XInstanceProvider > xInstanceProvider( new OInstanceProvider(
827 xContext, aImplName, aLocation, aServiceName, aInitParams,
828 bSingleInstance, aInstanceName ) );
830 nIndex = 0;
831 OUString aUnoUrlToken( aUnoUrl.getToken( 1, ';', nIndex ) );
832 for (;;)
834 // accepting
835 out( "\n> accepting " );
836 out( aConnectDescr );
837 out( "..." );
838 Reference< XConnection > xConnection( xAcceptor->accept( aConnectDescr ) );
839 out( "connection established." );
841 Reference< XBridgeFactory > xBridgeFactory;
842 createInstance(
843 xBridgeFactory, xContext,
844 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory") ) );
846 // bridge
847 Reference< XBridge > xBridge( xBridgeFactory->createBridge(
848 OUString(), aUnoUrlToken,
849 xConnection, xInstanceProvider ) );
851 if (bSingleAccept)
853 Reference< XComponent > xComp( xBridge, UNO_QUERY );
854 if (! xComp.is())
855 throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("bridge factory does not export interface \"com.sun.star.lang.XComponent\"!" ) ), Reference< XInterface >() );
856 ODisposingListener::waitFor( xComp );
857 break;
861 else // no uno url
863 Reference< XInterface > xInstance;
864 if (aImplName.getLength()) // manually via loader
865 xInstance = loadComponent( xContext, aImplName, aLocation );
866 else // via service manager
867 createInstance( xInstance, xContext, aServiceName );
869 // execution
870 Reference< XMain > xMain( xInstance, UNO_QUERY );
871 if (xMain.is())
873 nRet = xMain->run( aParams );
875 else
877 Reference< XComponent > xComp( xInstance, UNO_QUERY );
878 if (xComp.is())
879 xComp->dispose();
880 throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("component does not export interface interface \"com.sun.star.lang.XMain\"!" ) ), Reference< XInterface >() );
884 catch (Exception & rExc)
886 out( "\n> error: " );
887 out( rExc.Message );
888 out( "\n> dying..." );
889 nRet = 1;
892 // cleanup
893 Reference< XComponent > xComp( xContext, UNO_QUERY );
894 if (xComp.is())
895 xComp->dispose();
897 out( "\n" );
898 return nRet;