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