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_stoc.hxx"
30 
31 #include <hash_map>
32 #include <osl/mutex.hxx>
33 #include <osl/diagnose.h>
34 #include <uno/dispatcher.h>
35 #include <uno/mapping.hxx>
36 #include <cppuhelper/queryinterface.hxx>
37 #include <cppuhelper/weak.hxx>
38 #include <cppuhelper/factory.hxx>
39 #include <cppuhelper/component.hxx>
40 #include <cppuhelper/implbase2.hxx>
41 #ifndef _CPPUHELPER_IMPLEMENTATIONENTRY_HXX_
42 #include <cppuhelper/implementationentry.hxx>
43 #endif
44 
45 #include <com/sun/star/uno/XNamingService.hpp>
46 #include <com/sun/star/lang/XServiceInfo.hpp>
47 
48 using namespace cppu;
49 using namespace rtl;
50 using namespace osl;
51 using namespace std;
52 
53 using namespace com::sun::star::uno;
54 using namespace com::sun::star::lang;
55 using namespace com::sun::star::registry;
56 
57 #define SERVICENAME "com.sun.star.uno.NamingService"
58 #define IMPLNAME	"com.sun.star.comp.stoc.NamingService"
59 
60 namespace stoc_namingservice
61 {
62 static rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
63 
64 static Sequence< OUString > ns_getSupportedServiceNames()
65 {
66 	static Sequence < OUString > *pNames = 0;
67 	if( ! pNames )
68 	{
69 		MutexGuard guard( Mutex::getGlobalMutex() );
70 		if( !pNames )
71 		{
72 			static Sequence< OUString > seqNames(1);
73 			seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICENAME));
74 			pNames = &seqNames;
75 		}
76 	}
77 	return *pNames;
78 }
79 
80 static OUString ns_getImplementationName()
81 {
82 	static OUString *pImplName = 0;
83 	if( ! pImplName )
84 	{
85 		MutexGuard guard( Mutex::getGlobalMutex() );
86 		if( ! pImplName )
87 		{
88 			static OUString implName( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) );
89 			pImplName = &implName;
90 		}
91 	}
92 	return *pImplName;
93 }
94 
95 struct equalOWString_Impl
96 {
97   sal_Bool operator()(const OUString & s1, const OUString & s2) const
98 		{ return s1 == s2; }
99 };
100 
101 struct hashOWString_Impl
102 {
103 	size_t operator()(const OUString & rName) const
104 		{ return rName.hashCode(); }
105 };
106 
107 typedef hash_map
108 <
109 	OUString,
110 	Reference<XInterface >,
111 	hashOWString_Impl,
112 	equalOWString_Impl
113 > HashMap_OWString_Interface;
114 
115 //==================================================================================================
116 class NamingService_Impl
117 	: public WeakImplHelper2 < XServiceInfo, XNamingService >
118 {
119 	Mutex								aMutex;
120 	HashMap_OWString_Interface			aMap;
121 public:
122 	NamingService_Impl();
123 	~NamingService_Impl();
124 
125     // XServiceInfo
126     virtual OUString SAL_CALL getImplementationName()
127 	throw(::com::sun::star::uno::RuntimeException);
128     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName )
129 	throw(::com::sun::star::uno::RuntimeException);
130     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
131 	throw(::com::sun::star::uno::RuntimeException);
132     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static()
133 	{
134 		OUString aStr( OUString::createFromAscii( SERVICENAME ) );
135 		return Sequence< OUString >( &aStr, 1 );
136 	}
137 
138     virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL getRegisteredObject( const ::rtl::OUString& Name ) throw(Exception, RuntimeException);
139     virtual void SAL_CALL registerObject( const ::rtl::OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& Object ) throw(Exception, RuntimeException);
140     virtual void SAL_CALL revokeObject( const ::rtl::OUString& Name ) throw(Exception, RuntimeException);
141 };
142 
143 //==================================================================================================
144 static Reference<XInterface> SAL_CALL NamingService_Impl_create( const Reference<XComponentContext> & )
145 {
146 	return *new NamingService_Impl();
147 }
148 
149 //==================================================================================================
150 NamingService_Impl::NamingService_Impl()
151 {
152 	g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
153 }
154 
155 //==================================================================================================
156 NamingService_Impl::~NamingService_Impl()
157 {
158 	g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
159 }
160 
161 // XServiceInfo
162 OUString NamingService_Impl::getImplementationName()
163 	throw(::com::sun::star::uno::RuntimeException)
164 {
165 	return ns_getImplementationName();
166 }
167 
168 // XServiceInfo
169 sal_Bool NamingService_Impl::supportsService( const OUString & rServiceName )
170 	throw(::com::sun::star::uno::RuntimeException)
171 {
172 	const Sequence< OUString > & rSNL = getSupportedServiceNames();
173 	const OUString * pArray = rSNL.getConstArray();
174 	for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
175 	{
176 		if (pArray[nPos] == rServiceName)
177 			return sal_True;
178 	}
179 	return sal_False;
180 }
181 
182 // XServiceInfo
183 Sequence< OUString > NamingService_Impl::getSupportedServiceNames()
184 	throw(::com::sun::star::uno::RuntimeException)
185 {
186 	return ns_getSupportedServiceNames();
187 }
188 
189 // XServiceInfo
190 Reference< XInterface > NamingService_Impl::getRegisteredObject( const OUString& Name ) throw(Exception, RuntimeException)
191 {
192 	Guard< Mutex > aGuard( aMutex );
193 	Reference< XInterface > xRet;
194 	HashMap_OWString_Interface::iterator aIt = aMap.find( Name );
195 	if( aIt != aMap.end() )
196 		xRet = (*aIt).second;
197 	return xRet;
198 }
199 
200 // XServiceInfo
201 void NamingService_Impl::registerObject( const OUString& Name, const Reference< XInterface >& Object ) throw(Exception, RuntimeException)
202 {
203 	Guard< Mutex > aGuard( aMutex );
204 	aMap[ Name ] = Object;
205 }
206 
207 // XServiceInfo
208 void NamingService_Impl::revokeObject( const OUString& Name ) throw(Exception, RuntimeException)
209 {
210 	Guard< Mutex > aGuard( aMutex );
211 	aMap.erase( Name );
212 }
213 
214 }
215 
216 using namespace stoc_namingservice;
217 static struct ImplementationEntry g_entries[] =
218 {
219 	{
220 		NamingService_Impl_create, ns_getImplementationName,
221 		ns_getSupportedServiceNames, createSingleComponentFactory,
222 		&g_moduleCount.modCnt , 0
223 	},
224 	{ 0, 0, 0, 0, 0, 0 }
225 };
226 
227 extern "C"
228 {
229 sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
230 {
231 	return g_moduleCount.canUnload( &g_moduleCount , pTime );
232 }
233 
234 //==================================================================================================
235 void SAL_CALL component_getImplementationEnvironment(
236 	const sal_Char ** ppEnvTypeName, uno_Environment ** )
237 {
238 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
239 }
240 //==================================================================================================
241 void * SAL_CALL component_getFactory(
242 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
243 {
244 	return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
245 }
246 }
247