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