1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppuhelper.hxx"
26 
27 
28 #define CPPUHELPER_TEST_COMPONENT_IMPL
29 #include "TestComponent.hxx"
30 
31 
32 #include <string.h>
33 
34 #include "osl/thread.h"
35 
36 #include "cppuhelper/implbase1.hxx"
37 #include "cppuhelper/implementationentry.hxx"
38 
39 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
40 #include "com/sun/star/lang/XServiceInfo.hpp"
41 
42 #include "com/sun/star/uno/XComponentContext.hpp"
43 
44 
45 #include "cppu/EnvDcp.hxx"
46 
47 #include <uno/environment.hxx>
48 
49 using namespace ::com::sun::star;
50 
51 
52 #define LOG_LIFECYCLE_TestComponent
53 #ifdef LOG_LIFECYCLE_TestComponent
54 #  include <iostream>
55 #  define LOG_LIFECYCLE_TestComponent_emit(x) x
56 
57 #else
58 #  define LOG_LIFECYCLE_TestComponent_emit(x)
59 
60 #endif
61 
62 
63 class TestComponent: public cppu::WeakImplHelper1<lang::XServiceInfo>
64 {
65 	rtl::OUString m_implName;
66 
67 public:
68 	static uno::Reference<uno::XInterface> create(
69 		uno::Reference<uno::XComponentContext> const & xCtx
70     )
71 	SAL_THROW((uno::Exception));
72 
73 
74 	static uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames_Static();
75 
76 	explicit TestComponent(uno::Reference<uno::XComponentContext> const & xCtx);
77 	virtual ~TestComponent();
78 
79 	uno::Any SAL_CALL queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException);
80 	void SAL_CALL release() throw ();
81 	void SAL_CALL acquire() throw ();
82 
83 	// lang::XServiceInfo
84 	virtual rtl::OUString SAL_CALL getImplementationName() throw (uno::RuntimeException);
85 	virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
86 		throw (uno::RuntimeException);
87 	virtual uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames()
88 		throw (uno::RuntimeException);
89 
90 protected:
91 	uno::Reference<uno::XComponentContext> m_xComponentContext;
92 };
93 
94 
create(uno::Reference<uno::XComponentContext> const & xCtx)95 uno::Reference<uno::XInterface> SAL_CALL TestComponent::create(
96 	uno::Reference<uno::XComponentContext> const & xCtx
97 )
98 	SAL_THROW((uno::Exception))
99 {
100 	try
101 	{
102 		return static_cast<cppu::OWeakObject *>(new TestComponent(xCtx));
103 	}
104 	catch (std::bad_alloc &)
105 	{
106 		throw uno::RuntimeException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
107 									 uno::Reference<uno::XInterface>());
108 	}
109 }
110 
getSupportedServiceNames_Static()111 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames_Static()
112 {
113 	uno::Sequence<rtl::OUString> serviceNames(1);
114 	serviceNames[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.lang.ServiceInfo"));
115 
116 	return serviceNames;
117 }
118 
119 
TestComponent(uno::Reference<uno::XComponentContext> const & xCtx)120 TestComponent::TestComponent(uno::Reference<uno::XComponentContext> const & xCtx)
121 	: m_xComponentContext(xCtx)
122 {
123 	LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::TestComponent()", this));
124 }
125 
~TestComponent()126 TestComponent::~TestComponent()
127 {
128 	LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::~TestComponent", this));
129 }
130 
getImplementationName()131 rtl::OUString SAL_CALL TestComponent::getImplementationName()
132 	throw (uno::RuntimeException)
133 {
134 	return m_implName;
135 }
136 
acquire()137 void SAL_CALL TestComponent::acquire() throw ()
138 {
139  	cppu::WeakImplHelper1<lang::XServiceInfo>::acquire();
140 }
141 
release()142 void SAL_CALL TestComponent::release() throw ()
143 {
144 	cppu::WeakImplHelper1<lang::XServiceInfo>::release();
145 }
146 
queryInterface(uno::Type const & rType)147 uno::Any SAL_CALL TestComponent::queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException)
148 {
149 	return cppu::WeakImplHelper1<lang::XServiceInfo>::queryInterface(rType);
150 }
151 
supportsService(rtl::OUString const & ServiceName)152 sal_Bool SAL_CALL TestComponent::supportsService(rtl::OUString const & ServiceName)
153 	throw (uno::RuntimeException)
154 {
155 	uno::Sequence<rtl::OUString> serviceNames = getSupportedServiceNames_Static();
156 
157 	for (sal_Int32 n = 0; n < serviceNames.getLength(); ++n)
158 	{
159 		if (serviceNames[n] == ServiceName)
160 			return true;
161 	}
162 
163 	return false;
164 }
165 
getSupportedServiceNames()166 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames()
167 	throw (uno::RuntimeException)
168 {
169 	return getSupportedServiceNames_Static();
170 }
171 
component_writeInfo(void *,void *)172 extern "C" sal_Bool SAL_CALL component_writeInfo(
173 	void * /*serviceManager*/,
174 	void * /*registryKey*/
175 )
176 {
177 	g_envDcp = uno::Environment::getCurrent().getTypeName();
178 
179 	return true;
180 }
181 
component_getFactory(char const * pImplName,void *,void *)182 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
183 	char const * pImplName,
184 	void       * /*serviceManager*/,
185 	void       * /*registryKey*/
186 )
187 {
188 	g_envDcp = uno::Environment::getCurrent().getTypeName();
189 
190 	uno::Reference< lang::XSingleComponentFactory > xFactory;
191 
192     rtl::OUString uTmp(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
193 
194     rtl::OUString uImplName(cppu::EnvDcp::getTypeName(uTmp));
195 	rtl::OUString cmpName(RTL_CONSTASCII_USTRINGPARAM("impl.test.TestComponent"));
196 
197 	if (uImplName.equals(cmpName))
198 	{
199 		xFactory = cppu::createSingleComponentFactory(
200 			TestComponent::create,
201 			uImplName,
202 			TestComponent::getSupportedServiceNames_Static());
203 
204 		xFactory->acquire();
205 	}
206 
207 	return xFactory.get();
208 }
209 
component_getImplementationEnvironmentExt(sal_Char const ** envTypeName,uno_Environment **,sal_Char const * pImplName,uno_Environment *)210 extern "C" SAL_DLLPUBLIC_EXPORT  void SAL_CALL component_getImplementationEnvironmentExt(
211 	sal_Char        const ** envTypeName,
212 	uno_Environment       ** /*ppEnv*/,
213 	sal_Char        const  * pImplName,
214 	uno_Environment        * /*pSrcEnv*/
215 )
216 {
217 	rtl::OString purpose;
218 
219     if (pImplName) // this is the purpose for a specified impl
220     {
221         rtl::OUString uImplName(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
222         purpose = rtl::OUStringToOString(cppu::EnvDcp::getPurpose(uImplName), RTL_TEXTENCODING_ASCII_US);
223     }
224 
225     if (!purpose.getLength())
226     {
227         char * pPurpose = getenv("TestComponent.uno");
228         if (pPurpose)
229             purpose = rtl::OString(pPurpose);
230     }
231 
232 	if (purpose.getLength() == 0)
233 		*envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
234 
235 	else
236 	{
237 		char buff[256];
238 		strcpy(buff, CPPU_STRINGIFY(CPPU_ENV));
239 		strcat(buff, purpose.getStr());
240 
241 		*envTypeName = strdup(buff);
242 	}
243 }
244