1*9d7e27acSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9d7e27acSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9d7e27acSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9d7e27acSAndrew Rist  * distributed with this work for additional information
6*9d7e27acSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9d7e27acSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9d7e27acSAndrew Rist  * "License"); you may not use this file except in compliance
9*9d7e27acSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9d7e27acSAndrew Rist  *
11*9d7e27acSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9d7e27acSAndrew Rist  *
13*9d7e27acSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9d7e27acSAndrew Rist  * software distributed under the License is distributed on an
15*9d7e27acSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9d7e27acSAndrew Rist  * KIND, either express or implied.  See the License for the
17*9d7e27acSAndrew Rist  * specific language governing permissions and limitations
18*9d7e27acSAndrew Rist  * under the License.
19*9d7e27acSAndrew Rist  *
20*9d7e27acSAndrew Rist  *************************************************************/
21*9d7e27acSAndrew Rist 
22*9d7e27acSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "EXTERN.h"
28cdf0e10cSrcweir #include "perl.h"
29cdf0e10cSrcweir #include "XSUB.h"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <cstdio>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <osl/module.hxx>
34cdf0e10cSrcweir #include <rtl/process.h>
35cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace ::cppu;
40cdf0e10cSrcweir using namespace ::com::sun::star::lang;
41cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42cdf0e10cSrcweir using namespace ::rtl;
43cdf0e10cSrcweir 
tryService(const char * serviceName)44cdf0e10cSrcweir static sal_Bool tryService(const char * serviceName)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	// use micro deployment to create initial context
47cdf0e10cSrcweir 	OUString libraryFileUrl;
48cdf0e10cSrcweir 	::osl::Module::getUrlFromAddress((void *)tryService, libraryFileUrl);
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 	OUString iniName = libraryFileUrl.copy(0, libraryFileUrl.lastIndexOf((sal_Unicode)'.')); // cut the library extension
51cdf0e10cSrcweir 	iniName += OUString(RTL_CONSTASCII_USTRINGPARAM(SAL_CONFIGFILE(""))); // add the rc file extension
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
54cdf0e10cSrcweir 	OString sIniName = OUStringToOString(iniName, RTL_TEXTENCODING_ASCII_US);
55cdf0e10cSrcweir 	fprintf(stderr, "defbootstrap.cxx:tryService: using ini: %s\n", sIniName.getStr());
56cdf0e10cSrcweir #endif
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	sal_Bool result = sal_True;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	try {
61cdf0e10cSrcweir 		Reference<XComponentContext> xComponentContext = defaultBootstrap_InitialComponentContext(iniName);
62cdf0e10cSrcweir 		Reference<XMultiServiceFactory> smgr(xComponentContext->getServiceManager(), UNO_QUERY);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 		OUString arg = OUString::createFromAscii(serviceName);
65cdf0e10cSrcweir 		Reference<XInterface> xInterface = smgr->createInstance(arg);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
68cdf0e10cSrcweir 		fprintf(stderr, "got the %s service %p\n", serviceName, xInterface.get());
69cdf0e10cSrcweir #endif
70cdf0e10cSrcweir 		result = result && (xInterface.get() != 0);
71cdf0e10cSrcweir 	}
72cdf0e10cSrcweir 	catch(Exception & exception) {
73cdf0e10cSrcweir 		OString message = OUStringToOString(exception.Message, RTL_TEXTENCODING_ASCII_US);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 		fprintf(stderr, "an exception occurred: %s\n", message.getStr());
76cdf0e10cSrcweir 	}
77cdf0e10cSrcweir 
78cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
79cdf0e10cSrcweir 	OSL_TRACE("---------------------------------- %i", result);
80cdf0e10cSrcweir #endif
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	return result;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
XS(XS_UNO_tryService)85cdf0e10cSrcweir XS(XS_UNO_tryService)
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     dXSARGS;
88cdf0e10cSrcweir     if (items != 1)
89cdf0e10cSrcweir 	Perl_croak(aTHX_ "Usage: UNO::tryService(input)");
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir 		const char * input = (const char *)SvPV(ST(0),PL_na);
92cdf0e10cSrcweir 		int	RETVAL;
93cdf0e10cSrcweir 		dXSTARG;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 		RETVAL = tryService(input);
96cdf0e10cSrcweir 		XSprePUSH; PUSHi((IV)RETVAL);
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir     XSRETURN(1);
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir extern "C" {
XS(boot_UNO)102cdf0e10cSrcweir XS(boot_UNO)
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     dXSARGS;
105cdf0e10cSrcweir     char* file = __FILE__;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir /*    XS_VERSION_BOOTCHECK ;*/
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	newXS("UNO::tryService", XS_UNO_tryService, file);
110cdf0e10cSrcweir     XSRETURN_YES;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir }
114