Update ooo320-m1
[ooovba.git] / cppuhelper / test / testcmp / TestComponent.cxx
blob0829f3d80dce6d8c83c4f96916daabda173d1b92
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TestComponent.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppuhelper.hxx"
35 #define CPPUHELPER_TEST_COMPONENT_IMPL
36 #include "TestComponent.hxx"
39 #include <string.h>
41 #include "osl/thread.h"
43 #include "cppuhelper/implbase1.hxx"
44 #include "cppuhelper/implementationentry.hxx"
46 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
47 #include "com/sun/star/lang/XServiceInfo.hpp"
49 #include "com/sun/star/uno/XComponentContext.hpp"
52 #include "cppu/EnvDcp.hxx"
54 #include <uno/environment.hxx>
56 using namespace ::com::sun::star;
59 #define LOG_LIFECYCLE_TestComponent
60 #ifdef LOG_LIFECYCLE_TestComponent
61 # include <iostream>
62 # define LOG_LIFECYCLE_TestComponent_emit(x) x
64 #else
65 # define LOG_LIFECYCLE_TestComponent_emit(x)
67 #endif
70 class TestComponent: public cppu::WeakImplHelper1<lang::XServiceInfo>
72 rtl::OUString m_implName;
74 public:
75 static uno::Reference<uno::XInterface> create(
76 uno::Reference<uno::XComponentContext> const & xCtx
78 SAL_THROW((uno::Exception));
81 static uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames_Static();
83 explicit TestComponent(uno::Reference<uno::XComponentContext> const & xCtx);
84 virtual ~TestComponent();
86 uno::Any SAL_CALL queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException);
87 void SAL_CALL release() throw ();
88 void SAL_CALL acquire() throw ();
90 // lang::XServiceInfo
91 virtual rtl::OUString SAL_CALL getImplementationName() throw (uno::RuntimeException);
92 virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
93 throw (uno::RuntimeException);
94 virtual uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames()
95 throw (uno::RuntimeException);
97 protected:
98 uno::Reference<uno::XComponentContext> m_xComponentContext;
102 uno::Reference<uno::XInterface> SAL_CALL TestComponent::create(
103 uno::Reference<uno::XComponentContext> const & xCtx
105 SAL_THROW((uno::Exception))
109 return static_cast<cppu::OWeakObject *>(new TestComponent(xCtx));
111 catch (std::bad_alloc &)
113 throw uno::RuntimeException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
114 uno::Reference<uno::XInterface>());
118 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames_Static()
120 uno::Sequence<rtl::OUString> serviceNames(1);
121 serviceNames[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.lang.ServiceInfo"));
123 return serviceNames;
127 TestComponent::TestComponent(uno::Reference<uno::XComponentContext> const & xCtx)
128 : m_xComponentContext(xCtx)
130 LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::TestComponent()", this));
133 TestComponent::~TestComponent()
135 LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::~TestComponent", this));
138 rtl::OUString SAL_CALL TestComponent::getImplementationName()
139 throw (uno::RuntimeException)
141 return m_implName;
144 void SAL_CALL TestComponent::acquire() throw ()
146 cppu::WeakImplHelper1<lang::XServiceInfo>::acquire();
149 void SAL_CALL TestComponent::release() throw ()
151 cppu::WeakImplHelper1<lang::XServiceInfo>::release();
154 uno::Any SAL_CALL TestComponent::queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException)
156 return cppu::WeakImplHelper1<lang::XServiceInfo>::queryInterface(rType);
159 sal_Bool SAL_CALL TestComponent::supportsService(rtl::OUString const & ServiceName)
160 throw (uno::RuntimeException)
162 uno::Sequence<rtl::OUString> serviceNames = getSupportedServiceNames_Static();
164 for (sal_Int32 n = 0; n < serviceNames.getLength(); ++n)
166 if (serviceNames[n] == ServiceName)
167 return true;
170 return false;
173 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames()
174 throw (uno::RuntimeException)
176 return getSupportedServiceNames_Static();
179 extern "C" sal_Bool SAL_CALL component_writeInfo(
180 void * /*serviceManager*/,
181 void * /*registryKey*/
184 g_envDcp = uno::Environment::getCurrent().getTypeName();
186 return true;
189 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
190 char const * pImplName,
191 void * /*serviceManager*/,
192 void * /*registryKey*/
195 g_envDcp = uno::Environment::getCurrent().getTypeName();
197 uno::Reference< lang::XSingleComponentFactory > xFactory;
199 rtl::OUString uTmp(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
201 rtl::OUString uImplName(cppu::EnvDcp::getTypeName(uTmp));
202 rtl::OUString cmpName(RTL_CONSTASCII_USTRINGPARAM("impl.test.TestComponent"));
204 if (uImplName.equals(cmpName))
206 xFactory = cppu::createSingleComponentFactory(
207 TestComponent::create,
208 uImplName,
209 TestComponent::getSupportedServiceNames_Static());
211 xFactory->acquire();
214 return xFactory.get();
217 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironmentExt(
218 sal_Char const ** envTypeName,
219 uno_Environment ** /*ppEnv*/,
220 sal_Char const * pImplName,
221 uno_Environment * /*pSrcEnv*/
224 rtl::OString purpose;
226 if (pImplName) // this is the purpose for a specified impl
228 rtl::OUString uImplName(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
229 purpose = rtl::OUStringToOString(cppu::EnvDcp::getPurpose(uImplName), RTL_TEXTENCODING_ASCII_US);
232 if (!purpose.getLength())
234 char * pPurpose = getenv("TestComponent.uno");
235 if (pPurpose)
236 purpose = rtl::OString(pPurpose);
239 if (purpose.getLength() == 0)
240 *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
242 else
244 char buff[256];
245 strcpy(buff, CPPU_STRINGIFY(CPPU_ENV));
246 strcat(buff, purpose.getStr());
248 *envTypeName = strdup(buff);