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