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