testsmgr_cpnt.cxx 9.5 KB
Newer Older
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
Michael Meeks's avatar
Michael Meeks committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
19

20 21 22 23 24
#include <rtl/alloc.h>
#include <osl/security.h>
#include <osl/thread.h>
#include <osl/mutex.hxx>
#include <cppuhelper/queryinterface.hxx>
25
#include <cppuhelper/supportsservice.hxx>
26 27 28 29 30 31
#include <cppuhelper/weak.hxx>
#include <uno/mapping.hxx>


#include <cppuhelper/factory.hxx>
#include <cppuhelper/servicefactory.hxx>
32
#include <cppuhelper/implbase.hxx>
33 34 35 36 37 38 39 40 41 42 43 44
#include <registry/registry.hxx>

#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/registry/XImplementationRegistration.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/container/XContentEnumerationAccess.hpp>
#include <com/sun/star/container/XEnumerationAccess.hpp>
#include <com/sun/star/container/XEnumeration.hpp>

45 46
#include <stdio.h>

47
#if defined ( UNX )
48 49 50 51 52 53 54 55 56
#include <limits.h>
#define _MAX_PATH PATH_MAX
#endif


#define IMPLEMENTATION_NAME "com.sun.star.DummyService.V10"
#define SERVICE_NAME "com.sun.star.ts.TestManagerImpl"


57 58 59 60
using namespace css::uno;
using namespace css::registry;
using namespace css::lang;
using namespace css::container;
61 62 63
using namespace osl;
using namespace cppu;

64

65 66 67 68 69 70 71
Reference<XMultiServiceFactory> getProcessServiceManager()
{
    Reference<XMultiServiceFactory > s_x;
    if (! s_x.is())
    {
        MutexGuard aGuard( Mutex::getGlobalMutex() );
        if (! s_x.is())
72
            s_x = createRegistryServiceFactory( OUString("stoctest.rdb"), sal_False );
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    }
    return s_x;
}

Reference< XMultiServiceFactory > createRegistryServiceManager( const OUString& registryName )
{
    return createRegistryServiceFactory( registryName );
}


/**********************************
* The service, that is used to test the Service manager
*
*
*
*************************************/
static sal_uInt32 nInstanceCount = 0;
90
class Test_Manager_Impl : public WeakImplHelper< XServiceInfo >
91 92 93 94 95 96 97 98
{
public:
    Test_Manager_Impl(){ nInstanceCount++;}
    ~Test_Manager_Impl();

    // XServiceInfo
    OUString                    SAL_CALL getImplementationName() throw();
    sal_Bool                    SAL_CALL supportsService(const OUString& ServiceName) throw();
99 100
    Sequence< OUString >        SAL_CALL getSupportedServiceNames() throw();
    static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static() throw();
101 102 103 104 105 106 107 108 109 110 111

private:
//  static XIdlClassRef     getStaticIdlClass();
};

Test_Manager_Impl::~Test_Manager_Impl()
{
    nInstanceCount--;
}


112
// old, is no longer needed by the new Mimic
113 114 115 116 117 118 119
Reference< XInterface > SAL_CALL Test_Manager_Impl_CreateInstance_Impl()
{
    return (OWeakObject *)new Test_Manager_Impl();
}


// Test_Manager_Impl_CreateInstance()
120

121
Reference < XInterface > SAL_CALL Test_Manager_Impl_CreateInstance(
122
    const Reference< XMultiServiceFactory > & /*rSMgr*/ ) throw (Exception)
123 124 125 126 127 128 129 130
{
    Reference < XInterface >  xService = (XWeak *)(OWeakObject *)new Test_Manager_Impl( );

    return xService;
}


// Test_Manager_Impl::getImplementationName
131

132 133
OUString Test_Manager_Impl::getImplementationName() throw()
{
134
    return OUString(IMPLEMENTATION_NAME);
135 136 137 138 139
}

// Test_Manager_Impl::supportsService
sal_Bool Test_Manager_Impl::supportsService( const OUString& ServiceName ) throw()
{
140
    return cppu::supportsService(this, ServiceName);
141 142
}

143

144
// Test_Manager_Impl::getSupportedServiceNames
145

146
Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames() throw ()
147 148 149 150
{
    return getSupportedServiceNames_Static();
}

151

152
// Test_Manager_Impl::getSupportedServiceNames_Static
153

154
Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames_Static() throw ()
155 156
{
    Sequence< OUString > aSNS( 2 );
157 158
    aSNS.getArray()[0] = SERVICE_NAME;
    aSNS.getArray()[1] = "com.sun.star.bridge.Bridge";
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    return aSNS;
}


/****
*
*
*  This routine performs the test of the process service manager ( getProcessServiceManager is called )
*
*
*
****/

