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