1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "MacabDriver.hxx" 32*cdf0e10cSrcweir #include "MacabConnection.hxx" 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir /** === begin UNO includes === **/ 35*cdf0e10cSrcweir #include <com/sun/star/sdb/SQLContext.hpp> 36*cdf0e10cSrcweir #include <com/sun/star/lang/NullPointerException.hpp> 37*cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp> 38*cdf0e10cSrcweir /** === end UNO includes === **/ 39*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 40*cdf0e10cSrcweir #include <tools/diagnose_ex.h> 41*cdf0e10cSrcweir #include "resource/macab_res.hrc" 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir using namespace com::sun::star::uno; 44*cdf0e10cSrcweir using namespace com::sun::star::lang; 45*cdf0e10cSrcweir using namespace com::sun::star::beans; 46*cdf0e10cSrcweir using namespace com::sun::star::sdbc; 47*cdf0e10cSrcweir using namespace com::sun::star::sdb; 48*cdf0e10cSrcweir using namespace com::sun::star::frame; 49*cdf0e10cSrcweir using namespace connectivity::macab; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir // ======================================================================= 52*cdf0e10cSrcweir // = MacabImplModule 53*cdf0e10cSrcweir // ======================================================================= 54*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 55*cdf0e10cSrcweir MacabImplModule::MacabImplModule( const Reference< XMultiServiceFactory >& _rxFactory ) 56*cdf0e10cSrcweir :m_xORB(_rxFactory) 57*cdf0e10cSrcweir ,m_bAttemptedLoadModule(false) 58*cdf0e10cSrcweir ,m_hConnectorModule(NULL) 59*cdf0e10cSrcweir ,m_pConnectionFactoryFunc(NULL) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir if ( !m_xORB.is() ) 62*cdf0e10cSrcweir throw NullPointerException(); 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 66*cdf0e10cSrcweir bool MacabImplModule::isMacOSPresent() 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir return impl_loadModule(); 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 72*cdf0e10cSrcweir namespace 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir template< typename FUNCTION > 75*cdf0e10cSrcweir void lcl_getFunctionFromModuleOrUnload( oslModule& _rModule, const sal_Char* _pAsciiSymbolName, FUNCTION& _rFunction ) 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir _rFunction = NULL; 78*cdf0e10cSrcweir if ( _rModule ) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir // 81*cdf0e10cSrcweir const ::rtl::OUString sSymbolName = ::rtl::OUString::createFromAscii( _pAsciiSymbolName ); 82*cdf0e10cSrcweir _rFunction = (FUNCTION)( osl_getSymbol( _rModule, sSymbolName.pData ) ); 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir if ( !_rFunction ) 85*cdf0e10cSrcweir { // did not find the symbol 86*cdf0e10cSrcweir OSL_ENSURE( false, ::rtl::OString( "lcl_getFunctionFromModuleOrUnload: could not find the symbol " ) + ::rtl::OString( _pAsciiSymbolName ) ); 87*cdf0e10cSrcweir osl_unloadModule( _rModule ); 88*cdf0e10cSrcweir _rModule = NULL; 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 95*cdf0e10cSrcweir extern "C" { static void SAL_CALL thisModule() {} } 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir bool MacabImplModule::impl_loadModule() 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir if ( m_bAttemptedLoadModule ) 100*cdf0e10cSrcweir return ( m_hConnectorModule != NULL ); 101*cdf0e10cSrcweir m_bAttemptedLoadModule = true; 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir OSL_ENSURE( !m_hConnectorModule && !m_pConnectionFactoryFunc, 104*cdf0e10cSrcweir "MacabImplModule::impl_loadModule: inconsistence: inconsistency (never attempted load before, but some values already set)!"); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir const ::rtl::OUString sModuleName = ::rtl::OUString::createFromAscii( SAL_MODULENAME( "macabdrv1" ) ); 107*cdf0e10cSrcweir m_hConnectorModule = osl_loadModuleRelative( &thisModule, sModuleName.pData, SAL_LOADMODULE_NOW ); // LAZY! #i61335# 108*cdf0e10cSrcweir OSL_ENSURE( m_hConnectorModule, "MacabImplModule::impl_loadModule: could not load the implementation library!" ); 109*cdf0e10cSrcweir if ( !m_hConnectorModule ) 110*cdf0e10cSrcweir return false; 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir lcl_getFunctionFromModuleOrUnload( m_hConnectorModule, "createMacabConnection", m_pConnectionFactoryFunc ); 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir if ( !m_hConnectorModule ) 115*cdf0e10cSrcweir // one of the symbols did not exist 116*cdf0e10cSrcweir throw RuntimeException(); 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir return true; 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 122*cdf0e10cSrcweir void MacabImplModule::impl_unloadModule() 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir OSL_PRECOND( m_hConnectorModule != NULL, "MacabImplModule::impl_unloadModule: no module!" ); 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir osl_unloadModule( m_hConnectorModule ); 127*cdf0e10cSrcweir m_hConnectorModule = NULL; 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir m_pConnectionFactoryFunc = NULL; 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir m_bAttemptedLoadModule = false; 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 135*cdf0e10cSrcweir void MacabImplModule::init() 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir if ( !impl_loadModule() ) 138*cdf0e10cSrcweir impl_throwNoMacOSException(); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 143*cdf0e10cSrcweir void MacabImplModule::impl_throwNoMacOSException() 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir ::connectivity::SharedResources aResources; 146*cdf0e10cSrcweir const ::rtl::OUString sError( aResources.getResourceString( 147*cdf0e10cSrcweir STR_NO_MAC_OS_FOUND 148*cdf0e10cSrcweir ) ); 149*cdf0e10cSrcweir impl_throwGenericSQLException( sError ); 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 153*cdf0e10cSrcweir void MacabImplModule::impl_throwGenericSQLException( const ::rtl::OUString& _rMessage ) 154*cdf0e10cSrcweir { 155*cdf0e10cSrcweir SQLException aError; 156*cdf0e10cSrcweir aError.Message = _rMessage; 157*cdf0e10cSrcweir aError.SQLState = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "S1000" ) ); 158*cdf0e10cSrcweir aError.ErrorCode = 0; 159*cdf0e10cSrcweir throw aError; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 163*cdf0e10cSrcweir MacabConnection* MacabImplModule::createConnection( MacabDriver* _pDriver ) const 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir OSL_PRECOND( m_hConnectorModule, "MacabImplModule::createConnection: not initialized!" ); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir void* pUntypedConnection = (*m_pConnectionFactoryFunc)( _pDriver ); 168*cdf0e10cSrcweir if ( !pUntypedConnection ) 169*cdf0e10cSrcweir throw RuntimeException(); 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir return static_cast< MacabConnection* >( pUntypedConnection ); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 175*cdf0e10cSrcweir void MacabImplModule::shutdown() 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir if ( !m_hConnectorModule ) 178*cdf0e10cSrcweir return; 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir impl_unloadModule(); 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir // ======================================================================= 184*cdf0e10cSrcweir // = MacabDriver 185*cdf0e10cSrcweir // ======================================================================= 186*cdf0e10cSrcweir MacabDriver::MacabDriver( 187*cdf0e10cSrcweir const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory) 188*cdf0e10cSrcweir : MacabDriver_BASE(m_aMutex), 189*cdf0e10cSrcweir m_xMSFactory(_rxFactory), 190*cdf0e10cSrcweir m_aImplModule(_rxFactory) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir if ( !m_xMSFactory.is() ) 193*cdf0e10cSrcweir throw NullPointerException(); 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir osl_incrementInterlockedCount( &m_refCount ); 196*cdf0e10cSrcweir try 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir Reference< XDesktop > xDesktop( 199*cdf0e10cSrcweir m_xMSFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ) ), 200*cdf0e10cSrcweir UNO_QUERY_THROW ); 201*cdf0e10cSrcweir xDesktop->addTerminateListener( this ); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir catch( const Exception& ) 204*cdf0e10cSrcweir { 205*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir osl_decrementInterlockedCount( &m_refCount ); 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 210*cdf0e10cSrcweir void MacabDriver::disposing() 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex); 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir // when driver will be destroied so all our connections have to be destroied as well 215*cdf0e10cSrcweir for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir Reference< XComponent > xComp(i->get(), UNO_QUERY); 218*cdf0e10cSrcweir if (xComp.is()) 219*cdf0e10cSrcweir xComp->dispose(); 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir m_xConnections.clear(); 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir WeakComponentImplHelperBase::disposing(); 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir // static ServiceInfo 226*cdf0e10cSrcweir //------------------------------------------------------------------------------ 227*cdf0e10cSrcweir rtl::OUString MacabDriver::getImplementationName_Static( ) throw(RuntimeException) 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir return rtl::OUString::createFromAscii( impl_getAsciiImplementationName() ); 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir //------------------------------------------------------------------------------ 232*cdf0e10cSrcweir Sequence< ::rtl::OUString > MacabDriver::getSupportedServiceNames_Static( ) throw (RuntimeException) 233*cdf0e10cSrcweir { 234*cdf0e10cSrcweir // which service is supported 235*cdf0e10cSrcweir // for more information @see com.sun.star.sdbc.Driver 236*cdf0e10cSrcweir Sequence< ::rtl::OUString > aSNS( 1 ); 237*cdf0e10cSrcweir aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver"); 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir return aSNS; 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir //------------------------------------------------------------------ 242*cdf0e10cSrcweir ::rtl::OUString SAL_CALL MacabDriver::getImplementationName( ) throw(RuntimeException) 243*cdf0e10cSrcweir { 244*cdf0e10cSrcweir return getImplementationName_Static(); 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir //------------------------------------------------------------------ 247*cdf0e10cSrcweir sal_Bool SAL_CALL MacabDriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException) 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames()); 250*cdf0e10cSrcweir const ::rtl::OUString* pSupported = aSupported.getConstArray(); 251*cdf0e10cSrcweir const ::rtl::OUString* pEnd = pSupported + aSupported.getLength(); 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir while (pSupported != pEnd && !pSupported->equals(_rServiceName)) 254*cdf0e10cSrcweir ++pSupported; 255*cdf0e10cSrcweir return pSupported != pEnd; 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir //------------------------------------------------------------------ 258*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL MacabDriver::getSupportedServiceNames( ) throw(RuntimeException) 259*cdf0e10cSrcweir { 260*cdf0e10cSrcweir return getSupportedServiceNames_Static(); 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 263*cdf0e10cSrcweir Reference< XConnection > SAL_CALL MacabDriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException) 264*cdf0e10cSrcweir { 265*cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex); 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir m_aImplModule.init(); 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir // create a new connection with the given properties and append it to our vector 270*cdf0e10cSrcweir MacabConnection* pConnection = m_aImplModule.createConnection( this ); 271*cdf0e10cSrcweir OSL_POSTCOND( pConnection, "MacabDriver::connect: no connection has been created by the factory!" ); 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir // by definition, the factory function returned an object which was acquired once 274*cdf0e10cSrcweir Reference< XConnection > xConnection = pConnection; 275*cdf0e10cSrcweir pConnection->release(); 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir // late constructor call which can throw exception and allows a correct dtor call when so 278*cdf0e10cSrcweir pConnection->construct( url, info ); 279*cdf0e10cSrcweir 280*cdf0e10cSrcweir // remember it 281*cdf0e10cSrcweir m_xConnections.push_back( WeakReferenceHelper( *pConnection ) ); 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir return xConnection; 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 286*cdf0e10cSrcweir sal_Bool SAL_CALL MacabDriver::acceptsURL( const ::rtl::OUString& url ) 287*cdf0e10cSrcweir throw(SQLException, RuntimeException) 288*cdf0e10cSrcweir { 289*cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex); 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir if ( !m_aImplModule.isMacOSPresent() ) 292*cdf0e10cSrcweir return sal_False; 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir // here we have to look whether we support this URL format 295*cdf0e10cSrcweir return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:address:macab:"), 18)); 296*cdf0e10cSrcweir } 297*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 298*cdf0e10cSrcweir Sequence< DriverPropertyInfo > SAL_CALL MacabDriver::getPropertyInfo( const ::rtl::OUString&, const Sequence< PropertyValue >& ) throw(SQLException, RuntimeException) 299*cdf0e10cSrcweir { 300*cdf0e10cSrcweir // if you have something special to say, return it here :-) 301*cdf0e10cSrcweir return Sequence< DriverPropertyInfo >(); 302*cdf0e10cSrcweir } 303*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 304*cdf0e10cSrcweir sal_Int32 SAL_CALL MacabDriver::getMajorVersion( ) throw(RuntimeException) 305*cdf0e10cSrcweir { 306*cdf0e10cSrcweir return MACAB_DRIVER_VERSION_MAJOR; 307*cdf0e10cSrcweir } 308*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 309*cdf0e10cSrcweir sal_Int32 SAL_CALL MacabDriver::getMinorVersion( ) throw(RuntimeException) 310*cdf0e10cSrcweir { 311*cdf0e10cSrcweir return MACAB_DRIVER_VERSION_MINOR; 312*cdf0e10cSrcweir } 313*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 314*cdf0e10cSrcweir void SAL_CALL MacabDriver::queryTermination( const EventObject& ) throw (TerminationVetoException, RuntimeException) 315*cdf0e10cSrcweir { 316*cdf0e10cSrcweir // nothing to do, nothing to veto 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 319*cdf0e10cSrcweir void SAL_CALL MacabDriver::notifyTermination( const EventObject& ) throw (RuntimeException) 320*cdf0e10cSrcweir { 321*cdf0e10cSrcweir m_aImplModule.shutdown(); 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 324*cdf0e10cSrcweir void SAL_CALL MacabDriver::disposing( const EventObject& ) throw (RuntimeException) 325*cdf0e10cSrcweir { 326*cdf0e10cSrcweir // not interested in (this is the disposing of the desktop, if any) 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 329*cdf0e10cSrcweir const sal_Char* MacabDriver::impl_getAsciiImplementationName() 330*cdf0e10cSrcweir { 331*cdf0e10cSrcweir return "com.sun.star.comp.sdbc.macab.Driver"; 332*cdf0e10cSrcweir // this name is referenced in the configuration and in the macab.xml 333*cdf0e10cSrcweir // Please be careful when changing it. 334*cdf0e10cSrcweir } 335*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 336*cdf0e10cSrcweir ::rtl::OUString MacabDriver::impl_getConfigurationSettingsPath() 337*cdf0e10cSrcweir { 338*cdf0e10cSrcweir ::rtl::OUStringBuffer aPath; 339*cdf0e10cSrcweir aPath.appendAscii( "/org.openoffice.Office.DataAccess/DriverSettings/" ); 340*cdf0e10cSrcweir aPath.appendAscii( "com.sun.star.comp.sdbc.macab.Driver" ); 341*cdf0e10cSrcweir return aPath.makeStringAndClear(); 342*cdf0e10cSrcweir } 343*cdf0e10cSrcweir // -------------------------------------------------------------------------------- 344*cdf0e10cSrcweir Reference< XInterface > SAL_CALL MacabDriver::Create( const Reference< XMultiServiceFactory >& _rxFactory ) throw( Exception ) 345*cdf0e10cSrcweir { 346*cdf0e10cSrcweir return *(new MacabDriver(_rxFactory)); 347*cdf0e10cSrcweir } 348*cdf0e10cSrcweir 349