1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 30*cdf0e10cSrcweir #include "precompiled_sal.hxx" 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #include<osl/module.hxx> 33*cdf0e10cSrcweir #include <osl/time.h> 34*cdf0e10cSrcweir #include <rtl/ustring.hxx> 35*cdf0e10cSrcweir #include <stdio.h> 36*cdf0e10cSrcweir #include <cppuhelper/factory.hxx> 37*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx> 38*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp> 40*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 41*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp> 42*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 43*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp> 44*cdf0e10cSrcweir #include <com/sun/star/uno/XUnloadingPreference.hpp> 45*cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp> 46*cdf0e10cSrcweir #include <com/sun/star/container/XContentEnumerationAccess.hpp> 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir #include <stdio.h> 49*cdf0e10cSrcweir using namespace ::rtl; 50*cdf0e10cSrcweir using namespace ::osl; 51*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 52*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 53*cdf0e10cSrcweir using namespace ::cppu; 54*cdf0e10cSrcweir using namespace ::com::sun::star::beans; 55*cdf0e10cSrcweir using namespace ::com::sun::star::container; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir #define IMPLNAME1 "com.sun.star.comp.sal.UnloadingTest1" 58*cdf0e10cSrcweir #define SERVICENAME1 "com.sun.star.UnloadingTest1" 59*cdf0e10cSrcweir #define IMPLNAME2 "com.sun.star.comp.sal.UnloadingTest2" 60*cdf0e10cSrcweir #define SERVICENAME2 "com.sun.star.UnloadingTest2" 61*cdf0e10cSrcweir #define IMPLNAME3 "com.sun.star.comp.sal.UnloadingTest3" 62*cdf0e10cSrcweir #define SERVICENAME3 "com.sun.star.UnloadingTest3" 63*cdf0e10cSrcweir #define IMPLNAME4 "com.sun.star.comp.sal.OneInstanceTest" 64*cdf0e10cSrcweir #define SERVICENAME4 "com.sun.star.OneInstanceTest" 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir #define IMPLNAME21 "com.sun.star.comp.sal.UnloadingTest21" 67*cdf0e10cSrcweir #define SERVICENAME21 "com.sun.star.UnloadingTest21" 68*cdf0e10cSrcweir #define IMPLNAME22 "com.sun.star.comp.sal.UnloadingTest22" 69*cdf0e10cSrcweir #define SERVICENAME22 "com.sun.star.UnloadingTest22" 70*cdf0e10cSrcweir #define IMPLNAME23 "com.sun.star.comp.sal.UnloadingTest23" 71*cdf0e10cSrcweir #define SERVICENAME23 "com.sun.star.UnloadingTest23" 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir #ifdef UNX 74*cdf0e10cSrcweir #define LIBRARY1 "libsamplelib1.so" 75*cdf0e10cSrcweir #define LIBRARY2 "libsamplelib2.so" 76*cdf0e10cSrcweir #elif defined WNT 77*cdf0e10cSrcweir #define LIBRARY1 "samplelib1" 78*cdf0e10cSrcweir #define LIBRARY2 "samplelib2" 79*cdf0e10cSrcweir #endif 80*cdf0e10cSrcweir /* 81*cdf0e10cSrcweir Tested: rtl_registerModuleForUnloading, rtl_unregisterModuleForUnloading, rtl_unloadUnusedLibraries 82*cdf0e10cSrcweir 1 component. 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir next: Test with multiple components 85*cdf0e10cSrcweir Listener mechanism. 86*cdf0e10cSrcweir */ 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir sal_Bool test1(); 89*cdf0e10cSrcweir sal_Bool test2(); 90*cdf0e10cSrcweir sal_Bool test3(); 91*cdf0e10cSrcweir sal_Bool test4(); 92*cdf0e10cSrcweir sal_Bool test5(); 93*cdf0e10cSrcweir sal_Bool test6(); 94*cdf0e10cSrcweir sal_Bool test7(); 95*cdf0e10cSrcweir sal_Bool test8(); 96*cdf0e10cSrcweir sal_Bool test9(); 97*cdf0e10cSrcweir void SAL_CALL listenerCallback( void* id); 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir int main(int argc, char* argv[]) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir // Test if the servicemanager can be created and if the sample libs 102*cdf0e10cSrcweir // can be loaded 103*cdf0e10cSrcweir // Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory( 104*cdf0e10cSrcweir // OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb"))); 105*cdf0e10cSrcweir // if( !serviceManager.is()) 106*cdf0e10cSrcweir // { 107*cdf0e10cSrcweir // printf("\n ####################################################\n" 108*cdf0e10cSrcweir // "Error: could not create service manager. \n" 109*cdf0e10cSrcweir // "Is the executable in the office program directory?\n"); 110*cdf0e10cSrcweir // return -1; 111*cdf0e10cSrcweir // } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir // Reference<XInterface> xint1= serviceManager->createInstance( OUString( 114*cdf0e10cSrcweir // RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1))); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir // if( !xint1.is()) 117*cdf0e10cSrcweir // { 118*cdf0e10cSrcweir // printf("\n ###################################################\n" 119*cdf0e10cSrcweir // "Error: could not create service from samplelib1\n" 120*cdf0e10cSrcweir // "Is samplelib1 in the office program directory and is it " 121*cdf0e10cSrcweir // "registered?\n"); 122*cdf0e10cSrcweir // return -1; 123*cdf0e10cSrcweir // } 124*cdf0e10cSrcweir // Reference<XInterface> xint2= serviceManager->createInstance( OUString( 125*cdf0e10cSrcweir // RTL_CONSTASCII_USTRINGPARAM(SERVICENAME21))); 126*cdf0e10cSrcweir // if( !xint2.is()) 127*cdf0e10cSrcweir // { 128*cdf0e10cSrcweir // printf("\n ###################################################" 129*cdf0e10cSrcweir // "Error: could not create service from samplelib2\n" 130*cdf0e10cSrcweir // "Is samplelib2 in the office program directory and is it " 131*cdf0e10cSrcweir // "registered?\n"); 132*cdf0e10cSrcweir // return -1; 133*cdf0e10cSrcweir // } 134*cdf0e10cSrcweir // //destroy servicemanager 135*cdf0e10cSrcweir // Reference<XPropertySet> xSet( serviceManager, UNO_QUERY); 136*cdf0e10cSrcweir // Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))); 137*cdf0e10cSrcweir // Reference<XComponentContext> xContext; 138*cdf0e10cSrcweir // any_prop >>= xContext; 139*cdf0e10cSrcweir // Reference<XComponent> xComponent( xContext, UNO_QUERY); 140*cdf0e10cSrcweir // xComponent->dispose(); 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir // //unload samplelib1 and samplelib2. We need the handles, therefore load 143*cdf0e10cSrcweir // // the libs 144*cdf0e10cSrcweir // OUString libname1( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1)); 145*cdf0e10cSrcweir // OUString libname2( RTL_CONSTASCII_USTRINGPARAM(LIBRARY2)); 146*cdf0e10cSrcweir // oslModule m1= osl_loadModule(libname1.pData, 0); 147*cdf0e10cSrcweir // oslModule m2= osl_loadModule(libname2.pData, 0); 148*cdf0e10cSrcweir // osl_unloadModule( m1); 149*cdf0e10cSrcweir // osl_unloadModule( m1); 150*cdf0e10cSrcweir // osl_unloadModule( m2); 151*cdf0e10cSrcweir // osl_unloadModule( m2); 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir sal_Bool ret1= test1(); 155*cdf0e10cSrcweir if( ret1) printf( "\n Test 1 successful \n"); 156*cdf0e10cSrcweir else printf("\n !!!!!! Test 1 failed\n"); 157*cdf0e10cSrcweir sal_Bool ret2= test2(); 158*cdf0e10cSrcweir if( ret2) printf( "\n Test 2 successful \n"); 159*cdf0e10cSrcweir else printf("\n !!!!!! Test 2 failed\n"); 160*cdf0e10cSrcweir sal_Bool ret3= test3(); 161*cdf0e10cSrcweir if( ret3) printf( "\n Test 3 successful \n"); 162*cdf0e10cSrcweir else printf("\n !!!!!! Test 3 failed\n"); 163*cdf0e10cSrcweir sal_Bool ret4= test4(); 164*cdf0e10cSrcweir if( ret4) printf( "\n Test 4 successful \n"); 165*cdf0e10cSrcweir else printf("\n !!!!!! Test 4 failed\n"); 166*cdf0e10cSrcweir sal_Bool ret5= test5(); 167*cdf0e10cSrcweir if( ret5) printf( "\n Test 5 successful \n"); 168*cdf0e10cSrcweir else printf("\n !!!!!! Test 5 failed\n"); 169*cdf0e10cSrcweir // takes some time (10s) 170*cdf0e10cSrcweir sal_Bool ret6= test6(); 171*cdf0e10cSrcweir sal_Bool ret7= test7(); // prints message itself 172*cdf0e10cSrcweir sal_Bool ret8= test8(); 173*cdf0e10cSrcweir if( ret8) printf( "\n Test 8 successful \n"); 174*cdf0e10cSrcweir else printf("\n !!!!!! Test 8 failed\n"); 175*cdf0e10cSrcweir sal_Bool ret9= test9(); 176*cdf0e10cSrcweir if( ret9) printf( "\n Test 9 successful: service manager is unloading listener\n"); 177*cdf0e10cSrcweir else printf("\n !!!!! Test 9 failed\n"); 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir return 0; 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir /* Create an instance of SERVICENAME1, call a function and unload module. 183*cdf0e10cSrcweir This tests the loader and basic functionality. 184*cdf0e10cSrcweir The library will be loaded once manually and the handle will be stored. 185*cdf0e10cSrcweir Then the library will be unloaded. After rtl_unloadUnusedLibraries we try to 186*cdf0e10cSrcweir get a symbol of the unloaded lib. If this fails then the test is successful. 187*cdf0e10cSrcweir */ 188*cdf0e10cSrcweir sal_Bool test1() 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir printf("Test 1 ####################################################\n"); 191*cdf0e10cSrcweir oslModule handleMod=0; 192*cdf0e10cSrcweir OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1)); 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory( 195*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb"))); 196*cdf0e10cSrcweir Reference<XInterface> xint= serviceManager->createInstance( OUString( 197*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1))); 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir // get the handle 200*cdf0e10cSrcweir handleMod= osl_loadModule( lib1Name.pData, 0); 201*cdf0e10cSrcweir osl_unloadModule( handleMod); 202*cdf0e10cSrcweir xint=0; 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir Reference<XPropertySet> xSet( serviceManager, UNO_QUERY); 205*cdf0e10cSrcweir Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))); 206*cdf0e10cSrcweir Reference<XComponentContext> xContext; 207*cdf0e10cSrcweir any_prop >>= xContext; 208*cdf0e10cSrcweir Reference<XComponent> xComponent( xContext, UNO_QUERY); 209*cdf0e10cSrcweir xComponent->dispose(); 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir rtl_unloadUnusedModules( NULL); 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir // Try to get a symbol, must fail 214*cdf0e10cSrcweir OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory")); 215*cdf0e10cSrcweir void* pSymbol= osl_getSymbol( handleMod, sSymbol.pData); 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir if( !pSymbol) 218*cdf0e10cSrcweir return sal_True; 219*cdf0e10cSrcweir return sal_False; 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir /* Multipe loadModule + rtl_registerModuleForUnloading. 223*cdf0e10cSrcweir The module will be registered as often as it has been loaded. 224*cdf0e10cSrcweir */ 225*cdf0e10cSrcweir sal_Bool test2() 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir printf("Test 2 ####################################################\n"); 228*cdf0e10cSrcweir oslModule handleMod=0; 229*cdf0e10cSrcweir OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1)); 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory( 232*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb"))); 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir Reference<XInterface> xint= serviceManager->createInstance( OUString( 235*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1))); 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir handleMod= osl_loadModule( lib1Name.pData, 0); 238*cdf0e10cSrcweir osl_unloadModule( handleMod); 239*cdf0e10cSrcweir //----------------------------------------------------------- 240*cdf0e10cSrcweir oslModule mod1= osl_loadModule( lib1Name.pData, 0); 241*cdf0e10cSrcweir oslModule mod2= osl_loadModule( lib1Name.pData, 0); 242*cdf0e10cSrcweir oslModule mod3= osl_loadModule( lib1Name.pData, 0); 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir rtl_registerModuleForUnloading(mod1); 245*cdf0e10cSrcweir rtl_registerModuleForUnloading(mod2); 246*cdf0e10cSrcweir rtl_registerModuleForUnloading(mod3); 247*cdf0e10cSrcweir // ---------------------------------------------------------- 248*cdf0e10cSrcweir Reference<XPropertySet> xSet( serviceManager, UNO_QUERY); 249*cdf0e10cSrcweir Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))); 250*cdf0e10cSrcweir Reference<XComponentContext> xContext; 251*cdf0e10cSrcweir any_prop >>= xContext; 252*cdf0e10cSrcweir Reference<XComponent> xComponent( xContext, UNO_QUERY); 253*cdf0e10cSrcweir xComponent->dispose(); 254*cdf0e10cSrcweir } 255*cdf0e10cSrcweir rtl_unloadUnusedModules( NULL); 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir // Try to get a symbol, must fail 258*cdf0e10cSrcweir OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory")); 259*cdf0e10cSrcweir void* pSymbol= osl_getSymbol( handleMod, sSymbol.pData); 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir if( !pSymbol) 262*cdf0e10cSrcweir return sal_True; 263*cdf0e10cSrcweir return sal_False; 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir /* Multipe loadModule + rtl_registerModuleForUnloading. 267*cdf0e10cSrcweir The module will be registered one time less as it has been loaded. 268*cdf0e10cSrcweir */ 269*cdf0e10cSrcweir sal_Bool test3() 270*cdf0e10cSrcweir { 271*cdf0e10cSrcweir printf("Test 3 ####################################################\n"); 272*cdf0e10cSrcweir oslModule handleMod=0; 273*cdf0e10cSrcweir sal_Bool retval=sal_False; 274*cdf0e10cSrcweir OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1)); 275*cdf0e10cSrcweir { 276*cdf0e10cSrcweir Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory( 277*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb"))); 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir Reference<XInterface> xint= serviceManager->createInstance( OUString( 280*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1))); 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir handleMod= osl_loadModule( lib1Name.pData, 0); 283*cdf0e10cSrcweir osl_unloadModule( handleMod); 284*cdf0e10cSrcweir //----------------------------------------------------------- 285*cdf0e10cSrcweir oslModule mod1= osl_loadModule( lib1Name.pData, 0); 286*cdf0e10cSrcweir oslModule mod2= osl_loadModule( lib1Name.pData, 0); 287*cdf0e10cSrcweir oslModule mod3= osl_loadModule( lib1Name.pData, 0); 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir rtl_registerModuleForUnloading(mod1); 290*cdf0e10cSrcweir rtl_registerModuleForUnloading(mod2); 291*cdf0e10cSrcweir // ---------------------------------------------------------- 292*cdf0e10cSrcweir Reference<XPropertySet> xSet( serviceManager, UNO_QUERY); 293*cdf0e10cSrcweir Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))); 294*cdf0e10cSrcweir Reference<XComponentContext> xContext; 295*cdf0e10cSrcweir any_prop >>= xContext; 296*cdf0e10cSrcweir Reference<XComponent> xComponent( xContext, UNO_QUERY); 297*cdf0e10cSrcweir xComponent->dispose(); 298*cdf0e10cSrcweir } 299*cdf0e10cSrcweir rtl_unloadUnusedModules( NULL); 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir // Try to get a symbol, must succeed 302*cdf0e10cSrcweir OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory")); 303*cdf0e10cSrcweir void* pSymbol= osl_getSymbol( handleMod, sSymbol.pData); 304*cdf0e10cSrcweir 305*cdf0e10cSrcweir if( pSymbol) 306*cdf0e10cSrcweir { 307*cdf0e10cSrcweir retval= sal_True; 308*cdf0e10cSrcweir osl_unloadModule( handleMod); 309*cdf0e10cSrcweir pSymbol= osl_getSymbol( handleMod, sSymbol.pData); 310*cdf0e10cSrcweir } 311*cdf0e10cSrcweir return retval; 312*cdf0e10cSrcweir } 313*cdf0e10cSrcweir /* 2 Modules 314*cdf0e10cSrcweir 315*cdf0e10cSrcweir */ 316*cdf0e10cSrcweir sal_Bool test4() 317*cdf0e10cSrcweir { 318*cdf0e10cSrcweir printf("Test 4 ####################################################\n"); 319*cdf0e10cSrcweir oslModule handleMod1=0; 320*cdf0e10cSrcweir oslModule handleMod2=0; 321*cdf0e10cSrcweir OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1)); 322*cdf0e10cSrcweir OUString lib2Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY2)); 323*cdf0e10cSrcweir { 324*cdf0e10cSrcweir Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory( 325*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb"))); 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir Reference<XInterface> xint= serviceManager->createInstance( OUString( 328*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1))); 329*cdf0e10cSrcweir 330*cdf0e10cSrcweir handleMod1= osl_loadModule( lib1Name.pData, 0); 331*cdf0e10cSrcweir osl_unloadModule( handleMod1); 332*cdf0e10cSrcweir Reference<XInterface> xint2= serviceManager->createInstance( OUString( 333*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME21))); 334*cdf0e10cSrcweir 335*cdf0e10cSrcweir handleMod2= osl_loadModule( lib2Name.pData, 0); 336*cdf0e10cSrcweir osl_unloadModule( handleMod2); 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir //----------------------------------------------------------- 339*cdf0e10cSrcweir // ---------------------------------------------------------- 340*cdf0e10cSrcweir Reference<XPropertySet> xSet( serviceManager, UNO_QUERY); 341*cdf0e10cSrcweir Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))); 342*cdf0e10cSrcweir Reference<XComponentContext> xContext; 343*cdf0e10cSrcweir any_prop >>= xContext; 344*cdf0e10cSrcweir Reference<XComponent> xComponent( xContext, UNO_QUERY); 345*cdf0e10cSrcweir xComponent->dispose(); 346*cdf0e10cSrcweir } 347*cdf0e10cSrcweir rtl_unloadUnusedModules( NULL); 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir // Try to get a symbol, must fail 350*cdf0e10cSrcweir OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory")); 351*cdf0e10cSrcweir void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData); 352*cdf0e10cSrcweir 353*cdf0e10cSrcweir void* pSymbol2= osl_getSymbol( handleMod2, sSymbol.pData); 354*cdf0e10cSrcweir if( ! pSymbol && !pSymbol2) 355*cdf0e10cSrcweir return sal_True; 356*cdf0e10cSrcweir return sal_False; 357*cdf0e10cSrcweir } 358*cdf0e10cSrcweir 359*cdf0e10cSrcweir /* 2 Modules and 6 services 360*cdf0e10cSrcweir 361*cdf0e10cSrcweir */ 362*cdf0e10cSrcweir sal_Bool test5() 363*cdf0e10cSrcweir { 364*cdf0e10cSrcweir printf("test5 ####################################################\n"); 365*cdf0e10cSrcweir oslModule handleMod1=0; 366*cdf0e10cSrcweir oslModule handleMod2=0; 367*cdf0e10cSrcweir sal_Bool btest1= sal_False; 368*cdf0e10cSrcweir OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1)); 369*cdf0e10cSrcweir OUString lib2Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY2)); 370*cdf0e10cSrcweir OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory")); 371*cdf0e10cSrcweir { 372*cdf0e10cSrcweir Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory( 373*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb"))); 374*cdf0e10cSrcweir 375*cdf0e10cSrcweir //----------------------------------------------------------- 376*cdf0e10cSrcweir Reference<XInterface> xint= serviceManager->createInstance( OUString( 377*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1))); 378*cdf0e10cSrcweir Reference<XInterface> xint2= serviceManager->createInstance( OUString( 379*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME2))); 380*cdf0e10cSrcweir Reference<XInterface> xint3= serviceManager->createInstance( OUString( 381*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME3))); 382*cdf0e10cSrcweir Reference<XInterface> xint4= serviceManager->createInstance( OUString( 383*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME21))); 384*cdf0e10cSrcweir Reference<XInterface> xint5= serviceManager->createInstance( OUString( 385*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME22))); 386*cdf0e10cSrcweir Reference<XInterface> xint6= serviceManager->createInstance( OUString( 387*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME23))); 388*cdf0e10cSrcweir 389*cdf0e10cSrcweir // ---------------------------------------------------------- 390*cdf0e10cSrcweir handleMod1= osl_loadModule( lib1Name.pData, 0); 391*cdf0e10cSrcweir osl_unloadModule( handleMod1); 392*cdf0e10cSrcweir handleMod2= osl_loadModule( lib2Name.pData, 0); 393*cdf0e10cSrcweir osl_unloadModule( handleMod2); 394*cdf0e10cSrcweir 395*cdf0e10cSrcweir // get rid of the service manager 396*cdf0e10cSrcweir Reference<XPropertySet> xSet( serviceManager, UNO_QUERY); 397*cdf0e10cSrcweir Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))); 398*cdf0e10cSrcweir Reference<XComponentContext> xContext; 399*cdf0e10cSrcweir any_prop >>= xContext; 400*cdf0e10cSrcweir Reference<XComponent> xComponent( xContext, UNO_QUERY); 401*cdf0e10cSrcweir xComponent->dispose(); 402*cdf0e10cSrcweir 403*cdf0e10cSrcweir // try unloading, must fail 404*cdf0e10cSrcweir rtl_unloadUnusedModules( NULL); 405*cdf0e10cSrcweir void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData); 406*cdf0e10cSrcweir 407*cdf0e10cSrcweir void* pSymbol2= osl_getSymbol( handleMod2, sSymbol.pData); 408*cdf0e10cSrcweir if( pSymbol && pSymbol2) 409*cdf0e10cSrcweir btest1= sal_True; 410*cdf0e10cSrcweir 411*cdf0e10cSrcweir } 412*cdf0e10cSrcweir 413*cdf0e10cSrcweir // Try to get a symbol, must succeed 414*cdf0e10cSrcweir rtl_unloadUnusedModules( NULL); 415*cdf0e10cSrcweir 416*cdf0e10cSrcweir void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData); 417*cdf0e10cSrcweir void* pSymbol2= osl_getSymbol( handleMod2, sSymbol.pData); 418*cdf0e10cSrcweir if( ! pSymbol && !pSymbol2 && btest1) 419*cdf0e10cSrcweir return sal_True; 420*cdf0e10cSrcweir return sal_False; 421*cdf0e10cSrcweir } 422*cdf0e10cSrcweir 423*cdf0e10cSrcweir /* 424*cdf0e10cSrcweir TimeValue Test 425*cdf0e10cSrcweir rtl_unloadUnusedModules takes a TimeValue which determines a timespan 426*cdf0e10cSrcweir a module must have been constantly unused in order to be unloaded. 427*cdf0e10cSrcweir This is only a rough test. To make accurate tests, one should directly 428*cdf0e10cSrcweir write code in the unload.cxx file. 429*cdf0e10cSrcweir The function will not return (loop) when the test fails or the result value 430*cdf0e10cSrcweir is far off the 10 seconds value. 431*cdf0e10cSrcweir */ 432*cdf0e10cSrcweir sal_Bool test6() 433*cdf0e10cSrcweir { 434*cdf0e10cSrcweir printf("test6 ####################################################\n"); 435*cdf0e10cSrcweir oslModule handleMod1=0; 436*cdf0e10cSrcweir oslModule handleMod2=0; 437*cdf0e10cSrcweir OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1)); 438*cdf0e10cSrcweir OUString lib2Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY2)); 439*cdf0e10cSrcweir OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory")); 440*cdf0e10cSrcweir { 441*cdf0e10cSrcweir Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory( 442*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb"))); 443*cdf0e10cSrcweir Reference<XInterface> xint= serviceManager->createInstance( OUString( 444*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1))); 445*cdf0e10cSrcweir 446*cdf0e10cSrcweir // ---------------------------------------------------------- 447*cdf0e10cSrcweir handleMod1= osl_loadModule( lib1Name.pData, 0); 448*cdf0e10cSrcweir osl_unloadModule( handleMod1); 449*cdf0e10cSrcweir 450*cdf0e10cSrcweir // get rid of the service manager 451*cdf0e10cSrcweir Reference<XPropertySet> xSet( serviceManager, UNO_QUERY); 452*cdf0e10cSrcweir Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))); 453*cdf0e10cSrcweir Reference<XComponentContext> xContext; 454*cdf0e10cSrcweir any_prop >>= xContext; 455*cdf0e10cSrcweir Reference<XComponent> xComponent( xContext, UNO_QUERY); 456*cdf0e10cSrcweir xComponent->dispose(); 457*cdf0e10cSrcweir } 458*cdf0e10cSrcweir 459*cdf0e10cSrcweir // Enter a loop and try unloading. At least after 10 seconds 460*cdf0e10cSrcweir // this should be successful. 461*cdf0e10cSrcweir TimeValue time={10,0}; 462*cdf0e10cSrcweir 463*cdf0e10cSrcweir TimeValue beforeTime={0,0}; 464*cdf0e10cSrcweir printf("\n unloading should take about 10 seconds\n"); 465*cdf0e10cSrcweir osl_getSystemTime( &beforeTime); 466*cdf0e10cSrcweir for(;;) 467*cdf0e10cSrcweir { 468*cdf0e10cSrcweir 469*cdf0e10cSrcweir rtl_unloadUnusedModules( &time); 470*cdf0e10cSrcweir void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData); 471*cdf0e10cSrcweir if( ! pSymbol) 472*cdf0e10cSrcweir break; 473*cdf0e10cSrcweir } 474*cdf0e10cSrcweir TimeValue afterTime={0,0}; 475*cdf0e10cSrcweir osl_getSystemTime( &afterTime); 476*cdf0e10cSrcweir 477*cdf0e10cSrcweir printf("\n it took about %i seconds \n Check the value!!!", afterTime.Seconds - beforeTime.Seconds); 478*cdf0e10cSrcweir printf(" hit return to continue\n"); 479*cdf0e10cSrcweir getchar(); 480*cdf0e10cSrcweir 481*cdf0e10cSrcweir return sal_True; 482*cdf0e10cSrcweir } 483*cdf0e10cSrcweir 484*cdf0e10cSrcweir /* 485*cdf0e10cSrcweir */ 486*cdf0e10cSrcweir sal_Bool test7() 487*cdf0e10cSrcweir { 488*cdf0e10cSrcweir printf("Test 7 ####################################################" 489*cdf0e10cSrcweir "\nThe callback function should be called 3 times\n"); 490*cdf0e10cSrcweir sal_Int32 id1=1; 491*cdf0e10cSrcweir sal_Int32 id2=2; 492*cdf0e10cSrcweir sal_Int32 id3=3; 493*cdf0e10cSrcweir sal_Int32 cookie1= rtl_addUnloadingListener( listenerCallback, &id1); 494*cdf0e10cSrcweir sal_Int32 cookie2= rtl_addUnloadingListener( listenerCallback, &id2); 495*cdf0e10cSrcweir sal_Int32 cookie3= rtl_addUnloadingListener( listenerCallback, &id3); 496*cdf0e10cSrcweir 497*cdf0e10cSrcweir printf("\nTest 7 \nThe listener should be called 3 times\n"); 498*cdf0e10cSrcweir rtl_unloadUnusedModules( NULL); 499*cdf0e10cSrcweir 500*cdf0e10cSrcweir rtl_removeUnloadingListener( cookie1); 501*cdf0e10cSrcweir rtl_removeUnloadingListener( cookie2); 502*cdf0e10cSrcweir rtl_removeUnloadingListener( cookie3); 503*cdf0e10cSrcweir 504*cdf0e10cSrcweir sal_Int32 cookie4= rtl_addUnloadingListener( listenerCallback, &id1); 505*cdf0e10cSrcweir sal_Int32 cookie5= rtl_addUnloadingListener( listenerCallback, &id2); 506*cdf0e10cSrcweir sal_Int32 cookie6= rtl_addUnloadingListener( listenerCallback, &id3); 507*cdf0e10cSrcweir 508*cdf0e10cSrcweir if( cookie1 == cookie4 && 509*cdf0e10cSrcweir cookie2 == cookie5 ) 510*cdf0e10cSrcweir { 511*cdf0e10cSrcweir printf("\n###cookie recycling works\n"); 512*cdf0e10cSrcweir printf("hit return to continue\n"); 513*cdf0e10cSrcweir getchar(); 514*cdf0e10cSrcweir } 515*cdf0e10cSrcweir else 516*cdf0e10cSrcweir { 517*cdf0e10cSrcweir printf("\n###cookie recycling failed!!!\n"); 518*cdf0e10cSrcweir printf("hit return to continue\n"); 519*cdf0e10cSrcweir getchar(); 520*cdf0e10cSrcweir } 521*cdf0e10cSrcweir 522*cdf0e10cSrcweir rtl_removeUnloadingListener( cookie1); 523*cdf0e10cSrcweir rtl_removeUnloadingListener( cookie2); 524*cdf0e10cSrcweir rtl_removeUnloadingListener( cookie3); 525*cdf0e10cSrcweir return sal_True; 526*cdf0e10cSrcweir } 527*cdf0e10cSrcweir 528*cdf0e10cSrcweir /* Test one-instance-service default factory (XUnloadingPreference) 529*cdf0e10cSrcweir cppuhelper/source/factory.cxx 530*cdf0e10cSrcweir */ 531*cdf0e10cSrcweir sal_Bool test8() 532*cdf0e10cSrcweir { 533*cdf0e10cSrcweir printf("Test 8 ####################################################\n"); 534*cdf0e10cSrcweir oslModule handleMod1=0; 535*cdf0e10cSrcweir OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1)); 536*cdf0e10cSrcweir OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory")); 537*cdf0e10cSrcweir 538*cdf0e10cSrcweir sal_Bool b_ifaceSupported=sal_False; 539*cdf0e10cSrcweir sal_Bool b_instances_identical= sal_False; 540*cdf0e10cSrcweir sal_Bool b_releaseBeforeLoading= sal_False; 541*cdf0e10cSrcweir sal_Bool b_releaseAfterLoading= sal_False; 542*cdf0e10cSrcweir sal_Bool b_unloaded= sal_False; 543*cdf0e10cSrcweir 544*cdf0e10cSrcweir { 545*cdf0e10cSrcweir Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory( 546*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb"))); 547*cdf0e10cSrcweir Reference<XContentEnumerationAccess> xContent( serviceManager, UNO_QUERY); 548*cdf0e10cSrcweir Reference<XEnumeration> xenum= xContent->createContentEnumeration( 549*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4))); 550*cdf0e10cSrcweir 551*cdf0e10cSrcweir Any any_elem; 552*cdf0e10cSrcweir if( xenum->hasMoreElements()) 553*cdf0e10cSrcweir any_elem= xenum->nextElement(); 554*cdf0e10cSrcweir Reference<XInterface> xinterfaceFact; 555*cdf0e10cSrcweir any_elem>>=xinterfaceFact; 556*cdf0e10cSrcweir Reference<XTypeProvider> xprov( xinterfaceFact, UNO_QUERY); 557*cdf0e10cSrcweir 558*cdf0e10cSrcweir Sequence<Type> seqTypes= xprov->getTypes(); 559*cdf0e10cSrcweir 560*cdf0e10cSrcweir // XTypeProvider test 561*cdf0e10cSrcweir for( sal_Int32 i=0; i<seqTypes.getLength(); i++) 562*cdf0e10cSrcweir { 563*cdf0e10cSrcweir OUString name= seqTypes[i].getTypeName(); 564*cdf0e10cSrcweir if( name == OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XUnloadingPreference"))) 565*cdf0e10cSrcweir b_ifaceSupported= sal_True; 566*cdf0e10cSrcweir } 567*cdf0e10cSrcweir 568*cdf0e10cSrcweir // XUnloadingPreference::releaseOnNotification should return true now because we haven't 569*cdf0e10cSrcweir // created an instance yet 570*cdf0e10cSrcweir Reference<XUnloadingPreference> xreject( xinterfaceFact, UNO_QUERY); 571*cdf0e10cSrcweir b_releaseBeforeLoading= xreject->releaseOnNotification(); 572*cdf0e10cSrcweir 573*cdf0e10cSrcweir // Create instance. Afterwards releaseOnNotification should return false. 574*cdf0e10cSrcweir Reference<XInterface> xint= serviceManager->createInstance( OUString( 575*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME4))); 576*cdf0e10cSrcweir b_releaseAfterLoading= xreject->releaseOnNotification(); 577*cdf0e10cSrcweir b_releaseAfterLoading= b_releaseAfterLoading? sal_False : sal_True; 578*cdf0e10cSrcweir 579*cdf0e10cSrcweir // safe the handle of the module 580*cdf0e10cSrcweir handleMod1= osl_loadModule( lib1Name.pData, 0); 581*cdf0e10cSrcweir osl_unloadModule( handleMod1); 582*cdf0e10cSrcweir 583*cdf0e10cSrcweir // ---------------------------------------------------------- 584*cdf0e10cSrcweir // for debugging 585*cdf0e10cSrcweir Reference<XServiceInfo> info( xint, UNO_QUERY); 586*cdf0e10cSrcweir OUString s= info->getImplementationName(); 587*cdf0e10cSrcweir 588*cdf0e10cSrcweir // get another instance which must be the same 589*cdf0e10cSrcweir Reference<XInterface> xint2= serviceManager->createInstance( OUString( 590*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME4))); 591*cdf0e10cSrcweir 592*cdf0e10cSrcweir b_instances_identical= xint == xint2; 593*cdf0e10cSrcweir 594*cdf0e10cSrcweir // get rid of the service manager 595*cdf0e10cSrcweir Reference<XPropertySet> xSet( serviceManager, UNO_QUERY); 596*cdf0e10cSrcweir Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))); 597*cdf0e10cSrcweir Reference<XComponentContext> xContext; 598*cdf0e10cSrcweir any_prop >>= xContext; 599*cdf0e10cSrcweir Reference<XComponent> xComponent( xContext, UNO_QUERY); 600*cdf0e10cSrcweir xComponent->dispose(); 601*cdf0e10cSrcweir } 602*cdf0e10cSrcweir 603*cdf0e10cSrcweir rtl_unloadUnusedModules( NULL); 604*cdf0e10cSrcweir 605*cdf0e10cSrcweir // The library must be unloaded now 606*cdf0e10cSrcweir void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData); 607*cdf0e10cSrcweir if( ! pSymbol ) 608*cdf0e10cSrcweir b_unloaded= sal_True; 609*cdf0e10cSrcweir 610*cdf0e10cSrcweir if( b_ifaceSupported && b_instances_identical && b_releaseBeforeLoading && 611*cdf0e10cSrcweir b_releaseAfterLoading && b_unloaded) 612*cdf0e10cSrcweir return sal_True; 613*cdf0e10cSrcweir return sal_False; 614*cdf0e10cSrcweir } 615*cdf0e10cSrcweir 616*cdf0e10cSrcweir void SAL_CALL listenerCallback( void* id) 617*cdf0e10cSrcweir { 618*cdf0e10cSrcweir printf(" listener called with id= %i\n", *(sal_Int32*)id); 619*cdf0e10cSrcweir } 620*cdf0e10cSrcweir 621*cdf0e10cSrcweir /* 622*cdf0e10cSrcweir 623*cdf0e10cSrcweir */ 624*cdf0e10cSrcweir sal_Bool test9() 625*cdf0e10cSrcweir { 626*cdf0e10cSrcweir printf("Test 9 ####################################################\n"); 627*cdf0e10cSrcweir oslModule handleMod=0; 628*cdf0e10cSrcweir sal_Bool retval=sal_False; 629*cdf0e10cSrcweir OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1)); 630*cdf0e10cSrcweir 631*cdf0e10cSrcweir Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory( 632*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb"))); 633*cdf0e10cSrcweir 634*cdf0e10cSrcweir Reference<XInterface> xint= serviceManager->createInstance( OUString( 635*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1))); 636*cdf0e10cSrcweir // Release the service. The library refcount should be 1 637*cdf0e10cSrcweir xint=0; 638*cdf0e10cSrcweir 639*cdf0e10cSrcweir handleMod= osl_loadModule( lib1Name.pData, 0); 640*cdf0e10cSrcweir osl_unloadModule( handleMod); 641*cdf0e10cSrcweir //----------------------------------------------------------- 642*cdf0e10cSrcweir 643*cdf0e10cSrcweir // the service manager is still alive 644*cdf0e10cSrcweir rtl_unloadUnusedModules( NULL); 645*cdf0e10cSrcweir // Try to get a symbol, must fail 646*cdf0e10cSrcweir OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory")); 647*cdf0e10cSrcweir void* pSymbol= osl_getSymbol( handleMod, sSymbol.pData); 648*cdf0e10cSrcweir 649*cdf0e10cSrcweir if( pSymbol) 650*cdf0e10cSrcweir { 651*cdf0e10cSrcweir retval= sal_False; 652*cdf0e10cSrcweir } 653*cdf0e10cSrcweir else 654*cdf0e10cSrcweir retval= sal_True; 655*cdf0e10cSrcweir return retval; 656*cdf0e10cSrcweir } 657