Line data Source code
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 : #include "Connection.hxx"
21 : #include "Driver.hxx"
22 :
23 : #include <connectivity/dbexception.hxx>
24 : #include <resource/common_res.hrc>
25 : #include <resource/hsqldb_res.hrc>
26 : #include <resource/sharedresources.hxx>
27 :
28 : #include <comphelper/processfactory.hxx>
29 : #include <cppuhelper/supportsservice.hxx>
30 : #include <osl/file.hxx>
31 : #include <osl/process.h>
32 : #include <rtl/bootstrap.hxx>
33 : #include <svtools/miscopt.hxx>
34 : #include <unotools/localfilehelper.hxx>
35 :
36 : using namespace com::sun::star;
37 : using namespace com::sun::star::uno;
38 : using namespace com::sun::star::lang;
39 : using namespace com::sun::star::beans;
40 : using namespace com::sun::star::sdbc;
41 : using namespace com::sun::star::sdbcx;
42 :
43 : using namespace ::osl;
44 :
45 : using namespace connectivity::firebird;
46 :
47 : namespace connectivity
48 : {
49 : namespace firebird
50 : {
51 2 : Reference< XInterface > SAL_CALL FirebirdDriver_CreateInstance(
52 : const Reference< XMultiServiceFactory >& _rxFactory) throw( Exception )
53 : {
54 : SAL_INFO("connectivity.firebird", "FirebirdDriver_CreateInstance()" );
55 2 : return *(new FirebirdDriver(comphelper::getComponentContext(_rxFactory)));
56 : }
57 : }
58 : }
59 :
60 : // Static const member variables
61 4 : const OUString FirebirdDriver::our_sFirebirdTmpVar("FIREBIRD_TMP");
62 4 : const OUString FirebirdDriver::our_sFirebirdLockVar("FIREBIRD_LOCK");
63 4 : const OUString FirebirdDriver::our_sFirebirdMsgVar("FIREBIRD_MSG");
64 :
65 2 : FirebirdDriver::FirebirdDriver(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext)
66 : : ODriver_BASE(m_aMutex)
67 : , m_aContext(_rxContext)
68 : , m_firebirdTMPDirectory(NULL, true)
69 2 : , m_firebirdLockDirectory(NULL, true)
70 : {
71 : // Note: TempFile caches the URL on first access; call this here so that
72 : // ~FirebirdDriver is not the first access, because that is called
73 : // when the ServiceManager is disposing, so GetURL() would fail!
74 2 : m_firebirdTMPDirectory.GetURL();
75 2 : m_firebirdLockDirectory.GetURL();
76 :
77 : // ::utl::TempFile uses a unique temporary directory (subdirectory of
78 : // /tmp or other user specific tmp directory) per instance in which
79 : // we can create directories for firebird at will.
80 :
81 : // Overrides firebird's default of /tmp or c:\temp
82 2 : osl_setEnvironment(our_sFirebirdTmpVar.pData, m_firebirdTMPDirectory.GetFileName().pData);
83 :
84 : // Overrides firebird's default of /tmp/firebird or c:\temp\firebird
85 2 : osl_setEnvironment(our_sFirebirdLockVar.pData, m_firebirdLockDirectory.GetFileName().pData);
86 :
87 : #ifndef SYSTEM_FIREBIRD
88 : // Overrides firebird's hardcoded default of /usr/local/firebird on *nix,
89 : // however on Windows it seems to use the current directory as a default.
90 2 : OUString sMsgURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/firebird");
91 2 : ::rtl::Bootstrap::expandMacros(sMsgURL);
92 4 : OUString sMsgPath;
93 2 : ::osl::FileBase::getSystemPathFromFileURL(sMsgURL, sMsgPath);
94 4 : osl_setEnvironment(our_sFirebirdMsgVar.pData, sMsgPath.pData);
95 : #endif
96 2 : }
97 :
98 6 : FirebirdDriver::~FirebirdDriver()
99 : {
100 2 : utl::removeTree(m_firebirdTMPDirectory.GetURL());
101 2 : utl::removeTree(m_firebirdLockDirectory.GetURL());
102 4 : }
103 :
104 2 : void FirebirdDriver::disposing()
105 : {
106 2 : MutexGuard aGuard(m_aMutex);
107 :
108 6 : for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
109 : {
110 4 : Reference< XComponent > xComp(i->get(), UNO_QUERY);
111 4 : if (xComp.is())
112 2 : xComp->dispose();
113 4 : }
114 2 : m_xConnections.clear();
115 :
116 2 : osl_clearEnvironment(our_sFirebirdTmpVar.pData);
117 2 : osl_clearEnvironment(our_sFirebirdLockVar.pData);
118 :
119 : #ifndef SYSTEM_FIREBIRD
120 2 : osl_clearEnvironment(our_sFirebirdMsgVar.pData);
121 : #endif
122 :
123 2 : OSL_VERIFY(fb_shutdown(0, 1));
124 :
125 2 : ODriver_BASE::disposing();
126 2 : }
127 :
128 : //----- static ServiceInfo ---------------------------------------------------
129 2 : rtl::OUString FirebirdDriver::getImplementationName_Static() throw(RuntimeException)
130 : {
131 2 : return rtl::OUString("com.sun.star.comp.sdbc.firebird.Driver");
132 : }
133 :
134 2 : Sequence< OUString > FirebirdDriver::getSupportedServiceNames_Static() throw (RuntimeException)
135 : {
136 2 : Sequence< OUString > aSNS( 2 );
137 2 : aSNS[0] = "com.sun.star.sdbc.Driver";
138 2 : aSNS[0] = "com.sun.star.sdbcx.Driver";
139 2 : return aSNS;
140 : }
141 :
142 0 : OUString SAL_CALL FirebirdDriver::getImplementationName() throw(RuntimeException, std::exception)
143 : {
144 0 : return getImplementationName_Static();
145 : }
146 :
147 0 : sal_Bool SAL_CALL FirebirdDriver::supportsService(const OUString& _rServiceName)
148 : throw(RuntimeException, std::exception)
149 : {
150 0 : return cppu::supportsService(this, _rServiceName);
151 : }
152 :
153 0 : Sequence< OUString > SAL_CALL FirebirdDriver::getSupportedServiceNames()
154 : throw(RuntimeException, std::exception)
155 : {
156 0 : return getSupportedServiceNames_Static();
157 : }
158 :
159 : // ---- XDriver -------------------------------------------------------------
160 4 : Reference< XConnection > SAL_CALL FirebirdDriver::connect(
161 : const OUString& url, const Sequence< PropertyValue >& info )
162 : throw(SQLException, RuntimeException, std::exception)
163 : {
164 4 : Reference< XConnection > xConnection;
165 :
166 : SAL_INFO("connectivity.firebird", "connect(), URL: " << url );
167 :
168 8 : MutexGuard aGuard( m_aMutex );
169 4 : if (ODriver_BASE::rBHelper.bDisposed)
170 0 : throw DisposedException();
171 :
172 4 : if ( ! acceptsURL(url) )
173 0 : return NULL;
174 :
175 4 : Connection* pCon = new Connection(this);
176 8 : Reference< XConnection > xCon = pCon;
177 4 : pCon->construct(url, info);
178 4 : m_xConnections.push_back(WeakReferenceHelper(*pCon));
179 :
180 8 : return xCon;
181 : }
182 :
183 24 : sal_Bool SAL_CALL FirebirdDriver::acceptsURL( const OUString& url )
184 : throw(SQLException, RuntimeException, std::exception)
185 : {
186 24 : SvtMiscOptions aMiscOptions;
187 :
188 96 : return aMiscOptions.IsExperimentalMode() &&
189 72 : (url.equals("sdbc:embedded:firebird") || url.startsWith("sdbc:firebird:"));
190 : }
191 :
192 4 : Sequence< DriverPropertyInfo > SAL_CALL FirebirdDriver::getPropertyInfo(
193 : const OUString& url, const Sequence< PropertyValue >& info )
194 : throw(SQLException, RuntimeException, std::exception)
195 : {
196 : (void) info;
197 4 : if ( ! acceptsURL(url) )
198 : {
199 0 : ::connectivity::SharedResources aResources;
200 0 : const OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
201 0 : ::dbtools::throwGenericSQLException(sMessage ,*this);
202 : }
203 :
204 4 : return Sequence< DriverPropertyInfo >();
205 : }
206 :
207 0 : sal_Int32 SAL_CALL FirebirdDriver::getMajorVersion( ) throw(RuntimeException, std::exception)
208 : {
209 : // The major and minor version are sdbc driver specific. Must begin with 1.0
210 : // as per http://api.libreoffice.org/docs/common/ref/com/sun/star/sdbc/XDriver.html
211 0 : return 1;
212 : }
213 :
214 0 : sal_Int32 SAL_CALL FirebirdDriver::getMinorVersion( ) throw(RuntimeException, std::exception)
215 : {
216 0 : return 0;
217 : }
218 :
219 : //----- XDataDefinitionSupplier
220 4 : uno::Reference< XTablesSupplier > SAL_CALL FirebirdDriver::getDataDefinitionByConnection(
221 : const uno::Reference< XConnection >& rConnection)
222 : throw(SQLException, RuntimeException, std::exception)
223 : {
224 4 : Connection* pConnection = static_cast< Connection* >(rConnection.get());
225 4 : return uno::Reference< XTablesSupplier >(pConnection->createCatalog(), UNO_QUERY);
226 : }
227 :
228 0 : uno::Reference< XTablesSupplier > SAL_CALL FirebirdDriver::getDataDefinitionByURL(
229 : const OUString& rURL,
230 : const uno::Sequence< PropertyValue >& rInfo)
231 : throw(SQLException, RuntimeException, std::exception)
232 : {
233 0 : uno::Reference< XConnection > xConnection = connect(rURL, rInfo);
234 0 : return getDataDefinitionByConnection(xConnection);
235 : }
236 :
237 : namespace connectivity
238 : {
239 : namespace firebird
240 : {
241 :
242 0 : void release(oslInterlockedCount& _refCount, ::cppu::OBroadcastHelper& rBHelper,
243 : Reference< XInterface >& _xInterface, XComponent* _pObject)
244 : {
245 0 : if (osl_atomic_decrement( &_refCount ) == 0)
246 : {
247 0 : osl_atomic_increment( &_refCount );
248 :
249 0 : if (!rBHelper.bDisposed && !rBHelper.bInDispose)
250 : {
251 : // remember the parent
252 0 : Reference< XInterface > xParent;
253 : {
254 0 : MutexGuard aGuard( rBHelper.rMutex );
255 0 : xParent = _xInterface;
256 0 : _xInterface = NULL;
257 : }
258 :
259 : // First dispose
260 0 : _pObject->dispose();
261 :
262 : // only the alive ref holds the object
263 : OSL_ASSERT( _refCount == 1 );
264 :
265 : // release the parent in the ~
266 0 : if (xParent.is())
267 : {
268 0 : MutexGuard aGuard( rBHelper.rMutex );
269 0 : _xInterface = xParent;
270 0 : }
271 : }
272 : }
273 : else
274 0 : osl_atomic_increment( &_refCount );
275 0 : }
276 :
277 734 : void checkDisposed(bool _bThrow) throw ( DisposedException )
278 : {
279 734 : if (_bThrow)
280 0 : throw DisposedException();
281 :
282 734 : }
283 :
284 : }
285 12 : } // namespace connectivity
286 :
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|