xref: /trunk/main/stoc/source/namingservice/namingservice.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName );
124     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames();
getSupportedServiceNames_Static()125     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static()
126     {
127         OUString aStr( OUString::createFromAscii( SERVICENAME ) );
128         return Sequence< OUString >( &aStr, 1 );
129     }
130 
131     virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL getRegisteredObject( const ::rtl::OUString& Name );
132     virtual void SAL_CALL registerObject( const ::rtl::OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& Object );
133     virtual void SAL_CALL revokeObject( const ::rtl::OUString& Name );
134 };
135 
136 //==================================================================================================
NamingService_Impl_create(const Reference<XComponentContext> &)137 static Reference<XInterface> SAL_CALL NamingService_Impl_create( const Reference<XComponentContext> & )
138 {
139     return *new NamingService_Impl();
140 }
141 
142 //==================================================================================================
NamingService_Impl()143 NamingService_Impl::NamingService_Impl()
144 {
145     g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
146 }
147 
148 //==================================================================================================
~NamingService_Impl()149 NamingService_Impl::~NamingService_Impl()
150 {
151     g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
152 }
153 
154 // XServiceInfo
getImplementationName()155 OUString NamingService_Impl::getImplementationName()
156 {
157     return ns_getImplementationName();
158 }
159 
160 // XServiceInfo
supportsService(const OUString & rServiceName)161 sal_Bool NamingService_Impl::supportsService( const OUString & rServiceName )
162 {
163     const Sequence< OUString > & rSNL = getSupportedServiceNames();
164     const OUString * pArray = rSNL.getConstArray();
165     for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
166     {
167         if (pArray[nPos] == rServiceName)
168             return sal_True;
169     }
170     return sal_False;
171 }
172 
173 // XServiceInfo
getSupportedServiceNames()174 Sequence< OUString > NamingService_Impl::getSupportedServiceNames()
175 {
176     return ns_getSupportedServiceNames();
177 }
178 
179 // XServiceInfo
getRegisteredObject(const OUString & Name)180 Reference< XInterface > NamingService_Impl::getRegisteredObject( const OUString& Name )
181 {
182     Guard< Mutex > aGuard( aMutex );
183     Reference< XInterface > xRet;
184     HashMap_OWString_Interface::iterator aIt = aMap.find( Name );
185     if( aIt != aMap.end() )
186         xRet = (*aIt).second;
187     return xRet;
188 }
189 
190 // XServiceInfo
registerObject(const OUString & Name,const Reference<XInterface> & Object)191 void NamingService_Impl::registerObject( const OUString& Name, const Reference< XInterface >& Object )
192 {
193     Guard< Mutex > aGuard( aMutex );
194     aMap[ Name ] = Object;
195 }
196 
197 // XServiceInfo
revokeObject(const OUString & Name)198 void NamingService_Impl::revokeObject( const OUString& Name )
199 {
200     Guard< Mutex > aGuard( aMutex );
201     aMap.erase( Name );
202 }
203 
204 }
205 
206 using namespace stoc_namingservice;
207 static struct ImplementationEntry g_entries[] =
208 {
209     {
210         NamingService_Impl_create, ns_getImplementationName,
211         ns_getSupportedServiceNames, createSingleComponentFactory,
212         &g_moduleCount.modCnt , 0
213     },
214     { 0, 0, 0, 0, 0, 0 }
215 };
216 
217 extern "C"
218 {
component_canUnload(TimeValue * pTime)219 sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
220 {
221     return g_moduleCount.canUnload( &g_moduleCount , pTime );
222 }
223 
224 //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)225 void SAL_CALL component_getImplementationEnvironment(
226     const sal_Char ** ppEnvTypeName, uno_Environment ** )
227 {
228     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
229 }
230 //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)231 void * SAL_CALL component_getFactory(
232     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
233 {
234     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
235 }
236 }
237