Revert "new change in helpcontent2"
[LibreOffice.git] / cppuhelper / source / factory.cxx
blob6747892e3efc963cbfaa185fe7f2d1eb6dbecfa1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <osl/diagnose.h>
30 #include <osl/mutex.hxx>
31 #include <cppuhelper/weak.hxx>
32 #include <cppuhelper/bootstrap.hxx>
33 #include <cppuhelper/component.hxx>
34 #include <cppuhelper/factory.hxx>
35 #include <cppuhelper/implbase3.hxx>
36 #include <cppuhelper/shlib.hxx>
37 #include <cppuhelper/supportsservice.hxx>
38 #include <cppuhelper/typeprovider.hxx>
39 #include <rtl/instance.hxx>
40 #include <rtl/unload.h>
42 #include "cppuhelper/propshlp.hxx"
44 #include <com/sun/star/lang/XServiceInfo.hpp>
45 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
46 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
47 #include <com/sun/star/lang/XInitialization.hpp>
48 #include <com/sun/star/loader/XImplementationLoader.hpp>
49 #include <com/sun/star/lang/XComponent.hpp>
50 #include <com/sun/star/lang/IllegalArgumentException.hpp>
51 #include <com/sun/star/uno/XUnloadingPreference.hpp>
52 #include "com/sun/star/beans/PropertyAttribute.hpp"
54 #include <memory>
56 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
59 using namespace osl;
60 using namespace com::sun::star;
61 using namespace com::sun::star::uno;
62 using namespace com::sun::star::lang;
63 using namespace com::sun::star::loader;
64 using namespace com::sun::star::registry;
66 using ::rtl::OUString;
68 namespace cppu
71 class OSingleFactoryHelper
72 : public XServiceInfo
73 , public XSingleServiceFactory
74 , public lang::XSingleComponentFactory
75 , public XUnloadingPreference
77 public:
78 OSingleFactoryHelper(
79 const Reference<XMultiServiceFactory > & rServiceManager,
80 const OUString & rImplementationName_,
81 ComponentInstantiation pCreateFunction_,
82 ComponentFactoryFunc fptr,
83 const Sequence< OUString > * pServiceNames_ )
84 SAL_THROW(())
85 : xSMgr( rServiceManager )
86 , pCreateFunction( pCreateFunction_ )
87 , m_fptr( fptr )
88 , aImplementationName( rImplementationName_ )
90 if( pServiceNames_ )
91 aServiceNames = *pServiceNames_;
94 // old function, only for backward compatibility
95 OSingleFactoryHelper(
96 const Reference<XMultiServiceFactory > & rServiceManager,
97 const OUString & rImplementationName_ )
98 SAL_THROW(())
99 : xSMgr( rServiceManager )
100 , pCreateFunction( NULL )
101 , m_fptr( 0 )
102 , aImplementationName( rImplementationName_ )
105 virtual ~OSingleFactoryHelper();
107 // XInterface
108 Any SAL_CALL queryInterface( const Type & rType )
109 throw(::com::sun::star::uno::RuntimeException);
111 // XSingleServiceFactory
112 Reference<XInterface > SAL_CALL createInstance()
113 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
114 virtual Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments)
115 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
116 // XSingleComponentFactory
117 virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
118 Reference< XComponentContext > const & xContext )
119 throw (Exception, RuntimeException);
120 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
121 Sequence< Any > const & rArguments,
122 Reference< XComponentContext > const & xContext )
123 throw (Exception, RuntimeException);
125 // XServiceInfo
126 OUString SAL_CALL getImplementationName()
127 throw(::com::sun::star::uno::RuntimeException);
128 sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
129 throw(::com::sun::star::uno::RuntimeException);
130 Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
131 throw(::com::sun::star::uno::RuntimeException);
133 protected:
135 * Create an instance specified by the factory. The one instance logic is implemented
136 * in the createInstance and createInstanceWithArguments methods.
137 * @return the newly created instance. Do not return a previous (one instance) instance.
139 virtual Reference<XInterface > createInstanceEveryTime(
140 Reference< XComponentContext > const & xContext )
141 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
143 Reference<XMultiServiceFactory > xSMgr;
144 ComponentInstantiation pCreateFunction;
145 ComponentFactoryFunc m_fptr;
146 Sequence< OUString > aServiceNames;
147 OUString aImplementationName;
149 OSingleFactoryHelper::~OSingleFactoryHelper()
154 //-----------------------------------------------------------------------------
155 Any OSingleFactoryHelper::queryInterface( const Type & rType )
156 throw(::com::sun::star::uno::RuntimeException)
158 return ::cppu::queryInterface(
159 rType,
160 static_cast< XSingleComponentFactory * >( this ),
161 static_cast< XSingleServiceFactory * >( this ),
162 static_cast< XServiceInfo * >( this ) ,
163 static_cast< XUnloadingPreference * >( this ));
166 // OSingleFactoryHelper
167 Reference<XInterface > OSingleFactoryHelper::createInstanceEveryTime(
168 Reference< XComponentContext > const & xContext )
169 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
171 if (m_fptr)
173 return (*m_fptr)( xContext );
175 else if( pCreateFunction )
177 if (xContext.is())
179 Reference< lang::XMultiServiceFactory > xContextMgr(
180 xContext->getServiceManager(), UNO_QUERY );
181 if (xContextMgr.is())
182 return (*pCreateFunction)( xContextMgr );
184 return (*pCreateFunction)( xSMgr );
186 else
188 return Reference< XInterface >();
192 // XSingleServiceFactory
193 Reference<XInterface > OSingleFactoryHelper::createInstance()
194 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
196 return createInstanceWithContext( Reference< XComponentContext >() );
199 // XSingleServiceFactory
200 Reference<XInterface > OSingleFactoryHelper::createInstanceWithArguments(
201 const Sequence<Any>& Arguments )
202 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
204 return createInstanceWithArgumentsAndContext(
205 Arguments, Reference< XComponentContext >() );
208 // XSingleComponentFactory
209 //__________________________________________________________________________________________________
210 Reference< XInterface > OSingleFactoryHelper::createInstanceWithContext(
211 Reference< XComponentContext > const & xContext )
212 throw (Exception, RuntimeException)
214 return createInstanceEveryTime( xContext );
216 //__________________________________________________________________________________________________
217 Reference< XInterface > OSingleFactoryHelper::createInstanceWithArgumentsAndContext(
218 Sequence< Any > const & rArguments,
219 Reference< XComponentContext > const & xContext )
220 throw (Exception, RuntimeException)
222 Reference< XInterface > xRet( createInstanceWithContext( xContext ) );
224 Reference< lang::XInitialization > xInit( xRet, UNO_QUERY );
225 // always call initialize, even if there are no arguments.
226 // #i63511# / 2006-03-27 / frank.schoenheit@sun.com
227 if (xInit.is())
229 xInit->initialize( rArguments );
231 else
233 if ( rArguments.getLength() )
235 // dispose the here created UNO object before throwing out exception
236 // to avoid risk of memory leaks #i113722#
237 Reference<XComponent> xComp( xRet, UNO_QUERY );
238 if (xComp.is())
239 xComp->dispose();
241 throw lang::IllegalArgumentException(
242 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot pass arguments to component => no XInitialization implemented!") ),
243 Reference< XInterface >(), 0 );
247 return xRet;
250 // XServiceInfo
251 OUString OSingleFactoryHelper::getImplementationName()
252 throw(::com::sun::star::uno::RuntimeException)
254 return aImplementationName;
257 // XServiceInfo
258 sal_Bool OSingleFactoryHelper::supportsService(
259 const OUString& ServiceName )
260 throw(::com::sun::star::uno::RuntimeException)
262 return cppu::supportsService(this, ServiceName);
265 // XServiceInfo
266 Sequence< OUString > OSingleFactoryHelper::getSupportedServiceNames(void)
267 throw(::com::sun::star::uno::RuntimeException)
269 return aServiceNames;
272 struct OFactoryComponentHelper_Mutex
274 Mutex aMutex;
277 class OFactoryComponentHelper
278 : public OFactoryComponentHelper_Mutex
279 , public OComponentHelper
280 , public OSingleFactoryHelper
282 public:
283 OFactoryComponentHelper(
284 const Reference<XMultiServiceFactory > & rServiceManager,
285 const OUString & rImplementationName_,
286 ComponentInstantiation pCreateFunction_,
287 ComponentFactoryFunc fptr,
288 const Sequence< OUString > * pServiceNames_,
289 sal_Bool bOneInstance_ = sal_False )
290 SAL_THROW(())
291 : OComponentHelper( aMutex )
292 , OSingleFactoryHelper( rServiceManager, rImplementationName_, pCreateFunction_, fptr, pServiceNames_ )
293 , bOneInstance( bOneInstance_ )
294 , pModuleCount(0)
298 // Used by the createXXXFactory functions. The argument pModCount is used to prevent the unloading of the module
299 // which contains pCreateFunction_
300 OFactoryComponentHelper(
301 const Reference<XMultiServiceFactory > & rServiceManager,
302 const OUString & rImplementationName_,
303 ComponentInstantiation pCreateFunction_,
304 ComponentFactoryFunc fptr,
305 const Sequence< OUString > * pServiceNames_,
306 rtl_ModuleCount * pModCount,
307 sal_Bool bOneInstance_ = sal_False )
308 SAL_THROW(())
309 : OComponentHelper( aMutex )
310 , OSingleFactoryHelper( rServiceManager, rImplementationName_, pCreateFunction_, fptr, pServiceNames_ )
311 , bOneInstance( bOneInstance_ )
312 , pModuleCount(pModCount)
314 if(pModuleCount)
315 pModuleCount->acquire( pModuleCount);
318 // old function, only for backward compatibility
319 OFactoryComponentHelper(
320 const Reference<XMultiServiceFactory > & rServiceManager,
321 const OUString & rImplementationName_,
322 sal_Bool bOneInstance_ = sal_False )
323 SAL_THROW(())
324 : OComponentHelper( aMutex )
325 , OSingleFactoryHelper( rServiceManager, rImplementationName_ )
326 , bOneInstance( bOneInstance_ )
327 , pModuleCount(0)
331 ~OFactoryComponentHelper()
333 if(pModuleCount)
334 pModuleCount->release( pModuleCount);
337 // XInterface
338 Any SAL_CALL queryInterface( const Type & rType )
339 throw(::com::sun::star::uno::RuntimeException);
340 void SAL_CALL acquire() throw()
341 { OComponentHelper::acquire(); }
342 void SAL_CALL release() throw()
343 { OComponentHelper::release(); }
345 // XSingleServiceFactory
346 Reference<XInterface > SAL_CALL createInstance()
347 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
348 Reference<XInterface > SAL_CALL createInstanceWithArguments( const Sequence<Any>& Arguments )
349 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
350 // XSingleComponentFactory
351 virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
352 Reference< XComponentContext > const & xContext )
353 throw (Exception, RuntimeException);
354 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
355 Sequence< Any > const & rArguments,
356 Reference< XComponentContext > const & xContext )
357 throw (Exception, RuntimeException);
359 // XTypeProvider
360 virtual Sequence< Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
361 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() throw(::com::sun::star::uno::RuntimeException);
363 // XAggregation
364 Any SAL_CALL queryAggregation( const Type & rType )
365 throw(::com::sun::star::uno::RuntimeException);
367 // XUnloadingPreference
368 virtual sal_Bool SAL_CALL releaseOnNotification()
369 throw(::com::sun::star::uno::RuntimeException);
371 // OComponentHelper
372 void SAL_CALL dispose() throw(::com::sun::star::uno::RuntimeException);
374 private:
375 Reference<XInterface > xTheInstance;
376 sal_Bool bOneInstance;
377 rtl_ModuleCount * pModuleCount;
378 protected:
379 // needed for implementing XUnloadingPreference in inheriting classes
380 sal_Bool isOneInstance() {return bOneInstance;}
381 sal_Bool isInstance() {return xTheInstance.is();}
385 Any SAL_CALL OFactoryComponentHelper::queryInterface( const Type & rType )
386 throw(::com::sun::star::uno::RuntimeException)
388 if( rType == ::getCppuType( (Reference<XUnloadingPreference>*)0))
390 return makeAny(
391 Reference< XUnloadingPreference >(
392 static_cast< XUnloadingPreference * >(this) ) );
394 return OComponentHelper::queryInterface( rType );
397 // XAggregation
398 Any OFactoryComponentHelper::queryAggregation( const Type & rType )
399 throw(::com::sun::star::uno::RuntimeException)
401 Any aRet( OComponentHelper::queryAggregation( rType ) );
402 return (aRet.hasValue() ? aRet : OSingleFactoryHelper::queryInterface( rType ));
405 // XTypeProvider
406 Sequence< Type > OFactoryComponentHelper::getTypes()
407 throw (::com::sun::star::uno::RuntimeException)
409 Type ar[ 4 ];
410 ar[ 0 ] = ::getCppuType( (const Reference< XSingleServiceFactory > *)0 );
411 ar[ 1 ] = ::getCppuType( (const Reference< XServiceInfo > *)0 );
412 ar[ 2 ] = ::getCppuType( (const Reference< XUnloadingPreference > *)0 );
414 if (m_fptr)
415 ar[ 3 ] = ::getCppuType( (const Reference< XSingleComponentFactory > *)0 );
417 return Sequence< Type >( ar, m_fptr ? 4 : 3 );
420 namespace
422 class theOFactoryComponentHelperImplementationId :
423 public rtl::Static<OImplementationId, theOFactoryComponentHelperImplementationId>{};
426 Sequence< sal_Int8 > OFactoryComponentHelper::getImplementationId()
427 throw (::com::sun::star::uno::RuntimeException)
429 return theOFactoryComponentHelperImplementationId::get().getImplementationId();
432 // XSingleServiceFactory
433 Reference<XInterface > OFactoryComponentHelper::createInstance()
434 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
436 if( bOneInstance )
438 if( !xTheInstance.is() )
440 MutexGuard aGuard( aMutex );
441 if( !xTheInstance.is() )
442 xTheInstance = OSingleFactoryHelper::createInstance();
444 return xTheInstance;
446 return OSingleFactoryHelper::createInstance();
449 Reference<XInterface > OFactoryComponentHelper::createInstanceWithArguments(
450 const Sequence<Any>& Arguments )
451 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
453 if( bOneInstance )
455 if( !xTheInstance.is() )
457 MutexGuard aGuard( aMutex );
458 // OSL_ENSURE( !xTheInstance.is(), "### arguments will be ignored!" );
459 if( !xTheInstance.is() )
460 xTheInstance = OSingleFactoryHelper::createInstanceWithArguments( Arguments );
462 return xTheInstance;
464 return OSingleFactoryHelper::createInstanceWithArguments( Arguments );
467 // XSingleComponentFactory
468 //__________________________________________________________________________________________________
469 Reference< XInterface > OFactoryComponentHelper::createInstanceWithContext(
470 Reference< XComponentContext > const & xContext )
471 throw (Exception, RuntimeException)
473 if( bOneInstance )
475 if( !xTheInstance.is() )
477 MutexGuard aGuard( aMutex );
478 // OSL_ENSURE( !xTheInstance.is(), "### context will be ignored!" );
479 if( !xTheInstance.is() )
480 xTheInstance = OSingleFactoryHelper::createInstanceWithContext( xContext );
482 return xTheInstance;
484 return OSingleFactoryHelper::createInstanceWithContext( xContext );
486 //__________________________________________________________________________________________________
487 Reference< XInterface > OFactoryComponentHelper::createInstanceWithArgumentsAndContext(
488 Sequence< Any > const & rArguments,
489 Reference< XComponentContext > const & xContext )
490 throw (Exception, RuntimeException)
492 if( bOneInstance )
494 if( !xTheInstance.is() )
496 MutexGuard aGuard( aMutex );
497 // OSL_ENSURE( !xTheInstance.is(), "### context and arguments will be ignored!" );
498 if( !xTheInstance.is() )
499 xTheInstance = OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext );
501 return xTheInstance;
503 return OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext );
507 // OComponentHelper
508 void OFactoryComponentHelper::dispose()
509 throw(::com::sun::star::uno::RuntimeException)
511 OComponentHelper::dispose();
513 Reference<XInterface > x;
515 // do not delete in the guard section
516 MutexGuard aGuard( aMutex );
517 x = xTheInstance;
518 xTheInstance = Reference<XInterface >();
520 // if it is a component call dispose at the component
521 Reference<XComponent > xComp( x, UNO_QUERY );
522 if( xComp.is() )
523 xComp->dispose();
526 // XUnloadingPreference
527 // This class is used for single factories, component factories and
528 // one-instance factories. Depending on the usage this function has
529 // to return different values.
530 // one-instance factory: sal_False
531 // single factory: sal_True
532 // component factory: sal_True
533 sal_Bool SAL_CALL OFactoryComponentHelper::releaseOnNotification() throw(::com::sun::star::uno::RuntimeException)
535 if( bOneInstance)
536 return sal_False;
537 return sal_True;
540 class ORegistryFactoryHelper : public OFactoryComponentHelper,
541 public OPropertySetHelper
544 public:
545 ORegistryFactoryHelper(
546 const Reference<XMultiServiceFactory > & rServiceManager,
547 const OUString & rImplementationName_,
548 const Reference<XRegistryKey > & xImplementationKey_,
549 sal_Bool bOneInstance_ = sal_False ) SAL_THROW(())
550 : OFactoryComponentHelper(
551 rServiceManager, rImplementationName_, 0, 0, 0, bOneInstance_ ),
552 OPropertySetHelper( OComponentHelper::rBHelper ),
553 xImplementationKey( xImplementationKey_ )
556 // XInterface
557 virtual Any SAL_CALL queryInterface( Type const & type )
558 throw (RuntimeException);
559 virtual void SAL_CALL acquire() throw ();
560 virtual void SAL_CALL release() throw ();
561 // XTypeProvider
562 virtual Sequence< Type > SAL_CALL getTypes()
563 throw (RuntimeException);
564 // XPropertySet
565 virtual Reference< beans::XPropertySetInfo > SAL_CALL getPropertySetInfo()
566 throw (RuntimeException);
568 // OPropertySetHelper
569 virtual IPropertyArrayHelper & SAL_CALL getInfoHelper();
570 virtual sal_Bool SAL_CALL convertFastPropertyValue(
571 Any & rConvertedValue, Any & rOldValue,
572 sal_Int32 nHandle, Any const & rValue )
573 throw (lang::IllegalArgumentException);
574 virtual void SAL_CALL setFastPropertyValue_NoBroadcast(
575 sal_Int32 nHandle, Any const & rValue )
576 throw (Exception);
577 using OPropertySetHelper::getFastPropertyValue;
578 virtual void SAL_CALL getFastPropertyValue(
579 Any & rValue, sal_Int32 nHandle ) const;
581 // OSingleFactoryHelper
582 Reference<XInterface > createInstanceEveryTime(
583 Reference< XComponentContext > const & xContext )
584 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
586 // XSingleServiceFactory
587 Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments)
588 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
589 // XSingleComponentFactory
590 Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
591 Sequence< Any > const & rArguments,
592 Reference< XComponentContext > const & xContext )
593 throw (Exception, RuntimeException);
595 // XServiceInfo
596 Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
597 throw(::com::sun::star::uno::RuntimeException);
598 // XUnloadingPreference
599 sal_Bool SAL_CALL releaseOnNotification()
600 throw( RuntimeException);
603 private:
604 Reference< XInterface > createModuleFactory()
605 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
607 /** The registry key of the implementation section */
608 Reference<XRegistryKey > xImplementationKey;
609 /** The factory created with the loader. */
610 Reference<XSingleComponentFactory > xModuleFactory;
611 Reference<XSingleServiceFactory > xModuleFactoryDepr;
612 Reference< beans::XPropertySetInfo > m_xInfo;
613 ::std::auto_ptr< IPropertyArrayHelper > m_property_array_helper;
614 protected:
615 using OPropertySetHelper::getTypes;
618 // XInterface
619 //______________________________________________________________________________
620 Any SAL_CALL ORegistryFactoryHelper::queryInterface(
621 Type const & type ) throw (RuntimeException)
623 Any ret( OFactoryComponentHelper::queryInterface( type ) );
624 if (ret.hasValue())
625 return ret;
626 else
627 return OPropertySetHelper::queryInterface( type );
630 //______________________________________________________________________________
631 void ORegistryFactoryHelper::acquire() throw ()
633 OFactoryComponentHelper::acquire();
636 //______________________________________________________________________________
637 void ORegistryFactoryHelper::release() throw ()
639 OFactoryComponentHelper::release();
642 // XTypeProvider
643 //______________________________________________________________________________
644 Sequence< Type > ORegistryFactoryHelper::getTypes() throw (RuntimeException)
646 Sequence< Type > types( OFactoryComponentHelper::getTypes() );
647 sal_Int32 pos = types.getLength();
648 types.realloc( pos + 3 );
649 Type * p = types.getArray();
650 p[ pos++ ] = ::getCppuType(
651 reinterpret_cast< Reference< beans::XMultiPropertySet > const * >(0) );
652 p[ pos++ ] = ::getCppuType(
653 reinterpret_cast< Reference< beans::XFastPropertySet > const * >(0) );
654 p[ pos++ ] = ::getCppuType(
655 reinterpret_cast< Reference< beans::XPropertySet > const * >(0) );
656 return types;
659 // XPropertySet
660 //______________________________________________________________________________
661 Reference< beans::XPropertySetInfo >
662 ORegistryFactoryHelper::getPropertySetInfo() throw (RuntimeException)
664 ::osl::MutexGuard guard( aMutex );
665 if (! m_xInfo.is())
666 m_xInfo = createPropertySetInfo( getInfoHelper() );
667 return m_xInfo;
670 // OPropertySetHelper
671 //______________________________________________________________________________
672 IPropertyArrayHelper & ORegistryFactoryHelper::getInfoHelper()
674 ::osl::MutexGuard guard( aMutex );
675 if (m_property_array_helper.get() == 0)
677 beans::Property prop(
678 OUSTR("ImplementationKey") /* name */,
679 0 /* handle */,
680 ::getCppuType( &xImplementationKey ),
681 beans::PropertyAttribute::READONLY |
682 beans::PropertyAttribute::OPTIONAL );
683 m_property_array_helper.reset(
684 new ::cppu::OPropertyArrayHelper( &prop, 1 ) );
686 return *m_property_array_helper.get();
689 //______________________________________________________________________________
690 sal_Bool ORegistryFactoryHelper::convertFastPropertyValue(
691 Any &, Any &, sal_Int32, Any const & )
692 throw (lang::IllegalArgumentException)
694 OSL_FAIL( "unexpected!" );
695 return false;
698 //______________________________________________________________________________
699 void ORegistryFactoryHelper::setFastPropertyValue_NoBroadcast(
700 sal_Int32, Any const & )
701 throw (Exception)
703 throw beans::PropertyVetoException(
704 OUSTR("unexpected: only readonly properties!"),
705 static_cast< OWeakObject * >(this) );
708 //______________________________________________________________________________
709 void ORegistryFactoryHelper::getFastPropertyValue(
710 Any & rValue, sal_Int32 nHandle ) const
712 if (nHandle == 0)
714 rValue <<= xImplementationKey;
716 else
718 rValue.clear();
719 throw beans::UnknownPropertyException(
720 OUSTR("unknown property!"), static_cast< OWeakObject * >(
721 const_cast< ORegistryFactoryHelper * >(this) ) );
725 Reference<XInterface > ORegistryFactoryHelper::createInstanceEveryTime(
726 Reference< XComponentContext > const & xContext )
727 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
729 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
731 Reference< XInterface > x( createModuleFactory() );
732 if (x.is())
734 MutexGuard aGuard( aMutex );
735 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
737 xModuleFactory.set( x, UNO_QUERY );
738 xModuleFactoryDepr.set( x, UNO_QUERY );
742 if( xModuleFactory.is() )
744 return xModuleFactory->createInstanceWithContext( xContext );
746 else if( xModuleFactoryDepr.is() )
748 return xModuleFactoryDepr->createInstance();
751 return Reference<XInterface >();
754 Reference<XInterface > SAL_CALL ORegistryFactoryHelper::createInstanceWithArguments(
755 const Sequence<Any>& Arguments )
756 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
758 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
760 Reference< XInterface > x( createModuleFactory() );
761 if (x.is())
763 MutexGuard aGuard( aMutex );
764 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
766 xModuleFactory.set( x, UNO_QUERY );
767 xModuleFactoryDepr.set( x, UNO_QUERY );
771 if( xModuleFactoryDepr.is() )
773 return xModuleFactoryDepr->createInstanceWithArguments( Arguments );
775 else if( xModuleFactory.is() )
777 #if OSL_DEBUG_LEVEL > 1
778 OSL_TRACE( "### no context ORegistryFactoryHelper::createInstanceWithArgumentsAndContext()!" );
779 #endif
780 return xModuleFactory->createInstanceWithArgumentsAndContext( Arguments, Reference< XComponentContext >() );
783 return Reference<XInterface >();
786 Reference< XInterface > ORegistryFactoryHelper::createInstanceWithArgumentsAndContext(
787 Sequence< Any > const & rArguments,
788 Reference< XComponentContext > const & xContext )
789 throw (Exception, RuntimeException)
791 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
793 Reference< XInterface > x( createModuleFactory() );
794 if (x.is())
796 MutexGuard aGuard( aMutex );
797 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
799 xModuleFactory.set( x, UNO_QUERY );
800 xModuleFactoryDepr.set( x, UNO_QUERY );
804 if( xModuleFactory.is() )
806 return xModuleFactory->createInstanceWithArgumentsAndContext( rArguments, xContext );
808 else if( xModuleFactoryDepr.is() )
810 #if OSL_DEBUG_LEVEL > 1
811 if (xContext.is())
813 OSL_TRACE( "### ignoring context calling ORegistryFactoryHelper::createInstanceWithArgumentsAndContext()!" );
815 #endif
816 return xModuleFactoryDepr->createInstanceWithArguments( rArguments );
819 return Reference<XInterface >();
823 // OSingleFactoryHelper
824 Reference< XInterface > ORegistryFactoryHelper::createModuleFactory()
825 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
827 OUString aActivatorUrl;
828 OUString aActivatorName;
829 OUString aLocation;
831 Reference<XRegistryKey > xActivatorKey = xImplementationKey->openKey(
832 OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/ACTIVATOR") ) );
833 if( xActivatorKey.is() && xActivatorKey->getValueType() == RegistryValueType_ASCII )
835 aActivatorUrl = xActivatorKey->getAsciiValue();
837 OUString tmpActivator(aActivatorUrl.getStr());
838 sal_Int32 nIndex = 0;
839 aActivatorName = tmpActivator.getToken(0, ':', nIndex );
841 Reference<XRegistryKey > xLocationKey = xImplementationKey->openKey(
842 OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/LOCATION") ) );
843 if( xLocationKey.is() && xLocationKey->getValueType() == RegistryValueType_ASCII )
844 aLocation = xLocationKey->getAsciiValue();
846 else
848 // old style"url"
849 // the location of the program code of the implementation
850 Reference<XRegistryKey > xLocationKey = xImplementationKey->openKey(
851 OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/URL") ) );
852 // is the the key of the right type ?
853 if( xLocationKey.is() && xLocationKey->getValueType() == RegistryValueType_ASCII )
855 // one implementation found -> try to activate
856 aLocation = xLocationKey->getAsciiValue();
858 // search protocol delimiter
859 sal_Int32 nPos = aLocation.indexOf(
860 OUString( RTL_CONSTASCII_USTRINGPARAM("://") ) );
861 if( nPos != -1 )
863 aActivatorName = aLocation.copy( 0, nPos );
864 if( aActivatorName.compareToAscii( "java" ) == 0 )
865 aActivatorName = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.Java") );
866 else if( aActivatorName.compareToAscii( "module" ) == 0 )
867 aActivatorName = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary") );
868 aLocation = aLocation.copy( nPos + 3 );
873 Reference< XInterface > xFactory;
874 if( !aActivatorName.isEmpty() )
876 Reference<XInterface > x = xSMgr->createInstance( aActivatorName );
877 Reference<XImplementationLoader > xLoader( x, UNO_QUERY );
878 Reference<XInterface > xMF;
879 if (xLoader.is())
881 xFactory = xLoader->activate( aImplementationName, aActivatorUrl, aLocation, xImplementationKey );
884 return xFactory;
887 // XServiceInfo
888 Sequence< OUString > ORegistryFactoryHelper::getSupportedServiceNames(void)
889 throw(::com::sun::star::uno::RuntimeException)
891 MutexGuard aGuard( aMutex );
892 if( aServiceNames.getLength() == 0 )
894 // not yet loaded
897 Reference<XRegistryKey > xKey = xImplementationKey->openKey(
898 OUString( RTL_CONSTASCII_USTRINGPARAM("UNO/SERVICES") ) );
900 if (xKey.is())
902 // length of prefix. +1 for the '/' at the end
903 sal_Int32 nPrefixLen = xKey->getKeyName().getLength() + 1;
905 // Full qualified names like "IMPLEMENTATIONS/TEST/UNO/SERVICES/com.sun.star..."
906 Sequence<OUString> seqKeys = xKey->getKeyNames();
907 OUString* pKeys = seqKeys.getArray();
908 for( sal_Int32 i = 0; i < seqKeys.getLength(); i++ )
909 pKeys[i] = pKeys[i].copy(nPrefixLen);
911 aServiceNames = seqKeys;
914 catch (InvalidRegistryException &)
918 return aServiceNames;
921 sal_Bool SAL_CALL ORegistryFactoryHelper::releaseOnNotification() throw(::com::sun::star::uno::RuntimeException)
923 sal_Bool retVal= sal_True;
924 if( isOneInstance() && isInstance())
926 retVal= sal_False;
928 else if( ! isOneInstance())
930 // try to delegate
931 if( xModuleFactory.is())
933 Reference<XUnloadingPreference> xunloading( xModuleFactory, UNO_QUERY);
934 if( xunloading.is())
935 retVal= xunloading->releaseOnNotification();
937 else if( xModuleFactoryDepr.is())
939 Reference<XUnloadingPreference> xunloading( xModuleFactoryDepr, UNO_QUERY);
940 if( xunloading.is())
941 retVal= xunloading->releaseOnNotification();
944 return retVal;
947 class OFactoryProxyHelper : public WeakImplHelper3< XServiceInfo, XSingleServiceFactory,
948 XUnloadingPreference >
950 Reference<XSingleServiceFactory > xFactory;
952 public:
954 OFactoryProxyHelper( const Reference<XSingleServiceFactory > & rFactory )
955 SAL_THROW(())
956 : xFactory( rFactory )
959 // XSingleServiceFactory
960 Reference<XInterface > SAL_CALL createInstance()
961 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
962 Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments)
963 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
965 // XServiceInfo
966 OUString SAL_CALL getImplementationName()
967 throw(::com::sun::star::uno::RuntimeException);
968 sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
969 throw(::com::sun::star::uno::RuntimeException);
970 Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
971 throw(::com::sun::star::uno::RuntimeException);
972 //XUnloadingPreference
973 sal_Bool SAL_CALL releaseOnNotification()
974 throw(::com::sun::star::uno::RuntimeException);
978 // XSingleServiceFactory
979 Reference<XInterface > OFactoryProxyHelper::createInstance()
980 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
982 return xFactory->createInstance();
985 // XSingleServiceFactory
986 Reference<XInterface > OFactoryProxyHelper::createInstanceWithArguments
988 const Sequence<Any>& Arguments
990 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
992 return xFactory->createInstanceWithArguments( Arguments );
995 // XServiceInfo
996 OUString OFactoryProxyHelper::getImplementationName()
997 throw(::com::sun::star::uno::RuntimeException)
999 Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
1000 if( xInfo.is() )
1001 return xInfo->getImplementationName();
1002 return OUString();
1005 // XServiceInfo
1006 sal_Bool OFactoryProxyHelper::supportsService(const OUString& ServiceName)
1007 throw(::com::sun::star::uno::RuntimeException)
1009 Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
1010 return xInfo.is() && xInfo->supportsService( ServiceName );
1013 // XServiceInfo
1014 Sequence< OUString > OFactoryProxyHelper::getSupportedServiceNames(void)
1015 throw(::com::sun::star::uno::RuntimeException)
1017 Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
1018 if( xInfo.is() )
1019 return xInfo->getSupportedServiceNames();
1020 return Sequence< OUString >();
1023 sal_Bool SAL_CALL OFactoryProxyHelper::releaseOnNotification() throw(::com::sun::star::uno::RuntimeException)
1026 Reference<XUnloadingPreference> pref( xFactory, UNO_QUERY);
1027 if( pref.is())
1028 return pref->releaseOnNotification();
1029 return sal_True;
1032 // global function
1033 Reference<XSingleServiceFactory > SAL_CALL createSingleFactory(
1034 const Reference<XMultiServiceFactory > & rServiceManager,
1035 const OUString & rImplementationName,
1036 ComponentInstantiation pCreateFunction,
1037 const Sequence< OUString > & rServiceNames,
1038 rtl_ModuleCount *pModCount )
1039 SAL_THROW(())
1041 return new OFactoryComponentHelper(
1042 rServiceManager, rImplementationName, pCreateFunction, 0, &rServiceNames, pModCount, sal_False );
1045 // global function
1046 Reference<XSingleServiceFactory > SAL_CALL createFactoryProxy(
1047 SAL_UNUSED_PARAMETER const Reference<XMultiServiceFactory > &,
1048 const Reference<XSingleServiceFactory > & rFactory )
1049 SAL_THROW(())
1051 return new OFactoryProxyHelper( rFactory );
1054 // global function
1055 Reference<XSingleServiceFactory > SAL_CALL createOneInstanceFactory(
1056 const Reference<XMultiServiceFactory > & rServiceManager,
1057 const OUString & rImplementationName,
1058 ComponentInstantiation pCreateFunction,
1059 const Sequence< OUString > & rServiceNames,
1060 rtl_ModuleCount *pModCount )
1061 SAL_THROW(())
1063 return new OFactoryComponentHelper(
1064 rServiceManager, rImplementationName, pCreateFunction, 0, &rServiceNames, pModCount, sal_True );
1067 // global function
1068 Reference<XSingleServiceFactory > SAL_CALL createSingleRegistryFactory(
1069 const Reference<XMultiServiceFactory > & rServiceManager,
1070 const OUString & rImplementationName,
1071 const Reference<XRegistryKey > & rImplementationKey )
1072 SAL_THROW(())
1074 return new ORegistryFactoryHelper(
1075 rServiceManager, rImplementationName, rImplementationKey, sal_False );
1078 // global function
1079 Reference<XSingleServiceFactory > SAL_CALL createOneInstanceRegistryFactory(
1080 const Reference<XMultiServiceFactory > & rServiceManager,
1081 const OUString & rImplementationName,
1082 const Reference<XRegistryKey > & rImplementationKey )
1083 SAL_THROW(())
1085 return new ORegistryFactoryHelper(
1086 rServiceManager, rImplementationName, rImplementationKey, sal_True );
1089 //##################################################################################################
1090 Reference< lang::XSingleComponentFactory > SAL_CALL createSingleComponentFactory(
1091 ComponentFactoryFunc fptr,
1092 OUString const & rImplementationName,
1093 Sequence< OUString > const & rServiceNames,
1094 rtl_ModuleCount * pModCount)
1095 SAL_THROW(())
1097 return new OFactoryComponentHelper(
1098 Reference< XMultiServiceFactory >(), rImplementationName, 0, fptr, &rServiceNames, pModCount, sal_False );
1101 Reference< lang::XSingleComponentFactory > SAL_CALL createOneInstanceComponentFactory(
1102 ComponentFactoryFunc fptr,
1103 OUString const & rImplementationName,
1104 Sequence< OUString > const & rServiceNames,
1105 rtl_ModuleCount * pModCount)
1106 SAL_THROW(())
1108 return new OFactoryComponentHelper(
1109 Reference< XMultiServiceFactory >(), rImplementationName, 0, fptr, &rServiceNames, pModCount, sal_True );
1115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */