xref: /AOO41X/main/mysqlc/source/mysqlc_services.cxx (revision 079eb5772d0a9e49bbf5c2cd738fc5b5d43e5181)
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 #include "mysqlc_driver.hxx"
23 
24 #include <cppuhelper/factory.hxx>
25 #include <osl/diagnose.h>
26 #include <rtl/ustrbuf.hxx>
27 
28 using namespace connectivity::mysqlc;
29 using ::rtl::OUString;
30 using ::com::sun::star::uno::Reference;
31 using ::com::sun::star::uno::Sequence;
32 using ::com::sun::star::registry::XRegistryKey;
33 using ::com::sun::star::lang::XSingleServiceFactory;
34 using ::com::sun::star::lang::XMultiServiceFactory;
35 
36 typedef Reference< XSingleServiceFactory > (SAL_CALL *createFactoryFunc)
37         (
38             const Reference< XMultiServiceFactory > & rServiceManager,
39             const OUString & rComponentName,
40             ::cppu::ComponentInstantiation pCreateFunction,
41             const Sequence< OUString > & rServiceNames,
42             rtl_ModuleCount* _pTemp
43         );
44 
45 //***************************************************************************************
46 //
47 // Die vorgeschriebene C-API muss erfuellt werden!
48 // Sie besteht aus drei Funktionen, die von dem Modul exportiert werden muessen.
49 //
50 
51 //---------------------------------------------------------------------------------------
REGISTER_PROVIDER(const OUString & aServiceImplName,const Sequence<OUString> & Services,const Reference<XRegistryKey> & xKey)52 void REGISTER_PROVIDER(
53         const OUString& aServiceImplName,
54         const Sequence< OUString>& Services,
55         const Reference< XRegistryKey > & xKey)
56 {
57     ::rtl::OUStringBuffer aMainKeyName;
58     aMainKeyName.append( sal_Unicode( '/' ) );
59     aMainKeyName.append( aServiceImplName );
60     aMainKeyName.appendAscii( "/UNO/SERVICES" );
61 
62     Reference< XRegistryKey > xNewKey( xKey->createKey( aMainKeyName.makeStringAndClear() ) );
63     OSL_ENSURE(xNewKey.is(), "SKELETON::component_writeInfo : could not create a registry key !");
64 
65     for (sal_Int32 i = 0; i < Services.getLength(); ++i) {
66         xNewKey->createKey(Services[i]);
67     }
68 }
69 
70 
71 //---------------------------------------------------------------------------------------
72 struct ProviderRequest
73 {
74     Reference< XSingleServiceFactory > xRet;
75     Reference< XMultiServiceFactory > const xServiceManager;
76     OUString const sImplementationName;
77 
ProviderRequestProviderRequest78     ProviderRequest(
79         void* pServiceManager,
80         sal_Char const* pImplementationName
81     ) : xServiceManager(reinterpret_cast<XMultiServiceFactory*>(pServiceManager))
82       , sImplementationName(OUString::createFromAscii(pImplementationName))
83     {
84     }
85 
86     /* {{{ CREATE_PROVIDER -I- */
CREATE_PROVIDERProviderRequest87     inline sal_Bool CREATE_PROVIDER(
88                 const OUString& Implname,
89                 const Sequence< OUString > & Services,
90                 ::cppu::ComponentInstantiation Factory,
91                 createFactoryFunc creator
92             )
93     {
94         if (!xRet.is() && (Implname == sImplementationName)) {
95             try {
96                 xRet = creator( xServiceManager, sImplementationName,Factory, Services,0);
97             } catch (...) {
98             }
99         }
100         return xRet.is();
101     }
102 
getProviderProviderRequest103     void* getProvider() const { return xRet.get(); }
104 };
105 /* }}} */
106 
107 
108 /* {{{ component_getImplementationEnvironment -I- */
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)109 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
110                 const sal_Char  **ppEnvTypeName,
111                 uno_Environment ** /* ppEnv */
112             )
113 {
114     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
115 }
116 /* }}} */
117 
118 
119 /* {{{ component_writeInfo -I- */
component_writeInfo(void *,void * pRegistryKey)120 extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(void * /* pServiceManager */, void * pRegistryKey)
121 {
122     if (pRegistryKey) {
123         try {
124             Reference< XRegistryKey > xKey(reinterpret_cast< XRegistryKey*>(pRegistryKey));
125 
126             REGISTER_PROVIDER(
127                 MysqlCDriver::getImplementationName_Static(),
128                 MysqlCDriver::getSupportedServiceNames_Static(), xKey);
129 
130             return sal_True;
131         } catch (::com::sun::star::registry::InvalidRegistryException& ) {
132             OSL_ENSURE(sal_False, "SKELETON::component_writeInfo : could not create a registry key ! ## InvalidRegistryException !");
133         }
134     }
135     return sal_False;
136 }
137 /* }}} */
138 
139 
140 /* {{{ component_getFactory -I- */
component_getFactory(const sal_Char * pImplementationName,void * pServiceManager,void *)141 extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL component_getFactory(
142                     const sal_Char * pImplementationName,
143                     void * pServiceManager,
144                     void * /* pRegistryKey */)
145 {
146     void* pRet = 0;
147     if (pServiceManager) {
148         ProviderRequest aReq(pServiceManager,pImplementationName);
149 
150         aReq.CREATE_PROVIDER(
151             MysqlCDriver::getImplementationName_Static(),
152             MysqlCDriver::getSupportedServiceNames_Static(),
153             MysqlCDriver_CreateInstance, ::cppu::createSingleFactory)
154         ;
155 
156         if(aReq.xRet.is()) {
157             aReq.xRet->acquire();
158         }
159 
160         pRet = aReq.getProvider();
161     }
162 
163     return pRet;
164 };
165 /* }}} */
166 
167 
168 /*
169  * Local variables:
170  * tab-width: 4
171  * c-basic-offset: 4
172  * End:
173  * vim600: noet sw=4 ts=4 fdm=marker
174  * vim<600: noet sw=4 ts=4
175  */
176