merge the formfield patch from ooo-build
[ooovba.git] / stoc / source / namingservice / namingservice.cxx
blob440a36b96323969a0c913338c47cd89263152834
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: namingservice.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_stoc.hxx"
34 #include <hash_map>
35 #include <osl/mutex.hxx>
36 #include <osl/diagnose.h>
37 #include <uno/dispatcher.h>
38 #include <uno/mapping.hxx>
39 #include <cppuhelper/queryinterface.hxx>
40 #include <cppuhelper/weak.hxx>
41 #include <cppuhelper/factory.hxx>
42 #include <cppuhelper/component.hxx>
43 #include <cppuhelper/implbase2.hxx>
44 #ifndef _CPPUHELPER_IMPLEMENTATIONENTRY_HXX_
45 #include <cppuhelper/implementationentry.hxx>
46 #endif
48 #include <com/sun/star/uno/XNamingService.hpp>
49 #include <com/sun/star/lang/XServiceInfo.hpp>
51 using namespace cppu;
52 using namespace rtl;
53 using namespace osl;
54 using namespace std;
56 using namespace com::sun::star::uno;
57 using namespace com::sun::star::lang;
58 using namespace com::sun::star::registry;
60 #define SERVICENAME "com.sun.star.uno.NamingService"
61 #define IMPLNAME "com.sun.star.comp.stoc.NamingService"
63 namespace stoc_namingservice
65 static rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
67 static Sequence< OUString > ns_getSupportedServiceNames()
69 static Sequence < OUString > *pNames = 0;
70 if( ! pNames )
72 MutexGuard guard( Mutex::getGlobalMutex() );
73 if( !pNames )
75 static Sequence< OUString > seqNames(1);
76 seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICENAME));
77 pNames = &seqNames;
80 return *pNames;
83 static OUString ns_getImplementationName()
85 static OUString *pImplName = 0;
86 if( ! pImplName )
88 MutexGuard guard( Mutex::getGlobalMutex() );
89 if( ! pImplName )
91 static OUString implName( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) );
92 pImplName = &implName;
95 return *pImplName;
98 struct equalOWString_Impl
100 sal_Bool operator()(const OUString & s1, const OUString & s2) const
101 { return s1 == s2; }
104 struct hashOWString_Impl
106 size_t operator()(const OUString & rName) const
107 { return rName.hashCode(); }
110 typedef hash_map
112 OUString,
113 Reference<XInterface >,
114 hashOWString_Impl,
115 equalOWString_Impl
116 > HashMap_OWString_Interface;
118 //==================================================================================================
119 class NamingService_Impl
120 : public WeakImplHelper2 < XServiceInfo, XNamingService >
122 Mutex aMutex;
123 HashMap_OWString_Interface aMap;
124 public:
125 NamingService_Impl();
126 ~NamingService_Impl();
128 // XServiceInfo
129 virtual OUString SAL_CALL getImplementationName()
130 throw(::com::sun::star::uno::RuntimeException);
131 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName )
132 throw(::com::sun::star::uno::RuntimeException);
133 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
134 throw(::com::sun::star::uno::RuntimeException);
135 static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static()
137 OUString aStr( OUString::createFromAscii( SERVICENAME ) );
138 return Sequence< OUString >( &aStr, 1 );
141 virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL getRegisteredObject( const ::rtl::OUString& Name ) throw(Exception, RuntimeException);
142 virtual void SAL_CALL registerObject( const ::rtl::OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& Object ) throw(Exception, RuntimeException);
143 virtual void SAL_CALL revokeObject( const ::rtl::OUString& Name ) throw(Exception, RuntimeException);
146 //==================================================================================================
147 static Reference<XInterface> SAL_CALL NamingService_Impl_create( const Reference<XComponentContext> & )
149 return *new NamingService_Impl();
152 //==================================================================================================
153 NamingService_Impl::NamingService_Impl()
155 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
158 //==================================================================================================
159 NamingService_Impl::~NamingService_Impl()
161 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
164 // XServiceInfo
165 OUString NamingService_Impl::getImplementationName()
166 throw(::com::sun::star::uno::RuntimeException)
168 return ns_getImplementationName();
171 // XServiceInfo
172 sal_Bool NamingService_Impl::supportsService( const OUString & rServiceName )
173 throw(::com::sun::star::uno::RuntimeException)
175 const Sequence< OUString > & rSNL = getSupportedServiceNames();
176 const OUString * pArray = rSNL.getConstArray();
177 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
179 if (pArray[nPos] == rServiceName)
180 return sal_True;
182 return sal_False;
185 // XServiceInfo
186 Sequence< OUString > NamingService_Impl::getSupportedServiceNames()
187 throw(::com::sun::star::uno::RuntimeException)
189 return ns_getSupportedServiceNames();
192 // XServiceInfo
193 Reference< XInterface > NamingService_Impl::getRegisteredObject( const OUString& Name ) throw(Exception, RuntimeException)
195 Guard< Mutex > aGuard( aMutex );
196 Reference< XInterface > xRet;
197 HashMap_OWString_Interface::iterator aIt = aMap.find( Name );
198 if( aIt != aMap.end() )
199 xRet = (*aIt).second;
200 return xRet;
203 // XServiceInfo
204 void NamingService_Impl::registerObject( const OUString& Name, const Reference< XInterface >& Object ) throw(Exception, RuntimeException)
206 Guard< Mutex > aGuard( aMutex );
207 aMap[ Name ] = Object;
210 // XServiceInfo
211 void NamingService_Impl::revokeObject( const OUString& Name ) throw(Exception, RuntimeException)
213 Guard< Mutex > aGuard( aMutex );
214 aMap.erase( Name );
219 using namespace stoc_namingservice;
220 static struct ImplementationEntry g_entries[] =
223 NamingService_Impl_create, ns_getImplementationName,
224 ns_getSupportedServiceNames, createSingleComponentFactory,
225 &g_moduleCount.modCnt , 0
227 { 0, 0, 0, 0, 0, 0 }
230 extern "C"
232 sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
234 return g_moduleCount.canUnload( &g_moduleCount , pTime );
237 //==================================================================================================
238 void SAL_CALL component_getImplementationEnvironment(
239 const sal_Char ** ppEnvTypeName, uno_Environment ** )
241 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
243 //==================================================================================================
244 sal_Bool SAL_CALL component_writeInfo(
245 void * pServiceManager, void * pRegistryKey )
247 return component_writeInfoHelper( pServiceManager, pRegistryKey, g_entries );
249 //==================================================================================================
250 void * SAL_CALL component_getFactory(
251 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
253 return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );