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>
32 using namespace com::sun::star::uno
;
33 using namespace com::sun::star::lang
;
34 using namespace com::sun::star::beans
;
35 using namespace com::sun::star::sdbc
;
36 using namespace com::sun::star::sdb
;
37 using namespace com::sun::star::frame
;
38 using namespace connectivity::macab
;
40 // =======================================================================
42 // =======================================================================
43 // --------------------------------------------------------------------------------
44 MacabImplModule::MacabImplModule()
45 :m_bAttemptedLoadModule(false)
46 ,m_hConnectorModule(NULL
)
47 ,m_pConnectionFactoryFunc(NULL
)
51 // --------------------------------------------------------------------------------
52 bool MacabImplModule::isMacOSPresent()
54 return impl_loadModule();
57 // --------------------------------------------------------------------------------
60 template< typename FUNCTION
>
61 void lcl_getFunctionFromModuleOrUnload( oslModule
& _rModule
, const sal_Char
* _pAsciiSymbolName
, FUNCTION
& _rFunction
)
67 const OUString sSymbolName
= OUString::createFromAscii( _pAsciiSymbolName
);
68 _rFunction
= (FUNCTION
)( osl_getSymbol( _rModule
, sSymbolName
.pData
) );
71 { // did not find the symbol
72 OSL_FAIL( OString( OString( "lcl_getFunctionFromModuleOrUnload: could not find the symbol " ) + OString( _pAsciiSymbolName
) ).getStr() );
73 osl_unloadModule( _rModule
);
80 // --------------------------------------------------------------------------------
81 extern "C" { static void SAL_CALL
thisModule() {} }
83 bool MacabImplModule::impl_loadModule()
85 if ( m_bAttemptedLoadModule
)
86 return ( m_hConnectorModule
!= NULL
);
87 m_bAttemptedLoadModule
= true;
89 OSL_ENSURE( !m_hConnectorModule
&& !m_pConnectionFactoryFunc
,
90 "MacabImplModule::impl_loadModule: inconsistence: inconsistency (never attempted load before, but some values already set)!");
92 const OUString
sModuleName( SAL_MODULENAME( "macabdrv1" ) );
93 m_hConnectorModule
= osl_loadModuleRelative( &thisModule
, sModuleName
.pData
, SAL_LOADMODULE_NOW
); // LAZY! #i61335#
94 OSL_ENSURE( m_hConnectorModule
, "MacabImplModule::impl_loadModule: could not load the implementation library!" );
95 if ( !m_hConnectorModule
)
98 lcl_getFunctionFromModuleOrUnload( m_hConnectorModule
, "createMacabConnection", m_pConnectionFactoryFunc
);
100 if ( !m_hConnectorModule
)
101 // one of the symbols did not exist
102 throw RuntimeException();
107 // --------------------------------------------------------------------------------
108 void MacabImplModule::impl_unloadModule()
110 OSL_PRECOND( m_hConnectorModule
!= NULL
, "MacabImplModule::impl_unloadModule: no module!" );
112 osl_unloadModule( m_hConnectorModule
);
113 m_hConnectorModule
= NULL
;
115 m_pConnectionFactoryFunc
= NULL
;
117 m_bAttemptedLoadModule
= false;
120 // --------------------------------------------------------------------------------
121 void MacabImplModule::init()
123 if ( !impl_loadModule() )
124 impl_throwNoMacOSException();
128 // --------------------------------------------------------------------------------
129 void MacabImplModule::impl_throwNoMacOSException()
131 ::connectivity::SharedResources aResources
;
132 const OUString
sError( aResources
.getResourceString(
135 impl_throwGenericSQLException( sError
);
138 // --------------------------------------------------------------------------------
139 void MacabImplModule::impl_throwGenericSQLException( const OUString
& _rMessage
)
142 aError
.Message
= _rMessage
;
143 aError
.SQLState
= OUString( "S1000" );
144 aError
.ErrorCode
= 0;
148 // --------------------------------------------------------------------------------
149 MacabConnection
* MacabImplModule::createConnection( MacabDriver
* _pDriver
) const
151 OSL_PRECOND( m_hConnectorModule
, "MacabImplModule::createConnection: not initialized!" );
153 void* pUntypedConnection
= (*m_pConnectionFactoryFunc
)( _pDriver
);
154 if ( !pUntypedConnection
)
155 throw RuntimeException();
157 return static_cast< MacabConnection
* >( pUntypedConnection
);
160 // --------------------------------------------------------------------------------
161 void MacabImplModule::shutdown()
163 if ( !m_hConnectorModule
)
169 // =======================================================================
171 // =======================================================================
172 MacabDriver::MacabDriver(
173 const Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
)
174 : MacabDriver_BASE(m_aMutex
),
175 m_xContext(_rxContext
),
178 if ( !m_xContext
.is() )
179 throw NullPointerException();
181 osl_atomic_increment( &m_refCount
);
184 Reference
< XDesktop2
> xDesktop
= Desktop::create( m_xContext
);
185 xDesktop
->addTerminateListener( this );
187 catch( const Exception
& )
189 DBG_UNHANDLED_EXCEPTION();
191 osl_atomic_decrement( &m_refCount
);
193 // --------------------------------------------------------------------------------
194 void MacabDriver::disposing()
196 ::osl::MutexGuard
aGuard(m_aMutex
);
198 // when driver will be destroied so all our connections have to be destroied as well
199 for (OWeakRefArray::iterator i
= m_xConnections
.begin(); m_xConnections
.end() != i
; ++i
)
201 Reference
< XComponent
> xComp(i
->get(), UNO_QUERY
);
205 m_xConnections
.clear();
207 WeakComponentImplHelperBase::disposing();
209 // static ServiceInfo
210 //------------------------------------------------------------------------------
211 OUString
MacabDriver::getImplementationName_Static( ) throw(RuntimeException
)
213 return OUString::createFromAscii( impl_getAsciiImplementationName() );
215 //------------------------------------------------------------------------------
216 Sequence
< OUString
> MacabDriver::getSupportedServiceNames_Static( ) throw (RuntimeException
)
218 // which service is supported
219 // for more information @see com.sun.star.sdbc.Driver
220 Sequence
< OUString
> aSNS( 1 );
221 aSNS
[0] = OUString("com.sun.star.sdbc.Driver");
225 //------------------------------------------------------------------
226 OUString SAL_CALL
MacabDriver::getImplementationName( ) throw(RuntimeException
)
228 return getImplementationName_Static();
230 //------------------------------------------------------------------
231 sal_Bool SAL_CALL
MacabDriver::supportsService( const OUString
& _rServiceName
) throw(RuntimeException
)
233 Sequence
< OUString
> aSupported(getSupportedServiceNames());
234 const OUString
* pSupported
= aSupported
.getConstArray();
235 const OUString
* pEnd
= pSupported
+ aSupported
.getLength();
237 while (pSupported
!= pEnd
&& !pSupported
->equals(_rServiceName
))
239 return pSupported
!= pEnd
;
241 //------------------------------------------------------------------
242 Sequence
< OUString
> SAL_CALL
MacabDriver::getSupportedServiceNames( ) throw(RuntimeException
)
244 return getSupportedServiceNames_Static();
246 // --------------------------------------------------------------------------------
247 Reference
< XConnection
> SAL_CALL
MacabDriver::connect( const OUString
& url
, const Sequence
< PropertyValue
>& info
) throw(SQLException
, RuntimeException
)
249 ::osl::MutexGuard
aGuard(m_aMutex
);
251 m_aImplModule
.init();
253 // create a new connection with the given properties and append it to our vector
254 MacabConnection
* pConnection
= m_aImplModule
.createConnection( this );
255 OSL_POSTCOND( pConnection
, "MacabDriver::connect: no connection has been created by the factory!" );
257 // by definition, the factory function returned an object which was acquired once
258 Reference
< XConnection
> xConnection
= pConnection
;
259 pConnection
->release();
261 // late constructor call which can throw exception and allows a correct dtor call when so
262 pConnection
->construct( url
, info
);
265 m_xConnections
.push_back( WeakReferenceHelper( *pConnection
) );
269 // --------------------------------------------------------------------------------
270 sal_Bool SAL_CALL
MacabDriver::acceptsURL( const OUString
& url
)
271 throw(SQLException
, RuntimeException
)
273 ::osl::MutexGuard
aGuard(m_aMutex
);
275 if ( !m_aImplModule
.isMacOSPresent() )
278 // here we have to look whether we support this URL format
279 return url
.equals("sdbc:address:macab");
281 // --------------------------------------------------------------------------------
282 Sequence
< DriverPropertyInfo
> SAL_CALL
MacabDriver::getPropertyInfo( const OUString
&, const Sequence
< PropertyValue
>& ) throw(SQLException
, RuntimeException
)
284 // if you have something special to say, return it here :-)
285 return Sequence
< DriverPropertyInfo
>();
287 // --------------------------------------------------------------------------------
288 sal_Int32 SAL_CALL
MacabDriver::getMajorVersion( ) throw(RuntimeException
)
290 return MACAB_DRIVER_VERSION_MAJOR
;
292 // --------------------------------------------------------------------------------
293 sal_Int32 SAL_CALL
MacabDriver::getMinorVersion( ) throw(RuntimeException
)
295 return MACAB_DRIVER_VERSION_MINOR
;
297 // --------------------------------------------------------------------------------
298 void SAL_CALL
MacabDriver::queryTermination( const EventObject
& ) throw (TerminationVetoException
, RuntimeException
)
300 // nothing to do, nothing to veto
302 // --------------------------------------------------------------------------------
303 void SAL_CALL
MacabDriver::notifyTermination( const EventObject
& ) throw (RuntimeException
)
305 m_aImplModule
.shutdown();
307 // --------------------------------------------------------------------------------
308 void SAL_CALL
MacabDriver::disposing( const EventObject
& ) throw (RuntimeException
)
310 // not interested in (this is the disposing of the desktop, if any)
312 // --------------------------------------------------------------------------------
313 const sal_Char
* MacabDriver::impl_getAsciiImplementationName()
315 return "com.sun.star.comp.sdbc.macab.Driver";
316 // this name is referenced in the configuration and in the macab.xml
317 // Please be careful when changing it.
319 // --------------------------------------------------------------------------------
320 OUString
MacabDriver::impl_getConfigurationSettingsPath()
322 OUStringBuffer aPath
;
323 aPath
.appendAscii( "/org.openoffice.Office.DataAccess/DriverSettings/" );
324 aPath
.appendAscii( "com.sun.star.comp.sdbc.macab.Driver" );
325 return aPath
.makeStringAndClear();
327 // --------------------------------------------------------------------------------
328 Reference
< XInterface
> SAL_CALL
MacabDriver::Create( const Reference
< XMultiServiceFactory
>& _rxFactory
) throw( Exception
)
330 return *(new MacabDriver(comphelper::getComponentContext(_rxFactory
)));
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */