update dev300-m58
[ooovba.git] / cppuhelper / source / factory.cxx
blob6dbac0833fe0972e7e3db6a1b9fcb48465cc80f1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: factory.cxx,v $
10 * $Revision: 1.28.22.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppuhelper.hxx"
33 #include <osl/diagnose.h>
34 #include <osl/mutex.hxx>
35 #include <cppuhelper/weak.hxx>
36 #include <cppuhelper/component.hxx>
37 #include <cppuhelper/factory.hxx>
38 #ifndef _CPPUHELPER_IMPLBASE3_HXX
39 #include <cppuhelper/implbase3.hxx>
40 #endif
41 #include <cppuhelper/typeprovider.hxx>
42 #include <rtl/unload.h>
44 #include "cppuhelper/propshlp.hxx"
46 #include <com/sun/star/lang/XServiceInfo.hpp>
47 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
48 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
49 #include <com/sun/star/lang/XInitialization.hpp>
50 #include <com/sun/star/loader/XImplementationLoader.hpp>
51 #include <com/sun/star/lang/XComponent.hpp>
52 #include <com/sun/star/lang/IllegalArgumentException.hpp>
53 #include <com/sun/star/uno/XUnloadingPreference.hpp>
54 #include "com/sun/star/beans/PropertyAttribute.hpp"
56 #include <memory>
58 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
61 using namespace osl;
62 using namespace rtl;
63 using namespace com::sun::star;
64 using namespace com::sun::star::uno;
65 using namespace com::sun::star::lang;
66 using namespace com::sun::star::loader;
67 using namespace com::sun::star::registry;
69 namespace cppu
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 class OSingleFactoryHelper
76 : public XServiceInfo
77 , public XSingleServiceFactory
78 , public lang::XSingleComponentFactory
79 , public XUnloadingPreference
81 public:
82 OSingleFactoryHelper(
83 const Reference<XMultiServiceFactory > & rServiceManager,
84 const OUString & rImplementationName_,
85 ComponentInstantiation pCreateFunction_,
86 ComponentFactoryFunc fptr,
87 const Sequence< OUString > * pServiceNames_ )
88 SAL_THROW( () )
89 : xSMgr( rServiceManager )
90 , pCreateFunction( pCreateFunction_ )
91 , m_fptr( fptr )
92 , aImplementationName( rImplementationName_ )
94 if( pServiceNames_ )
95 aServiceNames = *pServiceNames_;
98 // old function, only for backward compatibility
99 OSingleFactoryHelper(
100 const Reference<XMultiServiceFactory > & rServiceManager,
101 const OUString & rImplementationName_ )
102 SAL_THROW( () )
103 : xSMgr( rServiceManager )
104 , pCreateFunction( NULL )
105 , m_fptr( 0 )
106 , aImplementationName( rImplementationName_ )
109 virtual ~OSingleFactoryHelper();
111 // XInterface
112 Any SAL_CALL queryInterface( const Type & rType )
113 throw(::com::sun::star::uno::RuntimeException);
115 // XSingleServiceFactory
116 Reference<XInterface > SAL_CALL createInstance()
117 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
118 virtual Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments)
119 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
120 // XSingleComponentFactory
121 virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
122 Reference< XComponentContext > const & xContext )
123 throw (Exception, RuntimeException);
124 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
125 Sequence< Any > const & rArguments,
126 Reference< XComponentContext > const & xContext )
127 throw (Exception, RuntimeException);
129 // XServiceInfo
130 OUString SAL_CALL getImplementationName()
131 throw(::com::sun::star::uno::RuntimeException);
132 sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
133 throw(::com::sun::star::uno::RuntimeException);
134 Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
135 throw(::com::sun::star::uno::RuntimeException);
137 protected:
139 * Create an instance specified by the factory. The one instance logic is implemented
140 * in the createInstance and createInstanceWithArguments methods.
141 * @return the newly created instance. Do not return a previous (one instance) instance.
143 virtual Reference<XInterface > createInstanceEveryTime(
144 Reference< XComponentContext > const & xContext )
145 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
147 Reference<XMultiServiceFactory > xSMgr;
148 ComponentInstantiation pCreateFunction;
149 ComponentFactoryFunc m_fptr;
150 Sequence< OUString > aServiceNames;
151 OUString aImplementationName;
153 OSingleFactoryHelper::~OSingleFactoryHelper()
158 //-----------------------------------------------------------------------------
159 Any OSingleFactoryHelper::queryInterface( const Type & rType )
160 throw(::com::sun::star::uno::RuntimeException)
162 return ::cppu::queryInterface(
163 rType,
164 static_cast< XSingleComponentFactory * >( this ),
165 static_cast< XSingleServiceFactory * >( this ),
166 static_cast< XServiceInfo * >( this ) ,
167 static_cast< XUnloadingPreference * >( this ));
170 // OSingleFactoryHelper
171 Reference<XInterface > OSingleFactoryHelper::createInstanceEveryTime(
172 Reference< XComponentContext > const & xContext )
173 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
175 if (m_fptr)
177 return (*m_fptr)( xContext );
179 else if( pCreateFunction )
181 if (xContext.is())
183 Reference< lang::XMultiServiceFactory > xContextMgr(
184 xContext->getServiceManager(), UNO_QUERY );
185 if (xContextMgr.is())
186 return (*pCreateFunction)( xContextMgr );
188 return (*pCreateFunction)( xSMgr );
190 else
192 return Reference< XInterface >();
196 // XSingleServiceFactory
197 Reference<XInterface > OSingleFactoryHelper::createInstance()
198 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
200 return createInstanceWithContext( Reference< XComponentContext >() );
203 // XSingleServiceFactory
204 Reference<XInterface > OSingleFactoryHelper::createInstanceWithArguments(
205 const Sequence<Any>& Arguments )
206 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
208 return createInstanceWithArgumentsAndContext(
209 Arguments, Reference< XComponentContext >() );
212 // XSingleComponentFactory
213 //__________________________________________________________________________________________________
214 Reference< XInterface > OSingleFactoryHelper::createInstanceWithContext(
215 Reference< XComponentContext > const & xContext )
216 throw (Exception, RuntimeException)
218 return createInstanceEveryTime( xContext );
220 //__________________________________________________________________________________________________
221 Reference< XInterface > OSingleFactoryHelper::createInstanceWithArgumentsAndContext(
222 Sequence< Any > const & rArguments,
223 Reference< XComponentContext > const & xContext )
224 throw (Exception, RuntimeException)
226 Reference< XInterface > xRet( createInstanceWithContext( xContext ) );
228 Reference< lang::XInitialization > xInit( xRet, UNO_QUERY );
229 // always call initialize, even if there are no arguments.
230 // #i63511# / 2006-03-27 / frank.schoenheit@sun.com
231 if (xInit.is())
233 xInit->initialize( rArguments );
235 else
237 if ( rArguments.getLength() )
238 throw lang::IllegalArgumentException(
239 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot pass arguments to component => no XInitialization implemented!") ),
240 Reference< XInterface >(), 0 );
243 return xRet;
246 // XServiceInfo
247 OUString OSingleFactoryHelper::getImplementationName()
248 throw(::com::sun::star::uno::RuntimeException)
250 return aImplementationName;
253 // XServiceInfo
254 sal_Bool OSingleFactoryHelper::supportsService(
255 const OUString& ServiceName )
256 throw(::com::sun::star::uno::RuntimeException)
258 Sequence< OUString > seqServices = getSupportedServiceNames();
259 const OUString * pServices = seqServices.getConstArray();
260 for( sal_Int32 i = 0; i < seqServices.getLength(); i++ )
261 if( pServices[i] == ServiceName )
262 return sal_True;
264 return sal_False;
267 // XServiceInfo
268 Sequence< OUString > OSingleFactoryHelper::getSupportedServiceNames(void)
269 throw(::com::sun::star::uno::RuntimeException)
271 return aServiceNames;
275 //----------------------------------------------------------------------
276 //----------------------------------------------------------------------
277 //----------------------------------------------------------------------
278 struct OFactoryComponentHelper_Mutex
280 Mutex aMutex;
283 class OFactoryComponentHelper
284 : public OFactoryComponentHelper_Mutex
285 , public OComponentHelper
286 , public OSingleFactoryHelper
288 public:
289 OFactoryComponentHelper(
290 const Reference<XMultiServiceFactory > & rServiceManager,
291 const OUString & rImplementationName_,
292 ComponentInstantiation pCreateFunction_,
293 ComponentFactoryFunc fptr,
294 const Sequence< OUString > * pServiceNames_,
295 sal_Bool bOneInstance_ = sal_False )
296 SAL_THROW( () )
297 : OComponentHelper( aMutex )
298 , OSingleFactoryHelper( rServiceManager, rImplementationName_, pCreateFunction_, fptr, pServiceNames_ )
299 , bOneInstance( bOneInstance_ )
300 , pModuleCount(0)
304 // Used by the createXXXFactory functions. The argument pModCount is used to prevent the unloading of the module
305 // which contains pCreateFunction_
306 OFactoryComponentHelper(
307 const Reference<XMultiServiceFactory > & rServiceManager,
308 const OUString & rImplementationName_,
309 ComponentInstantiation pCreateFunction_,
310 ComponentFactoryFunc fptr,
311 const Sequence< OUString > * pServiceNames_,
312 rtl_ModuleCount * pModCount,
313 sal_Bool bOneInstance_ = sal_False )
314 SAL_THROW( () )
315 : OComponentHelper( aMutex )
316 , OSingleFactoryHelper( rServiceManager, rImplementationName_, pCreateFunction_, fptr, pServiceNames_ )
317 , bOneInstance( bOneInstance_ )
318 , pModuleCount(pModCount)
320 if(pModuleCount)
321 pModuleCount->acquire( pModuleCount);
324 // old function, only for backward compatibility
325 OFactoryComponentHelper(
326 const Reference<XMultiServiceFactory > & rServiceManager,
327 const OUString & rImplementationName_,
328 sal_Bool bOneInstance_ = sal_False )
329 SAL_THROW( () )
330 : OComponentHelper( aMutex )
331 , OSingleFactoryHelper( rServiceManager, rImplementationName_ )
332 , bOneInstance( bOneInstance_ )
333 , pModuleCount(0)
337 ~OFactoryComponentHelper()
339 if(pModuleCount)
340 pModuleCount->release( pModuleCount);
343 // XInterface
344 Any SAL_CALL queryInterface( const Type & rType )
345 throw(::com::sun::star::uno::RuntimeException);
346 void SAL_CALL acquire() throw()
347 { OComponentHelper::acquire(); }
348 void SAL_CALL release() throw()
349 { OComponentHelper::release(); }
351 // XSingleServiceFactory
352 Reference<XInterface > SAL_CALL createInstance()
353 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
354 Reference<XInterface > SAL_CALL createInstanceWithArguments( const Sequence<Any>& Arguments )
355 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
356 // XSingleComponentFactory
357 virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
358 Reference< XComponentContext > const & xContext )
359 throw (Exception, RuntimeException);
360 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
361 Sequence< Any > const & rArguments,
362 Reference< XComponentContext > const & xContext )
363 throw (Exception, RuntimeException);
365 // XTypeProvider
366 virtual Sequence< Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
367 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() throw(::com::sun::star::uno::RuntimeException);
369 // XAggregation
370 Any SAL_CALL queryAggregation( const Type & rType )
371 throw(::com::sun::star::uno::RuntimeException);
373 // XUnloadingPreference
374 virtual sal_Bool SAL_CALL releaseOnNotification()
375 throw(::com::sun::star::uno::RuntimeException);
377 // OComponentHelper
378 void SAL_CALL dispose() throw(::com::sun::star::uno::RuntimeException);
380 private:
381 Reference<XInterface > xTheInstance;
382 sal_Bool bOneInstance;
383 rtl_ModuleCount * pModuleCount;
384 protected:
385 // needed for implementing XUnloadingPreference in inheriting classes
386 sal_Bool isOneInstance() {return bOneInstance;}
387 sal_Bool isInstance() {return xTheInstance.is();}
391 Any SAL_CALL OFactoryComponentHelper::queryInterface( const Type & rType )
392 throw(::com::sun::star::uno::RuntimeException)
394 if( rType == ::getCppuType( (Reference<XUnloadingPreference>*)0))
396 return makeAny(
397 Reference< XUnloadingPreference >(
398 static_cast< XUnloadingPreference * >(this) ) );
400 return OComponentHelper::queryInterface( rType );
403 // XAggregation
404 Any OFactoryComponentHelper::queryAggregation( const Type & rType )
405 throw(::com::sun::star::uno::RuntimeException)
407 Any aRet( OComponentHelper::queryAggregation( rType ) );
408 return (aRet.hasValue() ? aRet : OSingleFactoryHelper::queryInterface( rType ));
411 // XTypeProvider
412 Sequence< Type > OFactoryComponentHelper::getTypes()
413 throw (::com::sun::star::uno::RuntimeException)
415 Type ar[ 4 ];
416 ar[ 0 ] = ::getCppuType( (const Reference< XSingleServiceFactory > *)0 );
417 ar[ 1 ] = ::getCppuType( (const Reference< XServiceInfo > *)0 );
418 ar[ 2 ] = ::getCppuType( (const Reference< XUnloadingPreference > *)0 );
420 if (m_fptr)
421 ar[ 3 ] = ::getCppuType( (const Reference< XSingleComponentFactory > *)0 );
423 return Sequence< Type >( ar, m_fptr ? 4 : 3 );
426 Sequence< sal_Int8 > OFactoryComponentHelper::getImplementationId()
427 throw (::com::sun::star::uno::RuntimeException)
429 static OImplementationId * pId = 0;
430 if (! pId)
432 MutexGuard aGuard( Mutex::getGlobalMutex() );
433 if (! pId)
435 static OImplementationId aId;
436 pId = &aId;
439 return pId->getImplementationId();
442 // XSingleServiceFactory
443 Reference<XInterface > OFactoryComponentHelper::createInstance()
444 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
446 if( bOneInstance )
448 if( !xTheInstance.is() )
450 MutexGuard aGuard( aMutex );
451 if( !xTheInstance.is() )
452 xTheInstance = OSingleFactoryHelper::createInstance();
454 return xTheInstance;
456 return OSingleFactoryHelper::createInstance();
459 Reference<XInterface > OFactoryComponentHelper::createInstanceWithArguments(
460 const Sequence<Any>& Arguments )
461 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
463 if( bOneInstance )
465 if( !xTheInstance.is() )
467 MutexGuard aGuard( aMutex );
468 // OSL_ENSURE( !xTheInstance.is(), "### arguments will be ignored!" );
469 if( !xTheInstance.is() )
470 xTheInstance = OSingleFactoryHelper::createInstanceWithArguments( Arguments );
472 return xTheInstance;
474 return OSingleFactoryHelper::createInstanceWithArguments( Arguments );
477 // XSingleComponentFactory
478 //__________________________________________________________________________________________________
479 Reference< XInterface > OFactoryComponentHelper::createInstanceWithContext(
480 Reference< XComponentContext > const & xContext )
481 throw (Exception, RuntimeException)
483 if( bOneInstance )
485 if( !xTheInstance.is() )
487 MutexGuard aGuard( aMutex );
488 // OSL_ENSURE( !xTheInstance.is(), "### context will be ignored!" );
489 if( !xTheInstance.is() )
490 xTheInstance = OSingleFactoryHelper::createInstanceWithContext( xContext );
492 return xTheInstance;
494 return OSingleFactoryHelper::createInstanceWithContext( xContext );
496 //__________________________________________________________________________________________________
497 Reference< XInterface > OFactoryComponentHelper::createInstanceWithArgumentsAndContext(
498 Sequence< Any > const & rArguments,
499 Reference< XComponentContext > const & xContext )
500 throw (Exception, RuntimeException)
502 if( bOneInstance )
504 if( !xTheInstance.is() )
506 MutexGuard aGuard( aMutex );
507 // OSL_ENSURE( !xTheInstance.is(), "### context and arguments will be ignored!" );
508 if( !xTheInstance.is() )
509 xTheInstance = OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext );
511 return xTheInstance;
513 return OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext );
517 // OComponentHelper
518 void OFactoryComponentHelper::dispose()
519 throw(::com::sun::star::uno::RuntimeException)
521 OComponentHelper::dispose();
523 Reference<XInterface > x;
525 // do not delete in the guard section
526 MutexGuard aGuard( aMutex );
527 x = xTheInstance;
528 xTheInstance = Reference<XInterface >();
530 // if it is a component call dispose at the component
531 Reference<XComponent > xComp( x, UNO_QUERY );
532 if( xComp.is() )
533 xComp->dispose();
536 // XUnloadingPreference
537 // This class is used for single factories, component factories and
538 // one-instance factories. Depending on the usage this function has
539 // to return different values.
540 // one-instance factory: sal_False
541 // single factory: sal_True
542 // component factory: sal_True
543 sal_Bool SAL_CALL OFactoryComponentHelper::releaseOnNotification() throw(::com::sun::star::uno::RuntimeException)
545 if( bOneInstance)
546 return sal_False;
547 return sal_True;
551 //-----------------------------------------------------------------------------
552 //-----------------------------------------------------------------------------
553 //-----------------------------------------------------------------------------
554 class ORegistryFactoryHelper : public OFactoryComponentHelper,
555 public OPropertySetHelper
558 public:
559 ORegistryFactoryHelper(
560 const Reference<XMultiServiceFactory > & rServiceManager,
561 const OUString & rImplementationName_,
562 const Reference<XRegistryKey > & xImplementationKey_,
563 sal_Bool bOneInstance_ = sal_False ) SAL_THROW( () )
564 : OFactoryComponentHelper(
565 rServiceManager, rImplementationName_, 0, 0, 0, bOneInstance_ ),
566 OPropertySetHelper( OComponentHelper::rBHelper ),
567 xImplementationKey( xImplementationKey_ )
570 // XInterface
571 virtual Any SAL_CALL queryInterface( Type const & type )
572 throw (RuntimeException);
573 virtual void SAL_CALL acquire() throw ();
574 virtual void SAL_CALL release() throw ();
575 // XTypeProvider
576 virtual Sequence< Type > SAL_CALL getTypes()
577 throw (RuntimeException);
578 // XPropertySet
579 virtual Reference< beans::XPropertySetInfo > SAL_CALL getPropertySetInfo()
580 throw (RuntimeException);
582 // OPropertySetHelper
583 virtual IPropertyArrayHelper & SAL_CALL getInfoHelper();
584 virtual sal_Bool SAL_CALL convertFastPropertyValue(
585 Any & rConvertedValue, Any & rOldValue,
586 sal_Int32 nHandle, Any const & rValue )
587 throw (lang::IllegalArgumentException);
588 virtual void SAL_CALL setFastPropertyValue_NoBroadcast(
589 sal_Int32 nHandle, Any const & rValue )
590 throw (Exception);
591 using OPropertySetHelper::getFastPropertyValue;
592 virtual void SAL_CALL getFastPropertyValue(
593 Any & rValue, sal_Int32 nHandle ) const;
595 // OSingleFactoryHelper
596 Reference<XInterface > createInstanceEveryTime(
597 Reference< XComponentContext > const & xContext )
598 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
600 // XSingleServiceFactory
601 Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments)
602 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
603 // XSingleComponentFactory
604 Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
605 Sequence< Any > const & rArguments,
606 Reference< XComponentContext > const & xContext )
607 throw (Exception, RuntimeException);
609 // XServiceInfo
610 Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
611 throw(::com::sun::star::uno::RuntimeException);
612 // XUnloadingPreference
613 sal_Bool SAL_CALL releaseOnNotification()
614 throw( RuntimeException);
617 private:
618 Reference< XInterface > createModuleFactory()
619 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
621 /** The registry key of the implementation section */
622 Reference<XRegistryKey > xImplementationKey;
623 /** The factory created with the loader. */
624 Reference<XSingleComponentFactory > xModuleFactory;
625 Reference<XSingleServiceFactory > xModuleFactoryDepr;
626 Reference< beans::XPropertySetInfo > m_xInfo;
627 ::std::auto_ptr< IPropertyArrayHelper > m_property_array_helper;
628 protected:
629 using OPropertySetHelper::getTypes;
632 // XInterface
633 //______________________________________________________________________________
634 Any SAL_CALL ORegistryFactoryHelper::queryInterface(
635 Type const & type ) throw (RuntimeException)
637 Any ret( OFactoryComponentHelper::queryInterface( type ) );
638 if (ret.hasValue())
639 return ret;
640 else
641 return OPropertySetHelper::queryInterface( type );
644 //______________________________________________________________________________
645 void ORegistryFactoryHelper::acquire() throw ()
647 OFactoryComponentHelper::acquire();
650 //______________________________________________________________________________
651 void ORegistryFactoryHelper::release() throw ()
653 OFactoryComponentHelper::release();
656 // XTypeProvider
657 //______________________________________________________________________________
658 Sequence< Type > ORegistryFactoryHelper::getTypes() throw (RuntimeException)
660 Sequence< Type > types( OFactoryComponentHelper::getTypes() );
661 sal_Int32 pos = types.getLength();
662 types.realloc( pos + 3 );
663 Type * p = types.getArray();
664 p[ pos++ ] = ::getCppuType(
665 reinterpret_cast< Reference< beans::XMultiPropertySet > const * >(0) );
666 p[ pos++ ] = ::getCppuType(
667 reinterpret_cast< Reference< beans::XFastPropertySet > const * >(0) );
668 p[ pos++ ] = ::getCppuType(
669 reinterpret_cast< Reference< beans::XPropertySet > const * >(0) );
670 return types;
673 // XPropertySet
674 //______________________________________________________________________________
675 Reference< beans::XPropertySetInfo >
676 ORegistryFactoryHelper::getPropertySetInfo() throw (RuntimeException)
678 ::osl::MutexGuard guard( aMutex );
679 if (! m_xInfo.is())
680 m_xInfo = createPropertySetInfo( getInfoHelper() );
681 return m_xInfo;
684 // OPropertySetHelper
685 //______________________________________________________________________________
686 IPropertyArrayHelper & ORegistryFactoryHelper::getInfoHelper()
688 ::osl::MutexGuard guard( aMutex );
689 if (m_property_array_helper.get() == 0)
691 beans::Property prop(
692 OUSTR("ImplementationKey") /* name */,
693 0 /* handle */,
694 ::getCppuType( &xImplementationKey ),
695 beans::PropertyAttribute::READONLY |
696 beans::PropertyAttribute::OPTIONAL );
697 m_property_array_helper.reset(
698 new ::cppu::OPropertyArrayHelper( &prop, 1 ) );
700 return *m_property_array_helper.get();
703 //______________________________________________________________________________
704 sal_Bool ORegistryFactoryHelper::convertFastPropertyValue(
705 Any &, Any &, sal_Int32, Any const & )
706 throw (lang::IllegalArgumentException)
708 OSL_ENSURE( 0, "unexpected!" );
709 return false;
712 //______________________________________________________________________________
713 void ORegistryFactoryHelper::setFastPropertyValue_NoBroadcast(
714 sal_Int32, Any const & )
715 throw (Exception)
717 throw beans::PropertyVetoException(
718 OUSTR("unexpected: only readonly properties!"),
719 static_cast< OWeakObject * >(this) );
722 //______________________________________________________________________________
723 void ORegistryFactoryHelper::getFastPropertyValue(
724 Any & rValue, sal_Int32 nHandle ) const
726 if (nHandle == 0)
728 rValue <<= xImplementationKey;
730 else
732 rValue.clear();
733 throw beans::UnknownPropertyException(
734 OUSTR("unknown property!"), static_cast< OWeakObject * >(
735 const_cast< ORegistryFactoryHelper * >(this) ) );
739 Reference<XInterface > ORegistryFactoryHelper::createInstanceEveryTime(
740 Reference< XComponentContext > const & xContext )
741 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
743 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
745 Reference< XInterface > x( createModuleFactory() );
746 if (x.is())
748 MutexGuard aGuard( aMutex );
749 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
751 xModuleFactory.set( x, UNO_QUERY );
752 xModuleFactoryDepr.set( x, UNO_QUERY );
756 if( xModuleFactory.is() )
758 return xModuleFactory->createInstanceWithContext( xContext );
760 else if( xModuleFactoryDepr.is() )
762 return xModuleFactoryDepr->createInstance();
765 return Reference<XInterface >();
768 Reference<XInterface > SAL_CALL ORegistryFactoryHelper::createInstanceWithArguments(
769 const Sequence<Any>& Arguments )
770 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
772 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
774 Reference< XInterface > x( createModuleFactory() );
775 if (x.is())
777 MutexGuard aGuard( aMutex );
778 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
780 xModuleFactory.set( x, UNO_QUERY );
781 xModuleFactoryDepr.set( x, UNO_QUERY );
785 if( xModuleFactoryDepr.is() )
787 return xModuleFactoryDepr->createInstanceWithArguments( Arguments );
789 else if( xModuleFactory.is() )
791 #if OSL_DEBUG_LEVEL > 1
792 OSL_TRACE( "### no context ORegistryFactoryHelper::createInstanceWithArgumentsAndContext()!\n" );
793 #endif
794 return xModuleFactory->createInstanceWithArgumentsAndContext( Arguments, Reference< XComponentContext >() );
797 return Reference<XInterface >();
800 Reference< XInterface > ORegistryFactoryHelper::createInstanceWithArgumentsAndContext(
801 Sequence< Any > const & rArguments,
802 Reference< XComponentContext > const & xContext )
803 throw (Exception, RuntimeException)
805 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
807 Reference< XInterface > x( createModuleFactory() );
808 if (x.is())
810 MutexGuard aGuard( aMutex );
811 if( !xModuleFactory.is() && !xModuleFactoryDepr.is() )
813 xModuleFactory.set( x, UNO_QUERY );
814 xModuleFactoryDepr.set( x, UNO_QUERY );
818 if( xModuleFactory.is() )
820 return xModuleFactory->createInstanceWithArgumentsAndContext( rArguments, xContext );
822 else if( xModuleFactoryDepr.is() )
824 #if OSL_DEBUG_LEVEL > 1
825 if (xContext.is())
827 OSL_TRACE( "### ignoring context calling ORegistryFactoryHelper::createInstanceWithArgumentsAndContext()!\n" );
829 #endif
830 return xModuleFactoryDepr->createInstanceWithArguments( rArguments );
833 return Reference<XInterface >();
837 // OSingleFactoryHelper
838 Reference< XInterface > ORegistryFactoryHelper::createModuleFactory()
839 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
841 OUString aActivatorUrl;
842 OUString aActivatorName;
843 OUString aLocation;
845 Reference<XRegistryKey > xActivatorKey = xImplementationKey->openKey(
846 OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/ACTIVATOR") ) );
847 if( xActivatorKey.is() && xActivatorKey->getValueType() == RegistryValueType_ASCII )
849 aActivatorUrl = xActivatorKey->getAsciiValue();
851 OUString tmpActivator(aActivatorUrl.getStr());
852 sal_Int32 nIndex = 0;
853 aActivatorName = tmpActivator.getToken(0, ':', nIndex );
855 Reference<XRegistryKey > xLocationKey = xImplementationKey->openKey(
856 OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/LOCATION") ) );
857 if( xLocationKey.is() && xLocationKey->getValueType() == RegistryValueType_ASCII )
858 aLocation = xLocationKey->getAsciiValue();
860 else
862 // old style"url"
863 // the location of the program code of the implementation
864 Reference<XRegistryKey > xLocationKey = xImplementationKey->openKey(
865 OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/URL") ) );
866 // is the the key of the right type ?
867 if( xLocationKey.is() && xLocationKey->getValueType() == RegistryValueType_ASCII )
869 // one implementation found -> try to activate
870 aLocation = xLocationKey->getAsciiValue();
872 // search protocol delemitter
873 sal_Int32 nPos = aLocation.indexOf(
874 OUString( RTL_CONSTASCII_USTRINGPARAM("://") ) );
875 if( nPos != -1 )
877 aActivatorName = aLocation.copy( 0, nPos );
878 if( aActivatorName.compareToAscii( "java" ) == 0 )
879 aActivatorName = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.Java") );
880 else if( aActivatorName.compareToAscii( "module" ) == 0 )
881 aActivatorName = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary") );
882 aLocation = aLocation.copy( nPos + 3 );
887 Reference< XInterface > xFactory;
888 if( aActivatorName.getLength() != 0 )
890 Reference<XInterface > x = xSMgr->createInstance( aActivatorName );
891 Reference<XImplementationLoader > xLoader( x, UNO_QUERY );
892 Reference<XInterface > xMF;
893 if (xLoader.is())
895 xFactory = xLoader->activate( aImplementationName, aActivatorUrl, aLocation, xImplementationKey );
898 return xFactory;
901 // XServiceInfo
902 Sequence< OUString > ORegistryFactoryHelper::getSupportedServiceNames(void)
903 throw(::com::sun::star::uno::RuntimeException)
905 MutexGuard aGuard( aMutex );
906 if( aServiceNames.getLength() == 0 )
908 // not yet loaded
911 Reference<XRegistryKey > xKey = xImplementationKey->openKey(
912 OUString( RTL_CONSTASCII_USTRINGPARAM("UNO/SERVICES") ) );
914 if (xKey.is())
916 // length of prefix. +1 for the '/' at the end
917 sal_Int32 nPrefixLen = xKey->getKeyName().getLength() + 1;
919 // Full qualified names like "IMPLEMENTATIONS/TEST/UNO/SERVICES/com.sun.star..."
920 Sequence<OUString> seqKeys = xKey->getKeyNames();
921 OUString* pKeys = seqKeys.getArray();
922 for( sal_Int32 i = 0; i < seqKeys.getLength(); i++ )
923 pKeys[i] = pKeys[i].copy(nPrefixLen);
925 aServiceNames = seqKeys;
928 catch (InvalidRegistryException &)
932 return aServiceNames;
935 sal_Bool SAL_CALL ORegistryFactoryHelper::releaseOnNotification() throw(::com::sun::star::uno::RuntimeException)
937 sal_Bool retVal= sal_True;
938 if( isOneInstance() && isInstance())
940 retVal= sal_False;
942 else if( ! isOneInstance())
944 // try to delegate
945 if( xModuleFactory.is())
947 Reference<XUnloadingPreference> xunloading( xModuleFactory, UNO_QUERY);
948 if( xunloading.is())
949 retVal= xunloading->releaseOnNotification();
951 else if( xModuleFactoryDepr.is())
953 Reference<XUnloadingPreference> xunloading( xModuleFactoryDepr, UNO_QUERY);
954 if( xunloading.is())
955 retVal= xunloading->releaseOnNotification();
958 return retVal;
961 //-----------------------------------------------------------------------------
962 //-----------------------------------------------------------------------------
963 //-----------------------------------------------------------------------------
965 class OFactoryProxyHelper : public WeakImplHelper3< XServiceInfo, XSingleServiceFactory,
966 XUnloadingPreference >
968 Reference<XSingleServiceFactory > xFactory;
970 public:
972 OFactoryProxyHelper(
973 const Reference<XMultiServiceFactory > & /*rServiceManager*/,
974 const Reference<XSingleServiceFactory > & rFactory )
975 SAL_THROW( () )
976 : xFactory( rFactory )
979 // XSingleServiceFactory
980 Reference<XInterface > SAL_CALL createInstance()
981 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
982 Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments)
983 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
985 // XServiceInfo
986 OUString SAL_CALL getImplementationName()
987 throw(::com::sun::star::uno::RuntimeException);
988 sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
989 throw(::com::sun::star::uno::RuntimeException);
990 Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
991 throw(::com::sun::star::uno::RuntimeException);
992 //XUnloadingPreference
993 sal_Bool SAL_CALL releaseOnNotification()
994 throw(::com::sun::star::uno::RuntimeException);
998 // XSingleServiceFactory
999 Reference<XInterface > OFactoryProxyHelper::createInstance()
1000 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
1002 return xFactory->createInstance();
1005 // XSingleServiceFactory
1006 Reference<XInterface > OFactoryProxyHelper::createInstanceWithArguments
1008 const Sequence<Any>& Arguments
1010 throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
1012 return xFactory->createInstanceWithArguments( Arguments );
1015 // XServiceInfo
1016 OUString OFactoryProxyHelper::getImplementationName()
1017 throw(::com::sun::star::uno::RuntimeException)
1019 Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
1020 if( xInfo.is() )
1021 return xInfo->getImplementationName();
1022 return OUString();
1025 // XServiceInfo
1026 sal_Bool OFactoryProxyHelper::supportsService(const OUString& ServiceName)
1027 throw(::com::sun::star::uno::RuntimeException)
1029 Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
1030 if( xInfo.is() )
1031 return xInfo->supportsService( ServiceName );
1032 return sal_False;
1035 // XServiceInfo
1036 Sequence< OUString > OFactoryProxyHelper::getSupportedServiceNames(void)
1037 throw(::com::sun::star::uno::RuntimeException)
1039 Reference<XServiceInfo > xInfo( xFactory, UNO_QUERY );
1040 if( xInfo.is() )
1041 return xInfo->getSupportedServiceNames();
1042 return Sequence< OUString >();
1045 sal_Bool SAL_CALL OFactoryProxyHelper::releaseOnNotification() throw(::com::sun::star::uno::RuntimeException)
1048 Reference<XUnloadingPreference> pref( xFactory, UNO_QUERY);
1049 if( pref.is())
1050 return pref->releaseOnNotification();
1051 return sal_True;
1055 //-----------------------------------------------------------------------------
1056 //-----------------------------------------------------------------------------
1057 //-----------------------------------------------------------------------------
1058 // global function
1059 Reference<XSingleServiceFactory > SAL_CALL createSingleFactory(
1060 const Reference<XMultiServiceFactory > & rServiceManager,
1061 const OUString & rImplementationName,
1062 ComponentInstantiation pCreateFunction,
1063 const Sequence< OUString > & rServiceNames,
1064 rtl_ModuleCount *pModCount )
1065 SAL_THROW( () )
1067 return new OFactoryComponentHelper(
1068 rServiceManager, rImplementationName, pCreateFunction, 0, &rServiceNames, pModCount, sal_False );
1071 // global function
1072 Reference<XSingleServiceFactory > SAL_CALL createFactoryProxy(
1073 const Reference<XMultiServiceFactory > & rServiceManager,
1074 const Reference<XSingleServiceFactory > & rFactory )
1075 SAL_THROW( () )
1077 return new OFactoryProxyHelper(
1078 rServiceManager, rFactory );
1081 // global function
1082 Reference<XSingleServiceFactory > SAL_CALL createOneInstanceFactory(
1083 const Reference<XMultiServiceFactory > & rServiceManager,
1084 const OUString & rImplementationName,
1085 ComponentInstantiation pCreateFunction,
1086 const Sequence< OUString > & rServiceNames,
1087 rtl_ModuleCount *pModCount )
1088 SAL_THROW( () )
1090 return new OFactoryComponentHelper(
1091 rServiceManager, rImplementationName, pCreateFunction, 0, &rServiceNames, pModCount, sal_True );
1092 // return new OFactoryUnloadableComponentHelper(
1093 // rServiceManager, rImplementationName, pCreateFunction, 0, &rServiceNames, pModCount, sal_True );
1096 // global function
1097 Reference<XSingleServiceFactory > SAL_CALL createSingleRegistryFactory(
1098 const Reference<XMultiServiceFactory > & rServiceManager,
1099 const OUString & rImplementationName,
1100 const Reference<XRegistryKey > & rImplementationKey )
1101 SAL_THROW( () )
1103 return new ORegistryFactoryHelper(
1104 rServiceManager, rImplementationName, rImplementationKey, sal_False );
1107 // global function
1108 Reference<XSingleServiceFactory > SAL_CALL createOneInstanceRegistryFactory(
1109 const Reference<XMultiServiceFactory > & rServiceManager,
1110 const OUString & rImplementationName,
1111 const Reference<XRegistryKey > & rImplementationKey )
1112 SAL_THROW( () )
1114 return new ORegistryFactoryHelper(
1115 rServiceManager, rImplementationName, rImplementationKey, sal_True );
1118 //##################################################################################################
1119 Reference< lang::XSingleComponentFactory > SAL_CALL createSingleComponentFactory(
1120 ComponentFactoryFunc fptr,
1121 OUString const & rImplementationName,
1122 Sequence< OUString > const & rServiceNames,
1123 rtl_ModuleCount * pModCount)
1124 SAL_THROW( () )
1126 return new OFactoryComponentHelper(
1127 Reference< XMultiServiceFactory >(), rImplementationName, 0, fptr, &rServiceNames, pModCount, sal_False );