xref: /AOO41X/main/cpputools/source/unoexe/unoexe.cxx (revision 01797804fd0d9936c2b7e32adc3163a61a17074f)
1*01797804SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*01797804SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*01797804SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*01797804SAndrew Rist  * distributed with this work for additional information
6*01797804SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*01797804SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*01797804SAndrew Rist  * "License"); you may not use this file except in compliance
9*01797804SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*01797804SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*01797804SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*01797804SAndrew Rist  * software distributed under the License is distributed on an
15*01797804SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*01797804SAndrew Rist  * KIND, either express or implied.  See the License for the
17*01797804SAndrew Rist  * specific language governing permissions and limitations
18*01797804SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*01797804SAndrew Rist  *************************************************************/
21*01797804SAndrew Rist 
22*01797804SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <stdio.h>
25cdf0e10cSrcweir #include <vector>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sal/main.h"
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir #include <osl/mutex.hxx>
30cdf0e10cSrcweir #include <osl/conditn.hxx>
31cdf0e10cSrcweir #include <osl/module.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <rtl/process.h>
34cdf0e10cSrcweir #include <rtl/string.h>
35cdf0e10cSrcweir #include <rtl/strbuf.hxx>
36cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <uno/environment.h>
39cdf0e10cSrcweir #include <uno/mapping.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
42cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
43cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
44cdf0e10cSrcweir #include <cppuhelper/shlib.hxx>
45cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <com/sun/star/lang/XMain.hpp>
48cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
49cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
50cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
51cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
52cdf0e10cSrcweir #include <com/sun/star/lang/XEventListener.hpp>
53cdf0e10cSrcweir #include <com/sun/star/container/XSet.hpp>
54cdf0e10cSrcweir #include <com/sun/star/loader/XImplementationLoader.hpp>
55cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
56cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
57cdf0e10cSrcweir #include <com/sun/star/connection/XAcceptor.hpp>
58cdf0e10cSrcweir #include <com/sun/star/connection/XConnection.hpp>
59cdf0e10cSrcweir #include <com/sun/star/bridge/XBridgeFactory.hpp>
60cdf0e10cSrcweir #include <com/sun/star/bridge/XBridge.hpp>
61cdf0e10cSrcweir #include <osl/process.h>
62cdf0e10cSrcweir #include <osl/thread.h>
63cdf0e10cSrcweir #include <osl/file.hxx>
64cdf0e10cSrcweir 
65cdf0e10cSrcweir #ifdef SAL_UNX
66cdf0e10cSrcweir #define SEPARATOR '/'
67cdf0e10cSrcweir #else
68cdf0e10cSrcweir #define SEPARATOR '\\'
69cdf0e10cSrcweir #endif
70cdf0e10cSrcweir 
71cdf0e10cSrcweir using namespace std;
72cdf0e10cSrcweir using namespace rtl;
73cdf0e10cSrcweir using namespace osl;
74cdf0e10cSrcweir using namespace cppu;
75cdf0e10cSrcweir using namespace com::sun::star::uno;
76cdf0e10cSrcweir using namespace com::sun::star::lang;
77cdf0e10cSrcweir using namespace com::sun::star::loader;
78cdf0e10cSrcweir using namespace com::sun::star::registry;
79cdf0e10cSrcweir using namespace com::sun::star::connection;
80cdf0e10cSrcweir using namespace com::sun::star::bridge;
81cdf0e10cSrcweir using namespace com::sun::star::container;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir namespace unoexe
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 
isFileUrl(const OUString & fileName)86cdf0e10cSrcweir static sal_Bool isFileUrl(const OUString& fileName)
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     if (fileName.indexOf(OUString::createFromAscii("file://")) == 0 )
89cdf0e10cSrcweir         return sal_True;
90cdf0e10cSrcweir     return sal_False;
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
convertToFileUrl(const OUString & fileName)93cdf0e10cSrcweir static OUString convertToFileUrl(const OUString& fileName)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir     if ( isFileUrl(fileName) )
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         return fileName;
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     OUString uUrlFileName;
101cdf0e10cSrcweir     if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         OUString uWorkingDir;
104cdf0e10cSrcweir         if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) {
105cdf0e10cSrcweir             OSL_ASSERT(false);
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir         if (FileBase::getAbsoluteFileURL(uWorkingDir, fileName, uUrlFileName)
108cdf0e10cSrcweir             != FileBase::E_None)
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             OSL_ASSERT(false);
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir     } else
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         if (FileBase::getFileURLFromSystemPath(fileName, uUrlFileName)
115cdf0e10cSrcweir             != FileBase::E_None)
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             OSL_ASSERT(false);
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     return uUrlFileName;
122cdf0e10cSrcweir }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir static sal_Bool s_quiet = false;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
out(const sal_Char * pText)127cdf0e10cSrcweir static inline void out( const sal_Char * pText )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     if (! s_quiet)
130cdf0e10cSrcweir         fprintf( stderr, pText );
131cdf0e10cSrcweir }
132cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
out(const OUString & rText)133cdf0e10cSrcweir static inline void out( const OUString & rText )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     if (! s_quiet)
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         OString aText( OUStringToOString( rText, RTL_TEXTENCODING_ASCII_US ) );
138cdf0e10cSrcweir         fprintf( stderr, aText.getStr() );
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
143cdf0e10cSrcweir static const char arUsingText[] =
144cdf0e10cSrcweir "\nusing:\n\n"
145cdf0e10cSrcweir "uno [-c ComponentImplementationName -l LocationUrl | -s ServiceName]\n"
146cdf0e10cSrcweir "    [-ro ReadOnlyRegistry1] [-ro ReadOnlyRegistry2] ... [-rw ReadWriteRegistry]\n"
147cdf0e10cSrcweir "    [-u uno:(socket[,host=HostName][,port=nnn]|pipe[,name=PipeName]);<protocol>;Name\n"
148cdf0e10cSrcweir "        [--singleaccept] [--singleinstance]]\n"
149cdf0e10cSrcweir "    [--quiet]\n"
150cdf0e10cSrcweir "    [-- Argument1 Argument2 ...]\n";
151cdf0e10cSrcweir 
152cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
readOption(OUString * pValue,const sal_Char * pOpt,sal_Int32 * pnIndex,const OUString & aArg)153cdf0e10cSrcweir static sal_Bool readOption( OUString * pValue, const sal_Char * pOpt,
154cdf0e10cSrcweir 							sal_Int32 * pnIndex, const OUString & aArg)
155cdf0e10cSrcweir 	throw (RuntimeException)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	const OUString dash = OUString(RTL_CONSTASCII_USTRINGPARAM("-"));
158cdf0e10cSrcweir 	if(aArg.indexOf(dash) != 0)
159cdf0e10cSrcweir 		return sal_False;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	OUString aOpt = OUString::createFromAscii( pOpt );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 	if (aArg.getLength() < aOpt.getLength())
164cdf0e10cSrcweir 		return sal_False;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	if (aOpt.equalsIgnoreAsciiCase( aArg.copy(1) ))
167cdf0e10cSrcweir 	{
168cdf0e10cSrcweir 		// take next argument
169cdf0e10cSrcweir 		++(*pnIndex);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 		rtl_getAppCommandArg(*pnIndex, &pValue->pData);
172cdf0e10cSrcweir 		if (*pnIndex >= (sal_Int32)rtl_getAppCommandArgCount() || pValue->copy(1).equals(dash))
173cdf0e10cSrcweir 		{
174cdf0e10cSrcweir 			OUStringBuffer buf( 32 );
175cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("incomplete option \"-") );
176cdf0e10cSrcweir 			buf.appendAscii( pOpt );
177cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" given!") );
178cdf0e10cSrcweir 			throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
179cdf0e10cSrcweir 		}
180cdf0e10cSrcweir 		else
181cdf0e10cSrcweir 		{
182cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
183cdf0e10cSrcweir 			out( "\n> identified option -" );
184cdf0e10cSrcweir 			out( pOpt );
185cdf0e10cSrcweir 			out( " = " );
186cdf0e10cSrcweir 			OString tmp = OUStringToOString(aArg, RTL_TEXTENCODING_ASCII_US);
187cdf0e10cSrcweir   			out( tmp.getStr() );
188cdf0e10cSrcweir #endif
189cdf0e10cSrcweir 			++(*pnIndex);
190cdf0e10cSrcweir 			return sal_True;
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 	}
193cdf0e10cSrcweir   	else if (aArg.indexOf(aOpt) == 1)
194cdf0e10cSrcweir 	{
195cdf0e10cSrcweir 		*pValue = aArg.copy(1 + aOpt.getLength());
196cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
197cdf0e10cSrcweir 		out( "\n> identified option -" );
198cdf0e10cSrcweir 		out( pOpt );
199cdf0e10cSrcweir 		out( " = " );
200cdf0e10cSrcweir 		OString tmp = OUStringToOString(aArg.copy(aOpt.getLength()), RTL_TEXTENCODING_ASCII_US);
201cdf0e10cSrcweir 		out( tmp.getStr() );
202cdf0e10cSrcweir #endif
203cdf0e10cSrcweir 		++(*pnIndex);
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 		return sal_True;
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir 	return sal_False;
208cdf0e10cSrcweir }
209cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
readOption(sal_Bool * pbOpt,const sal_Char * pOpt,sal_Int32 * pnIndex,const OUString & aArg)210cdf0e10cSrcweir static sal_Bool readOption( sal_Bool * pbOpt, const sal_Char * pOpt,
211cdf0e10cSrcweir 							sal_Int32 * pnIndex, const OUString & aArg)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	const OUString dashdash(RTL_CONSTASCII_USTRINGPARAM("--"));
214cdf0e10cSrcweir 	OUString aOpt = OUString::createFromAscii(pOpt);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	if(aArg.indexOf(dashdash) == 0 && aOpt.equals(aArg.copy(2)))
217cdf0e10cSrcweir 	{
218cdf0e10cSrcweir 		++(*pnIndex);
219cdf0e10cSrcweir 		*pbOpt = sal_True;
220cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
221cdf0e10cSrcweir 		out( "\n> identified option --" );
222cdf0e10cSrcweir 		out( pOpt );
223cdf0e10cSrcweir #endif
224cdf0e10cSrcweir 		return sal_True;
225cdf0e10cSrcweir 	}
226cdf0e10cSrcweir 	return sal_False;
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 
230cdf0e10cSrcweir //##################################################################################################
231cdf0e10cSrcweir //##################################################################################################
232cdf0e10cSrcweir //##################################################################################################
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 
235cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
236cdf0e10cSrcweir template< class T >
createInstance(Reference<T> & rxOut,const Reference<XComponentContext> & xContext,const OUString & rServiceName)237cdf0e10cSrcweir void createInstance(
238cdf0e10cSrcweir 	Reference< T > & rxOut,
239cdf0e10cSrcweir 	const Reference< XComponentContext > & xContext,
240cdf0e10cSrcweir 	const OUString & rServiceName )
241cdf0e10cSrcweir 	throw (Exception)
242cdf0e10cSrcweir {
243cdf0e10cSrcweir     Reference< XMultiComponentFactory > xMgr( xContext->getServiceManager() );
244cdf0e10cSrcweir 	Reference< XInterface > x( xMgr->createInstanceWithContext( rServiceName, xContext ) );
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	if (! x.is())
247cdf0e10cSrcweir 	{
248cdf0e10cSrcweir 		static sal_Bool s_bSet = sal_False;
249cdf0e10cSrcweir 		if (! s_bSet)
250cdf0e10cSrcweir 		{
251cdf0e10cSrcweir 			MutexGuard aGuard( Mutex::getGlobalMutex() );
252cdf0e10cSrcweir 			if (! s_bSet)
253cdf0e10cSrcweir 			{
254cdf0e10cSrcweir 				Reference< XSet > xSet( xMgr, UNO_QUERY );
255cdf0e10cSrcweir 				if (xSet.is())
256cdf0e10cSrcweir 				{
257cdf0e10cSrcweir                     Reference< XMultiServiceFactory > xSF( xMgr, UNO_QUERY );
258cdf0e10cSrcweir 					// acceptor
259cdf0e10cSrcweir 					xSet->insert( makeAny( loadSharedLibComponentFactory(
260cdf0e10cSrcweir 						OUString( RTL_CONSTASCII_USTRINGPARAM(
261cdf0e10cSrcweir                                       "acceptor.uno" SAL_DLLEXTENSION) ),
262cdf0e10cSrcweir                         OUString(),
263cdf0e10cSrcweir 						OUString( RTL_CONSTASCII_USTRINGPARAM(
264cdf0e10cSrcweir                                       "com.sun.star.comp.io.Acceptor") ),
265cdf0e10cSrcweir 						xSF, Reference< XRegistryKey >() ) ) );
266cdf0e10cSrcweir 					// connector
267cdf0e10cSrcweir 					xSet->insert( makeAny( loadSharedLibComponentFactory(
268cdf0e10cSrcweir 						OUString( RTL_CONSTASCII_USTRINGPARAM(
269cdf0e10cSrcweir                                       "connector.uno" SAL_DLLEXTENSION) ),
270cdf0e10cSrcweir                         OUString(),
271cdf0e10cSrcweir 						OUString( RTL_CONSTASCII_USTRINGPARAM(
272cdf0e10cSrcweir                                       "com.sun.star.comp.io.Connector") ),
273cdf0e10cSrcweir 						xSF, Reference< XRegistryKey >() ) ) );
274cdf0e10cSrcweir 					// bridge factory
275cdf0e10cSrcweir 					xSet->insert( makeAny( loadSharedLibComponentFactory(
276cdf0e10cSrcweir 						OUString( RTL_CONSTASCII_USTRINGPARAM(
277cdf0e10cSrcweir                                       "binaryurp.uno" SAL_DLLEXTENSION) ),
278cdf0e10cSrcweir                         OUString(),
279cdf0e10cSrcweir 						OUString(
280cdf0e10cSrcweir                             RTL_CONSTASCII_USTRINGPARAM(
281cdf0e10cSrcweir                                 "com.sun.star.comp.bridge.BridgeFactory") ),
282cdf0e10cSrcweir 						xSF, Reference< XRegistryKey >() ) ) );
283cdf0e10cSrcweir 				}
284cdf0e10cSrcweir 				s_bSet = sal_True;
285cdf0e10cSrcweir 			}
286cdf0e10cSrcweir 		}
287cdf0e10cSrcweir 		x = xMgr->createInstanceWithContext( rServiceName, xContext );
288cdf0e10cSrcweir 	}
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 	if (! x.is())
291cdf0e10cSrcweir 	{
292cdf0e10cSrcweir 		OUStringBuffer buf( 64 );
293cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("cannot get service instance \"") );
294cdf0e10cSrcweir 		buf.append( rServiceName );
295cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
296cdf0e10cSrcweir 		throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
297cdf0e10cSrcweir 	}
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 	rxOut = Reference< T >::query( x );
300cdf0e10cSrcweir 	if (! rxOut.is())
301cdf0e10cSrcweir 	{
302cdf0e10cSrcweir 		OUStringBuffer buf( 64 );
303cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("service instance \"") );
304cdf0e10cSrcweir 		buf.append( rServiceName );
305cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" does not support demanded interface \"") );
306cdf0e10cSrcweir 		const Type & rType = ::getCppuType( (const Reference< T > *)0 );
307cdf0e10cSrcweir 		buf.append( rType.getTypeName() );
308cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
309cdf0e10cSrcweir 		throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
310cdf0e10cSrcweir 	}
311cdf0e10cSrcweir }
312cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
nestRegistries(const Reference<XSimpleRegistry> & xReadWrite,const Reference<XSimpleRegistry> & xReadOnly)313cdf0e10cSrcweir static Reference< XSimpleRegistry > nestRegistries(
314cdf0e10cSrcweir 	const Reference< XSimpleRegistry > & xReadWrite,
315cdf0e10cSrcweir 	const Reference< XSimpleRegistry > & xReadOnly )
316cdf0e10cSrcweir 	throw (Exception)
317cdf0e10cSrcweir {
318cdf0e10cSrcweir 	Reference< XSimpleRegistry > xReg( createNestedRegistry() );
319cdf0e10cSrcweir     if (! xReg.is())
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir 		throw RuntimeException(
322cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("no nested registry service!" ) ),
323cdf0e10cSrcweir             Reference< XInterface >() );
324cdf0e10cSrcweir     }
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 	Reference< XInitialization > xInit( xReg, UNO_QUERY );
327cdf0e10cSrcweir 	if (! xInit.is())
328cdf0e10cSrcweir 		throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("nested registry does not export interface \"com.sun.star.lang.XInitialization\"!" ) ), Reference< XInterface >() );
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 	Sequence< Any > aArgs( 2 );
331cdf0e10cSrcweir 	aArgs[0] <<= xReadWrite;
332cdf0e10cSrcweir 	aArgs[1] <<= xReadOnly;
333cdf0e10cSrcweir 	xInit->initialize( aArgs );
334cdf0e10cSrcweir 
335cdf0e10cSrcweir 	return xReg;
336cdf0e10cSrcweir }
337cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
openRegistry(const OUString & rURL,sal_Bool bReadOnly,sal_Bool bCreate)338cdf0e10cSrcweir static Reference< XSimpleRegistry > openRegistry(
339cdf0e10cSrcweir 	const OUString & rURL,
340cdf0e10cSrcweir 	sal_Bool bReadOnly, sal_Bool bCreate )
341cdf0e10cSrcweir 	throw (Exception)
342cdf0e10cSrcweir {
343cdf0e10cSrcweir 	Reference< XSimpleRegistry > xNewReg( createSimpleRegistry() );
344cdf0e10cSrcweir     if (! xNewReg.is())
345cdf0e10cSrcweir     {
346cdf0e10cSrcweir 		throw RuntimeException(
347cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("no simple registry service!" ) ),
348cdf0e10cSrcweir             Reference< XInterface >() );
349cdf0e10cSrcweir     }
350cdf0e10cSrcweir 
351cdf0e10cSrcweir 	try
352cdf0e10cSrcweir 	{
353cdf0e10cSrcweir 		xNewReg->open( convertToFileUrl(rURL), bReadOnly, bCreate );
354cdf0e10cSrcweir 		if (xNewReg->isValid())
355cdf0e10cSrcweir 			return xNewReg;
356cdf0e10cSrcweir 		else
357cdf0e10cSrcweir 			xNewReg->close();
358cdf0e10cSrcweir 	}
359cdf0e10cSrcweir 	catch (Exception &)
360cdf0e10cSrcweir 	{
361cdf0e10cSrcweir 	}
362cdf0e10cSrcweir 
363cdf0e10cSrcweir 	out( "\n> warning: cannot open registry \"" );
364cdf0e10cSrcweir 	out( rURL );
365cdf0e10cSrcweir 	if (bReadOnly)
366cdf0e10cSrcweir 		out( "\" for reading, ignoring!" );
367cdf0e10cSrcweir 	else
368cdf0e10cSrcweir 		out( "\" for reading and writing, ignoring!" );
369cdf0e10cSrcweir 	return Reference< XSimpleRegistry >();
370cdf0e10cSrcweir }
371cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
loadComponent(const Reference<XComponentContext> & xContext,const OUString & rImplName,const OUString & rLocation)372cdf0e10cSrcweir static Reference< XInterface > loadComponent(
373cdf0e10cSrcweir 	const Reference< XComponentContext > & xContext,
374cdf0e10cSrcweir 	const OUString & rImplName, const OUString & rLocation )
375cdf0e10cSrcweir 	throw (Exception)
376cdf0e10cSrcweir {
377cdf0e10cSrcweir 	// determine loader to be used
378cdf0e10cSrcweir 	sal_Int32 nDot = rLocation.lastIndexOf( '.' );
379cdf0e10cSrcweir 	if (nDot > 0 && nDot < rLocation.getLength())
380cdf0e10cSrcweir 	{
381cdf0e10cSrcweir 		Reference< XImplementationLoader > xLoader;
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 		OUString aExt( rLocation.copy( nDot +1 ) );
384cdf0e10cSrcweir 
385cdf0e10cSrcweir 		if (aExt.compareToAscii( "dll" ) == 0 ||
386cdf0e10cSrcweir 			aExt.compareToAscii( "exe" ) == 0 ||
387cdf0e10cSrcweir 			aExt.compareToAscii( "dylib" ) == 0 ||
388cdf0e10cSrcweir 			aExt.compareToAscii( "so" ) == 0)
389cdf0e10cSrcweir 		{
390cdf0e10cSrcweir 			createInstance(
391cdf0e10cSrcweir 				xLoader, xContext, OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary") ) );
392cdf0e10cSrcweir 		}
393cdf0e10cSrcweir 		else if (aExt.compareToAscii( "jar" ) == 0 ||
394cdf0e10cSrcweir 				 aExt.compareToAscii( "class" ) == 0)
395cdf0e10cSrcweir 		{
396cdf0e10cSrcweir 			createInstance(
397cdf0e10cSrcweir 				xLoader, xContext, OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.Java") ) );
398cdf0e10cSrcweir 		}
399cdf0e10cSrcweir 		else
400cdf0e10cSrcweir 		{
401cdf0e10cSrcweir 			OUStringBuffer buf( 64 );
402cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("unknown extension of \"") );
403cdf0e10cSrcweir 			buf.append( rLocation );
404cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!  No loader available!") );
405cdf0e10cSrcweir 			throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
406cdf0e10cSrcweir 		}
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 		Reference< XInterface > xInstance;
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 		// activate
411cdf0e10cSrcweir 		Reference< XInterface > xFactory( xLoader->activate(
412cdf0e10cSrcweir 			rImplName, OUString(), rLocation, Reference< XRegistryKey >() ) );
413cdf0e10cSrcweir 		if (xFactory.is())
414cdf0e10cSrcweir         {
415cdf0e10cSrcweir             Reference< XSingleComponentFactory > xCFac( xFactory, UNO_QUERY );
416cdf0e10cSrcweir             if (xCFac.is())
417cdf0e10cSrcweir             {
418cdf0e10cSrcweir                 xInstance = xCFac->createInstanceWithContext( xContext );
419cdf0e10cSrcweir             }
420cdf0e10cSrcweir             else
421cdf0e10cSrcweir             {
422cdf0e10cSrcweir                 Reference< XSingleServiceFactory > xSFac( xFactory, UNO_QUERY );
423cdf0e10cSrcweir                 if (xSFac.is())
424cdf0e10cSrcweir                 {
425cdf0e10cSrcweir                     out( "\n> warning: ignroing context for implementation \"" );
426cdf0e10cSrcweir                     out( rImplName );
427cdf0e10cSrcweir                     out( "\"!" );
428cdf0e10cSrcweir                     xInstance = xSFac->createInstance();
429cdf0e10cSrcweir                 }
430cdf0e10cSrcweir             }
431cdf0e10cSrcweir         }
432cdf0e10cSrcweir 
433cdf0e10cSrcweir 		if (! xInstance.is())
434cdf0e10cSrcweir 		{
435cdf0e10cSrcweir 			OUStringBuffer buf( 64 );
436cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("activating component \"") );
437cdf0e10cSrcweir 			buf.append( rImplName );
438cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" from location \"") );
439cdf0e10cSrcweir 			buf.append( rLocation );
440cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" failed!") );
441cdf0e10cSrcweir 			throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
442cdf0e10cSrcweir 		}
443cdf0e10cSrcweir 
444cdf0e10cSrcweir 		return xInstance;
445cdf0e10cSrcweir 	}
446cdf0e10cSrcweir 	else
447cdf0e10cSrcweir 	{
448cdf0e10cSrcweir 		OUStringBuffer buf( 64 );
449cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("location \"") );
450cdf0e10cSrcweir 		buf.append( rLocation );
451cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" has no extension!  Cannot determine loader to be used!") );
452cdf0e10cSrcweir 		throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
453cdf0e10cSrcweir 	}
454cdf0e10cSrcweir }
455cdf0e10cSrcweir 
456cdf0e10cSrcweir 
457cdf0e10cSrcweir //##################################################################################################
458cdf0e10cSrcweir //##################################################################################################
459cdf0e10cSrcweir //##################################################################################################
460cdf0e10cSrcweir 
461cdf0e10cSrcweir 
462cdf0e10cSrcweir //==================================================================================================
463cdf0e10cSrcweir class OInstanceProvider
464cdf0e10cSrcweir 	: public WeakImplHelper1< XInstanceProvider >
465cdf0e10cSrcweir {
466cdf0e10cSrcweir 	Reference< XComponentContext > _xContext;
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 	Mutex							  _aSingleInstanceMutex;
469cdf0e10cSrcweir 	Reference< XInterface >			  _xSingleInstance;
470cdf0e10cSrcweir 	sal_Bool						  _bSingleInstance;
471cdf0e10cSrcweir 
472cdf0e10cSrcweir 	OUString						  _aImplName;
473cdf0e10cSrcweir 	OUString						  _aLocation;
474cdf0e10cSrcweir 	OUString						  _aServiceName;
475cdf0e10cSrcweir 	Sequence< Any >					  _aInitParams;
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 	OUString						  _aInstanceName;
478cdf0e10cSrcweir 
479cdf0e10cSrcweir 	inline Reference< XInterface > createInstance() throw (Exception);
480cdf0e10cSrcweir 
481cdf0e10cSrcweir 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)482cdf0e10cSrcweir 	OInstanceProvider( const Reference< XComponentContext > & xContext,
483cdf0e10cSrcweir 					   const OUString & rImplName, const OUString & rLocation,
484cdf0e10cSrcweir 					   const OUString & rServiceName, const Sequence< Any > & rInitParams,
485cdf0e10cSrcweir 					   sal_Bool bSingleInstance, const OUString & rInstanceName )
486cdf0e10cSrcweir 		: _xContext( xContext )
487cdf0e10cSrcweir 		, _bSingleInstance( bSingleInstance )
488cdf0e10cSrcweir 		, _aImplName( rImplName )
489cdf0e10cSrcweir 		, _aLocation( rLocation )
490cdf0e10cSrcweir 		, _aServiceName( rServiceName )
491cdf0e10cSrcweir 		, _aInitParams( rInitParams )
492cdf0e10cSrcweir 		, _aInstanceName( rInstanceName )
493cdf0e10cSrcweir 		{}
494cdf0e10cSrcweir 
495cdf0e10cSrcweir 	// XInstanceProvider
496cdf0e10cSrcweir 	virtual Reference< XInterface > SAL_CALL getInstance( const OUString & rName )
497cdf0e10cSrcweir 		throw (NoSuchElementException, RuntimeException);
498cdf0e10cSrcweir };
499cdf0e10cSrcweir //__________________________________________________________________________________________________
createInstance()500cdf0e10cSrcweir inline Reference< XInterface > OInstanceProvider::createInstance()
501cdf0e10cSrcweir 	throw (Exception)
502cdf0e10cSrcweir {
503cdf0e10cSrcweir 	Reference< XInterface > xRet;
504cdf0e10cSrcweir 	if (_aImplName.getLength()) // manually via loader
505cdf0e10cSrcweir 		xRet = loadComponent( _xContext, _aImplName, _aLocation );
506cdf0e10cSrcweir 	else // via service manager
507cdf0e10cSrcweir 		unoexe::createInstance( xRet, _xContext, _aServiceName );
508cdf0e10cSrcweir 
509cdf0e10cSrcweir 	// opt XInit
510cdf0e10cSrcweir 	Reference< XInitialization > xInit( xRet, UNO_QUERY );
511cdf0e10cSrcweir 	if (xInit.is())
512cdf0e10cSrcweir 		xInit->initialize( _aInitParams );
513cdf0e10cSrcweir 
514cdf0e10cSrcweir 	return xRet;
515cdf0e10cSrcweir }
516cdf0e10cSrcweir //__________________________________________________________________________________________________
getInstance(const OUString & rName)517cdf0e10cSrcweir Reference< XInterface > OInstanceProvider::getInstance( const OUString & rName )
518cdf0e10cSrcweir 	throw (NoSuchElementException, RuntimeException)
519cdf0e10cSrcweir {
520cdf0e10cSrcweir 	try
521cdf0e10cSrcweir 	{
522cdf0e10cSrcweir 		if (_aInstanceName == rName)
523cdf0e10cSrcweir 		{
524cdf0e10cSrcweir 			Reference< XInterface > xRet;
525cdf0e10cSrcweir 
526cdf0e10cSrcweir             if (_aImplName.getLength() == 0 && _aServiceName.getLength() == 0)
527cdf0e10cSrcweir             {
528cdf0e10cSrcweir                 OSL_ASSERT(
529cdf0e10cSrcweir                     rName.equalsAsciiL(
530cdf0e10cSrcweir                         RTL_CONSTASCII_STRINGPARAM("uno.ComponentContext") ) );
531cdf0e10cSrcweir                 xRet = _xContext;
532cdf0e10cSrcweir             }
533cdf0e10cSrcweir 			else if (_bSingleInstance)
534cdf0e10cSrcweir 			{
535cdf0e10cSrcweir 				if (! _xSingleInstance.is())
536cdf0e10cSrcweir 				{
537cdf0e10cSrcweir 					MutexGuard aGuard( _aSingleInstanceMutex );
538cdf0e10cSrcweir 					if (! _xSingleInstance.is())
539cdf0e10cSrcweir 					{
540cdf0e10cSrcweir 						_xSingleInstance = createInstance();
541cdf0e10cSrcweir 					}
542cdf0e10cSrcweir 				}
543cdf0e10cSrcweir 				xRet = _xSingleInstance;
544cdf0e10cSrcweir 			}
545cdf0e10cSrcweir 			else
546cdf0e10cSrcweir 			{
547cdf0e10cSrcweir 				xRet = createInstance();
548cdf0e10cSrcweir 			}
549cdf0e10cSrcweir 
550cdf0e10cSrcweir 			return xRet;
551cdf0e10cSrcweir 		}
552cdf0e10cSrcweir 	}
553cdf0e10cSrcweir 	catch (Exception & rExc)
554cdf0e10cSrcweir 	{
555cdf0e10cSrcweir 		out( "\n> error: " );
556cdf0e10cSrcweir 		out( rExc.Message );
557cdf0e10cSrcweir 	}
558cdf0e10cSrcweir 	OUStringBuffer buf( 64 );
559cdf0e10cSrcweir 	buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("no such element \"") );
560cdf0e10cSrcweir 	buf.append( rName );
561cdf0e10cSrcweir 	buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
562cdf0e10cSrcweir 	throw NoSuchElementException( buf.makeStringAndClear(), Reference< XInterface >() );
563cdf0e10cSrcweir }
564cdf0e10cSrcweir 
565cdf0e10cSrcweir //==================================================================================================
566cdf0e10cSrcweir struct ODisposingListener : public WeakImplHelper1< XEventListener >
567cdf0e10cSrcweir {
568cdf0e10cSrcweir 	Condition cDisposed;
569cdf0e10cSrcweir 
570cdf0e10cSrcweir 	// XEventListener
571cdf0e10cSrcweir 	virtual void SAL_CALL disposing( const EventObject & rEvt )
572cdf0e10cSrcweir 		throw (RuntimeException);
573cdf0e10cSrcweir 
574cdf0e10cSrcweir 	//----------------------------------------------------------------------------------------------
575cdf0e10cSrcweir 	static void waitFor( const Reference< XComponent > & xComp );
576cdf0e10cSrcweir };
577cdf0e10cSrcweir //__________________________________________________________________________________________________
disposing(const EventObject &)578cdf0e10cSrcweir void ODisposingListener::disposing( const EventObject & )
579cdf0e10cSrcweir 	throw (RuntimeException)
580cdf0e10cSrcweir {
581cdf0e10cSrcweir 	cDisposed.set();
582cdf0e10cSrcweir }
583cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
waitFor(const Reference<XComponent> & xComp)584cdf0e10cSrcweir void ODisposingListener::waitFor( const Reference< XComponent > & xComp )
585cdf0e10cSrcweir {
586cdf0e10cSrcweir 	ODisposingListener * pListener = new ODisposingListener();
587cdf0e10cSrcweir 	Reference< XEventListener > xListener( pListener );
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 	xComp->addEventListener( xListener );
590cdf0e10cSrcweir 	pListener->cDisposed.wait();
591cdf0e10cSrcweir }
592cdf0e10cSrcweir 
593cdf0e10cSrcweir 
594cdf0e10cSrcweir //##################################################################################################
595cdf0e10cSrcweir //##################################################################################################
596cdf0e10cSrcweir //##################################################################################################
597cdf0e10cSrcweir 
598cdf0e10cSrcweir 
599cdf0e10cSrcweir //##################################################################################################
600cdf0e10cSrcweir } // namespace unoexe
601cdf0e10cSrcweir 
602cdf0e10cSrcweir using namespace unoexe;
603cdf0e10cSrcweir 
604cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,)
605cdf0e10cSrcweir {
606cdf0e10cSrcweir 	if (argc <= 1)
607cdf0e10cSrcweir 	{
608cdf0e10cSrcweir 		out( arUsingText );
609cdf0e10cSrcweir 		return 0;
610cdf0e10cSrcweir 	}
611cdf0e10cSrcweir 
612cdf0e10cSrcweir 	sal_Int32 nRet = 0;
613cdf0e10cSrcweir 	Reference< XComponentContext > xContext;
614cdf0e10cSrcweir 
615cdf0e10cSrcweir 
616cdf0e10cSrcweir 	try
617cdf0e10cSrcweir 	{
618cdf0e10cSrcweir 		OUString aImplName, aLocation, aServiceName, aUnoUrl;
619cdf0e10cSrcweir 		vector< OUString > aReadOnlyRegistries;
620cdf0e10cSrcweir 		Sequence< OUString > aParams;
621cdf0e10cSrcweir 		sal_Bool bSingleAccept = sal_False;
622cdf0e10cSrcweir 		sal_Bool bSingleInstance = sal_False;
623cdf0e10cSrcweir 
624cdf0e10cSrcweir 		//#### read command line arguments #########################################################
625cdf0e10cSrcweir 
626cdf0e10cSrcweir 		bool bOldRegistryMimic = false;
627cdf0e10cSrcweir 		bool bNewRegistryMimic = false;
628cdf0e10cSrcweir 		OUString aReadWriteRegistry;
629cdf0e10cSrcweir 
630cdf0e10cSrcweir 		sal_Int32 nPos = 0;
631cdf0e10cSrcweir         sal_Int32 nCount = (sal_Int32)rtl_getAppCommandArgCount();
632cdf0e10cSrcweir 		// read up to arguments
633cdf0e10cSrcweir 		while (nPos < nCount)
634cdf0e10cSrcweir 		{
635cdf0e10cSrcweir 			OUString arg;
636cdf0e10cSrcweir 
637cdf0e10cSrcweir 			rtl_getAppCommandArg(nPos, &arg.pData);
638cdf0e10cSrcweir 
639cdf0e10cSrcweir 			const OUString dashdash = OUString(RTL_CONSTASCII_USTRINGPARAM("--"));
640cdf0e10cSrcweir 			if (dashdash == arg)
641cdf0e10cSrcweir 			{
642cdf0e10cSrcweir 				++nPos;
643cdf0e10cSrcweir 				break;
644cdf0e10cSrcweir 			}
645cdf0e10cSrcweir 
646cdf0e10cSrcweir 			if (readOption( &aImplName, "c", &nPos, arg)				||
647cdf0e10cSrcweir 				readOption( &aLocation, "l", &nPos, arg)				||
648cdf0e10cSrcweir 				readOption( &aServiceName, "s", &nPos, arg)				||
649cdf0e10cSrcweir 				readOption( &aUnoUrl, "u", &nPos, arg)					||
650cdf0e10cSrcweir 				readOption( &s_quiet, "quiet", &nPos, arg)              ||
651cdf0e10cSrcweir 				readOption( &bSingleAccept, "singleaccept", &nPos, arg)	||
652cdf0e10cSrcweir 				readOption( &bSingleInstance, "singleinstance", &nPos, arg))
653cdf0e10cSrcweir 			{
654cdf0e10cSrcweir 				continue;
655cdf0e10cSrcweir 			}
656cdf0e10cSrcweir 			OUString aRegistry;
657cdf0e10cSrcweir 			if (readOption( &aRegistry, "ro", &nPos, arg))
658cdf0e10cSrcweir 			{
659cdf0e10cSrcweir 				aReadOnlyRegistries.push_back( aRegistry );
660cdf0e10cSrcweir 				bNewRegistryMimic = true;
661cdf0e10cSrcweir 				continue;
662cdf0e10cSrcweir 			}
663cdf0e10cSrcweir 			if (readOption( &aReadWriteRegistry, "rw", &nPos, arg))
664cdf0e10cSrcweir 			{
665cdf0e10cSrcweir 				bNewRegistryMimic = true;
666cdf0e10cSrcweir 				continue;
667cdf0e10cSrcweir 			}
668cdf0e10cSrcweir 			if (readOption( &aRegistry, "r", &nPos, arg))
669cdf0e10cSrcweir 			{
670cdf0e10cSrcweir 				aReadOnlyRegistries.push_back( aRegistry );
671cdf0e10cSrcweir 				aReadWriteRegistry = aRegistry;
672cdf0e10cSrcweir 				out( "\n> warning: DEPRECATED use of option -r, use -ro or -rw!" );
673cdf0e10cSrcweir 				bOldRegistryMimic = true;
674cdf0e10cSrcweir 				continue;
675cdf0e10cSrcweir 			}
676cdf0e10cSrcweir 
677cdf0e10cSrcweir 			// else illegal argument
678cdf0e10cSrcweir 			OUStringBuffer buf( 64 );
679cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("unexpected parameter \"") );
680cdf0e10cSrcweir 			buf.append(arg);
681cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
682cdf0e10cSrcweir 			throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
683cdf0e10cSrcweir 		}
684cdf0e10cSrcweir 
685cdf0e10cSrcweir 		if (bOldRegistryMimic) // last one was set to be read-write
686cdf0e10cSrcweir 		{
687cdf0e10cSrcweir 			aReadOnlyRegistries.pop_back();
688cdf0e10cSrcweir 			if (bOldRegistryMimic && bNewRegistryMimic)
689cdf0e10cSrcweir 			{
690cdf0e10cSrcweir 				throw RuntimeException(
691cdf0e10cSrcweir 					OUString( RTL_CONSTASCII_USTRINGPARAM("mixing with DEPRECATED registry options!") ),
692cdf0e10cSrcweir 					Reference< XInterface >() );
693cdf0e10cSrcweir 			}
694cdf0e10cSrcweir 		}
695cdf0e10cSrcweir 
696cdf0e10cSrcweir 		if ((aImplName.getLength() != 0) && (aServiceName.getLength() != 0))
697cdf0e10cSrcweir 			throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("give component exOR service name!" ) ), Reference< XInterface >() );
698cdf0e10cSrcweir         if (aImplName.getLength() == 0 && aServiceName.getLength() == 0)
699cdf0e10cSrcweir         {
700cdf0e10cSrcweir             if (! aUnoUrl.endsWithIgnoreAsciiCaseAsciiL(
701cdf0e10cSrcweir                     RTL_CONSTASCII_STRINGPARAM(";uno.ComponentContext") ))
702cdf0e10cSrcweir                 throw RuntimeException(
703cdf0e10cSrcweir                     OUString( RTL_CONSTASCII_USTRINGPARAM(
704cdf0e10cSrcweir                                   "expected UNO-URL with instance name "
705cdf0e10cSrcweir                                   "uno.ComponentContext!") ),
706cdf0e10cSrcweir                     Reference<XInterface>() );
707cdf0e10cSrcweir             if (bSingleInstance)
708cdf0e10cSrcweir                 throw RuntimeException(
709cdf0e10cSrcweir                     OUString( RTL_CONSTASCII_USTRINGPARAM(
710cdf0e10cSrcweir                                   "unexpected option --singleinstance!") ),
711cdf0e10cSrcweir                     Reference<XInterface>() );
712cdf0e10cSrcweir         }
713cdf0e10cSrcweir 		if (aImplName.getLength() && !aLocation.getLength())
714cdf0e10cSrcweir 			throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("give component location!" ) ), Reference< XInterface >() );
715cdf0e10cSrcweir 		if (aServiceName.getLength() && aLocation.getLength())
716cdf0e10cSrcweir 			out( "\n> warning: service name given, will ignore location!" );
717cdf0e10cSrcweir 
718cdf0e10cSrcweir 		// read component params
719cdf0e10cSrcweir 		aParams.realloc( nCount - nPos );
720cdf0e10cSrcweir 		OUString * pParams = aParams.getArray();
721cdf0e10cSrcweir 
722cdf0e10cSrcweir 		sal_Int32 nOffset = nPos;
723cdf0e10cSrcweir 		for ( ; nPos < nCount; ++nPos )
724cdf0e10cSrcweir 		{
725cdf0e10cSrcweir             if (rtl_getAppCommandArg( nPos, &pParams[nPos -nOffset].pData )
726cdf0e10cSrcweir                 != osl_Process_E_None)
727cdf0e10cSrcweir             {
728cdf0e10cSrcweir                 OSL_ASSERT(false);
729cdf0e10cSrcweir             }
730cdf0e10cSrcweir 		}
731cdf0e10cSrcweir 
732cdf0e10cSrcweir         if (aReadOnlyRegistries.size() > 0 ||
733cdf0e10cSrcweir             aReadWriteRegistry.getLength() > 0)
734cdf0e10cSrcweir         {
735cdf0e10cSrcweir             //#### create registry #############################################
736cdf0e10cSrcweir 
737cdf0e10cSrcweir             Reference< XSimpleRegistry > xRegistry;
738cdf0e10cSrcweir 
739cdf0e10cSrcweir             // ReadOnly registries
740cdf0e10cSrcweir             for ( size_t nReg = 0; nReg < aReadOnlyRegistries.size(); ++nReg )
741cdf0e10cSrcweir             {
742cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
743cdf0e10cSrcweir                 out( "\n> trying to open ro registry: " );
744cdf0e10cSrcweir                 out( OUStringToOString(
745cdf0e10cSrcweir                          aReadOnlyRegistries[ nReg ],
746cdf0e10cSrcweir                          RTL_TEXTENCODING_ASCII_US ).getStr() );
747cdf0e10cSrcweir #endif
748cdf0e10cSrcweir                 Reference< XSimpleRegistry > xNewReg(
749cdf0e10cSrcweir                     openRegistry(
750cdf0e10cSrcweir                         aReadOnlyRegistries[ nReg ], sal_True, sal_False ) );
751cdf0e10cSrcweir                 if (xNewReg.is())
752cdf0e10cSrcweir                     xRegistry = (xRegistry.is() ? nestRegistries(
753cdf0e10cSrcweir                                      xNewReg, xRegistry ) : xNewReg);
754cdf0e10cSrcweir             }
755cdf0e10cSrcweir             if (aReadWriteRegistry.getLength())
756cdf0e10cSrcweir             {
757cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
758cdf0e10cSrcweir                 out( "\n> trying to open rw registry: " );
759cdf0e10cSrcweir                 out( OUStringToOString(
760cdf0e10cSrcweir                          aReadWriteRegistry,
761cdf0e10cSrcweir                          RTL_TEXTENCODING_ASCII_US ).getStr() );
762cdf0e10cSrcweir #endif
763cdf0e10cSrcweir                 // ReadWrite registry
764cdf0e10cSrcweir                 Reference< XSimpleRegistry > xNewReg(
765cdf0e10cSrcweir                     openRegistry( aReadWriteRegistry, sal_False, sal_True ) );
766cdf0e10cSrcweir                 if (xNewReg.is())
767cdf0e10cSrcweir                     xRegistry = (xRegistry.is()
768cdf0e10cSrcweir                                  ? nestRegistries( xNewReg, xRegistry )
769cdf0e10cSrcweir                                  : xNewReg);
770cdf0e10cSrcweir             }
771cdf0e10cSrcweir 
772cdf0e10cSrcweir             OSL_ASSERT( xRegistry.is() );
773cdf0e10cSrcweir             xContext = bootstrap_InitialComponentContext( xRegistry );
774cdf0e10cSrcweir         }
775cdf0e10cSrcweir         else // defaulting
776cdf0e10cSrcweir         {
777cdf0e10cSrcweir             xContext = defaultBootstrap_InitialComponentContext();
778cdf0e10cSrcweir         }
779cdf0e10cSrcweir 
780cdf0e10cSrcweir 		//#### accept, instanciate, etc. ###########################################################
781cdf0e10cSrcweir 
782cdf0e10cSrcweir 		if (aUnoUrl.getLength()) // accepting connections
783cdf0e10cSrcweir 		{
784cdf0e10cSrcweir             sal_Int32 nIndex = 0, nTokens = 0;
785cdf0e10cSrcweir             do { aUnoUrl.getToken( 0, ';', nIndex ); nTokens++; } while( nIndex != -1 );
786cdf0e10cSrcweir 			if (nTokens != 3 || aUnoUrl.getLength() < 10 ||
787cdf0e10cSrcweir 				!aUnoUrl.copy( 0, 4 ).equalsIgnoreAsciiCase( OUString( RTL_CONSTASCII_USTRINGPARAM("uno:") ) ))
788cdf0e10cSrcweir 			{
789cdf0e10cSrcweir 				throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("illegal uno url given!" ) ), Reference< XInterface >() );
790cdf0e10cSrcweir 			}
791cdf0e10cSrcweir             nIndex = 0;
792cdf0e10cSrcweir 			OUString aConnectDescr( aUnoUrl.getToken( 0, ';', nIndex ).copy( 4 ) ); // uno:CONNECTDESCR;iiop;InstanceName
793cdf0e10cSrcweir 			OUString aInstanceName( aUnoUrl.getToken( 1, ';', nIndex ) );
794cdf0e10cSrcweir 
795cdf0e10cSrcweir 			Reference< XAcceptor > xAcceptor;
796cdf0e10cSrcweir 			createInstance(
797cdf0e10cSrcweir 				xAcceptor, xContext,
798cdf0e10cSrcweir 				OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor") ) );
799cdf0e10cSrcweir 
800cdf0e10cSrcweir 			// init params
801cdf0e10cSrcweir 			Sequence< Any > aInitParams( aParams.getLength() );
802cdf0e10cSrcweir 			const OUString * p = aParams.getConstArray();
803cdf0e10cSrcweir 			Any * pInitParams = aInitParams.getArray();
804cdf0e10cSrcweir 			for ( sal_Int32 i = aParams.getLength(); i--; )
805cdf0e10cSrcweir 			{
806cdf0e10cSrcweir 				pInitParams[i] = makeAny( p[i] );
807cdf0e10cSrcweir 			}
808cdf0e10cSrcweir 
809cdf0e10cSrcweir 			// instance provider
810cdf0e10cSrcweir 			Reference< XInstanceProvider > xInstanceProvider( new OInstanceProvider(
811cdf0e10cSrcweir 				xContext, aImplName, aLocation, aServiceName, aInitParams,
812cdf0e10cSrcweir 				bSingleInstance, aInstanceName ) );
813cdf0e10cSrcweir 
814cdf0e10cSrcweir             nIndex = 0;
815cdf0e10cSrcweir             OUString aUnoUrlToken( aUnoUrl.getToken( 1, ';', nIndex ) );
816cdf0e10cSrcweir 			for (;;)
817cdf0e10cSrcweir 			{
818cdf0e10cSrcweir 				// accepting
819cdf0e10cSrcweir 				out( "\n> accepting " );
820cdf0e10cSrcweir 				out( aConnectDescr );
821cdf0e10cSrcweir 				out( "..." );
822cdf0e10cSrcweir 				Reference< XConnection > xConnection( xAcceptor->accept( aConnectDescr ) );
823cdf0e10cSrcweir 				out( "connection established." );
824cdf0e10cSrcweir 
825cdf0e10cSrcweir 				Reference< XBridgeFactory > xBridgeFactory;
826cdf0e10cSrcweir 				createInstance(
827cdf0e10cSrcweir 					xBridgeFactory, xContext,
828cdf0e10cSrcweir 					OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory") ) );
829cdf0e10cSrcweir 
830cdf0e10cSrcweir 				// bridge
831cdf0e10cSrcweir 				Reference< XBridge > xBridge( xBridgeFactory->createBridge(
832cdf0e10cSrcweir 					OUString(), aUnoUrlToken,
833cdf0e10cSrcweir 					xConnection, xInstanceProvider ) );
834cdf0e10cSrcweir 
835cdf0e10cSrcweir 				if (bSingleAccept)
836cdf0e10cSrcweir 				{
837cdf0e10cSrcweir 					Reference< XComponent > xComp( xBridge, UNO_QUERY );
838cdf0e10cSrcweir 					if (! xComp.is())
839cdf0e10cSrcweir 						throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("bridge factory does not export interface \"com.sun.star.lang.XComponent\"!" ) ), Reference< XInterface >() );
840cdf0e10cSrcweir 					ODisposingListener::waitFor( xComp );
841cdf0e10cSrcweir 					break;
842cdf0e10cSrcweir 				}
843cdf0e10cSrcweir 			}
844cdf0e10cSrcweir 		}
845cdf0e10cSrcweir 		else // no uno url
846cdf0e10cSrcweir 		{
847cdf0e10cSrcweir 			Reference< XInterface > xInstance;
848cdf0e10cSrcweir 			if (aImplName.getLength()) // manually via loader
849cdf0e10cSrcweir 				xInstance = loadComponent( xContext, aImplName, aLocation );
850cdf0e10cSrcweir 			else // via service manager
851cdf0e10cSrcweir 				createInstance( xInstance, xContext, aServiceName );
852cdf0e10cSrcweir 
853cdf0e10cSrcweir 			// execution
854cdf0e10cSrcweir 			Reference< XMain > xMain( xInstance, UNO_QUERY );
855cdf0e10cSrcweir 			if (xMain.is())
856cdf0e10cSrcweir 			{
857cdf0e10cSrcweir 				nRet = xMain->run( aParams );
858cdf0e10cSrcweir 			}
859cdf0e10cSrcweir 			else
860cdf0e10cSrcweir 			{
861cdf0e10cSrcweir 				Reference< XComponent > xComp( xInstance, UNO_QUERY );
862cdf0e10cSrcweir 				if (xComp.is())
863cdf0e10cSrcweir 					xComp->dispose();
864cdf0e10cSrcweir 				throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("component does not export interface interface \"com.sun.star.lang.XMain\"!" ) ), Reference< XInterface >() );
865cdf0e10cSrcweir 			}
866cdf0e10cSrcweir 		}
867cdf0e10cSrcweir 	}
868cdf0e10cSrcweir 	catch (Exception & rExc)
869cdf0e10cSrcweir 	{
870cdf0e10cSrcweir 		out( "\n> error: " );
871cdf0e10cSrcweir 		out( rExc.Message );
872cdf0e10cSrcweir 		out( "\n> dying..." );
873cdf0e10cSrcweir 		nRet = 1;
874cdf0e10cSrcweir 	}
875cdf0e10cSrcweir 
876cdf0e10cSrcweir 	// cleanup
877cdf0e10cSrcweir     Reference< XComponent > xComp( xContext, UNO_QUERY );
878cdf0e10cSrcweir     if (xComp.is())
879cdf0e10cSrcweir         xComp->dispose();
880cdf0e10cSrcweir 
881cdf0e10cSrcweir 	out( "\n" );
882cdf0e10cSrcweir 	return nRet;
883cdf0e10cSrcweir }
884cdf0e10cSrcweir 
885cdf0e10cSrcweir 
886cdf0e10cSrcweir 
887