merge the formfield patch from ooo-build
[ooovba.git] / comphelper / source / misc / componentmodule.cxx
blobdab5cf3d77ac5947ca889920433cfb750d02273a
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: componentmodule.cxx,v $
10 * $Revision: 1.7 $
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_comphelper.hxx"
33 #include <comphelper/componentmodule.hxx>
35 /** === begin UNO includes === **/
36 /** === end UNO includes === **/
37 #include <comphelper/sequence.hxx>
38 #include <osl/diagnose.h>
40 #include <vector>
42 //........................................................................
43 namespace comphelper
45 //........................................................................
47 using namespace ::cppu;
48 /** === being UNO using === **/
49 using ::com::sun::star::uno::Sequence;
50 using ::com::sun::star::uno::RuntimeException;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::lang::XMultiServiceFactory;
53 using ::com::sun::star::registry::XRegistryKey;
54 using ::com::sun::star::uno::Exception;
55 using ::com::sun::star::uno::XInterface;
56 /** === end UNO using === **/
58 typedef ::std::vector< ComponentDescription > ComponentDescriptions;
60 //=========================================================================
61 //= OModuleImpl
62 //=========================================================================
63 /** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
65 class OModuleImpl
67 public:
68 ComponentDescriptions m_aRegisteredComponents;
70 OModuleImpl();
71 ~OModuleImpl();
74 //-------------------------------------------------------------------------
75 OModuleImpl::OModuleImpl()
79 //-------------------------------------------------------------------------
80 OModuleImpl::~OModuleImpl()
84 //=========================================================================
85 //= OModule
86 //=========================================================================
87 //-------------------------------------------------------------------------
88 OModule::OModule()
89 :m_nClients( 0 )
90 ,m_pImpl( new OModuleImpl )
94 OModule::~OModule() {}
96 //-------------------------------------------------------------------------
97 void OModule::registerClient( OModule::ClientAccess )
99 ::osl::MutexGuard aGuard(m_aMutex);
100 if ( 1 == osl_incrementInterlockedCount( &m_nClients ) )
101 onFirstClient();
104 //-------------------------------------------------------------------------
105 void OModule::revokeClient( OModule::ClientAccess )
107 ::osl::MutexGuard aGuard(m_aMutex);
108 if ( 0 == osl_decrementInterlockedCount( &m_nClients ) )
109 onLastClient();
112 //--------------------------------------------------------------------------
113 void OModule::onFirstClient()
117 //--------------------------------------------------------------------------
118 void OModule::onLastClient()
122 //--------------------------------------------------------------------------
123 void OModule::registerImplementation( const ComponentDescription& _rComp )
125 ::osl::MutexGuard aGuard( m_aMutex );
126 if ( !m_pImpl )
127 throw RuntimeException();
129 m_pImpl->m_aRegisteredComponents.push_back( _rComp );
132 //--------------------------------------------------------------------------
133 void OModule::registerImplementation( const ::rtl::OUString& _rImplementationName, const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
134 ::cppu::ComponentFactoryFunc _pCreateFunction, FactoryInstantiation _pFactoryFunction )
136 ComponentDescription aComponent( _rImplementationName, _rServiceNames, ::rtl::OUString(), _pCreateFunction, _pFactoryFunction );
137 registerImplementation( aComponent );
140 //--------------------------------------------------------------------------
141 sal_Bool OModule::writeComponentInfos( void* pServiceManager, void* pRegistryKey )
143 Reference< XMultiServiceFactory > xFactory( static_cast< XMultiServiceFactory* >( pServiceManager ) );
144 Reference< XRegistryKey > xRegistryKey( static_cast< XRegistryKey* >( pRegistryKey ) );
145 return writeComponentInfos( xFactory, xRegistryKey );
148 //--------------------------------------------------------------------------
149 sal_Bool OModule::writeComponentInfos(
150 const Reference< XMultiServiceFactory >& /*_rxServiceManager*/,
151 const Reference< XRegistryKey >& _rxRootKey )
153 OSL_ENSURE( _rxRootKey.is(), "OModule::writeComponentInfos: invalid argument!" );
155 ::rtl::OUString sRootKey( "/", 1, RTL_TEXTENCODING_ASCII_US );
157 for ( ComponentDescriptions::const_iterator component = m_pImpl->m_aRegisteredComponents.begin();
158 component != m_pImpl->m_aRegisteredComponents.end();
159 ++component
162 ::rtl::OUString sMainKeyName( sRootKey );
163 sMainKeyName += component->sImplementationName;
164 sMainKeyName += ::rtl::OUString::createFromAscii( "/UNO/SERVICES" );
168 Reference< XRegistryKey > xNewKey( _rxRootKey->createKey( sMainKeyName ) );
170 const ::rtl::OUString* pService = component->aSupportedServices.getConstArray();
171 const ::rtl::OUString* pServiceEnd = component->aSupportedServices.getConstArray() + component->aSupportedServices.getLength();
172 for ( ; pService != pServiceEnd; ++pService )
173 xNewKey->createKey( *pService );
175 if ( component->sSingletonName.getLength() )
177 OSL_ENSURE( component->aSupportedServices.getLength() == 1, "OModule::writeComponentInfos: singletons should support exactly one service, shouldn't they?" );
179 ::rtl::OUString sSingletonKeyName( sRootKey );
180 sSingletonKeyName += component->sImplementationName;
181 sSingletonKeyName += ::rtl::OUString::createFromAscii( "/UNO/SINGLETONS/" );
182 sSingletonKeyName += component->sSingletonName;
184 xNewKey = _rxRootKey->createKey( sSingletonKeyName );
185 xNewKey->setStringValue( component->aSupportedServices[ 0 ] );
188 catch( Exception& )
190 OSL_ASSERT( "OModule::writeComponentInfos: something went wrong while creating the keys!" );
191 return sal_False;
195 return sal_True;
198 //--------------------------------------------------------------------------
199 void* OModule::getComponentFactory( const sal_Char* _pImplementationName, void* _pServiceManager, void* /*_pRegistryKey*/ )
201 Reference< XInterface > xFactory( getComponentFactory(
202 ::rtl::OUString::createFromAscii( _pImplementationName ),
203 Reference< XMultiServiceFactory >( static_cast< XMultiServiceFactory* >( _pServiceManager ) )
204 ) );
205 return xFactory.get();
208 //--------------------------------------------------------------------------
209 Reference< XInterface > OModule::getComponentFactory( const ::rtl::OUString& _rImplementationName,
210 const Reference< XMultiServiceFactory >& /* _rxServiceManager */ )
212 Reference< XInterface > xReturn;
214 for ( ComponentDescriptions::const_iterator component = m_pImpl->m_aRegisteredComponents.begin();
215 component != m_pImpl->m_aRegisteredComponents.end();
216 ++component
219 if ( component->sImplementationName == _rImplementationName )
221 xReturn = component->pFactoryCreationFunc(
222 component->pComponentCreationFunc,
223 component->sImplementationName,
224 component->aSupportedServices,
225 NULL
227 if ( xReturn.is() )
229 xReturn->acquire();
230 return xReturn.get();
235 return NULL;
238 //........................................................................
239 } // namespace comphelper
240 //........................................................................