extern "C" void SAL_CALL test_ServiceManager()
{
#if ! defined SAL_DLLPREFIX
#define SAL_DLLPREFIX ""
#endif
177
     OUString atUModule2 = SAL_DLLPREFIX "testsmgr_component" SAL_DLLEXTENSION ;
178 179 180 181 182 183 184

    // expand shared library name
    OString  atModule2( OUStringToOString(atUModule2, RTL_TEXTENCODING_ASCII_US) );

    // get the process servicemanager
    Reference <XMultiServiceFactory>  xSMgr = getProcessServiceManager();

185
    OSL_ENSURE( xSMgr.is() , "query on XServiceManager failed" );
186 187

    Reference<XContentEnumerationAccess> xContEnum(xSMgr, UNO_QUERY);
188
    OSL_ENSURE( xContEnum.is() , "query on XContentEnumerationAccess failed" );
189
    Reference<XEnumeration > xEnum(xContEnum->createContentEnumeration(OUString("com.sun.star.registry.SimpleRegistry")));
190
    OSL_ENSURE( xEnum.is() , "createContentEnumeration failed" );
191 192 193 194 195 196
    sal_Int32 nLen = 0;
    while( xEnum->hasMoreElements() )
    {
        nLen++;
        xEnum->nextElement();
    }
197
    OSL_ENSURE( nLen == 1, "more than one implementation for SimpleRegistry" );
198 199

    Reference<XEnumerationAccess> xImplEnum(xSMgr, UNO_QUERY);
200
    OSL_ENSURE( xImplEnum.is() , "query on XEnumeration failed" );
201
    xEnum.set(xImplEnum->createEnumeration());
202
    OSL_ENSURE( xEnum.is() , "createEnumeration failed" );
203 204 205 206 207 208 209 210
    nLen = 0;
    while( xEnum->hasMoreElements() )
    {
        nLen++;
        Reference< XServiceInfo > sf( xEnum->nextElement(), UNO_QUERY );
        OString str( OUStringToOString( sf->getImplementationName(), RTL_TEXTENCODING_ASCII_US ) );
        ::fprintf( stderr, "> implementation name: %s\n", str.getStr() );
    }
211
    OSL_ENSURE( nLen == 8, "more than 6 factories" );
212 213

    // try to get an instance for a unknown service
214
    OSL_VERIFY( !xSMgr->createInstance("bla.blup.Q").is() );
215

216

217
    // First test : register service via the internal function of the component itself
218

219 220
    {
        Reference< XImplementationRegistration >
221
            xInst( xSMgr->createInstance("com.sun.star.registry.ImplementationRegistration"), UNO_QUERY );
222
        OSL_ENSURE( xInst.is(), "no ImplementationRegistration" );
223 224 225

        try {
            // register the services via writeComponentRegInfo (see at end of this file)
226
            xInst->registerImplementation(OUString("com.sun.star.loader.SharedLibrary"), atUModule2, Reference< XSimpleRegistry >() );
227
        }
228
        catch(const CannotRegisterImplementationException &) {
229
            OSL_ENSURE( 0, "register implementation failed" );
230 231 232
        }

        // getImplementations() check
233
         Sequence<OUString> seqImpl = xInst->getImplementations(OUString("com.sun.star.loader.SharedLibrary"), atUModule2);
Andrea Gelmini's avatar
Andrea Gelmini committed
234
        OSL_ENSURE( seqImpl.getLength() == 1, "count of implementations is wrong" );
235
        OSL_ENSURE( seqImpl.getConstArray()[0] == "com.sun.star.DummyService.V10", "implementation name is not equal" );
236 237 238 239


        // tests, if a service provider can be instantiated.

240
        Reference< XInterface > xIFace(xSMgr->createInstance("com.sun.star.ts.TestManagerImpl"));
241
        OSL_ENSURE( xIFace.is(), "loadable service not found" );
242 243

        // remove the service
244
        OSL_VERIFY( xInst->revokeImplementation(atUModule2, Reference< XSimpleRegistry > ()) );
245 246 247 248 249 250 251 252 253 254 255 256
    }

    Reference<XComponent> xComp(xSMgr, UNO_QUERY);
    xComp->dispose();

    xComp.clear();
    xSMgr.clear();
}


extern "C"
{
257

258
sal_Bool SAL_CALL component_writeInfo(
259
    void * /*pServiceManager*/, void * pRegistryKey )
260 261 262 263 264 265 266
{
    if (pRegistryKey)
    {
        try
        {
            Reference< XRegistryKey > xNewKey(
                reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
267
                    OUString( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );
268 269 270 271 272 273 274 275 276 277 278

            const Sequence< OUString > & rSNL =
                Test_Manager_Impl::getSupportedServiceNames_Static();
            const OUString * pArray = rSNL.getConstArray();
            for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
                xNewKey->createKey( pArray[nPos] );

            return sal_True;
        }
        catch (InvalidRegistryException &)
        {
279
            OSL_FAIL( "### InvalidRegistryException!" );
280 281 282 283
        }
    }
    return sal_False;
}
284

285
SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
286
    const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
287 288 289 290 291 292 293
{
    void * pRet = 0;

    if (rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
    {
        Reference< XSingleServiceFactory > xFactory( createSingleFactory(
            reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
294
            OUString( IMPLEMENTATION_NAME ),
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
            Test_Manager_Impl_CreateInstance,
            Test_Manager_Impl::getSupportedServiceNames_Static() ) );

        if (xFactory.is())
        {
            xFactory->acquire();
            pRet = xFactory.get();
        }
    }

    return pRet;
}
}


310
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */