2006-03-14 10:40:14 +00:00
/*************************************************************************
*
* OpenOffice . org - a multi - platform office productivity suite
*
* $ RCSfile : componentmodule . cxx , v $
*
2007-06-27 13:54:36 +00:00
* $ Revision : 1.6 $
2006-03-14 10:40:14 +00:00
*
2007-06-27 13:54:36 +00:00
* last change : $ Author : hr $ $ Date : 2007 - 06 - 27 14 : 54 : 36 $
2006-03-14 10:40:14 +00:00
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1 .
*
*
* GNU Lesser General Public License Version 2.1
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
* Copyright 2005 by Sun Microsystems , Inc .
* 901 San Antonio Road , Palo Alto , CA 94303 , USA
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 , as published by the Free Software Foundation .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place , Suite 330 , Boston ,
* MA 02111 - 1307 USA
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-09-17 16:10:24 +00:00
// MARKER(update_precomp.py): autogen include statement, do not remove
# include "precompiled_comphelper.hxx"
2006-03-14 10:40:14 +00:00
# ifndef COMPHELPER_INC_COMPHELPER_COMPONENTMODULE_HXX
# include <comphelper/componentmodule.hxx>
# endif
/** === begin UNO includes === **/
/** === end UNO includes === **/
# ifndef _COMPHELPER_SEQUENCE_HXX_
# include <comphelper/sequence.hxx>
# endif
# ifndef _OSL_DIAGNOSE_H_
# include <osl/diagnose.h>
# endif
# include <vector>
//........................................................................
namespace comphelper
{
//........................................................................
using namespace : : cppu ;
/** === being UNO using === **/
using : : com : : sun : : star : : uno : : Sequence ;
using : : com : : sun : : star : : uno : : RuntimeException ;
using : : com : : sun : : star : : uno : : Reference ;
using : : com : : sun : : star : : lang : : XMultiServiceFactory ;
using : : com : : sun : : star : : registry : : XRegistryKey ;
using : : com : : sun : : star : : uno : : Exception ;
using : : com : : sun : : star : : uno : : XInterface ;
/** === end UNO using === **/
typedef : : std : : vector < ComponentDescription > ComponentDescriptions ;
//=========================================================================
//= OModuleImpl
//=========================================================================
/** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
*/
class OModuleImpl
{
public :
ComponentDescriptions m_aRegisteredComponents ;
OModuleImpl ( ) ;
~ OModuleImpl ( ) ;
} ;
//-------------------------------------------------------------------------
OModuleImpl : : OModuleImpl ( )
{
}
//-------------------------------------------------------------------------
OModuleImpl : : ~ OModuleImpl ( )
{
}
//=========================================================================
//= OModule
//=========================================================================
//-------------------------------------------------------------------------
OModule : : OModule ( )
: m_nClients ( 0 )
, m_pImpl ( new OModuleImpl )
{
}
2006-06-19 21:47:45 +00:00
OModule : : ~ OModule ( ) { }
2006-03-14 10:40:14 +00:00
//-------------------------------------------------------------------------
void OModule : : registerClient ( OModule : : ClientAccess )
{
: : osl : : MutexGuard aGuard ( m_aMutex ) ;
if ( 1 = = osl_incrementInterlockedCount ( & m_nClients ) )
onFirstClient ( ) ;
}
//-------------------------------------------------------------------------
void OModule : : revokeClient ( OModule : : ClientAccess )
{
: : osl : : MutexGuard aGuard ( m_aMutex ) ;
if ( 0 = = osl_decrementInterlockedCount ( & m_nClients ) )
onLastClient ( ) ;
}
//--------------------------------------------------------------------------
void OModule : : onFirstClient ( )
{
}
//--------------------------------------------------------------------------
void OModule : : onLastClient ( )
{
}
//--------------------------------------------------------------------------
void OModule : : registerImplementation ( const ComponentDescription & _rComp )
{
: : osl : : MutexGuard aGuard ( m_aMutex ) ;
if ( ! m_pImpl )
throw RuntimeException ( ) ;
m_pImpl - > m_aRegisteredComponents . push_back ( _rComp ) ;
}
//--------------------------------------------------------------------------
void OModule : : registerImplementation ( const : : rtl : : OUString & _rImplementationName , const : : com : : sun : : star : : uno : : Sequence < : : rtl : : OUString > & _rServiceNames ,
: : cppu : : ComponentFactoryFunc _pCreateFunction , FactoryInstantiation _pFactoryFunction )
{
2007-06-27 13:54:36 +00:00
ComponentDescription aComponent ( _rImplementationName , _rServiceNames , : : rtl : : OUString ( ) , _pCreateFunction , _pFactoryFunction ) ;
2006-03-14 10:40:14 +00:00
registerImplementation ( aComponent ) ;
}
2006-11-08 11:01:30 +00:00
//--------------------------------------------------------------------------
sal_Bool OModule : : writeComponentInfos ( void * pServiceManager , void * pRegistryKey )
{
Reference < XMultiServiceFactory > xFactory ( static_cast < XMultiServiceFactory * > ( pServiceManager ) ) ;
Reference < XRegistryKey > xRegistryKey ( static_cast < XRegistryKey * > ( pRegistryKey ) ) ;
return writeComponentInfos ( xFactory , xRegistryKey ) ;
}
2006-03-14 10:40:14 +00:00
//--------------------------------------------------------------------------
sal_Bool OModule : : writeComponentInfos (
const Reference < XMultiServiceFactory > & /*_rxServiceManager*/ ,
const Reference < XRegistryKey > & _rxRootKey )
{
2007-06-27 13:54:36 +00:00
OSL_ENSURE ( _rxRootKey . is ( ) , " OModule::writeComponentInfos: invalid argument! " ) ;
2006-03-14 10:40:14 +00:00
: : rtl : : OUString sRootKey ( " / " , 1 , RTL_TEXTENCODING_ASCII_US ) ;
for ( ComponentDescriptions : : const_iterator component = m_pImpl - > m_aRegisteredComponents . begin ( ) ;
component ! = m_pImpl - > m_aRegisteredComponents . end ( ) ;
+ + component
)
{
: : rtl : : OUString sMainKeyName ( sRootKey ) ;
sMainKeyName + = component - > sImplementationName ;
sMainKeyName + = : : rtl : : OUString : : createFromAscii ( " /UNO/SERVICES " ) ;
try
{
Reference < XRegistryKey > xNewKey ( _rxRootKey - > createKey ( sMainKeyName ) ) ;
const : : rtl : : OUString * pService = component - > aSupportedServices . getConstArray ( ) ;
const : : rtl : : OUString * pServiceEnd = component - > aSupportedServices . getConstArray ( ) + component - > aSupportedServices . getLength ( ) ;
for ( ; pService ! = pServiceEnd ; + + pService )
xNewKey - > createKey ( * pService ) ;
2007-06-27 13:54:36 +00:00
if ( component - > sSingletonName . getLength ( ) )
{
OSL_ENSURE ( component - > aSupportedServices . getLength ( ) = = 1 , " OModule::writeComponentInfos: singletons should support exactly one service, shouldn't they? " ) ;
: : rtl : : OUString sSingletonKeyName ( sRootKey ) ;
sSingletonKeyName + = component - > sImplementationName ;
sSingletonKeyName + = : : rtl : : OUString : : createFromAscii ( " /UNO/SINGLETONS/ " ) ;
sSingletonKeyName + = component - > sSingletonName ;
xNewKey = _rxRootKey - > createKey ( sSingletonKeyName ) ;
xNewKey - > setStringValue ( component - > aSupportedServices [ 0 ] ) ;
}
2006-03-14 10:40:14 +00:00
}
catch ( Exception & )
{
OSL_ASSERT ( " OModule::writeComponentInfos: something went wrong while creating the keys! " ) ;
return sal_False ;
}
}
return sal_True ;
}
2006-11-08 11:01:30 +00:00
//--------------------------------------------------------------------------
void * OModule : : getComponentFactory ( const sal_Char * _pImplementationName , void * _pServiceManager , void * /*_pRegistryKey*/ )
{
Reference < XInterface > xFactory ( getComponentFactory (
: : rtl : : OUString : : createFromAscii ( _pImplementationName ) ,
Reference < XMultiServiceFactory > ( static_cast < XMultiServiceFactory * > ( _pServiceManager ) )
) ) ;
return xFactory . get ( ) ;
}
2006-03-14 10:40:14 +00:00
//--------------------------------------------------------------------------
Reference < XInterface > OModule : : getComponentFactory ( const : : rtl : : OUString & _rImplementationName ,
const Reference < XMultiServiceFactory > & /* _rxServiceManager */ )
{
Reference < XInterface > xReturn ;
for ( ComponentDescriptions : : const_iterator component = m_pImpl - > m_aRegisteredComponents . begin ( ) ;
component ! = m_pImpl - > m_aRegisteredComponents . end ( ) ;
+ + component
)
{
if ( component - > sImplementationName = = _rImplementationName )
{
xReturn = component - > pFactoryCreationFunc (
component - > pComponentCreationFunc ,
component - > sImplementationName ,
component - > aSupportedServices ,
NULL
) ;
if ( xReturn . is ( ) )
{
xReturn - > acquire ( ) ;
return xReturn . get ( ) ;
}
}
}
return NULL ;
}
//........................................................................
} // namespace comphelper
//........................................................................