xref: /AOO41X/main/stoc/source/namingservice/namingservice.cxx (revision 647a425c57429e1e89af1c7acf2de6af6f1bb730)
1*647a425cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647a425cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647a425cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647a425cSAndrew Rist  * distributed with this work for additional information
6*647a425cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647a425cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647a425cSAndrew Rist  * "License"); you may not use this file except in compliance
9*647a425cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*647a425cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*647a425cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647a425cSAndrew Rist  * software distributed under the License is distributed on an
15*647a425cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647a425cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647a425cSAndrew Rist  * specific language governing permissions and limitations
18*647a425cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*647a425cSAndrew Rist  *************************************************************/
21*647a425cSAndrew Rist 
22*647a425cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_stoc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <hash_map>
28cdf0e10cSrcweir #include <osl/mutex.hxx>
29cdf0e10cSrcweir #include <osl/diagnose.h>
30cdf0e10cSrcweir #include <uno/dispatcher.h>
31cdf0e10cSrcweir #include <uno/mapping.hxx>
32cdf0e10cSrcweir #include <cppuhelper/queryinterface.hxx>
33cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
34cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
35cdf0e10cSrcweir #include <cppuhelper/component.hxx>
36cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
37cdf0e10cSrcweir #ifndef _CPPUHELPER_IMPLEMENTATIONENTRY_HXX_
38cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <com/sun/star/uno/XNamingService.hpp>
42cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace cppu;
45cdf0e10cSrcweir using namespace rtl;
46cdf0e10cSrcweir using namespace osl;
47cdf0e10cSrcweir using namespace std;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir using namespace com::sun::star::uno;
50cdf0e10cSrcweir using namespace com::sun::star::lang;
51cdf0e10cSrcweir using namespace com::sun::star::registry;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #define SERVICENAME "com.sun.star.uno.NamingService"
54cdf0e10cSrcweir #define IMPLNAME	"com.sun.star.comp.stoc.NamingService"
55cdf0e10cSrcweir 
56cdf0e10cSrcweir namespace stoc_namingservice
57cdf0e10cSrcweir {
58cdf0e10cSrcweir static rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
59cdf0e10cSrcweir 
ns_getSupportedServiceNames()60cdf0e10cSrcweir static Sequence< OUString > ns_getSupportedServiceNames()
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 	static Sequence < OUString > *pNames = 0;
63cdf0e10cSrcweir 	if( ! pNames )
64cdf0e10cSrcweir 	{
65cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
66cdf0e10cSrcweir 		if( !pNames )
67cdf0e10cSrcweir 		{
68cdf0e10cSrcweir 			static Sequence< OUString > seqNames(1);
69cdf0e10cSrcweir 			seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICENAME));
70cdf0e10cSrcweir 			pNames = &seqNames;
71cdf0e10cSrcweir 		}
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 	return *pNames;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
ns_getImplementationName()76cdf0e10cSrcweir static OUString ns_getImplementationName()
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	static OUString *pImplName = 0;
79cdf0e10cSrcweir 	if( ! pImplName )
80cdf0e10cSrcweir 	{
81cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
82cdf0e10cSrcweir 		if( ! pImplName )
83cdf0e10cSrcweir 		{
84cdf0e10cSrcweir 			static OUString implName( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) );
85cdf0e10cSrcweir 			pImplName = &implName;
86cdf0e10cSrcweir 		}
87cdf0e10cSrcweir 	}
88cdf0e10cSrcweir 	return *pImplName;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir struct equalOWString_Impl
92cdf0e10cSrcweir {
operator ()stoc_namingservice::equalOWString_Impl93cdf0e10cSrcweir   sal_Bool operator()(const OUString & s1, const OUString & s2) const
94cdf0e10cSrcweir 		{ return s1 == s2; }
95cdf0e10cSrcweir };
96cdf0e10cSrcweir 
97cdf0e10cSrcweir struct hashOWString_Impl
98cdf0e10cSrcweir {
operator ()stoc_namingservice::hashOWString_Impl99cdf0e10cSrcweir 	size_t operator()(const OUString & rName) const
100cdf0e10cSrcweir 		{ return rName.hashCode(); }
101cdf0e10cSrcweir };
102cdf0e10cSrcweir 
103cdf0e10cSrcweir typedef hash_map
104cdf0e10cSrcweir <
105cdf0e10cSrcweir 	OUString,
106cdf0e10cSrcweir 	Reference<XInterface >,
107cdf0e10cSrcweir 	hashOWString_Impl,
108cdf0e10cSrcweir 	equalOWString_Impl
109cdf0e10cSrcweir > HashMap_OWString_Interface;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir //==================================================================================================
112cdf0e10cSrcweir class NamingService_Impl
113cdf0e10cSrcweir 	: public WeakImplHelper2 < XServiceInfo, XNamingService >
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	Mutex								aMutex;
116cdf0e10cSrcweir 	HashMap_OWString_Interface			aMap;
117cdf0e10cSrcweir public:
118cdf0e10cSrcweir 	NamingService_Impl();
119cdf0e10cSrcweir 	~NamingService_Impl();
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     // XServiceInfo
122cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName()
123cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException);
124cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName )
125cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException);
126cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
127cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException);
getSupportedServiceNames_Static()128cdf0e10cSrcweir     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static()
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		OUString aStr( OUString::createFromAscii( SERVICENAME ) );
131cdf0e10cSrcweir 		return Sequence< OUString >( &aStr, 1 );
132cdf0e10cSrcweir 	}
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL getRegisteredObject( const ::rtl::OUString& Name ) throw(Exception, RuntimeException);
135cdf0e10cSrcweir     virtual void SAL_CALL registerObject( const ::rtl::OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& Object ) throw(Exception, RuntimeException);
136cdf0e10cSrcweir     virtual void SAL_CALL revokeObject( const ::rtl::OUString& Name ) throw(Exception, RuntimeException);
137cdf0e10cSrcweir };
138cdf0e10cSrcweir 
139cdf0e10cSrcweir //==================================================================================================
NamingService_Impl_create(const Reference<XComponentContext> &)140cdf0e10cSrcweir static Reference<XInterface> SAL_CALL NamingService_Impl_create( const Reference<XComponentContext> & )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir 	return *new NamingService_Impl();
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir //==================================================================================================
NamingService_Impl()146cdf0e10cSrcweir NamingService_Impl::NamingService_Impl()
147cdf0e10cSrcweir {
148cdf0e10cSrcweir 	g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
149cdf0e10cSrcweir }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir //==================================================================================================
~NamingService_Impl()152cdf0e10cSrcweir NamingService_Impl::~NamingService_Impl()
153cdf0e10cSrcweir {
154cdf0e10cSrcweir 	g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir // XServiceInfo
getImplementationName()158cdf0e10cSrcweir OUString NamingService_Impl::getImplementationName()
159cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
160cdf0e10cSrcweir {
161cdf0e10cSrcweir 	return ns_getImplementationName();
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir // XServiceInfo
supportsService(const OUString & rServiceName)165cdf0e10cSrcweir sal_Bool NamingService_Impl::supportsService( const OUString & rServiceName )
166cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 	const Sequence< OUString > & rSNL = getSupportedServiceNames();
169cdf0e10cSrcweir 	const OUString * pArray = rSNL.getConstArray();
170cdf0e10cSrcweir 	for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
171cdf0e10cSrcweir 	{
172cdf0e10cSrcweir 		if (pArray[nPos] == rServiceName)
173cdf0e10cSrcweir 			return sal_True;
174cdf0e10cSrcweir 	}
175cdf0e10cSrcweir 	return sal_False;
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir // XServiceInfo
getSupportedServiceNames()179cdf0e10cSrcweir Sequence< OUString > NamingService_Impl::getSupportedServiceNames()
180cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
181cdf0e10cSrcweir {
182cdf0e10cSrcweir 	return ns_getSupportedServiceNames();
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir // XServiceInfo
getRegisteredObject(const OUString & Name)186cdf0e10cSrcweir Reference< XInterface > NamingService_Impl::getRegisteredObject( const OUString& Name ) throw(Exception, RuntimeException)
187cdf0e10cSrcweir {
188cdf0e10cSrcweir 	Guard< Mutex > aGuard( aMutex );
189cdf0e10cSrcweir 	Reference< XInterface > xRet;
190cdf0e10cSrcweir 	HashMap_OWString_Interface::iterator aIt = aMap.find( Name );
191cdf0e10cSrcweir 	if( aIt != aMap.end() )
192cdf0e10cSrcweir 		xRet = (*aIt).second;
193cdf0e10cSrcweir 	return xRet;
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir // XServiceInfo
registerObject(const OUString & Name,const Reference<XInterface> & Object)197cdf0e10cSrcweir void NamingService_Impl::registerObject( const OUString& Name, const Reference< XInterface >& Object ) throw(Exception, RuntimeException)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir 	Guard< Mutex > aGuard( aMutex );
200cdf0e10cSrcweir 	aMap[ Name ] = Object;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir // XServiceInfo
revokeObject(const OUString & Name)204cdf0e10cSrcweir void NamingService_Impl::revokeObject( const OUString& Name ) throw(Exception, RuntimeException)
205cdf0e10cSrcweir {
206cdf0e10cSrcweir 	Guard< Mutex > aGuard( aMutex );
207cdf0e10cSrcweir 	aMap.erase( Name );
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir using namespace stoc_namingservice;
213cdf0e10cSrcweir static struct ImplementationEntry g_entries[] =
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	{
216cdf0e10cSrcweir 		NamingService_Impl_create, ns_getImplementationName,
217cdf0e10cSrcweir 		ns_getSupportedServiceNames, createSingleComponentFactory,
218cdf0e10cSrcweir 		&g_moduleCount.modCnt , 0
219cdf0e10cSrcweir 	},
220cdf0e10cSrcweir 	{ 0, 0, 0, 0, 0, 0 }
221cdf0e10cSrcweir };
222cdf0e10cSrcweir 
223cdf0e10cSrcweir extern "C"
224cdf0e10cSrcweir {
component_canUnload(TimeValue * pTime)225cdf0e10cSrcweir sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
226cdf0e10cSrcweir {
227cdf0e10cSrcweir 	return g_moduleCount.canUnload( &g_moduleCount , pTime );
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)231cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
232cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** )
233cdf0e10cSrcweir {
234cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
235cdf0e10cSrcweir }
236cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)237cdf0e10cSrcweir void * SAL_CALL component_getFactory(
238cdf0e10cSrcweir 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
239cdf0e10cSrcweir {
240cdf0e10cSrcweir 	return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
241cdf0e10cSrcweir }
242cdf0e10cSrcweir }
243