1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppuhelper.hxx"
30 
31 
32 #define CPPUHELPER_TEST_COMPONENT_IMPL
33 #include "TestComponent.hxx"
34 
35 
36 #include <string.h>
37 
38 #include "osl/thread.h"
39 
40 #include "cppuhelper/implbase1.hxx"
41 #include "cppuhelper/implementationentry.hxx"
42 
43 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
44 #include "com/sun/star/lang/XServiceInfo.hpp"
45 
46 #include "com/sun/star/uno/XComponentContext.hpp"
47 
48 
49 #include "cppu/EnvDcp.hxx"
50 
51 #include <uno/environment.hxx>
52 
53 using namespace ::com::sun::star;
54 
55 
56 #define LOG_LIFECYCLE_TestComponent
57 #ifdef LOG_LIFECYCLE_TestComponent
58 #  include <iostream>
59 #  define LOG_LIFECYCLE_TestComponent_emit(x) x
60 
61 #else
62 #  define LOG_LIFECYCLE_TestComponent_emit(x)
63 
64 #endif
65 
66 
67 class TestComponent: public cppu::WeakImplHelper1<lang::XServiceInfo>
68 {
69 	rtl::OUString m_implName;
70 
71 public:
72 	static uno::Reference<uno::XInterface> create(
73 		uno::Reference<uno::XComponentContext> const & xCtx
74     )
75 	SAL_THROW((uno::Exception));
76 
77 
78 	static uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames_Static();
79 
80 	explicit TestComponent(uno::Reference<uno::XComponentContext> const & xCtx);
81 	virtual ~TestComponent();
82 
83 	uno::Any SAL_CALL queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException);
84 	void SAL_CALL release() throw ();
85 	void SAL_CALL acquire() throw ();
86 
87 	// lang::XServiceInfo
88 	virtual rtl::OUString SAL_CALL getImplementationName() throw (uno::RuntimeException);
89 	virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
90 		throw (uno::RuntimeException);
91 	virtual uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames()
92 		throw (uno::RuntimeException);
93 
94 protected:
95 	uno::Reference<uno::XComponentContext> m_xComponentContext;
96 };
97 
98 
99 uno::Reference<uno::XInterface> SAL_CALL TestComponent::create(
100 	uno::Reference<uno::XComponentContext> const & xCtx
101 )
102 	SAL_THROW((uno::Exception))
103 {
104 	try
105 	{
106 		return static_cast<cppu::OWeakObject *>(new TestComponent(xCtx));
107 	}
108 	catch (std::bad_alloc &)
109 	{
110 		throw uno::RuntimeException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
111 									 uno::Reference<uno::XInterface>());
112 	}
113 }
114 
115 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames_Static()
116 {
117 	uno::Sequence<rtl::OUString> serviceNames(1);
118 	serviceNames[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.lang.ServiceInfo"));
119 
120 	return serviceNames;
121 }
122 
123 
124 TestComponent::TestComponent(uno::Reference<uno::XComponentContext> const & xCtx)
125 	: m_xComponentContext(xCtx)
126 {
127 	LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::TestComponent()", this));
128 }
129 
130 TestComponent::~TestComponent()
131 {
132 	LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::~TestComponent", this));
133 }
134 
135 rtl::OUString SAL_CALL TestComponent::getImplementationName()
136 	throw (uno::RuntimeException)
137 {
138 	return m_implName;
139 }
140 
141 void SAL_CALL TestComponent::acquire() throw ()
142 {
143  	cppu::WeakImplHelper1<lang::XServiceInfo>::acquire();
144 }
145 
146 void SAL_CALL TestComponent::release() throw ()
147 {
148 	cppu::WeakImplHelper1<lang::XServiceInfo>::release();
149 }
150 
151 uno::Any SAL_CALL TestComponent::queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException)
152 {
153 	return cppu::WeakImplHelper1<lang::XServiceInfo>::queryInterface(rType);
154 }
155 
156 sal_Bool SAL_CALL TestComponent::supportsService(rtl::OUString const & ServiceName)
157 	throw (uno::RuntimeException)
158 {
159 	uno::Sequence<rtl::OUString> serviceNames = getSupportedServiceNames_Static();
160 
161 	for (sal_Int32 n = 0; n < serviceNames.getLength(); ++n)
162 	{
163 		if (serviceNames[n] == ServiceName)
164 			return true;
165 	}
166 
167 	return false;
168 }
169 
170 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames()
171 	throw (uno::RuntimeException)
172 {
173 	return getSupportedServiceNames_Static();
174 }
175 
176 extern "C" sal_Bool SAL_CALL component_writeInfo(
177 	void * /*serviceManager*/,
178 	void * /*registryKey*/
179 )
180 {
181 	g_envDcp = uno::Environment::getCurrent().getTypeName();
182 
183 	return true;
184 }
185 
186 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
187 	char const * pImplName,
188 	void       * /*serviceManager*/,
189 	void       * /*registryKey*/
190 )
191 {
192 	g_envDcp = uno::Environment::getCurrent().getTypeName();
193 
194 	uno::Reference< lang::XSingleComponentFactory > xFactory;
195 
196     rtl::OUString uTmp(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
197 
198     rtl::OUString uImplName(cppu::EnvDcp::getTypeName(uTmp));
199 	rtl::OUString cmpName(RTL_CONSTASCII_USTRINGPARAM("impl.test.TestComponent"));
200 
201 	if (uImplName.equals(cmpName))
202 	{
203 		xFactory = cppu::createSingleComponentFactory(
204 			TestComponent::create,
205 			uImplName,
206 			TestComponent::getSupportedServiceNames_Static());
207 
208 		xFactory->acquire();
209 	}
210 
211 	return xFactory.get();
212 }
213 
214 extern "C" SAL_DLLPUBLIC_EXPORT  void SAL_CALL component_getImplementationEnvironmentExt(
215 	sal_Char        const ** envTypeName,
216 	uno_Environment       ** /*ppEnv*/,
217 	sal_Char        const  * pImplName,
218 	uno_Environment        * /*pSrcEnv*/
219 )
220 {
221 	rtl::OString purpose;
222 
223     if (pImplName) // this is the purpose for a specified impl
224     {
225         rtl::OUString uImplName(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
226         purpose = rtl::OUStringToOString(cppu::EnvDcp::getPurpose(uImplName), RTL_TEXTENCODING_ASCII_US);
227     }
228 
229     if (!purpose.getLength())
230     {
231         char * pPurpose = getenv("TestComponent.uno");
232         if (pPurpose)
233             purpose = rtl::OString(pPurpose);
234     }
235 
236 	if (purpose.getLength() == 0)
237 		*envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
238 
239 	else
240 	{
241 		char buff[256];
242 		strcpy(buff, CPPU_STRINGIFY(CPPU_ENV));
243 		strcat(buff, purpose.getStr());
244 
245 		*envTypeName = strdup(buff);
246 	}
247 }
248