1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 .
*/
#include <osl/diagnose.h>
#include <osl/mutex.hxx>
#include <cppuhelper/weak.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <cppuhelper/component.hxx>
#include <cppuhelper/factory.hxx>
#include <cppuhelper/implbase3.hxx>
#include <cppuhelper/shlib.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <cppuhelper/typeprovider.hxx>
#include <rtl/instance.hxx>
#include <rtl/unload.h>
#include "cppuhelper/propshlp.hxx"
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/lang/XSingleComponentFactory.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/loader/XImplementationLoader.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/uno/XUnloadingPreference.hpp>
#include "com/sun/star/beans/PropertyAttribute.hpp"
#include <memory>
#define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
using namespace osl;
using namespace com::sun::star;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::loader;
using namespace com::sun::star::registry;
using ::rtl::OUString;
namespace cppu
{
class OSingleFactoryHelper
: public XServiceInfo
, public XSingleServiceFactory
, public lang::XSingleComponentFactory
, public XUnloadingPreference
{
public:
OSingleFactoryHelper(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName_,
ComponentInstantiation pCreateFunction_,
ComponentFactoryFunc fptr,
const Sequence< OUString > * pServiceNames_ )
SAL_THROW(())
: xSMgr( rServiceManager )
, pCreateFunction( pCreateFunction_ )
, m_fptr( fptr )
, aImplementationName( rImplementationName_ )
{
if( pServiceNames_ )
aServiceNames = *pServiceNames_;
}
// old function, only for backward compatibility
OSingleFactoryHelper(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName_ )
SAL_THROW(())
: xSMgr( rServiceManager )
, pCreateFunction( NULL )
, m_fptr( 0 )
, aImplementationName( rImplementationName_ )
{}
virtual ~OSingleFactoryHelper();
// XInterface
Any SAL_CALL queryInterface( const Type & rType )
throw(::com::sun::star::uno::RuntimeException);
// XSingleServiceFactory
Reference<XInterface > SAL_CALL createInstance()
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
virtual Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments)
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
// XSingleComponentFactory
virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException);
virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
Sequence< Any > const & rArguments,
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException);
// XServiceInfo
OUString SAL_CALL getImplementationName()
throw(::com::sun::star::uno::RuntimeException);
sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
throw(::com::sun::star::uno::RuntimeException);
Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
throw(::com::sun::star::uno::RuntimeException);
protected:
/**
* Create an instance specified by the factory. The one instance logic is implemented
* in the createInstance and createInstanceWithArguments methods.
* @return the newly created instance. Do not return a previous (one instance) instance.
*/
virtual Reference<XInterface > createInstanceEveryTime(
Reference< XComponentContext > const & xContext )
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
Reference<XMultiServiceFactory > xSMgr;
ComponentInstantiation pCreateFunction;
ComponentFactoryFunc m_fptr;
Sequence< OUString > aServiceNames;
OUString aImplementationName;
};
OSingleFactoryHelper::~OSingleFactoryHelper()
{
}
//-----------------------------------------------------------------------------
Any OSingleFactoryHelper::queryInterface( const Type & rType )
throw(::com::sun::star::uno::RuntimeException)
{
return ::cppu::queryInterface(
rType,
static_cast< XSingleComponentFactory * >( this ),
static_cast< XSingleServiceFactory * >( this ),
static_cast< XServiceInfo * >( this ) ,
static_cast< XUnloadingPreference * >( this ));
}
// OSingleFactoryHelper
Reference<XInterface > OSingleFactoryHelper::createInstanceEveryTime(
Reference< XComponentContext > const & xContext )
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
if (m_fptr)
{
return (*m_fptr)( xContext );
}
else if( pCreateFunction )
{
if (xContext.is())
{
Reference< lang::XMultiServiceFactory > xContextMgr(
xContext->getServiceManager(), UNO_QUERY );
if (xContextMgr.is())
return (*pCreateFunction)( xContextMgr );
}
return (*pCreateFunction)( xSMgr );
}
else
{
return Reference< XInterface >();
}
}
// XSingleServiceFactory
Reference<XInterface > OSingleFactoryHelper::createInstance()
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
return createInstanceWithContext( Reference< XComponentContext >() );
}
// XSingleServiceFactory
Reference<XInterface > OSingleFactoryHelper::createInstanceWithArguments(
const Sequence<Any>& Arguments )
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
return createInstanceWithArgumentsAndContext(
Arguments, Reference< XComponentContext >() );
}
// XSingleComponentFactory
//__________________________________________________________________________________________________
Reference< XInterface > OSingleFactoryHelper::createInstanceWithContext(
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException)
{
return createInstanceEveryTime( xContext );
}
//__________________________________________________________________________________________________
Reference< XInterface > OSingleFactoryHelper::createInstanceWithArgumentsAndContext(
Sequence< Any > const & rArguments,
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException)
{
Reference< XInterface > xRet( createInstanceWithContext( xContext ) );
Reference< lang::XInitialization > xInit( xRet, UNO_QUERY );
// always call initialize, even if there are no arguments.
// #i63511# / 2006-03-27 / frank.schoenheit@sun.com
if (xInit.is())
{
xInit->initialize( rArguments );
}
else
{
if ( rArguments.getLength() )
{
// dispose the here created UNO object before throwing out exception
// to avoid risk of memory leaks #i113722#
Reference<XComponent> xComp( xRet, UNO_QUERY );
if (xComp.is())
xComp->dispose();
throw lang::IllegalArgumentException(
OUString( RTL_CONSTASCII_USTRINGPARAM("cannot pass arguments to component => no XInitialization implemented!") ),
Reference< XInterface >(), 0 );
}
}
return xRet;
}
// XServiceInfo
OUString OSingleFactoryHelper::getImplementationName()
throw(::com::sun::star::uno::RuntimeException)
{
return aImplementationName;
}
// XServiceInfo
sal_Bool OSingleFactoryHelper::supportsService(
const OUString& ServiceName )
throw(::com::sun::star::uno::RuntimeException)
{
return cppu::supportsService(this, ServiceName);
}
// XServiceInfo
Sequence< OUString > OSingleFactoryHelper::getSupportedServiceNames(void)
throw(::com::sun::star::uno::RuntimeException)
{
return aServiceNames;
}
struct OFactoryComponentHelper_Mutex
{
Mutex aMutex;
};
class OFactoryComponentHelper
: public OFactoryComponentHelper_Mutex
, public OComponentHelper
, public OSingleFactoryHelper
{
public:
OFactoryComponentHelper(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName_,
ComponentInstantiation pCreateFunction_,
ComponentFactoryFunc fptr,
const Sequence< OUString > * pServiceNames_,
sal_Bool bOneInstance_ = sal_False )
SAL_THROW(())
: OComponentHelper( aMutex )
, OSingleFactoryHelper( rServiceManager, rImplementationName_, pCreateFunction_, fptr, pServiceNames_ )
, bOneInstance( bOneInstance_ )
, pModuleCount(0)
{
}
// Used by the createXXXFactory functions. The argument pModCount is used to prevent the unloading of the module
// which contains pCreateFunction_
OFactoryComponentHelper(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName_,
ComponentInstantiation pCreateFunction_,
ComponentFactoryFunc fptr,
const Sequence< OUString > * pServiceNames_,
rtl_ModuleCount * pModCount,
sal_Bool bOneInstance_ = sal_False )
SAL_THROW(())
: OComponentHelper( aMutex )
, OSingleFactoryHelper( rServiceManager, rImplementationName_, pCreateFunction_, fptr, pServiceNames_ )
, bOneInstance( bOneInstance_ )
, pModuleCount(pModCount)
{
if(pModuleCount)
pModuleCount->acquire( pModuleCount);
}
// old function, only for backward compatibility
OFactoryComponentHelper(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName_,
sal_Bool bOneInstance_ = sal_False )
SAL_THROW(())
: OComponentHelper( aMutex )
, OSingleFactoryHelper( rServiceManager, rImplementationName_ )
, bOneInstance( bOneInstance_ )
, pModuleCount(0)
{
}
~OFactoryComponentHelper()
{
if(pModuleCount)
pModuleCount->release( pModuleCount);
}
// XInterface
Any SAL_CALL queryInterface( const Type & rType )
throw(::com::sun::star::uno::RuntimeException);
void SAL_CALL acquire() throw()
{ OComponentHelper::acquire(); }
void SAL_CALL release() throw()
{ OComponentHelper::release(); }
// XSingleServiceFactory
Reference<XInterface > SAL_CALL createInstance()
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
Reference<XInterface > SAL_CALL createInstanceWithArguments( const Sequence<Any>& Arguments )
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
// XSingleComponentFactory
virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException);
virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
Sequence< Any > const & rArguments,
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException);
// XTypeProvider
virtual Sequence< Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() throw(::com::sun::star::uno::RuntimeException);
// XAggregation
Any SAL_CALL queryAggregation( const Type & rType )
throw(::com::sun::star::uno::RuntimeException);
// XUnloadingPreference
virtual sal_Bool SAL_CALL releaseOnNotification()
throw(::com::sun::star::uno::RuntimeException);
// OComponentHelper
void SAL_CALL dispose() throw(::com::sun::star::uno::RuntimeException);
private:
Reference<XInterface > xTheInstance;
sal_Bool bOneInstance;
rtl_ModuleCount * pModuleCount;
protected:
// needed for implementing XUnloadingPreference in inheriting classes
sal_Bool isOneInstance() {return bOneInstance;}
sal_Bool isInstance() {return xTheInstance.is();}
};
Any SAL_CALL OFactoryComponentHelper::queryInterface( const Type & rType )
throw(::com::sun::star::uno::RuntimeException)
{
if( rType == ::getCppuType( (Reference<XUnloadingPreference>*)0))
{
return makeAny(
Reference< XUnloadingPreference >(
static_cast< XUnloadingPreference * >(this) ) );
}
return OComponentHelper::queryInterface( rType );
}
// XAggregation
Any OFactoryComponentHelper::queryAggregation( const Type & rType )
throw(::com::sun::star::uno::RuntimeException)
{
Any aRet( OComponentHelper::queryAggregation( rType ) );
return (aRet.hasValue() ? aRet : OSingleFactoryHelper::queryInterface( rType ));
}
// XTypeProvider
Sequence< Type > OFactoryComponentHelper::getTypes()
throw (::com::sun::star::uno::RuntimeException)
{
Type ar[ 4 ];
ar[ 0 ] = ::getCppuType( (const Reference< XSingleServiceFactory > *)0 );
ar[ 1 ] = ::getCppuType( (const Reference< XServiceInfo > *)0 );
ar[ 2 ] = ::getCppuType( (const Reference< XUnloadingPreference > *)0 );
if (m_fptr)
ar[ 3 ] = ::getCppuType( (const Reference< XSingleComponentFactory > *)0 );
return Sequence< Type >( ar, m_fptr ? 4 : 3 );
}
namespace
{
class theOFactoryComponentHelperImplementationId :
public rtl::Static<OImplementationId, theOFactoryComponentHelperImplementationId>{};
}
Sequence< sal_Int8 > OFactoryComponentHelper::getImplementationId()
throw (::com::sun::star::uno::RuntimeException)
{
return theOFactoryComponentHelperImplementationId::get().getImplementationId();
}
// XSingleServiceFactory
Reference<XInterface > OFactoryComponentHelper::createInstance()
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
if( bOneInstance )
{
if( !xTheInstance.is() )
{
MutexGuard aGuard( aMutex );
if( !xTheInstance.is() )
xTheInstance = OSingleFactoryHelper::createInstance();
}
return xTheInstance;
}
return OSingleFactoryHelper::createInstance();
}
Reference<XInterface > OFactoryComponentHelper::createInstanceWithArguments(
const Sequence<Any>& Arguments )
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
if( bOneInstance )
{
if( !xTheInstance.is() )
{
MutexGuard aGuard( aMutex );
// OSL_ENSURE( !xTheInstance.is(), "### arguments will be ignored!" );
if( !xTheInstance.is() )
xTheInstance = OSingleFactoryHelper::createInstanceWithArguments( Arguments );
}
return xTheInstance;
}
return OSingleFactoryHelper::createInstanceWithArguments( Arguments );
}
// XSingleComponentFactory
//__________________________________________________________________________________________________
Reference< XInterface > OFactoryComponentHelper::createInstanceWithContext(
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException)
{
if( bOneInstance )
{
if( !xTheInstance.is() )
{
MutexGuard aGuard( aMutex );
// OSL_ENSURE( !xTheInstance.is(), "### context will be ignored!" );
if( !xTheInstance.is() )
xTheInstance = OSingleFactoryHelper::createInstanceWithContext( xContext );
}
return xTheInstance;
}
return OSingleFactoryHelper::createInstanceWithContext( xContext );
}
//__________________________________________________________________________________________________
Reference< XInterface > OFactoryComponentHelper::createInstanceWithArgumentsAndContext(
Sequence< Any > const & rArguments,
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException)
{
if( bOneInstance )
{
if( !xTheInstance.is() )
{
MutexGuard aGuard( aMutex );
// OSL_ENSURE( !xTheInstance.is(), "### context and arguments will be ignored!" );
if( !xTheInstance.is() )
xTheInstance = OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext );
}
return xTheInstance;
}
return OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext );
}
// OComponentHelper
void OFactoryComponentHelper::dispose()
throw(::com::sun::star::uno::RuntimeException)
{
OComponentHelper::dispose();
Reference<XInterface > x;
{
// do not delete in the guard section
MutexGuard aGuard( aMutex );
x = xTheInstance;
xTheInstance = Reference<XInterface >();
}
// if it is a component call dispose at the component
Reference<XComponent > xComp( x, UNO_QUERY );
if( xComp.is() )
xComp->dispose();
}
// XUnloadingPreference
// This class is used for single factories, component factories and
// one-instance factories. Depending on the usage this function has
// to return different values.
// one-instance factory: sal_False
// single factory: sal_True
// component factory: sal_True
sal_Bool SAL_CALL OFactoryComponentHelper::releaseOnNotification() throw(::com::sun::star::uno::RuntimeException)
{
if( bOneInstance)
return sal_False;
return sal_True;
}
class ORegistryFactoryHelper : public OFactoryComponentHelper,
public OPropertySetHelper
{
public:
ORegistryFactoryHelper(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName_,
const Reference<XRegistryKey > & xImplementationKey_,
sal_Bool bOneInstance_ = sal_False ) SAL_THROW(())
: OFactoryComponentHelper(
rServiceManager, rImplementationName_, 0, 0, 0, bOneInstance_ ),
OPropertySetHelper( OComponentHelper::rBHelper ),
xImplementationKey( xImplementationKey_ )
{}
// XInterface
virtual Any SAL_CALL queryInterface( Type const & type )
throw (RuntimeException);
virtual void SAL_CALL acquire() throw ();
virtual void SAL_CALL release() throw ();
// XTypeProvider
virtual Sequence< Type > SAL_CALL getTypes()
throw (RuntimeException);
// XPropertySet
virtual Reference< beans::XPropertySetInfo > SAL_CALL getPropertySetInfo()
throw (RuntimeException);
// OPropertySetHelper
virtual IPropertyArrayHelper & SAL_CALL getInfoHelper();
virtual sal_Bool SAL_CALL convertFastPropertyValue(
Any & rConvertedValue, Any & rOldValue,
sal_Int32 nHandle, Any const & rValue )
throw (lang::IllegalArgumentException);
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(
sal_Int32 nHandle, Any const & rValue )
throw (Exception);
using OPropertySetHelper::getFastPropertyValue;
virtual void SAL_CALL getFastPropertyValue(
Any & rValue, sal_Int32 nHandle ) const;
// OSingleFactoryHelper
Reference<XInterface > createInstanceEveryTime(
Reference< XComponentContext > const & xContext )
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
// XSingleServiceFactory
Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments)
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
// XSingleComponentFactory
Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
Sequence< Any > const & rArguments,
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException);
// XServiceInfo
Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
throw(::com::sun::star::uno::RuntimeException);
// XUnloadingPreference
sal_Bool SAL_CALL releaseOnNotification()
throw( RuntimeException);
private:
Reference< XInterface > createModuleFactory()
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
/** The registry key of the implementation section */
Reference<XRegistryKey > xImplementationKey;
/** The factory created with the loader. */
Reference<XSingleComponentFactory > xModuleFactory;
Reference<XSingleServiceFactory > xModuleFactoryDepr;
Reference< beans::XPropertySetInfo > m_xInfo;
::std::auto_ptr< IPropertyArrayHelper > m_property_array_helper;
protected:
using OPropertySetHelper::getTypes;
};
// XInterface
//______________________________________________________________________________
Any SAL_CALL ORegistryFactoryHelper::queryInterface(
Type const & type ) throw (RuntimeException)
{
Any ret( OFactoryComponentHelper::queryInterface( type ) );
if (ret.hasValue())
return ret;
else
return OPropertySetHelper::queryInterface( type );
}
//______________________________________________________________________________
void ORegistryFactoryHelper::acquire() throw ()
{
OFactoryComponentHelper::acquire();
}
//______________________________________________________________________________
void ORegistryFactoryHelper::release() throw ()
{
OFactoryComponentHelper::release();
}
// XTypeProvider
//______________________________________________________________________________
Sequence< Type > ORegistryFactoryHelper::getTypes() throw (RuntimeException)
{
Sequence< Type > types( OFactoryComponentHelper::getTypes() );
sal_Int32 pos = types.getLength();
types.realloc( pos + 3 );
Type * p = types.getArray();
p[ pos++ ] = ::getCppuType(
reinterpret_cast< Reference< beans::XMultiPropertySet > const * >(0) );
p[ pos++ ] = ::getCppuType(
reinterpret_cast< Reference< beans::XFastPropertySet > const * >(0) );
p[ pos++ ] = ::getCppuType(
reinterpret_cast< Reference< beans::XPropertySet > const * >(0) );
return types;
}
// XPropertySet
//______________________________________________________________________________
Reference< beans::XPropertySetInfo >
ORegistryFactoryHelper::getPropertySetInfo() throw (RuntimeException)
{
::osl::MutexGuard guard( aMutex );
if (! m_xInfo.is())
m_xInfo = createPropertySetInfo( getInfoHelper() );
return m_xInfo;
}
// OPropertySetHelper
//______________________________________________________________________________
IPropertyArrayHelper & ORegistryFactoryHelper::getInfoHelper()
{
::osl::MutexGuard guard( aMutex );
if (m_property_array_helper.get() == 0)
{
beans::Property prop(
OUSTR("ImplementationKey") /* name */,
0 /* handle */,
::getCppuType( &xImplementationKey ),
beans::PropertyAttribute::READONLY |
beans::PropertyAttribute::OPTIONAL );
m_property_array_helper.reset(
new ::cppu::OPropertyArrayHelper( &prop, 1 ) );
}
return *m_property_array_helper.get();
}
//______________________________________________________________________________
sal_Bool ORegistryFactoryHelper::convertFastPropertyValue(
Any &, Any &, sal_Int32, Any const & )
throw (lang::IllegalArgumentException)
{
OSL_FAIL( "unexpected!" );
return false;
}
//______________________________________________________________________________
void ORegistryFactoryHelper::setFastPropertyValue_NoBroadcast(
sal_Int32, Any const & )
throw (Exception)
{
throw beans::PropertyVetoException(
OUSTR("unexpected: only readonly properties!"),
static_cast< OWeakObject * >(this) );
}
//______________________________________________________________________________
void ORegistryFactoryHelper::getFastPropertyValue(
Any & rValue, sal_Int32 nHandle ) const
{
if (nHandle == 0)
{
rValue <<= xImplementationKey;
}
else
{
rValue.clear();
throw beans::UnknownPropertyException(
OUSTR("unknown property!"), static_cast< OWeakObject * >(
const_cast< ORegistryFactoryHelper * >(this) ) );
}
}
Reference<XInterface > ORegistryFactoryHelper::createInstanceEveryTime(
Reference< XComponentContext > const & xContext )
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
{
Reference< XInterface > x( createModuleFactory() );
if (x.is())
{
MutexGuard aGuard( aMutex );
if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
{
xModuleFactory.set( x, UNO_QUERY );
xModuleFactoryDepr.set( x, UNO_QUERY );
}
}
}
if( xModuleFactory.is() )
{
return xModuleFactory->createInstanceWithContext( xContext );
}
else if( xModuleFactoryDepr.is() )
{
return xModuleFactoryDepr->createInstance();
}
return Reference<XInterface >();
}
Reference<XInterface > SAL_CALL ORegistryFactoryHelper::createInstanceWithArguments(
const Sequence<Any>& Arguments )
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
{
Reference< XInterface > x( createModuleFactory() );
if (x.is())
{
MutexGuard aGuard( aMutex );
if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
{
xModuleFactory.set( x, UNO_QUERY );
xModuleFactoryDepr.set( x, UNO_QUERY );
}
}
}
if( xModuleFactoryDepr.is() )
{
return xModuleFactoryDepr->createInstanceWithArguments( Arguments );
}
else if( xModuleFactory.is() )
{
#if OSL_DEBUG_LEVEL > 1
OSL_TRACE( "### no context ORegistryFactoryHelper::createInstanceWithArgumentsAndContext()!" );
#endif
return xModuleFactory->createInstanceWithArgumentsAndContext( Arguments, Reference< XComponentContext >() );
}
return Reference<XInterface >();
}
Reference< XInterface > ORegistryFactoryHelper::createInstanceWithArgumentsAndContext(
Sequence< Any > const & rArguments,
Reference< XComponentContext > const & xContext )
throw (Exception, RuntimeException)
{
if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
{
Reference< XInterface > x( createModuleFactory() );
if (x.is())
{
MutexGuard aGuard( aMutex );
if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
{
xModuleFactory.set( x, UNO_QUERY );
xModuleFactoryDepr.set( x, UNO_QUERY );
}
}
}
if( xModuleFactory.is() )
{
return xModuleFactory->createInstanceWithArgumentsAndContext( rArguments, xContext );
}
else if( xModuleFactoryDepr.is() )
{
#if OSL_DEBUG_LEVEL > 1
if (xContext.is())
{
OSL_TRACE( "### ignoring context calling ORegistryFactoryHelper::createInstanceWithArgumentsAndContext()!" );
}
#endif
return xModuleFactoryDepr->createInstanceWithArguments( rArguments );
}
return Reference<XInterface >();
}
// OSingleFactoryHelper
Reference< XInterface > ORegistryFactoryHelper::createModuleFactory()
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
OUString aActivatorUrl;
OUString aActivatorName;
OUString aLocation;
Reference<XRegistryKey > xActivatorKey = xImplementationKey->openKey(
OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/ACTIVATOR") ) );
if( xActivatorKey.is() && xActivatorKey->getValueType() == RegistryValueType_ASCII )
{
aActivatorUrl = xActivatorKey->getAsciiValue();
OUString tmpActivator(aActivatorUrl.getStr());
sal_Int32 nIndex = 0;
aActivatorName = tmpActivator.getToken(0, ':', nIndex );
Reference<XRegistryKey > xLocationKey = xImplementationKey->openKey(
OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/LOCATION") ) );
if( xLocationKey.is() && xLocationKey->getValueType() == RegistryValueType_ASCII )
aLocation = xLocationKey->getAsciiValue();
}
else
{
// old style"url"
// the location of the program code of the implementation
Reference<XRegistryKey > xLocationKey = xImplementationKey->openKey(
OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/URL") ) );
// is the the key of the right type ?
if( xLocationKey.is() && xLocationKey->getValueType() == RegistryValueType_ASCII )
{
// one implementation found -> try to activate
aLocation = xLocationKey->getAsciiValue();
// search protocol delimiter
sal_Int32 nPos = aLocation.indexOf(
OUString( RTL_CONSTASCII_USTRINGPARAM("://") ) );
if( nPos != -1 )
{
aActivatorName = aLocation.copy( 0, nPos );
if( aActivatorName.compareToAscii( "java" ) == 0 )
aActivatorName = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.Java") );
else if( aActivatorName.compareToAscii( "module" ) == 0 )
aActivatorName = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary") );
aLocation = aLocation.copy( nPos + 3 );
}
}
}
Reference< XInterface > xFactory;
if( !aActivatorName.isEmpty() )
{
Reference<XInterface > x = xSMgr->createInstance( aActivatorName );
Reference<XImplementationLoader > xLoader( x, UNO_QUERY );
Reference<XInterface > xMF;
if (xLoader.is())
{
xFactory = xLoader->activate( aImplementationName, aActivatorUrl, aLocation, xImplementationKey );
}
}
return xFactory;
}
// XServiceInfo
Sequence< OUString > ORegistryFactoryHelper::getSupportedServiceNames(void)
throw(::com::sun::star::uno::RuntimeException)
{
MutexGuard aGuard( aMutex );
if( aServiceNames.getLength() == 0 )
{
// not yet loaded
try
{
Reference<XRegistryKey > xKey = xImplementationKey->openKey(
OUString( RTL_CONSTASCII_USTRINGPARAM("UNO/SERVICES") ) );
if (xKey.is())
{
// length of prefix. +1 for the '/' at the end
sal_Int32 nPrefixLen = xKey->getKeyName().getLength() + 1;
// Full qualified names like "IMPLEMENTATIONS/TEST/UNO/SERVICES/com.sun.star..."
Sequence<OUString> seqKeys = xKey->getKeyNames();
OUString* pKeys = seqKeys.getArray();
for( sal_Int32 i = 0; i < seqKeys.getLength(); i++ )
pKeys[i] = pKeys[i].copy(nPrefixLen);
aServiceNames = seqKeys;
}
}
catch (InvalidRegistryException &)
{
}
}
return aServiceNames;
}
sal_Bool SAL_CALL ORegistryFactoryHelper::releaseOnNotification() throw(::com::sun::star::uno::RuntimeException)
{
sal_Bool retVal= sal_True;
if( isOneInstance() && isInstance())
{
retVal= sal_False;
}
else if( ! isOneInstance())
{
// try to delegate
if( xModuleFactory.is())
{
Reference<XUnloadingPreference> xunloading( xModuleFactory, UNO_QUERY);
if( xunloading.is())
retVal= xunloading->releaseOnNotification();
}
else if( xModuleFactoryDepr.is())
{
Reference<XUnloadingPreference> xunloading( xModuleFactoryDepr, UNO_QUERY);
if( xunloading.is())
retVal= xunloading->releaseOnNotification();
}
}
return retVal;
}
class OFactoryProxyHelper : public WeakImplHelper3< XServiceInfo, XSingleServiceFactory,
XUnloadingPreference >
{
Reference<XSingleServiceFactory > xFactory;
public:
OFactoryProxyHelper( const Reference<XSingleServiceFactory > & rFactory )
SAL_THROW(())
: xFactory( rFactory )
{}
// XSingleServiceFactory
Reference<XInterface > SAL_CALL createInstance()
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments)
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
// XServiceInfo
OUString SAL_CALL getImplementationName()
throw(::com::sun::star::uno::RuntimeException);
sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
throw(::com::sun::star::uno::RuntimeException);
Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
throw(::com::sun::star::uno::RuntimeException);
//XUnloadingPreference
sal_Bool SAL_CALL releaseOnNotification()
throw(::com::sun::star::uno::RuntimeException);
};
// XSingleServiceFactory
Reference<XInterface > OFactoryProxyHelper::createInstance()
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
return xFactory->createInstance();
}
// XSingleServiceFactory
Reference<XInterface > OFactoryProxyHelper::createInstanceWithArguments
(
const Sequence<Any>& Arguments
)
throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
{
return xFactory->createInstanceWithArguments( Arguments );
}
// XServiceInfo
OUString OFactoryProxyHelper::getImplementationName()
throw(::com::sun::star::uno::RuntimeException)
{
Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
if( xInfo.is() )
return xInfo->getImplementationName();
return OUString();
}
// XServiceInfo
sal_Bool OFactoryProxyHelper::supportsService(const OUString& ServiceName)
throw(::com::sun::star::uno::RuntimeException)
{
Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
return xInfo.is() && xInfo->supportsService( ServiceName );
}
// XServiceInfo
Sequence< OUString > OFactoryProxyHelper::getSupportedServiceNames(void)
throw(::com::sun::star::uno::RuntimeException)
{
Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
if( xInfo.is() )
return xInfo->getSupportedServiceNames();
return Sequence< OUString >();
}
sal_Bool SAL_CALL OFactoryProxyHelper::releaseOnNotification() throw(::com::sun::star::uno::RuntimeException)
{
Reference<XUnloadingPreference> pref( xFactory, UNO_QUERY);
if( pref.is())
return pref->releaseOnNotification();
return sal_True;
}
// global function
Reference<XSingleServiceFactory > SAL_CALL createSingleFactory(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName,
ComponentInstantiation pCreateFunction,
const Sequence< OUString > & rServiceNames,
rtl_ModuleCount *pModCount )
SAL_THROW(())
{
return new OFactoryComponentHelper(
rServiceManager, rImplementationName, pCreateFunction, 0, &rServiceNames, pModCount, sal_False );
}
// global function
Reference<XSingleServiceFactory > SAL_CALL createFactoryProxy(
SAL_UNUSED_PARAMETER const Reference<XMultiServiceFactory > &,
const Reference<XSingleServiceFactory > & rFactory )
SAL_THROW(())
{
return new OFactoryProxyHelper( rFactory );
}
// global function
Reference<XSingleServiceFactory > SAL_CALL createOneInstanceFactory(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName,
ComponentInstantiation pCreateFunction,
const Sequence< OUString > & rServiceNames,
rtl_ModuleCount *pModCount )
SAL_THROW(())
{
return new OFactoryComponentHelper(
rServiceManager, rImplementationName, pCreateFunction, 0, &rServiceNames, pModCount, sal_True );
}
// global function
Reference<XSingleServiceFactory > SAL_CALL createSingleRegistryFactory(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName,
const Reference<XRegistryKey > & rImplementationKey )
SAL_THROW(())
{
return new ORegistryFactoryHelper(
rServiceManager, rImplementationName, rImplementationKey, sal_False );
}
// global function
Reference<XSingleServiceFactory > SAL_CALL createOneInstanceRegistryFactory(
const Reference<XMultiServiceFactory > & rServiceManager,
const OUString & rImplementationName,
const Reference<XRegistryKey > & rImplementationKey )
SAL_THROW(())
{
return new ORegistryFactoryHelper(
rServiceManager, rImplementationName, rImplementationKey, sal_True );
}
//##################################################################################################
Reference< lang::XSingleComponentFactory > SAL_CALL createSingleComponentFactory(
ComponentFactoryFunc fptr,
OUString const & rImplementationName,
Sequence< OUString > const & rServiceNames,
rtl_ModuleCount * pModCount)
SAL_THROW(())
{
return new OFactoryComponentHelper(
Reference< XMultiServiceFactory >(), rImplementationName, 0, fptr, &rServiceNames, pModCount, sal_False );
}
Reference< lang::XSingleComponentFactory > SAL_CALL createOneInstanceComponentFactory(
ComponentFactoryFunc fptr,
OUString const & rImplementationName,
Sequence< OUString > const & rServiceNames,
rtl_ModuleCount * pModCount)
SAL_THROW(())
{
return new OFactoryComponentHelper(
Reference< XMultiServiceFactory >(), rImplementationName, 0, fptr, &rServiceNames, pModCount, sal_True );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|