Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cppuhelper / source / factory.cxx
blobd61639566110404968907fc9f6ef6ec4725be4ea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/log.hxx>
21 #include <osl/diagnose.h>
22 #include <osl/mutex.hxx>
23 #include <cppuhelper/weak.hxx>
24 #include <cppuhelper/component.hxx>
25 #include <cppuhelper/factory.hxx>
26 #include <cppuhelper/implbase.hxx>
27 #include <cppuhelper/queryinterface.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <rtl/unload.h>
31 #include <cppuhelper/propshlp.hxx>
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
35 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
36 #include <com/sun/star/lang/XInitialization.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/loader/XImplementationLoader.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/lang/IllegalArgumentException.hpp>
41 #include <com/sun/star/uno/XUnloadingPreference.hpp>
42 #include <com/sun/star/beans/PropertyAttribute.hpp>
44 #include <memory>
47 using namespace osl;
48 using namespace com::sun::star;
49 using namespace com::sun::star::uno;
50 using namespace com::sun::star::lang;
51 using namespace com::sun::star::loader;
52 using namespace com::sun::star::registry;
54 namespace cppu
57 class OSingleFactoryHelper
58 : public XServiceInfo
59 , public XSingleServiceFactory
60 , public lang::XSingleComponentFactory
61 , public XUnloadingPreference
63 public:
64 OSingleFactoryHelper(
65 const Reference<XMultiServiceFactory > & rServiceManager,
66 const OUString & rImplementationName_,
67 ComponentInstantiation pCreateFunction_,
68 ComponentFactoryFunc fptr,
69 const Sequence< OUString > * pServiceNames_ )
70 : xSMgr( rServiceManager )
71 , pCreateFunction( pCreateFunction_ )
72 , m_fptr( fptr )
73 , aImplementationName( rImplementationName_ )
75 if( pServiceNames_ )
76 aServiceNames = *pServiceNames_;
79 virtual ~OSingleFactoryHelper();
81 // XInterface
82 Any SAL_CALL queryInterface( const Type & rType ) override;
84 // XSingleServiceFactory
85 Reference<XInterface > SAL_CALL createInstance() override;
86 virtual Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments) override;
87 // XSingleComponentFactory
88 virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
89 Reference< XComponentContext > const & xContext ) override;
90 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
91 Sequence< Any > const & rArguments,
92 Reference< XComponentContext > const & xContext ) override;
94 // XServiceInfo
95 OUString SAL_CALL getImplementationName() override;
96 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
97 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
99 protected:
101 * Create an instance specified by the factory. The one instance logic is implemented
102 * in the createInstance and createInstanceWithArguments methods.
103 * @return the newly created instance. Do not return a previous (one instance) instance.
104 * @throw css::uno::Exception
105 * @throw css::uno::RuntimeException
107 virtual Reference<XInterface > createInstanceEveryTime(
108 Reference< XComponentContext > const & xContext );
110 Reference<XMultiServiceFactory > xSMgr;
111 ComponentInstantiation pCreateFunction;
112 ComponentFactoryFunc m_fptr;
113 Sequence< OUString > aServiceNames;
114 OUString aImplementationName;
116 OSingleFactoryHelper::~OSingleFactoryHelper()
121 Any OSingleFactoryHelper::queryInterface( const Type & rType )
123 return ::cppu::queryInterface(
124 rType,
125 static_cast< XSingleComponentFactory * >( this ),
126 static_cast< XSingleServiceFactory * >( this ),
127 static_cast< XServiceInfo * >( this ) ,
128 static_cast< XUnloadingPreference * >( this ));
131 // OSingleFactoryHelper
132 Reference<XInterface > OSingleFactoryHelper::createInstanceEveryTime(
133 Reference< XComponentContext > const & xContext )
135 if (m_fptr)
137 return (*m_fptr)( xContext );
139 if( pCreateFunction )
141 if (xContext.is())
143 Reference< lang::XMultiServiceFactory > xContextMgr(
144 xContext->getServiceManager(), UNO_QUERY );
145 if (xContextMgr.is())
146 return (*pCreateFunction)( xContextMgr );
148 return (*pCreateFunction)( xSMgr );
150 return Reference< XInterface >();
153 // XSingleServiceFactory
154 Reference<XInterface > OSingleFactoryHelper::createInstance()
156 return createInstanceWithContext( Reference< XComponentContext >() );
159 // XSingleServiceFactory
160 Reference<XInterface > OSingleFactoryHelper::createInstanceWithArguments(
161 const Sequence<Any>& Arguments )
163 return createInstanceWithArgumentsAndContext(
164 Arguments, Reference< XComponentContext >() );
167 // XSingleComponentFactory
169 Reference< XInterface > OSingleFactoryHelper::createInstanceWithContext(
170 Reference< XComponentContext > const & xContext )
172 return createInstanceEveryTime( xContext );
175 Reference< XInterface > OSingleFactoryHelper::createInstanceWithArgumentsAndContext(
176 Sequence< Any > const & rArguments,
177 Reference< XComponentContext > const & xContext )
179 Reference< XInterface > xRet( createInstanceWithContext( xContext ) );
181 Reference< lang::XInitialization > xInit( xRet, UNO_QUERY );
182 // always call initialize, even if there are no arguments. #i63511#
183 if (xInit.is())
185 xInit->initialize( rArguments );
187 else
189 if ( rArguments.hasElements() )
191 // dispose the here created UNO object before throwing out exception
192 // to avoid risk of memory leaks #i113722#
193 Reference<XComponent> xComp( xRet, UNO_QUERY );
194 if (xComp.is())
195 xComp->dispose();
197 throw lang::IllegalArgumentException(
198 "cannot pass arguments to component => no XInitialization implemented!",
199 Reference< XInterface >(), 0 );
203 return xRet;
206 // XServiceInfo
207 OUString OSingleFactoryHelper::getImplementationName()
209 return aImplementationName;
212 // XServiceInfo
213 sal_Bool OSingleFactoryHelper::supportsService(
214 const OUString& ServiceName )
216 return cppu::supportsService(this, ServiceName);
219 // XServiceInfo
220 Sequence< OUString > OSingleFactoryHelper::getSupportedServiceNames()
222 return aServiceNames;
225 struct OFactoryComponentHelper_Mutex
227 Mutex aMutex;
230 class OFactoryComponentHelper
231 : public OFactoryComponentHelper_Mutex
232 , public OComponentHelper
233 , public OSingleFactoryHelper
235 public:
236 OFactoryComponentHelper(
237 const Reference<XMultiServiceFactory > & rServiceManager,
238 const OUString & rImplementationName_,
239 ComponentInstantiation pCreateFunction_,
240 ComponentFactoryFunc fptr,
241 const Sequence< OUString > * pServiceNames_,
242 bool bOneInstance_ )
243 : OComponentHelper( aMutex )
244 , OSingleFactoryHelper( rServiceManager, rImplementationName_, pCreateFunction_, fptr, pServiceNames_ )
245 , bOneInstance( bOneInstance_ )
249 // XInterface
250 Any SAL_CALL queryInterface( const Type & rType ) override;
251 void SAL_CALL acquire() throw() override
252 { OComponentHelper::acquire(); }
253 void SAL_CALL release() throw() override
254 { OComponentHelper::release(); }
256 // XSingleServiceFactory
257 Reference<XInterface > SAL_CALL createInstance() override;
258 Reference<XInterface > SAL_CALL createInstanceWithArguments( const Sequence<Any>& Arguments ) override;
259 // XSingleComponentFactory
260 virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
261 Reference< XComponentContext > const & xContext ) override;
262 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
263 Sequence< Any > const & rArguments,
264 Reference< XComponentContext > const & xContext ) override;
266 // XTypeProvider
267 virtual Sequence< Type > SAL_CALL getTypes() override;
268 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
270 // XAggregation
271 Any SAL_CALL queryAggregation( const Type & rType ) override;
273 // XUnloadingPreference
274 virtual sal_Bool SAL_CALL releaseOnNotification() override;
276 // OComponentHelper
277 void SAL_CALL dispose() override;
279 private:
280 Reference<XInterface > xTheInstance;
281 bool bOneInstance;
282 protected:
283 // needed for implementing XUnloadingPreference in inheriting classes
284 bool isOneInstance() const {return bOneInstance;}
285 bool isInstance() const {return xTheInstance.is();}
289 Any SAL_CALL OFactoryComponentHelper::queryInterface( const Type & rType )
291 if( rType == cppu::UnoType<XUnloadingPreference>::get() )
293 return Any(
294 Reference< XUnloadingPreference >(
295 static_cast< XUnloadingPreference * >(this) ) );
297 return OComponentHelper::queryInterface( rType );
300 // XAggregation
301 Any OFactoryComponentHelper::queryAggregation( const Type & rType )
303 Any aRet( OComponentHelper::queryAggregation( rType ) );
304 return (aRet.hasValue() ? aRet : OSingleFactoryHelper::queryInterface( rType ));
307 // XTypeProvider
308 Sequence< Type > OFactoryComponentHelper::getTypes()
310 Type ar[ 4 ];
311 ar[ 0 ] = cppu::UnoType<XSingleServiceFactory>::get();
312 ar[ 1 ] = cppu::UnoType<XServiceInfo>::get();
313 ar[ 2 ] = cppu::UnoType<XUnloadingPreference>::get();
315 if (m_fptr)
316 ar[ 3 ] = cppu::UnoType<XSingleComponentFactory>::get();
318 return Sequence< Type >( ar, m_fptr ? 4 : 3 );
321 Sequence< sal_Int8 > OFactoryComponentHelper::getImplementationId()
323 return css::uno::Sequence<sal_Int8>();
326 // XSingleServiceFactory
327 Reference<XInterface > OFactoryComponentHelper::createInstance()
329 if( bOneInstance )
331 if( !xTheInstance.is() )
333 MutexGuard aGuard( aMutex );
334 if( !xTheInstance.is() )
335 xTheInstance = OSingleFactoryHelper::createInstance();
337 return xTheInstance;
339 return OSingleFactoryHelper::createInstance();
342 Reference<XInterface > OFactoryComponentHelper::createInstanceWithArguments(
343 const Sequence<Any>& Arguments )
345 if( bOneInstance )
347 if( !xTheInstance.is() )
349 MutexGuard aGuard( aMutex );
350 // OSL_ENSURE( !xTheInstance.is(), "### arguments will be ignored!" );
351 if( !xTheInstance.is() )
352 xTheInstance = OSingleFactoryHelper::createInstanceWithArguments( Arguments );
354 return xTheInstance;
356 return OSingleFactoryHelper::createInstanceWithArguments( Arguments );
359 // XSingleComponentFactory
361 Reference< XInterface > OFactoryComponentHelper::createInstanceWithContext(
362 Reference< XComponentContext > const & xContext )
364 if( bOneInstance )
366 if( !xTheInstance.is() )
368 MutexGuard aGuard( aMutex );
369 // OSL_ENSURE( !xTheInstance.is(), "### context will be ignored!" );
370 if( !xTheInstance.is() )
371 xTheInstance = OSingleFactoryHelper::createInstanceWithContext( xContext );
373 return xTheInstance;
375 return OSingleFactoryHelper::createInstanceWithContext( xContext );
378 Reference< XInterface > OFactoryComponentHelper::createInstanceWithArgumentsAndContext(
379 Sequence< Any > const & rArguments,
380 Reference< XComponentContext > const & xContext )
382 if( bOneInstance )
384 if( !xTheInstance.is() )
386 MutexGuard aGuard( aMutex );
387 // OSL_ENSURE( !xTheInstance.is(), "### context and arguments will be ignored!" );
388 if( !xTheInstance.is() )
389 xTheInstance = OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext );
391 return xTheInstance;
393 return OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext );
397 // OComponentHelper
398 void OFactoryComponentHelper::dispose()
400 OComponentHelper::dispose();
402 Reference<XInterface > x;
404 // do not delete in the guard section
405 MutexGuard aGuard( aMutex );
406 x = xTheInstance;
407 xTheInstance.clear();
409 // if it is a component call dispose at the component
410 Reference<XComponent > xComp( x, UNO_QUERY );
411 if( xComp.is() )
412 xComp->dispose();
415 // XUnloadingPreference
416 // This class is used for single factories, component factories and
417 // one-instance factories. Depending on the usage this function has
418 // to return different values.
419 // one-instance factory: sal_False
420 // single factory: sal_True
421 // component factory: sal_True
422 sal_Bool SAL_CALL OFactoryComponentHelper::releaseOnNotification()
424 if( bOneInstance)
425 return false;
426 return true;
429 class ORegistryFactoryHelper : public OFactoryComponentHelper,
430 public OPropertySetHelper
433 public:
434 ORegistryFactoryHelper(
435 const Reference<XMultiServiceFactory > & rServiceManager,
436 const OUString & rImplementationName_,
437 const Reference<XRegistryKey > & xImplementationKey_,
438 bool bOneInstance_ )
439 : OFactoryComponentHelper(
440 rServiceManager, rImplementationName_, nullptr, nullptr, nullptr, bOneInstance_ ),
441 OPropertySetHelper( OComponentHelper::rBHelper ),
442 xImplementationKey( xImplementationKey_ )
445 // XInterface
446 virtual Any SAL_CALL queryInterface( Type const & type ) override;
447 virtual void SAL_CALL acquire() throw () override;
448 virtual void SAL_CALL release() throw () override;
449 // XTypeProvider
450 virtual Sequence< Type > SAL_CALL getTypes() override;
451 // XPropertySet
452 virtual Reference< beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override;
454 // OPropertySetHelper
455 virtual IPropertyArrayHelper & SAL_CALL getInfoHelper() override;
456 virtual sal_Bool SAL_CALL convertFastPropertyValue(
457 Any & rConvertedValue, Any & rOldValue,
458 sal_Int32 nHandle, Any const & rValue ) override;
459 virtual void SAL_CALL setFastPropertyValue_NoBroadcast(
460 sal_Int32 nHandle, Any const & rValue ) override;
461 using OPropertySetHelper::getFastPropertyValue;
462 virtual void SAL_CALL getFastPropertyValue(
463 Any & rValue, sal_Int32 nHandle ) const override;
465 // OSingleFactoryHelper
466 Reference<XInterface > createInstanceEveryTime(
467 Reference< XComponentContext > const & xContext ) override;
469 // XSingleServiceFactory
470 Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments) override;
471 // XSingleComponentFactory
472 Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
473 Sequence< Any > const & rArguments,
474 Reference< XComponentContext > const & xContext ) override;
476 // XServiceInfo
477 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
478 // XUnloadingPreference
479 sal_Bool SAL_CALL releaseOnNotification() override;
482 private:
483 /// @throws css::uno::Exception
484 /// @throws css::uno::RuntimeException
485 Reference< XInterface > createModuleFactory();
487 /** The registry key of the implementation section */
488 Reference<XRegistryKey > xImplementationKey;
489 /** The factory created with the loader. */
490 Reference<XSingleComponentFactory > xModuleFactory;
491 Reference<XSingleServiceFactory > xModuleFactoryDepr;
492 Reference< beans::XPropertySetInfo > m_xInfo;
493 std::unique_ptr< IPropertyArrayHelper > m_property_array_helper;
494 protected:
495 using OPropertySetHelper::getTypes;
498 // XInterface
500 Any SAL_CALL ORegistryFactoryHelper::queryInterface(
501 Type const & type )
503 Any ret( OFactoryComponentHelper::queryInterface( type ) );
504 if (ret.hasValue())
505 return ret;
506 return OPropertySetHelper::queryInterface( type );
510 void ORegistryFactoryHelper::acquire() throw ()
512 OFactoryComponentHelper::acquire();
516 void ORegistryFactoryHelper::release() throw ()
518 OFactoryComponentHelper::release();
521 // XTypeProvider
523 Sequence< Type > ORegistryFactoryHelper::getTypes()
525 Sequence< Type > types( OFactoryComponentHelper::getTypes() );
526 sal_Int32 pos = types.getLength();
527 types.realloc( pos + 3 );
528 Type * p = types.getArray();
529 p[ pos++ ] = cppu::UnoType<beans::XMultiPropertySet>::get();
530 p[ pos++ ] = cppu::UnoType<beans::XFastPropertySet>::get();
531 p[ pos++ ] = cppu::UnoType<beans::XPropertySet>::get();
532 return types;
535 // XPropertySet
537 Reference< beans::XPropertySetInfo >
538 ORegistryFactoryHelper::getPropertySetInfo()
540 ::osl::MutexGuard guard( aMutex );
541 if (! m_xInfo.is())
542 m_xInfo = createPropertySetInfo( getInfoHelper() );
543 return m_xInfo;
546 // OPropertySetHelper
548 IPropertyArrayHelper & ORegistryFactoryHelper::getInfoHelper()
550 ::osl::MutexGuard guard( aMutex );
551 if (m_property_array_helper == nullptr)
553 beans::Property prop(
554 "ImplementationKey" /* name */,
555 0 /* handle */,
556 cppu::UnoType<decltype(xImplementationKey)>::get(),
557 beans::PropertyAttribute::READONLY |
558 beans::PropertyAttribute::OPTIONAL );
559 m_property_array_helper.reset(
560 new ::cppu::OPropertyArrayHelper( &prop, 1 ) );
562 return *m_property_array_helper;
566 sal_Bool ORegistryFactoryHelper::convertFastPropertyValue(
567 Any &, Any &, sal_Int32, Any const & )
569 OSL_FAIL( "unexpected!" );
570 return false;
574 void ORegistryFactoryHelper::setFastPropertyValue_NoBroadcast(
575 sal_Int32, Any const & )
577 throw beans::PropertyVetoException(
578 "unexpected: only readonly properties!",
579 static_cast< OWeakObject * >(this) );
583 void ORegistryFactoryHelper::getFastPropertyValue(
584 Any & rValue, sal_Int32 nHandle ) const
586 if (nHandle == 0)
588 rValue <<= xImplementationKey;
590 else
592 rValue.clear();
593 throw beans::UnknownPropertyException(
594 "unknown property!", static_cast< OWeakObject * >(
595 const_cast< ORegistryFactoryHelper * >(this) ) );
599 Reference<XInterface > ORegistryFactoryHelper::createInstanceEveryTime(
600 Reference< XComponentContext > const & xContext )
602 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
604 Reference< XInterface > x( createModuleFactory() );
605 if (x.is())
607 MutexGuard aGuard( aMutex );
608 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
610 xModuleFactory.set( x, UNO_QUERY );
611 xModuleFactoryDepr.set( x, UNO_QUERY );
615 if( xModuleFactory.is() )
617 return xModuleFactory->createInstanceWithContext( xContext );
619 if( xModuleFactoryDepr.is() )
621 return xModuleFactoryDepr->createInstance();
624 return Reference<XInterface >();
627 Reference<XInterface > SAL_CALL ORegistryFactoryHelper::createInstanceWithArguments(
628 const Sequence<Any>& Arguments )
630 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
632 Reference< XInterface > x( createModuleFactory() );
633 if (x.is())
635 MutexGuard aGuard( aMutex );
636 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
638 xModuleFactory.set( x, UNO_QUERY );
639 xModuleFactoryDepr.set( x, UNO_QUERY );
643 if( xModuleFactoryDepr.is() )
645 return xModuleFactoryDepr->createInstanceWithArguments( Arguments );
647 if( xModuleFactory.is() )
649 SAL_INFO("cppuhelper", "no context ORegistryFactoryHelper::createInstanceWithArgumentsAndContext()!");
650 return xModuleFactory->createInstanceWithArgumentsAndContext( Arguments, Reference< XComponentContext >() );
653 return Reference<XInterface >();
656 Reference< XInterface > ORegistryFactoryHelper::createInstanceWithArgumentsAndContext(
657 Sequence< Any > const & rArguments,
658 Reference< XComponentContext > const & xContext )
660 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
662 Reference< XInterface > x( createModuleFactory() );
663 if (x.is())
665 MutexGuard aGuard( aMutex );
666 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
668 xModuleFactory.set( x, UNO_QUERY );
669 xModuleFactoryDepr.set( x, UNO_QUERY );
673 if( xModuleFactory.is() )
675 return xModuleFactory->createInstanceWithArgumentsAndContext( rArguments, xContext );
677 if( xModuleFactoryDepr.is() )
679 SAL_INFO_IF(xContext.is(), "cppuhelper", "ignoring context calling ORegistryFactoryHelper::createInstaceWithArgumentsAndContext()!");
680 return xModuleFactoryDepr->createInstanceWithArguments( rArguments );
683 return Reference<XInterface >();
687 // OSingleFactoryHelper
688 Reference< XInterface > ORegistryFactoryHelper::createModuleFactory()
690 OUString aActivatorUrl;
691 OUString aActivatorName;
692 OUString aLocation;
694 Reference<XRegistryKey > xActivatorKey = xImplementationKey->openKey(
695 "/UNO/ACTIVATOR" );
696 if( xActivatorKey.is() && xActivatorKey->getValueType() == RegistryValueType_ASCII )
698 aActivatorUrl = xActivatorKey->getAsciiValue();
700 aActivatorName = aActivatorUrl.getToken(0, ':');
702 Reference<XRegistryKey > xLocationKey = xImplementationKey->openKey(
703 "/UNO/LOCATION" );
704 if( xLocationKey.is() && xLocationKey->getValueType() == RegistryValueType_ASCII )
705 aLocation = xLocationKey->getAsciiValue();
707 else
709 // old style"url"
710 // the location of the program code of the implementation
711 Reference<XRegistryKey > xLocationKey = xImplementationKey->openKey(
712 "/UNO/URL" );
713 // is the key of the right type ?
714 if( xLocationKey.is() && xLocationKey->getValueType() == RegistryValueType_ASCII )
716 // one implementation found -> try to activate
717 aLocation = xLocationKey->getAsciiValue();
719 // search protocol delimiter
720 sal_Int32 nPos = aLocation.indexOf("://");
721 if( nPos != -1 )
723 aActivatorName = aLocation.copy( 0, nPos );
724 if( aActivatorName == "java" )
725 aActivatorName = "com.sun.star.loader.Java";
726 else if( aActivatorName == "module" )
727 aActivatorName = "com.sun.star.loader.SharedLibrary";
728 aLocation = aLocation.copy( nPos + 3 );
733 Reference< XInterface > xFactory;
734 if( !aActivatorName.isEmpty() )
736 Reference<XInterface > x = xSMgr->createInstance( aActivatorName );
737 Reference<XImplementationLoader > xLoader( x, UNO_QUERY );
738 if (xLoader.is())
740 xFactory = xLoader->activate( aImplementationName, aActivatorUrl, aLocation, xImplementationKey );
743 return xFactory;
746 // XServiceInfo
747 Sequence< OUString > ORegistryFactoryHelper::getSupportedServiceNames()
749 MutexGuard aGuard( aMutex );
750 if( !aServiceNames.hasElements() )
752 // not yet loaded
755 Reference<XRegistryKey > xKey = xImplementationKey->openKey( "UNO/SERVICES" );
757 if (xKey.is())
759 // length of prefix. +1 for the '/' at the end
760 sal_Int32 nPrefixLen = xKey->getKeyName().getLength() + 1;
762 // Full qualified names like "IMPLEMENTATIONS/TEST/UNO/SERVICES/com.sun.star..."
763 Sequence<OUString> seqKeys = xKey->getKeyNames();
764 for( OUString & key : seqKeys )
765 key = key.copy(nPrefixLen);
767 aServiceNames = seqKeys;
770 catch (InvalidRegistryException &)
774 return aServiceNames;
777 sal_Bool SAL_CALL ORegistryFactoryHelper::releaseOnNotification()
779 bool retVal= true;
780 if( isOneInstance() && isInstance())
782 retVal= false;
784 else if( ! isOneInstance())
786 // try to delegate
787 if( xModuleFactory.is())
789 Reference<XUnloadingPreference> xunloading( xModuleFactory, UNO_QUERY);
790 if( xunloading.is())
791 retVal= xunloading->releaseOnNotification();
793 else if( xModuleFactoryDepr.is())
795 Reference<XUnloadingPreference> xunloading( xModuleFactoryDepr, UNO_QUERY);
796 if( xunloading.is())
797 retVal= xunloading->releaseOnNotification();
800 return retVal;
803 class OFactoryProxyHelper : public WeakImplHelper< XServiceInfo, XSingleServiceFactory,
804 XUnloadingPreference >
806 Reference<XSingleServiceFactory > xFactory;
808 public:
810 explicit OFactoryProxyHelper( const Reference<XSingleServiceFactory > & rFactory )
811 : xFactory( rFactory )
814 // XSingleServiceFactory
815 Reference<XInterface > SAL_CALL createInstance() override;
816 Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments) override;
818 // XServiceInfo
819 OUString SAL_CALL getImplementationName() override;
820 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
821 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
822 //XUnloadingPreference
823 sal_Bool SAL_CALL releaseOnNotification() override;
827 // XSingleServiceFactory
828 Reference<XInterface > OFactoryProxyHelper::createInstance()
830 return xFactory->createInstance();
833 // XSingleServiceFactory
834 Reference<XInterface > OFactoryProxyHelper::createInstanceWithArguments
836 const Sequence<Any>& Arguments
839 return xFactory->createInstanceWithArguments( Arguments );
842 // XServiceInfo
843 OUString OFactoryProxyHelper::getImplementationName()
845 Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
846 if( xInfo.is() )
847 return xInfo->getImplementationName();
848 return OUString();
851 // XServiceInfo
852 sal_Bool OFactoryProxyHelper::supportsService(const OUString& ServiceName)
854 return cppu::supportsService(this, ServiceName);
857 // XServiceInfo
858 Sequence< OUString > OFactoryProxyHelper::getSupportedServiceNames()
860 Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
861 if( xInfo.is() )
862 return xInfo->getSupportedServiceNames();
863 return Sequence< OUString >();
866 sal_Bool SAL_CALL OFactoryProxyHelper::releaseOnNotification()
869 Reference<XUnloadingPreference> pref( xFactory, UNO_QUERY);
870 if( pref.is())
871 return pref->releaseOnNotification();
872 return true;
875 // global function
876 Reference<XSingleServiceFactory > SAL_CALL createSingleFactory(
877 const Reference<XMultiServiceFactory > & rServiceManager,
878 const OUString & rImplementationName,
879 ComponentInstantiation pCreateFunction,
880 const Sequence< OUString > & rServiceNames,
881 rtl_ModuleCount * )
883 return new OFactoryComponentHelper(
884 rServiceManager, rImplementationName, pCreateFunction, nullptr, &rServiceNames, false );
887 // global function
888 Reference<XSingleServiceFactory > SAL_CALL createFactoryProxy(
889 SAL_UNUSED_PARAMETER const Reference<XMultiServiceFactory > &,
890 const Reference<XSingleServiceFactory > & rFactory )
892 return new OFactoryProxyHelper( rFactory );
895 // global function
896 Reference<XSingleServiceFactory > SAL_CALL createOneInstanceFactory(
897 const Reference<XMultiServiceFactory > & rServiceManager,
898 const OUString & rImplementationName,
899 ComponentInstantiation pCreateFunction,
900 const Sequence< OUString > & rServiceNames,
901 rtl_ModuleCount * )
903 return new OFactoryComponentHelper(
904 rServiceManager, rImplementationName, pCreateFunction, nullptr, &rServiceNames, true );
907 // global function
908 Reference<XSingleServiceFactory > SAL_CALL createSingleRegistryFactory(
909 const Reference<XMultiServiceFactory > & rServiceManager,
910 const OUString & rImplementationName,
911 const Reference<XRegistryKey > & rImplementationKey )
913 return new ORegistryFactoryHelper(
914 rServiceManager, rImplementationName, rImplementationKey, false );
917 // global function
918 Reference<XSingleServiceFactory > SAL_CALL createOneInstanceRegistryFactory(
919 const Reference<XMultiServiceFactory > & rServiceManager,
920 const OUString & rImplementationName,
921 const Reference<XRegistryKey > & rImplementationKey )
923 return new ORegistryFactoryHelper(
924 rServiceManager, rImplementationName, rImplementationKey, true );
928 Reference< lang::XSingleComponentFactory > SAL_CALL createSingleComponentFactory(
929 ComponentFactoryFunc fptr,
930 OUString const & rImplementationName,
931 Sequence< OUString > const & rServiceNames,
932 rtl_ModuleCount *)
934 return new OFactoryComponentHelper(
935 Reference< XMultiServiceFactory >(), rImplementationName, nullptr, fptr, &rServiceNames, false );
938 Reference< lang::XSingleComponentFactory > SAL_CALL createOneInstanceComponentFactory(
939 ComponentFactoryFunc fptr,
940 OUString const & rImplementationName,
941 Sequence< OUString > const & rServiceNames,
942 rtl_ModuleCount *)
944 return new OFactoryComponentHelper(
945 Reference< XMultiServiceFactory >(), rImplementationName, nullptr, fptr, &rServiceNames, true );
951 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */