xref: /AOO41X/main/cppuhelper/test/testcmp/TestComponent.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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #define CPPUHELPER_TEST_COMPONENT_IMPL
33*cdf0e10cSrcweir #include "TestComponent.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <string.h>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "osl/thread.h"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
41*cdf0e10cSrcweir #include "cppuhelper/implementationentry.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include "com/sun/star/lang/XMultiComponentFactory.hpp"
44*cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include "cppu/EnvDcp.hxx"
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir #include <uno/environment.hxx>
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir using namespace ::com::sun::star;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir #define LOG_LIFECYCLE_TestComponent
57*cdf0e10cSrcweir #ifdef LOG_LIFECYCLE_TestComponent
58*cdf0e10cSrcweir #  include <iostream>
59*cdf0e10cSrcweir #  define LOG_LIFECYCLE_TestComponent_emit(x) x
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir #else
62*cdf0e10cSrcweir #  define LOG_LIFECYCLE_TestComponent_emit(x)
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir #endif
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir class TestComponent: public cppu::WeakImplHelper1<lang::XServiceInfo>
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	rtl::OUString m_implName;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir public:
72*cdf0e10cSrcweir 	static uno::Reference<uno::XInterface> create(
73*cdf0e10cSrcweir 		uno::Reference<uno::XComponentContext> const & xCtx
74*cdf0e10cSrcweir     )
75*cdf0e10cSrcweir 	SAL_THROW((uno::Exception));
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	static uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames_Static();
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	explicit TestComponent(uno::Reference<uno::XComponentContext> const & xCtx);
81*cdf0e10cSrcweir 	virtual ~TestComponent();
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	uno::Any SAL_CALL queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException);
84*cdf0e10cSrcweir 	void SAL_CALL release() throw ();
85*cdf0e10cSrcweir 	void SAL_CALL acquire() throw ();
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 	// lang::XServiceInfo
88*cdf0e10cSrcweir 	virtual rtl::OUString SAL_CALL getImplementationName() throw (uno::RuntimeException);
89*cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
90*cdf0e10cSrcweir 		throw (uno::RuntimeException);
91*cdf0e10cSrcweir 	virtual uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames()
92*cdf0e10cSrcweir 		throw (uno::RuntimeException);
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir protected:
95*cdf0e10cSrcweir 	uno::Reference<uno::XComponentContext> m_xComponentContext;
96*cdf0e10cSrcweir };
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir uno::Reference<uno::XInterface> SAL_CALL TestComponent::create(
100*cdf0e10cSrcweir 	uno::Reference<uno::XComponentContext> const & xCtx
101*cdf0e10cSrcweir )
102*cdf0e10cSrcweir 	SAL_THROW((uno::Exception))
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	try
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		return static_cast<cppu::OWeakObject *>(new TestComponent(xCtx));
107*cdf0e10cSrcweir 	}
108*cdf0e10cSrcweir 	catch (std::bad_alloc &)
109*cdf0e10cSrcweir 	{
110*cdf0e10cSrcweir 		throw uno::RuntimeException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
111*cdf0e10cSrcweir 									 uno::Reference<uno::XInterface>());
112*cdf0e10cSrcweir 	}
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames_Static()
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir 	uno::Sequence<rtl::OUString> serviceNames(1);
118*cdf0e10cSrcweir 	serviceNames[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.lang.ServiceInfo"));
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	return serviceNames;
121*cdf0e10cSrcweir }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir TestComponent::TestComponent(uno::Reference<uno::XComponentContext> const & xCtx)
125*cdf0e10cSrcweir 	: m_xComponentContext(xCtx)
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir 	LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::TestComponent()", this));
128*cdf0e10cSrcweir }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir TestComponent::~TestComponent()
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir 	LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::~TestComponent", this));
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir rtl::OUString SAL_CALL TestComponent::getImplementationName()
136*cdf0e10cSrcweir 	throw (uno::RuntimeException)
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir 	return m_implName;
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir void SAL_CALL TestComponent::acquire() throw ()
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir  	cppu::WeakImplHelper1<lang::XServiceInfo>::acquire();
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir void SAL_CALL TestComponent::release() throw ()
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir 	cppu::WeakImplHelper1<lang::XServiceInfo>::release();
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir uno::Any SAL_CALL TestComponent::queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException)
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	return cppu::WeakImplHelper1<lang::XServiceInfo>::queryInterface(rType);
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir sal_Bool SAL_CALL TestComponent::supportsService(rtl::OUString const & ServiceName)
157*cdf0e10cSrcweir 	throw (uno::RuntimeException)
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	uno::Sequence<rtl::OUString> serviceNames = getSupportedServiceNames_Static();
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	for (sal_Int32 n = 0; n < serviceNames.getLength(); ++n)
162*cdf0e10cSrcweir 	{
163*cdf0e10cSrcweir 		if (serviceNames[n] == ServiceName)
164*cdf0e10cSrcweir 			return true;
165*cdf0e10cSrcweir 	}
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	return false;
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames()
171*cdf0e10cSrcweir 	throw (uno::RuntimeException)
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir extern "C" sal_Bool SAL_CALL component_writeInfo(
177*cdf0e10cSrcweir 	void * /*serviceManager*/,
178*cdf0e10cSrcweir 	void * /*registryKey*/
179*cdf0e10cSrcweir )
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir 	g_envDcp = uno::Environment::getCurrent().getTypeName();
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	return true;
184*cdf0e10cSrcweir }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
187*cdf0e10cSrcweir 	char const * pImplName,
188*cdf0e10cSrcweir 	void       * /*serviceManager*/,
189*cdf0e10cSrcweir 	void       * /*registryKey*/
190*cdf0e10cSrcweir )
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir 	g_envDcp = uno::Environment::getCurrent().getTypeName();
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 	uno::Reference< lang::XSingleComponentFactory > xFactory;
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     rtl::OUString uTmp(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     rtl::OUString uImplName(cppu::EnvDcp::getTypeName(uTmp));
199*cdf0e10cSrcweir 	rtl::OUString cmpName(RTL_CONSTASCII_USTRINGPARAM("impl.test.TestComponent"));
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	if (uImplName.equals(cmpName))
202*cdf0e10cSrcweir 	{
203*cdf0e10cSrcweir 		xFactory = cppu::createSingleComponentFactory(
204*cdf0e10cSrcweir 			TestComponent::create,
205*cdf0e10cSrcweir 			uImplName,
206*cdf0e10cSrcweir 			TestComponent::getSupportedServiceNames_Static());
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 		xFactory->acquire();
209*cdf0e10cSrcweir 	}
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 	return xFactory.get();
212*cdf0e10cSrcweir }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT  void SAL_CALL component_getImplementationEnvironmentExt(
215*cdf0e10cSrcweir 	sal_Char        const ** envTypeName,
216*cdf0e10cSrcweir 	uno_Environment       ** /*ppEnv*/,
217*cdf0e10cSrcweir 	sal_Char        const  * pImplName,
218*cdf0e10cSrcweir 	uno_Environment        * /*pSrcEnv*/
219*cdf0e10cSrcweir )
220*cdf0e10cSrcweir {
221*cdf0e10cSrcweir 	rtl::OString purpose;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     if (pImplName) // this is the purpose for a specified impl
224*cdf0e10cSrcweir     {
225*cdf0e10cSrcweir         rtl::OUString uImplName(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
226*cdf0e10cSrcweir         purpose = rtl::OUStringToOString(cppu::EnvDcp::getPurpose(uImplName), RTL_TEXTENCODING_ASCII_US);
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     if (!purpose.getLength())
230*cdf0e10cSrcweir     {
231*cdf0e10cSrcweir         char * pPurpose = getenv("TestComponent.uno");
232*cdf0e10cSrcweir         if (pPurpose)
233*cdf0e10cSrcweir             purpose = rtl::OString(pPurpose);
234*cdf0e10cSrcweir     }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 	if (purpose.getLength() == 0)
237*cdf0e10cSrcweir 		*envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 	else
240*cdf0e10cSrcweir 	{
241*cdf0e10cSrcweir 		char buff[256];
242*cdf0e10cSrcweir 		strcpy(buff, CPPU_STRINGIFY(CPPU_ENV));
243*cdf0e10cSrcweir 		strcat(buff, purpose.getStr());
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 		*envTypeName = strdup(buff);
246*cdf0e10cSrcweir 	}
247*cdf0e10cSrcweir }
248