LibreOffice Module connectivity (master) 1
MacabDriver.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20
21#include "MacabDriver.hxx"
22#include "MacabConnection.hxx"
23
24#include <com/sun/star/sdb/SQLContext.hpp>
25#include <com/sun/star/lang/NullPointerException.hpp>
26#include <com/sun/star/frame/Desktop.hpp>
27#include <sal/log.hxx>
29#include <strings.hrc>
31
32using namespace com::sun::star::uno;
33using namespace com::sun::star::lang;
34using namespace com::sun::star::beans;
35using namespace com::sun::star::sdbc;
36using namespace com::sun::star::sdb;
37using namespace com::sun::star::frame;
38using namespace connectivity::macab;
39
40namespace {
41
44void throwGenericSQLException( const OUString& _rMessage )
45{
46 SQLException aError;
47 aError.Message = _rMessage;
48 aError.SQLState = "S1000";
49 aError.ErrorCode = 0;
50 throw aError;
51}
52
55void throwNoMacOSException()
56{
58 const OUString sError( aResources.getResourceString(
59 STR_NO_MAC_OS_FOUND
60 ) );
62}
63
64
65}
66
67// = MacabImplModule
68
69
70MacabImplModule::MacabImplModule()
71 :m_bAttemptedLoadModule(false)
72 ,m_hConnectorModule(nullptr)
73 ,m_pConnectionFactoryFunc(nullptr)
74{
75}
76
77
79{
80 return impl_loadModule();
81}
82
83
84namespace
85{
86 template< typename FUNCTION >
87 void lcl_getFunctionFromModuleOrUnload( oslModule& _rModule, const char* _pAsciiSymbolName, FUNCTION& _rFunction )
88 {
89 _rFunction = nullptr;
90 if ( _rModule )
91 {
92
93 const OUString sSymbolName = OUString::createFromAscii( _pAsciiSymbolName );
94 _rFunction = reinterpret_cast<FUNCTION>( osl_getSymbol( _rModule, sSymbolName.pData ) );
95
96 if ( !_rFunction )
97 { // did not find the symbol
98 SAL_WARN( "connectivity.macab", "lcl_getFunctionFromModuleOrUnload: could not find the symbol " << _pAsciiSymbolName );
99 osl_unloadModule( _rModule );
100 _rModule = nullptr;
101 }
102 }
103 }
104}
105
106
107extern "C" { static void thisModule() {} }
108
110{
112 return ( m_hConnectorModule != nullptr );
114
116 "MacabImplModule::impl_loadModule: inconsistence: inconsistency (never attempted load before, but some values already set)!");
117
118 const OUString sModuleName( SAL_MODULENAME( "macabdrv1" ) );
119 m_hConnectorModule = osl_loadModuleRelative( &thisModule, sModuleName.pData, SAL_LOADMODULE_NOW ); // LAZY! #i61335#
120 OSL_ENSURE( m_hConnectorModule, "MacabImplModule::impl_loadModule: could not load the implementation library!" );
121 if ( !m_hConnectorModule )
122 return false;
123
124 lcl_getFunctionFromModuleOrUnload( m_hConnectorModule, "createMacabConnection", m_pConnectionFactoryFunc );
125
126 if ( !m_hConnectorModule )
127 // one of the symbols did not exist
128 throw RuntimeException();
129
130 return true;
131}
132
133
135{
136 OSL_PRECOND( m_hConnectorModule != nullptr, "MacabImplModule::impl_unloadModule: no module!" );
137
138 osl_unloadModule( m_hConnectorModule );
139 m_hConnectorModule = nullptr;
140
141 m_pConnectionFactoryFunc = nullptr;
142
144}
145
146
148{
149 if ( !impl_loadModule() )
150 throwNoMacOSException();
151
152}
153
154
156{
157 OSL_PRECOND( m_hConnectorModule, "MacabImplModule::createConnection: not initialized!" );
158
159 void* pUntypedConnection = (*m_pConnectionFactoryFunc)( _pDriver );
160 if ( !pUntypedConnection )
161 throw RuntimeException();
162
163 return static_cast< MacabConnection* >( pUntypedConnection );
164}
165
166
168{
169 if ( !m_hConnectorModule )
170 return;
171
173}
174
175
176// = MacabDriver
177
181 m_xContext(_rxContext),
182 m_aImplModule()
183{
184 if ( !m_xContext.is() )
185 throw NullPointerException();
186
187 osl_atomic_increment( &m_refCount );
188 try
189 {
190 Reference< XDesktop2 > xDesktop = Desktop::create( m_xContext );
191 xDesktop->addTerminateListener( this );
192 }
193 catch( const Exception& )
194 {
195 DBG_UNHANDLED_EXCEPTION("connectivity.macab");
196 }
197 osl_atomic_decrement( &m_refCount );
198}
199
201{
202 ::osl::MutexGuard aGuard(m_aMutex);
203
204 // when driver will be destroyed so all our connections have to be destroyed as well
205 for (auto& rxConnection : m_xConnections)
206 {
207 Reference< XComponent > xComp(rxConnection.get(), UNO_QUERY);
208 if (xComp.is())
209 xComp->dispose();
210 }
211 m_xConnections.clear();
212
214}
215
217{
218 return "com.sun.star.comp.sdbc.macab.Driver";
219}
220
221sal_Bool SAL_CALL MacabDriver::supportsService( const OUString& _rServiceName )
222{
223 return cppu::supportsService(this, _rServiceName);
224}
225
227{
228 // which service is supported
229 // for more information @see com.sun.star.sdbc.Driver
230 return { "com.sun.star.sdbc.Driver" };
231}
232
234{
235 ::osl::MutexGuard aGuard(m_aMutex);
236
238
239 // create a new connection with the given properties and append it to our vector
240 MacabConnection* pConnection = m_aImplModule.createConnection( this );
241 SAL_WARN_IF( !pConnection, "connectivity.macab", "MacabDriver::connect: no connection has been created by the factory!" );
242
243 // by definition, the factory function returned an object which was acquired once
244 Reference< XConnection > xConnection = pConnection;
245 pConnection->release();
246
247 // late constructor call which can throw exception and allows a correct dtor call when so
248 pConnection->construct( url, info );
249
250 // remember it
251 m_xConnections.push_back( WeakReferenceHelper( *pConnection ) );
252
253 return xConnection;
254}
255
256sal_Bool SAL_CALL MacabDriver::acceptsURL( const OUString& url )
257{
258 ::osl::MutexGuard aGuard(m_aMutex);
259
261 return false;
262
263 // here we have to look whether we support this URL format
264 return url == "sdbc:address:macab";
265}
266
268{
269 // if you have something special to say, return it here :-)
271}
272
273sal_Int32 SAL_CALL MacabDriver::getMajorVersion( )
274{
276}
277
278sal_Int32 SAL_CALL MacabDriver::getMinorVersion( )
279{
281}
282
283void SAL_CALL MacabDriver::queryTermination( const EventObject& )
284{
285 // nothing to do, nothing to veto
286}
287
288void SAL_CALL MacabDriver::notifyTermination( const EventObject& )
289{
291}
292
293void SAL_CALL MacabDriver::disposing( const EventObject& )
294{
295 // not interested in (this is the disposing of the desktop, if any)
296}
297
299{
300 return "/org.openoffice.Office.DataAccess/DriverSettings/com.sun.star.comp.sdbc.macab.Driver";
301}
302
303extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
305 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
306{
307 return cppu::acquire(new MacabDriver(context));
308}
309
310/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * connectivity_MacabDriver_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
static void thisModule()
#define MACAB_DRIVER_VERSION_MINOR
Definition: MacabDriver.hxx:32
#define MACAB_DRIVER_VERSION_MAJOR
Definition: MacabDriver.hxx:31
Reference< XComponentContext > m_xContext
virtual void disposing(std::unique_lock< std::mutex > &)
helper class for accessing resources shared by different libraries in the connectivity module
OUString getResourceString(TranslateId pResId) const
loads a string from the shared resource file
virtual void construct(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info)
virtual void SAL_CALL queryTermination(const css::lang::EventObject &Event) override
virtual void SAL_CALL disposing() override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL connect(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info) override
MacabDriver(const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual void SAL_CALL notifyTermination(const css::lang::EventObject &Event) override
static OUString impl_getConfigurationSettingsPath()
returns the path of our configuration settings
virtual sal_Bool SAL_CALL acceptsURL(const OUString &url) override
virtual sal_Int32 SAL_CALL getMinorVersion() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual css::uno::Sequence< css::sdbc::DriverPropertyInfo > SAL_CALL getPropertyInfo(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info) override
virtual sal_Int32 SAL_CALL getMajorVersion() override
bool impl_loadModule()
loads the implementation module and retrieves the needed symbols
bool m_bAttemptedLoadModule
Did we already attempt to load the module and to retrieve the symbols?
Definition: MacabDriver.hxx:50
ConnectionFactoryFunction m_pConnectionFactoryFunc
Definition: MacabDriver.hxx:52
MacabConnection * createConnection(MacabDriver *_pDriver) const
creates a new connection @precond <member>init</member> has been called before
void shutdown()
shuts down the impl module
void impl_unloadModule()
unloads the implementation module, and resets all function pointers to <NULL> @precond m_hConnectorMo...
void init()
initializes the implementation module.
bool isMacOSPresent()
determines whether there is a mac OS present in the environment
Definition: MacabDriver.cxx:78
#define DBG_UNHANDLED_EXCEPTION(...)
ULONG m_refCount
FUNCTION
std::mutex m_aMutex
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
@ Exception
::cppu::WeakComponentImplHelper< css::sdbc::XDriver, css::lang::XServiceInfo, css::frame::XTerminateListener > MacabDriver_BASE
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
void throwGenericSQLException(const OUString &_rMsg, const css::uno::Reference< css::uno::XInterface > &_rxSource)
throw a generic SQLException, i.e.
unsigned char sal_Bool