xref: /trunk/main/cppuhelper/source/bootstrap.cxx (revision cdf0e10c)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <string.h>
32*cdf0e10cSrcweir #include <vector>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include "rtl/process.h"
35*cdf0e10cSrcweir #include "rtl/bootstrap.hxx"
36*cdf0e10cSrcweir #include "rtl/random.h"
37*cdf0e10cSrcweir #include "rtl/string.hxx"
38*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
39*cdf0e10cSrcweir #include "rtl/uri.hxx"
40*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
41*cdf0e10cSrcweir #include "rtl/strbuf.hxx"
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir #include "osl/diagnose.h"
44*cdf0e10cSrcweir #include "osl/file.hxx"
45*cdf0e10cSrcweir #include "osl/module.hxx"
46*cdf0e10cSrcweir #include "osl/security.hxx"
47*cdf0e10cSrcweir #include "osl/thread.hxx"
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include "cppuhelper/shlib.hxx"
50*cdf0e10cSrcweir #include "cppuhelper/bootstrap.hxx"
51*cdf0e10cSrcweir #include "cppuhelper/component_context.hxx"
52*cdf0e10cSrcweir #include "cppuhelper/access_control.hxx"
53*cdf0e10cSrcweir #include "cppuhelper/findsofficepath.h"
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
56*cdf0e10cSrcweir #include "com/sun/star/uno/XCurrentContext.hpp"
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir #include "com/sun/star/lang/XSingleServiceFactory.hpp"
59*cdf0e10cSrcweir #include "com/sun/star/lang/XSingleComponentFactory.hpp"
60*cdf0e10cSrcweir #include "com/sun/star/lang/XInitialization.hpp"
61*cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
62*cdf0e10cSrcweir #include "com/sun/star/registry/XSimpleRegistry.hpp"
63*cdf0e10cSrcweir #include "com/sun/star/container/XSet.hpp"
64*cdf0e10cSrcweir #include "com/sun/star/beans/PropertyValue.hpp"
65*cdf0e10cSrcweir #include "com/sun/star/io/IOException.hpp"
66*cdf0e10cSrcweir #include "com/sun/star/bridge/UnoUrlResolver.hpp"
67*cdf0e10cSrcweir #include "com/sun/star/bridge/XUnoUrlResolver.hpp"
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir #include "macro_expander.hxx"
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
72*cdf0e10cSrcweir #define ARLEN(x) sizeof (x) / sizeof *(x)
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir using namespace ::rtl;
76*cdf0e10cSrcweir using namespace ::osl;
77*cdf0e10cSrcweir using namespace ::com::sun::star;
78*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir namespace cppu
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir OUString const & get_this_libpath()
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir     static OUString s_path;
86*cdf0e10cSrcweir     if (0 == s_path.getLength())
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         OUString path;
89*cdf0e10cSrcweir         Module::getUrlFromAddress( reinterpret_cast<oslGenericFunction>(get_this_libpath), path );
90*cdf0e10cSrcweir         path = path.copy( 0, path.lastIndexOf( '/' ) );
91*cdf0e10cSrcweir         MutexGuard guard( Mutex::getGlobalMutex() );
92*cdf0e10cSrcweir         if (0 == s_path.getLength())
93*cdf0e10cSrcweir             s_path = path;
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir     return s_path;
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir Bootstrap const & get_unorc() SAL_THROW( () )
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir     static rtlBootstrapHandle s_bstrap = 0;
101*cdf0e10cSrcweir     if (! s_bstrap)
102*cdf0e10cSrcweir     {
103*cdf0e10cSrcweir         OUString iniName(
104*cdf0e10cSrcweir             get_this_libpath() + OUSTR("/" SAL_CONFIGFILE("uno")) );
105*cdf0e10cSrcweir         rtlBootstrapHandle bstrap = rtl_bootstrap_args_open( iniName.pData );
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir         ClearableMutexGuard guard( Mutex::getGlobalMutex() );
108*cdf0e10cSrcweir         if (s_bstrap)
109*cdf0e10cSrcweir         {
110*cdf0e10cSrcweir             guard.clear();
111*cdf0e10cSrcweir             rtl_bootstrap_args_close( bstrap );
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir         else
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             s_bstrap = bstrap;
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir     return *(Bootstrap const *)&s_bstrap;
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir void addFactories(
123*cdf0e10cSrcweir     char const * const * ppNames /* lib, implname, ..., 0 */,
124*cdf0e10cSrcweir     OUString const & bootstrapPath,
125*cdf0e10cSrcweir     Reference< lang::XMultiComponentFactory > const & xMgr,
126*cdf0e10cSrcweir     Reference< registry::XRegistryKey > const & xKey )
127*cdf0e10cSrcweir     SAL_THROW( (Exception) )
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir     Reference< container::XSet > xSet( xMgr, UNO_QUERY );
130*cdf0e10cSrcweir     OSL_ASSERT( xSet.is() );
131*cdf0e10cSrcweir     Reference< lang::XMultiServiceFactory > xSF( xMgr, UNO_QUERY );
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     while (*ppNames)
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir         OUString lib( OUString::createFromAscii( *ppNames++ ) );
136*cdf0e10cSrcweir         OUString implName( OUString::createFromAscii( *ppNames++ ) );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         Any aFac( makeAny( loadSharedLibComponentFactory(
139*cdf0e10cSrcweir                                lib, bootstrapPath, implName, xSF, xKey ) ) );
140*cdf0e10cSrcweir         xSet->insert( aFac );
141*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
142*cdf0e10cSrcweir         if (xSet->has( aFac ))
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             Reference< lang::XServiceInfo > xInfo;
145*cdf0e10cSrcweir             if (aFac >>= xInfo)
146*cdf0e10cSrcweir             {
147*cdf0e10cSrcweir                 ::fprintf(
148*cdf0e10cSrcweir                     stderr, "> implementation %s supports: ", ppNames[ -1 ] );
149*cdf0e10cSrcweir                 Sequence< OUString > supported(
150*cdf0e10cSrcweir                     xInfo->getSupportedServiceNames() );
151*cdf0e10cSrcweir                 for ( sal_Int32 nPos = supported.getLength(); nPos--; )
152*cdf0e10cSrcweir                 {
153*cdf0e10cSrcweir                     OString str( OUStringToOString(
154*cdf0e10cSrcweir                         supported[ nPos ], RTL_TEXTENCODING_ASCII_US ) );
155*cdf0e10cSrcweir                     ::fprintf( stderr, nPos ? "%s, " : "%s\n", str.getStr() );
156*cdf0e10cSrcweir                 }
157*cdf0e10cSrcweir             }
158*cdf0e10cSrcweir             else
159*cdf0e10cSrcweir             {
160*cdf0e10cSrcweir                 ::fprintf(
161*cdf0e10cSrcweir                     stderr,
162*cdf0e10cSrcweir                     "> implementation %s provides NO lang::XServiceInfo!!!\n",
163*cdf0e10cSrcweir                     ppNames[ -1 ] );
164*cdf0e10cSrcweir             }
165*cdf0e10cSrcweir         }
166*cdf0e10cSrcweir #endif
167*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
168*cdf0e10cSrcweir         if (! xSet->has( aFac ))
169*cdf0e10cSrcweir         {
170*cdf0e10cSrcweir             OStringBuffer buf( 64 );
171*cdf0e10cSrcweir             buf.append( "### failed inserting shared lib \"" );
172*cdf0e10cSrcweir             buf.append( ppNames[ -2 ] );
173*cdf0e10cSrcweir             buf.append( "\"!!!" );
174*cdf0e10cSrcweir             OString str( buf.makeStringAndClear() );
175*cdf0e10cSrcweir             OSL_ENSURE( 0, str.getStr() );
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir #endif
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir // private forward decl
182*cdf0e10cSrcweir Reference< lang::XMultiComponentFactory > bootstrapInitialSF(
183*cdf0e10cSrcweir     OUString const & rBootstrapPath )
184*cdf0e10cSrcweir     SAL_THROW( (Exception) );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir Reference< XComponentContext > bootstrapInitialContext(
187*cdf0e10cSrcweir     Reference< lang::XMultiComponentFactory > const & xSF,
188*cdf0e10cSrcweir     Reference< registry::XSimpleRegistry > const & types_xRegistry,
189*cdf0e10cSrcweir     Reference< registry::XSimpleRegistry > const & services_xRegistry,
190*cdf0e10cSrcweir     OUString const & rBootstrapPath, Bootstrap const & bootstrap )
191*cdf0e10cSrcweir     SAL_THROW( (Exception) );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir Reference< XComponentContext > SAL_CALL createInitialCfgComponentContext(
194*cdf0e10cSrcweir     ContextEntry_Init const * pEntries, sal_Int32 nEntries,
195*cdf0e10cSrcweir     Reference< XComponentContext > const & xDelegate )
196*cdf0e10cSrcweir     SAL_THROW( () );
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir Reference< registry::XSimpleRegistry > SAL_CALL createRegistryWrapper(
199*cdf0e10cSrcweir     const Reference< XComponentContext >& xContext );
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir namespace {
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir template< class T >
204*cdf0e10cSrcweir inline beans::PropertyValue createPropertyValue(
205*cdf0e10cSrcweir     OUString const & name, T const & value )
206*cdf0e10cSrcweir     SAL_THROW( () )
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir     return beans::PropertyValue(
209*cdf0e10cSrcweir         name, -1, makeAny( value ), beans::PropertyState_DIRECT_VALUE );
210*cdf0e10cSrcweir }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir OUString findBoostrapArgument(
213*cdf0e10cSrcweir     const Bootstrap & bootstrap,
214*cdf0e10cSrcweir     const OUString & arg_name,
215*cdf0e10cSrcweir     sal_Bool * pFallenBack )
216*cdf0e10cSrcweir     SAL_THROW(())
217*cdf0e10cSrcweir {
218*cdf0e10cSrcweir     OUString result;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     OUString prefixed_arg_name = OUSTR("UNO_");
221*cdf0e10cSrcweir     prefixed_arg_name += arg_name.toAsciiUpperCase();
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     // environment not set -> try relative to executable
224*cdf0e10cSrcweir     if(!bootstrap.getFrom(prefixed_arg_name, result))
225*cdf0e10cSrcweir     {
226*cdf0e10cSrcweir         if(pFallenBack)
227*cdf0e10cSrcweir             *pFallenBack = sal_True;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir         OUString fileName;
230*cdf0e10cSrcweir         bootstrap.getIniName(fileName);
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir         // cut the rc extension
233*cdf0e10cSrcweir         OUStringBuffer result_buf( 64 );
234*cdf0e10cSrcweir         result_buf.append(
235*cdf0e10cSrcweir             fileName.copy(
236*cdf0e10cSrcweir                 0, fileName.getLength() - strlen(SAL_CONFIGFILE(""))) );
237*cdf0e10cSrcweir         result_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("_") );
238*cdf0e10cSrcweir         result_buf.append( arg_name.toAsciiLowerCase() );
239*cdf0e10cSrcweir         result_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(".rdb") );
240*cdf0e10cSrcweir         result = result_buf.makeStringAndClear();
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
243*cdf0e10cSrcweir         OString result_dbg =
244*cdf0e10cSrcweir             OUStringToOString(result, RTL_TEXTENCODING_ASCII_US);
245*cdf0e10cSrcweir         OString arg_name_dbg =
246*cdf0e10cSrcweir             OUStringToOString(arg_name, RTL_TEXTENCODING_ASCII_US);
247*cdf0e10cSrcweir         OSL_TRACE(
248*cdf0e10cSrcweir             "cppuhelper::findBoostrapArgument - "
249*cdf0e10cSrcweir             "setting %s relative to executable: %s\n",
250*cdf0e10cSrcweir             arg_name_dbg.getStr(),
251*cdf0e10cSrcweir             result_dbg.getStr() );
252*cdf0e10cSrcweir #endif
253*cdf0e10cSrcweir     }
254*cdf0e10cSrcweir     else
255*cdf0e10cSrcweir     {
256*cdf0e10cSrcweir         if(pFallenBack)
257*cdf0e10cSrcweir             *pFallenBack = sal_False;
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
260*cdf0e10cSrcweir         OString prefixed_arg_name_dbg = OUStringToOString(
261*cdf0e10cSrcweir             prefixed_arg_name, RTL_TEXTENCODING_ASCII_US );
262*cdf0e10cSrcweir         OString result_dbg = OUStringToOString(
263*cdf0e10cSrcweir             result, RTL_TEXTENCODING_ASCII_US );
264*cdf0e10cSrcweir         OSL_TRACE(
265*cdf0e10cSrcweir             "cppuhelper::findBoostrapArgument - found %s in env: %s",
266*cdf0e10cSrcweir             prefixed_arg_name_dbg.getStr(),
267*cdf0e10cSrcweir             result_dbg.getStr() );
268*cdf0e10cSrcweir #endif
269*cdf0e10cSrcweir     }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir     return result;
272*cdf0e10cSrcweir }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir Reference< registry::XSimpleRegistry > nestRegistries(
275*cdf0e10cSrcweir     const OUString baseDir,
276*cdf0e10cSrcweir     const Reference< lang::XSingleServiceFactory > & xSimRegFac,
277*cdf0e10cSrcweir     const Reference< lang::XSingleServiceFactory > & xNesRegFac,
278*cdf0e10cSrcweir     OUString csl_rdbs,
279*cdf0e10cSrcweir     const OUString & write_rdb,
280*cdf0e10cSrcweir     sal_Bool forceWrite_rdb,
281*cdf0e10cSrcweir     sal_Bool bFallenBack )
282*cdf0e10cSrcweir     SAL_THROW((Exception))
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir     sal_Int32 index;
285*cdf0e10cSrcweir     Reference< registry::XSimpleRegistry > lastRegistry;
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir     if(write_rdb.getLength()) // is there a write registry given?
288*cdf0e10cSrcweir     {
289*cdf0e10cSrcweir         lastRegistry.set(xSimRegFac->createInstance(), UNO_QUERY);
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir         try
292*cdf0e10cSrcweir         {
293*cdf0e10cSrcweir             lastRegistry->open(write_rdb, sal_False, forceWrite_rdb);
294*cdf0e10cSrcweir         }
295*cdf0e10cSrcweir         catch (registry::InvalidRegistryException & invalidRegistryException)
296*cdf0e10cSrcweir         {
297*cdf0e10cSrcweir             (void) invalidRegistryException;
298*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
299*cdf0e10cSrcweir             OString rdb_name_tmp = OUStringToOString(
300*cdf0e10cSrcweir                 write_rdb, RTL_TEXTENCODING_ASCII_US);
301*cdf0e10cSrcweir             OString message_dbg = OUStringToOString(
302*cdf0e10cSrcweir                 invalidRegistryException.Message, RTL_TEXTENCODING_ASCII_US);
303*cdf0e10cSrcweir             OSL_TRACE(
304*cdf0e10cSrcweir                 "warning: couldn't open %s cause of %s",
305*cdf0e10cSrcweir                 rdb_name_tmp.getStr(), message_dbg.getStr() );
306*cdf0e10cSrcweir #endif
307*cdf0e10cSrcweir         }
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir         if(!lastRegistry->isValid())
310*cdf0e10cSrcweir             lastRegistry.clear();
311*cdf0e10cSrcweir     }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir     do
314*cdf0e10cSrcweir     {
315*cdf0e10cSrcweir         index = csl_rdbs.indexOf((sal_Unicode)' ');
316*cdf0e10cSrcweir         OUString rdb_name = (index == -1) ? csl_rdbs : csl_rdbs.copy(0, index);
317*cdf0e10cSrcweir         csl_rdbs = (index == -1) ? OUString() : csl_rdbs.copy(index + 1);
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir         if (! rdb_name.getLength())
320*cdf0e10cSrcweir             continue;
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir         bool optional = ('?' == rdb_name[ 0 ]);
323*cdf0e10cSrcweir         if (optional)
324*cdf0e10cSrcweir             rdb_name = rdb_name.copy( 1 );
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir         try
327*cdf0e10cSrcweir         {
328*cdf0e10cSrcweir             Reference<registry::XSimpleRegistry> simpleRegistry(
329*cdf0e10cSrcweir                 xSimRegFac->createInstance(), UNO_QUERY_THROW );
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir             osl::FileBase::getAbsoluteFileURL(baseDir, rdb_name, rdb_name);
332*cdf0e10cSrcweir             simpleRegistry->open(rdb_name, sal_True, sal_False);
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir             if(lastRegistry.is())
335*cdf0e10cSrcweir             {
336*cdf0e10cSrcweir                 Reference< registry::XSimpleRegistry > nestedRegistry(
337*cdf0e10cSrcweir                     xNesRegFac->createInstance(), UNO_QUERY );
338*cdf0e10cSrcweir                 Reference< lang::XInitialization > nestedRegistry_xInit(
339*cdf0e10cSrcweir                     nestedRegistry, UNO_QUERY );
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir                 Sequence<Any> aArgs(2);
342*cdf0e10cSrcweir                 aArgs[0] <<= lastRegistry;
343*cdf0e10cSrcweir                 aArgs[1] <<= simpleRegistry;
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir                 nestedRegistry_xInit->initialize(aArgs);
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir                 lastRegistry = nestedRegistry;
348*cdf0e10cSrcweir             }
349*cdf0e10cSrcweir             else
350*cdf0e10cSrcweir                 lastRegistry = simpleRegistry;
351*cdf0e10cSrcweir         }
352*cdf0e10cSrcweir         catch(registry::InvalidRegistryException & invalidRegistryException)
353*cdf0e10cSrcweir         {
354*cdf0e10cSrcweir             if (! optional)
355*cdf0e10cSrcweir             {
356*cdf0e10cSrcweir                 // if a registry was explicitly given, the exception shall fly
357*cdf0e10cSrcweir                 if( ! bFallenBack )
358*cdf0e10cSrcweir                     throw;
359*cdf0e10cSrcweir             }
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir             (void) invalidRegistryException;
362*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
363*cdf0e10cSrcweir             OString rdb_name_tmp = OUStringToOString(
364*cdf0e10cSrcweir                 rdb_name, RTL_TEXTENCODING_ASCII_US );
365*cdf0e10cSrcweir             OString message_dbg = OUStringToOString(
366*cdf0e10cSrcweir                 invalidRegistryException.Message, RTL_TEXTENCODING_ASCII_US );
367*cdf0e10cSrcweir             OSL_TRACE(
368*cdf0e10cSrcweir                 "warning: couldn't open %s cause of %s",
369*cdf0e10cSrcweir                 rdb_name_tmp.getStr(), message_dbg.getStr() );
370*cdf0e10cSrcweir #endif
371*cdf0e10cSrcweir         }
372*cdf0e10cSrcweir     }
373*cdf0e10cSrcweir     while(index != -1 && csl_rdbs.getLength()); // are there more rdbs in list?
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir     return lastRegistry;
376*cdf0e10cSrcweir }
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir Reference< XComponentContext >
379*cdf0e10cSrcweir SAL_CALL defaultBootstrap_InitialComponentContext(
380*cdf0e10cSrcweir     Bootstrap const & bootstrap )
381*cdf0e10cSrcweir     SAL_THROW( (Exception) )
382*cdf0e10cSrcweir {
383*cdf0e10cSrcweir     OUString bootstrapPath;
384*cdf0e10cSrcweir     if (!bootstrap.getFrom(
385*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("URE_INTERNAL_LIB_DIR")),
386*cdf0e10cSrcweir             bootstrapPath))
387*cdf0e10cSrcweir     {
388*cdf0e10cSrcweir         bootstrapPath = get_this_libpath();
389*cdf0e10cSrcweir     }
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir     OUString iniDir;
392*cdf0e10cSrcweir     osl_getProcessWorkingDir(&iniDir.pData);
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir     Reference<lang::XMultiComponentFactory> smgr_XMultiComponentFactory(
395*cdf0e10cSrcweir         bootstrapInitialSF(bootstrapPath) );
396*cdf0e10cSrcweir     Reference<lang::XMultiServiceFactory> smgr_XMultiServiceFactory(
397*cdf0e10cSrcweir         smgr_XMultiComponentFactory, UNO_QUERY );
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir     Reference<registry::XRegistryKey> xEmptyKey;
400*cdf0e10cSrcweir     Reference<lang::XSingleServiceFactory> xSimRegFac(
401*cdf0e10cSrcweir         loadSharedLibComponentFactory(
402*cdf0e10cSrcweir             OUSTR("bootstrap.uno" SAL_DLLEXTENSION), bootstrapPath,
403*cdf0e10cSrcweir             OUSTR("com.sun.star.comp.stoc.SimpleRegistry"),
404*cdf0e10cSrcweir             smgr_XMultiServiceFactory,
405*cdf0e10cSrcweir             xEmptyKey),
406*cdf0e10cSrcweir         UNO_QUERY);
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir     Reference<lang::XSingleServiceFactory> xNesRegFac(
409*cdf0e10cSrcweir         loadSharedLibComponentFactory(
410*cdf0e10cSrcweir             OUSTR("bootstrap.uno" SAL_DLLEXTENSION), bootstrapPath,
411*cdf0e10cSrcweir             OUSTR("com.sun.star.comp.stoc.NestedRegistry"),
412*cdf0e10cSrcweir             smgr_XMultiServiceFactory,
413*cdf0e10cSrcweir             xEmptyKey),
414*cdf0e10cSrcweir         UNO_QUERY);
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir     sal_Bool bFallenback_types;
417*cdf0e10cSrcweir     OUString cls_uno_types =
418*cdf0e10cSrcweir         findBoostrapArgument( bootstrap, OUSTR("TYPES"), &bFallenback_types );
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir     Reference<registry::XSimpleRegistry> types_xRegistry =
421*cdf0e10cSrcweir         nestRegistries(
422*cdf0e10cSrcweir             iniDir, xSimRegFac, xNesRegFac, cls_uno_types,
423*cdf0e10cSrcweir             OUString(), sal_False, bFallenback_types );
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir     // ==== bootstrap from services registry ====
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir     sal_Bool bFallenback_services;
428*cdf0e10cSrcweir     OUString cls_uno_services = findBoostrapArgument(
429*cdf0e10cSrcweir         bootstrap, OUSTR("SERVICES"), &bFallenback_services );
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir     sal_Bool fallenBackWriteRegistry;
432*cdf0e10cSrcweir     OUString write_rdb = findBoostrapArgument(
433*cdf0e10cSrcweir         bootstrap, OUSTR("WRITERDB"), &fallenBackWriteRegistry );
434*cdf0e10cSrcweir     if (fallenBackWriteRegistry)
435*cdf0e10cSrcweir     {
436*cdf0e10cSrcweir         // no standard write rdb anymore
437*cdf0e10cSrcweir         write_rdb = OUString();
438*cdf0e10cSrcweir     }
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir     Reference<registry::XSimpleRegistry> services_xRegistry = nestRegistries(
441*cdf0e10cSrcweir         iniDir, xSimRegFac, xNesRegFac, cls_uno_services, write_rdb,
442*cdf0e10cSrcweir         !fallenBackWriteRegistry, bFallenback_services );
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir     Reference< XComponentContext > xContext(
445*cdf0e10cSrcweir         bootstrapInitialContext(
446*cdf0e10cSrcweir             smgr_XMultiComponentFactory, types_xRegistry, services_xRegistry,
447*cdf0e10cSrcweir             bootstrapPath, bootstrap ) );
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir     // initialize sf
450*cdf0e10cSrcweir     Reference< lang::XInitialization > xInit(
451*cdf0e10cSrcweir         smgr_XMultiComponentFactory, UNO_QUERY );
452*cdf0e10cSrcweir     OSL_ASSERT( xInit.is() );
453*cdf0e10cSrcweir     Sequence< Any > aSFInit( 1 );
454*cdf0e10cSrcweir     aSFInit[ 0 ] <<= services_xRegistry;
455*cdf0e10cSrcweir     xInit->initialize( aSFInit );
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir     return xContext;
458*cdf0e10cSrcweir }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir }
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir Reference< XComponentContext >
463*cdf0e10cSrcweir SAL_CALL defaultBootstrap_InitialComponentContext(
464*cdf0e10cSrcweir     OUString const & iniFile )
465*cdf0e10cSrcweir     SAL_THROW( (Exception) )
466*cdf0e10cSrcweir {
467*cdf0e10cSrcweir     Bootstrap bootstrap( iniFile );
468*cdf0e10cSrcweir     if (bootstrap.getHandle() == 0)
469*cdf0e10cSrcweir         throw io::IOException(OUSTR("Cannot open for reading: ") + iniFile, 0);
470*cdf0e10cSrcweir     return defaultBootstrap_InitialComponentContext( bootstrap );
471*cdf0e10cSrcweir }
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir Reference< XComponentContext >
474*cdf0e10cSrcweir SAL_CALL defaultBootstrap_InitialComponentContext()
475*cdf0e10cSrcweir     SAL_THROW( (Exception) )
476*cdf0e10cSrcweir {
477*cdf0e10cSrcweir     return defaultBootstrap_InitialComponentContext( get_unorc() );
478*cdf0e10cSrcweir }
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir BootstrapException::BootstrapException()
481*cdf0e10cSrcweir {
482*cdf0e10cSrcweir }
483*cdf0e10cSrcweir 
484*cdf0e10cSrcweir BootstrapException::BootstrapException( const ::rtl::OUString & rMessage )
485*cdf0e10cSrcweir     :m_aMessage( rMessage )
486*cdf0e10cSrcweir {
487*cdf0e10cSrcweir }
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir BootstrapException::BootstrapException( const BootstrapException & e )
490*cdf0e10cSrcweir {
491*cdf0e10cSrcweir     m_aMessage = e.m_aMessage;
492*cdf0e10cSrcweir }
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir BootstrapException::~BootstrapException()
495*cdf0e10cSrcweir {
496*cdf0e10cSrcweir }
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir BootstrapException & BootstrapException::operator=( const BootstrapException & e )
499*cdf0e10cSrcweir {
500*cdf0e10cSrcweir     m_aMessage = e.m_aMessage;
501*cdf0e10cSrcweir     return *this;
502*cdf0e10cSrcweir }
503*cdf0e10cSrcweir 
504*cdf0e10cSrcweir const ::rtl::OUString & BootstrapException::getMessage() const
505*cdf0e10cSrcweir {
506*cdf0e10cSrcweir     return m_aMessage;
507*cdf0e10cSrcweir }
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir Reference< XComponentContext > SAL_CALL bootstrap()
510*cdf0e10cSrcweir {
511*cdf0e10cSrcweir     Reference< XComponentContext > xRemoteContext;
512*cdf0e10cSrcweir 
513*cdf0e10cSrcweir     try
514*cdf0e10cSrcweir     {
515*cdf0e10cSrcweir         char const * p1 = cppuhelper_detail_findSofficePath();
516*cdf0e10cSrcweir         if (p1 == NULL) {
517*cdf0e10cSrcweir             throw BootstrapException(
518*cdf0e10cSrcweir                 OUSTR("no soffice installation found!"));
519*cdf0e10cSrcweir         }
520*cdf0e10cSrcweir         rtl::OUString p2;
521*cdf0e10cSrcweir         if (!rtl_convertStringToUString(
522*cdf0e10cSrcweir                 &p2.pData, p1, strlen(p1), osl_getThreadTextEncoding(),
523*cdf0e10cSrcweir                 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
524*cdf0e10cSrcweir                  RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
525*cdf0e10cSrcweir                  RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
526*cdf0e10cSrcweir         {
527*cdf0e10cSrcweir             throw BootstrapException(
528*cdf0e10cSrcweir                 OUSTR("bad characters in soffice installation path!"));
529*cdf0e10cSrcweir         }
530*cdf0e10cSrcweir         OUString path;
531*cdf0e10cSrcweir         if (osl::FileBase::getFileURLFromSystemPath(p2, path) !=
532*cdf0e10cSrcweir             osl::FileBase::E_None)
533*cdf0e10cSrcweir         {
534*cdf0e10cSrcweir             throw BootstrapException(
535*cdf0e10cSrcweir                 OUSTR("cannot convert soffice installation path to URL!"));
536*cdf0e10cSrcweir         }
537*cdf0e10cSrcweir         if (path.getLength() > 0 && path[path.getLength() - 1] != '/') {
538*cdf0e10cSrcweir             path += OUSTR("/");
539*cdf0e10cSrcweir         }
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir         OUString uri;
542*cdf0e10cSrcweir         if (!Bootstrap::get(OUSTR("URE_BOOTSTRAP"), uri)) {
543*cdf0e10cSrcweir             Bootstrap::set(
544*cdf0e10cSrcweir                 OUSTR("URE_BOOTSTRAP"),
545*cdf0e10cSrcweir                 Bootstrap::encode(path + OUSTR(SAL_CONFIGFILE("fundamental"))));
546*cdf0e10cSrcweir         }
547*cdf0e10cSrcweir 
548*cdf0e10cSrcweir         // create default local component context
549*cdf0e10cSrcweir         Reference< XComponentContext > xLocalContext(
550*cdf0e10cSrcweir             defaultBootstrap_InitialComponentContext() );
551*cdf0e10cSrcweir         if ( !xLocalContext.is() )
552*cdf0e10cSrcweir             throw BootstrapException( OUSTR( "no local component context!" ) );
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir         // create a random pipe name
555*cdf0e10cSrcweir         rtlRandomPool hPool = rtl_random_createPool();
556*cdf0e10cSrcweir         if ( hPool == 0 )
557*cdf0e10cSrcweir             throw BootstrapException( OUSTR( "cannot create random pool!" ) );
558*cdf0e10cSrcweir         sal_uInt8 bytes[ 16 ];
559*cdf0e10cSrcweir         if ( rtl_random_getBytes( hPool, bytes, ARLEN( bytes ) )
560*cdf0e10cSrcweir             != rtl_Random_E_None )
561*cdf0e10cSrcweir             throw BootstrapException( OUSTR( "random pool error!" ) );
562*cdf0e10cSrcweir         rtl_random_destroyPool( hPool );
563*cdf0e10cSrcweir         ::rtl::OUStringBuffer buf;
564*cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "uno" ) );
565*cdf0e10cSrcweir         for ( sal_uInt32 i = 0; i < ARLEN( bytes ); ++i )
566*cdf0e10cSrcweir             buf.append( static_cast< sal_Int32 >( bytes[ i ] ) );
567*cdf0e10cSrcweir         OUString sPipeName( buf.makeStringAndClear() );
568*cdf0e10cSrcweir 
569*cdf0e10cSrcweir         // accept string
570*cdf0e10cSrcweir         OSL_ASSERT( buf.getLength() == 0 );
571*cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "-accept=pipe,name=" ) );
572*cdf0e10cSrcweir         buf.append( sPipeName );
573*cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( ";urp;" ) );
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir         // arguments
576*cdf0e10cSrcweir         OUString args [] = {
577*cdf0e10cSrcweir             OUSTR( "-nologo" ),
578*cdf0e10cSrcweir             OUSTR( "-nodefault" ),
579*cdf0e10cSrcweir             OUSTR( "-norestore" ),
580*cdf0e10cSrcweir             OUSTR( "-nocrashreport" ),
581*cdf0e10cSrcweir             OUSTR( "-nolockcheck" ),
582*cdf0e10cSrcweir             buf.makeStringAndClear()
583*cdf0e10cSrcweir         };
584*cdf0e10cSrcweir         rtl_uString * ar_args [] = {
585*cdf0e10cSrcweir             args[ 0 ].pData,
586*cdf0e10cSrcweir             args[ 1 ].pData,
587*cdf0e10cSrcweir             args[ 2 ].pData,
588*cdf0e10cSrcweir             args[ 3 ].pData,
589*cdf0e10cSrcweir             args[ 4 ].pData,
590*cdf0e10cSrcweir             args[ 5 ].pData
591*cdf0e10cSrcweir         };
592*cdf0e10cSrcweir         ::osl::Security sec;
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir         // start office process
595*cdf0e10cSrcweir         oslProcess hProcess = 0;
596*cdf0e10cSrcweir         oslProcessError rc = osl_executeProcess(
597*cdf0e10cSrcweir             (path + OUSTR("soffice")).pData, ar_args, ARLEN( ar_args ),
598*cdf0e10cSrcweir             osl_Process_DETACHED,
599*cdf0e10cSrcweir             sec.getHandle(),
600*cdf0e10cSrcweir             0, // => current working dir
601*cdf0e10cSrcweir             0, 0, // => no env vars
602*cdf0e10cSrcweir             &hProcess );
603*cdf0e10cSrcweir         switch ( rc )
604*cdf0e10cSrcweir         {
605*cdf0e10cSrcweir             case osl_Process_E_None:
606*cdf0e10cSrcweir                 osl_freeProcessHandle( hProcess );
607*cdf0e10cSrcweir                 break;
608*cdf0e10cSrcweir             case osl_Process_E_NotFound:
609*cdf0e10cSrcweir                 throw BootstrapException( OUSTR( "image not found!" ) );
610*cdf0e10cSrcweir             case osl_Process_E_TimedOut:
611*cdf0e10cSrcweir                 throw BootstrapException( OUSTR( "timout occured!" ) );
612*cdf0e10cSrcweir             case osl_Process_E_NoPermission:
613*cdf0e10cSrcweir                 throw BootstrapException( OUSTR( "permission denied!" ) );
614*cdf0e10cSrcweir             case osl_Process_E_Unknown:
615*cdf0e10cSrcweir                 throw BootstrapException( OUSTR( "unknown error!" ) );
616*cdf0e10cSrcweir             case osl_Process_E_InvalidError:
617*cdf0e10cSrcweir             default:
618*cdf0e10cSrcweir                 throw BootstrapException( OUSTR( "unmapped error!" ) );
619*cdf0e10cSrcweir         }
620*cdf0e10cSrcweir 
621*cdf0e10cSrcweir         // create a URL resolver
622*cdf0e10cSrcweir         Reference< bridge::XUnoUrlResolver > xUrlResolver(
623*cdf0e10cSrcweir             bridge::UnoUrlResolver::create( xLocalContext ) );
624*cdf0e10cSrcweir 
625*cdf0e10cSrcweir         // connection string
626*cdf0e10cSrcweir         OSL_ASSERT( buf.getLength() == 0 );
627*cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "uno:pipe,name=" ) );
628*cdf0e10cSrcweir         buf.append( sPipeName );
629*cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
630*cdf0e10cSrcweir             ";urp;StarOffice.ComponentContext" ) );
631*cdf0e10cSrcweir         OUString sConnectString( buf.makeStringAndClear() );
632*cdf0e10cSrcweir 
633*cdf0e10cSrcweir         // wait until office is started
634*cdf0e10cSrcweir         for ( ; ; )
635*cdf0e10cSrcweir         {
636*cdf0e10cSrcweir             try
637*cdf0e10cSrcweir             {
638*cdf0e10cSrcweir                 // try to connect to office
639*cdf0e10cSrcweir                 xRemoteContext.set(
640*cdf0e10cSrcweir                     xUrlResolver->resolve( sConnectString ), UNO_QUERY_THROW );
641*cdf0e10cSrcweir                 break;
642*cdf0e10cSrcweir             }
643*cdf0e10cSrcweir             catch ( connection::NoConnectException & )
644*cdf0e10cSrcweir             {
645*cdf0e10cSrcweir                 // wait 500 ms, then try to connect again
646*cdf0e10cSrcweir                 TimeValue tv = { 0 /* secs */, 500000000 /* nanosecs */ };
647*cdf0e10cSrcweir                 ::osl::Thread::wait( tv );
648*cdf0e10cSrcweir             }
649*cdf0e10cSrcweir         }
650*cdf0e10cSrcweir     }
651*cdf0e10cSrcweir     catch ( Exception & e )
652*cdf0e10cSrcweir     {
653*cdf0e10cSrcweir         throw BootstrapException(
654*cdf0e10cSrcweir             OUSTR( "unexpected UNO exception caught: " ) + e.Message );
655*cdf0e10cSrcweir     }
656*cdf0e10cSrcweir 
657*cdf0e10cSrcweir     return xRemoteContext;
658*cdf0e10cSrcweir }
659*cdf0e10cSrcweir 
660*cdf0e10cSrcweir OUString bootstrap_expandUri(OUString const & uri) {
661*cdf0e10cSrcweir     static char const PREFIX[] = "vnd.sun.star.expand:";
662*cdf0e10cSrcweir     return uri.matchAsciiL(RTL_CONSTASCII_STRINGPARAM(PREFIX))
663*cdf0e10cSrcweir         ? cppuhelper::detail::expandMacros(
664*cdf0e10cSrcweir             rtl::Uri::decode(
665*cdf0e10cSrcweir                 uri.copy(RTL_CONSTASCII_LENGTH(PREFIX)),
666*cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8))
667*cdf0e10cSrcweir         : uri;
668*cdf0e10cSrcweir }
669*cdf0e10cSrcweir 
670*cdf0e10cSrcweir } // namespace cppu
671