1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/componentmodule.hxx>
31 
32 /** === begin UNO includes === **/
33 /** === end UNO includes === **/
34 #include <comphelper/sequence.hxx>
35 #include <osl/diagnose.h>
36 
37 #include <vector>
38 
39 //........................................................................
40 namespace comphelper
41 {
42 //........................................................................
43 
44     using namespace ::cppu;
45     /** === being UNO using === **/
46     using ::com::sun::star::uno::Sequence;
47     using ::com::sun::star::uno::RuntimeException;
48     using ::com::sun::star::uno::Reference;
49     using ::com::sun::star::lang::XMultiServiceFactory;
50     using ::com::sun::star::registry::XRegistryKey;
51     using ::com::sun::star::uno::Exception;
52     using ::com::sun::star::uno::XInterface;
53     /** === end UNO using === **/
54 
55     typedef ::std::vector< ComponentDescription >   ComponentDescriptions;
56 
57 	//=========================================================================
58 	//= OModuleImpl
59 	//=========================================================================
60 	/** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
61 	*/
62 	class OModuleImpl
63 	{
64     public:
65         ComponentDescriptions                           m_aRegisteredComponents;
66 
67 		OModuleImpl();
68 		~OModuleImpl();
69 	};
70 
71 	//-------------------------------------------------------------------------
72 	OModuleImpl::OModuleImpl()
73 	{
74 	}
75 
76 	//-------------------------------------------------------------------------
77 	OModuleImpl::~OModuleImpl()
78 	{
79 	}
80 
81 	//=========================================================================
82 	//= OModule
83 	//=========================================================================
84 	//-------------------------------------------------------------------------
85     OModule::OModule()
86         :m_nClients( 0 )
87 	    ,m_pImpl( new OModuleImpl )
88     {
89     }
90 
91     OModule::~OModule() {}
92 
93 	//-------------------------------------------------------------------------
94     void OModule::registerClient( OModule::ClientAccess )
95 	{
96 		::osl::MutexGuard aGuard(m_aMutex);
97         if ( 1 == osl_incrementInterlockedCount( &m_nClients ) )
98             onFirstClient();
99 	}
100 
101 	//-------------------------------------------------------------------------
102 	void OModule::revokeClient( OModule::ClientAccess )
103 	{
104 		::osl::MutexGuard aGuard(m_aMutex);
105         if ( 0 == osl_decrementInterlockedCount( &m_nClients ) )
106             onLastClient();
107 	}
108 
109 	//--------------------------------------------------------------------------
110     void OModule::onFirstClient()
111     {
112     }
113 
114 	//--------------------------------------------------------------------------
115     void OModule::onLastClient()
116     {
117     }
118 
119     //--------------------------------------------------------------------------
120     void OModule::registerImplementation( const ComponentDescription& _rComp )
121     {
122         ::osl::MutexGuard aGuard( m_aMutex );
123         if ( !m_pImpl )
124             throw RuntimeException();
125 
126         m_pImpl->m_aRegisteredComponents.push_back( _rComp );
127     }
128 
129 	//--------------------------------------------------------------------------
130     void OModule::registerImplementation( const ::rtl::OUString& _rImplementationName, const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
131         ::cppu::ComponentFactoryFunc _pCreateFunction, FactoryInstantiation _pFactoryFunction )
132 	{
133         ComponentDescription aComponent( _rImplementationName, _rServiceNames, ::rtl::OUString(), _pCreateFunction, _pFactoryFunction );
134         registerImplementation( aComponent );
135 	}
136 
137 	//--------------------------------------------------------------------------
138 	void* OModule::getComponentFactory( const sal_Char* _pImplementationName, void* _pServiceManager, void* /*_pRegistryKey*/ )
139 	{
140         Reference< XInterface > xFactory( getComponentFactory(
141             ::rtl::OUString::createFromAscii( _pImplementationName ),
142             Reference< XMultiServiceFactory >( static_cast< XMultiServiceFactory* >( _pServiceManager ) )
143         ) );
144         return xFactory.get();
145     }
146 
147 	//--------------------------------------------------------------------------
148 	Reference< XInterface > OModule::getComponentFactory( const ::rtl::OUString& _rImplementationName,
149         const Reference< XMultiServiceFactory >& /* _rxServiceManager */ )
150 	{
151 		Reference< XInterface > xReturn;
152 
153         for (   ComponentDescriptions::const_iterator component = m_pImpl->m_aRegisteredComponents.begin();
154                 component != m_pImpl->m_aRegisteredComponents.end();
155                 ++component
156             )
157 		{
158 			if ( component->sImplementationName == _rImplementationName )
159 			{
160 				xReturn = component->pFactoryCreationFunc(
161                     component->pComponentCreationFunc,
162                     component->sImplementationName,
163                     component->aSupportedServices,
164                     NULL
165                 );
166 				if ( xReturn.is() )
167 				{
168 					xReturn->acquire();
169 					return xReturn.get();
170 				}
171 			}
172 		}
173 
174 		return NULL;
175 	}
176 
177 //........................................................................
178 } // namespace comphelper
179 //........................................................................
180