1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "MacabDriver.hxx"
22 #include "MacabConnection.hxx"
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 <rtl/ustrbuf.hxx>
28 #include <tools/diagnose_ex.h>
29 #include "resource/macab_res.hrc"
30 #include <comphelper/processfactory.hxx>
31 #include <cppuhelper/supportsservice.hxx>
33 using namespace com::sun::star::uno
;
34 using namespace com::sun::star::lang
;
35 using namespace com::sun::star::beans
;
36 using namespace com::sun::star::sdbc
;
37 using namespace com::sun::star::sdb
;
38 using namespace com::sun::star::frame
;
39 using namespace connectivity::macab
;
43 /** throws a generic SQL exception with SQLState S1000 and error code 0
45 void throwGenericSQLException( const OUString
& _rMessage
)
48 aError
.Message
= _rMessage
;
49 aError
.SQLState
= "S1000";
54 /** throws an SQLException saying than no Mac OS installation was found
56 void throwNoMacOSException()
58 ::connectivity::SharedResources aResources
;
59 const OUString
sError( aResources
.getResourceString(
62 throwGenericSQLException( sError
);
71 MacabImplModule::MacabImplModule()
72 :m_bAttemptedLoadModule(false)
73 ,m_hConnectorModule(NULL
)
74 ,m_pConnectionFactoryFunc(NULL
)
79 bool MacabImplModule::isMacOSPresent()
81 return impl_loadModule();
87 template< typename FUNCTION
>
88 void lcl_getFunctionFromModuleOrUnload( oslModule
& _rModule
, const sal_Char
* _pAsciiSymbolName
, FUNCTION
& _rFunction
)
94 const OUString sSymbolName
= OUString::createFromAscii( _pAsciiSymbolName
);
95 _rFunction
= reinterpret_cast<FUNCTION
>( osl_getSymbol( _rModule
, sSymbolName
.pData
) );
98 { // did not find the symbol
99 OSL_FAIL( OString( OString( "lcl_getFunctionFromModuleOrUnload: could not find the symbol " ) + OString( _pAsciiSymbolName
) ).getStr() );
100 osl_unloadModule( _rModule
);
108 extern "C" { static void SAL_CALL
thisModule() {} }
110 bool MacabImplModule::impl_loadModule()
112 if ( m_bAttemptedLoadModule
)
113 return ( m_hConnectorModule
!= NULL
);
114 m_bAttemptedLoadModule
= true;
116 OSL_ENSURE( !m_hConnectorModule
&& !m_pConnectionFactoryFunc
,
117 "MacabImplModule::impl_loadModule: inconsistence: inconsistency (never attempted load before, but some values already set)!");
119 const OUString
sModuleName( SAL_MODULENAME( "macabdrv1" ) );
120 m_hConnectorModule
= osl_loadModuleRelative( &thisModule
, sModuleName
.pData
, SAL_LOADMODULE_NOW
); // LAZY! #i61335#
121 OSL_ENSURE( m_hConnectorModule
, "MacabImplModule::impl_loadModule: could not load the implementation library!" );
122 if ( !m_hConnectorModule
)
125 lcl_getFunctionFromModuleOrUnload( m_hConnectorModule
, "createMacabConnection", m_pConnectionFactoryFunc
);
127 if ( !m_hConnectorModule
)
128 // one of the symbols did not exist
129 throw RuntimeException();
135 void MacabImplModule::impl_unloadModule()
137 OSL_PRECOND( m_hConnectorModule
!= NULL
, "MacabImplModule::impl_unloadModule: no module!" );
139 osl_unloadModule( m_hConnectorModule
);
140 m_hConnectorModule
= NULL
;
142 m_pConnectionFactoryFunc
= NULL
;
144 m_bAttemptedLoadModule
= false;
148 void MacabImplModule::init()
150 if ( !impl_loadModule() )
151 throwNoMacOSException();
156 MacabConnection
* MacabImplModule::createConnection( MacabDriver
* _pDriver
) const
158 OSL_PRECOND( m_hConnectorModule
, "MacabImplModule::createConnection: not initialized!" );
160 void* pUntypedConnection
= (*m_pConnectionFactoryFunc
)( _pDriver
);
161 if ( !pUntypedConnection
)
162 throw RuntimeException();
164 return static_cast< MacabConnection
* >( pUntypedConnection
);
168 void MacabImplModule::shutdown()
170 if ( !m_hConnectorModule
)
179 MacabDriver::MacabDriver(
180 const Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
)
181 : MacabDriver_BASE(m_aMutex
),
182 m_xContext(_rxContext
),
185 if ( !m_xContext
.is() )
186 throw NullPointerException();
188 osl_atomic_increment( &m_refCount
);
191 Reference
< XDesktop2
> xDesktop
= Desktop::create( m_xContext
);
192 xDesktop
->addTerminateListener( this );
194 catch( const Exception
& )
196 DBG_UNHANDLED_EXCEPTION();
198 osl_atomic_decrement( &m_refCount
);
201 void MacabDriver::disposing()
203 ::osl::MutexGuard
aGuard(m_aMutex
);
205 // when driver will be destroied so all our connections have to be destroied as well
206 for (OWeakRefArray::iterator i
= m_xConnections
.begin(); m_xConnections
.end() != i
; ++i
)
208 Reference
< XComponent
> xComp(i
->get(), UNO_QUERY
);
212 m_xConnections
.clear();
214 WeakComponentImplHelperBase::disposing();
216 // static ServiceInfo
218 OUString
MacabDriver::getImplementationName_Static( ) throw(RuntimeException
)
220 return OUString("com.sun.star.comp.sdbc.macab.Driver");
223 Sequence
< OUString
> MacabDriver::getSupportedServiceNames_Static( ) throw (RuntimeException
)
225 // which service is supported
226 // for more information @see com.sun.star.sdbc.Driver
227 Sequence
< OUString
> aSNS( 1 );
228 aSNS
[0] = "com.sun.star.sdbc.Driver";
233 OUString SAL_CALL
MacabDriver::getImplementationName( ) throw(RuntimeException
)
235 return getImplementationName_Static();
238 sal_Bool SAL_CALL
MacabDriver::supportsService( const OUString
& _rServiceName
) throw(RuntimeException
)
240 return cppu::supportsService(this, _rServiceName
);
243 Sequence
< OUString
> SAL_CALL
MacabDriver::getSupportedServiceNames( ) throw(RuntimeException
)
245 return getSupportedServiceNames_Static();
248 Reference
< XConnection
> SAL_CALL
MacabDriver::connect( const OUString
& url
, const Sequence
< PropertyValue
>& info
) throw(SQLException
, RuntimeException
)
250 ::osl::MutexGuard
aGuard(m_aMutex
);
252 m_aImplModule
.init();
254 // create a new connection with the given properties and append it to our vector
255 MacabConnection
* pConnection
= m_aImplModule
.createConnection( this );
256 SAL_WARN_IF( !pConnection
, "connectivity.macab", "MacabDriver::connect: no connection has been created by the factory!" );
258 // by definition, the factory function returned an object which was acquired once
259 Reference
< XConnection
> xConnection
= pConnection
;
260 pConnection
->release();
262 // late constructor call which can throw exception and allows a correct dtor call when so
263 pConnection
->construct( url
, info
);
266 m_xConnections
.push_back( WeakReferenceHelper( *pConnection
) );
271 sal_Bool SAL_CALL
MacabDriver::acceptsURL( const OUString
& url
)
272 throw(SQLException
, RuntimeException
)
274 ::osl::MutexGuard
aGuard(m_aMutex
);
276 if ( !m_aImplModule
.isMacOSPresent() )
279 // here we have to look whether we support this URL format
280 return url
== "sdbc:address:macab";
283 Sequence
< DriverPropertyInfo
> SAL_CALL
MacabDriver::getPropertyInfo( const OUString
&, const Sequence
< PropertyValue
>& ) throw(SQLException
, RuntimeException
)
285 // if you have something special to say, return it here :-)
286 return Sequence
< DriverPropertyInfo
>();
289 sal_Int32 SAL_CALL
MacabDriver::getMajorVersion( ) throw(RuntimeException
)
291 return MACAB_DRIVER_VERSION_MAJOR
;
294 sal_Int32 SAL_CALL
MacabDriver::getMinorVersion( ) throw(RuntimeException
)
296 return MACAB_DRIVER_VERSION_MINOR
;
299 void SAL_CALL
MacabDriver::queryTermination( const EventObject
& ) throw (TerminationVetoException
, RuntimeException
)
301 // nothing to do, nothing to veto
304 void SAL_CALL
MacabDriver::notifyTermination( const EventObject
& ) throw (RuntimeException
)
306 m_aImplModule
.shutdown();
309 void SAL_CALL
MacabDriver::disposing( const EventObject
& ) throw (RuntimeException
)
311 // not interested in (this is the disposing of the desktop, if any)
314 OUString
MacabDriver::impl_getConfigurationSettingsPath()
316 OUStringBuffer aPath
;
317 aPath
.appendAscii( "/org.openoffice.Office.DataAccess/DriverSettings/" );
318 aPath
.appendAscii( "com.sun.star.comp.sdbc.macab.Driver" );
319 return aPath
.makeStringAndClear();
322 Reference
< XInterface
> SAL_CALL
MacabDriver::Create( const Reference
< XMultiServiceFactory
>& _rxFactory
) throw( Exception
)
324 return *(new MacabDriver(comphelper::getComponentContext(_rxFactory
)));
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */