xref: /AOO41X/main/connectivity/source/drivers/jdbc/JConnection.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 "java/sql/Connection.hxx"
32*cdf0e10cSrcweir #include "java/lang/Class.hxx"
33*cdf0e10cSrcweir #include "java/tools.hxx"
34*cdf0e10cSrcweir #include "java/ContextClassLoader.hxx"
35*cdf0e10cSrcweir #include "java/sql/DatabaseMetaData.hxx"
36*cdf0e10cSrcweir #include "java/sql/JStatement.hxx"
37*cdf0e10cSrcweir #include "java/sql/Driver.hxx"
38*cdf0e10cSrcweir #include "java/sql/PreparedStatement.hxx"
39*cdf0e10cSrcweir #include "java/sql/CallableStatement.hxx"
40*cdf0e10cSrcweir #include "java/sql/SQLWarning.hxx"
41*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLWarning.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLWarning.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
45*cdf0e10cSrcweir #include "connectivity/sqlparse.hxx"
46*cdf0e10cSrcweir #include "connectivity/dbexception.hxx"
47*cdf0e10cSrcweir #include "java/util/Property.hxx"
48*cdf0e10cSrcweir #include "java/LocalRef.hxx"
49*cdf0e10cSrcweir #include "resource/jdbc_log.hrc"
50*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
51*cdf0e10cSrcweir #include "jvmaccess/classpath.hxx"
52*cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
53*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
54*cdf0e10cSrcweir #include <jni.h>
55*cdf0e10cSrcweir #include "resource/common_res.hrc"
56*cdf0e10cSrcweir #include <unotools/confignode.hxx>
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir #include <list>
59*cdf0e10cSrcweir #include <memory>
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir using namespace connectivity;
62*cdf0e10cSrcweir using namespace connectivity::jdbc;
63*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
64*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
65*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
66*cdf0e10cSrcweir using namespace ::com::sun::star::container;
67*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir namespace {
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir struct ClassMapEntry {
72*cdf0e10cSrcweir     ClassMapEntry(
73*cdf0e10cSrcweir         rtl::OUString const & theClassPath, rtl::OUString const & theClassName):
74*cdf0e10cSrcweir         classPath(theClassPath), className(theClassName), classLoader(NULL),
75*cdf0e10cSrcweir         classObject(NULL) {}
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     rtl::OUString classPath;
78*cdf0e10cSrcweir     rtl::OUString className;
79*cdf0e10cSrcweir     jweak classLoader;
80*cdf0e10cSrcweir     jweak classObject;
81*cdf0e10cSrcweir };
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir typedef std::list< ClassMapEntry > ClassMap;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir struct ClassMapData {
86*cdf0e10cSrcweir     osl::Mutex mutex;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     ClassMap map;
89*cdf0e10cSrcweir };
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir struct ClassMapDataInit {
92*cdf0e10cSrcweir     ClassMapData * operator()() {
93*cdf0e10cSrcweir         static ClassMapData instance;
94*cdf0e10cSrcweir         return &instance;
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir };
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir template < typename T >
99*cdf0e10cSrcweir bool getLocalFromWeakRef( jweak& _weak, LocalRef< T >& _inout_local )
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir     _inout_local.set( static_cast< T >( _inout_local.env().NewLocalRef( _weak ) ) );
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     if ( !_inout_local.is() )
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         if ( _inout_local.env().ExceptionCheck())
106*cdf0e10cSrcweir         {
107*cdf0e10cSrcweir             return false;
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir         else if ( _weak != NULL )
110*cdf0e10cSrcweir         {
111*cdf0e10cSrcweir             _inout_local.env().DeleteWeakGlobalRef( _weak );
112*cdf0e10cSrcweir             _weak = NULL;
113*cdf0e10cSrcweir         }
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir     return true;
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir // Load a class.  A map from pairs of (classPath, name) to pairs of weak Java
119*cdf0e10cSrcweir // references to (ClassLoader, Class) is maintained, so that a class is only
120*cdf0e10cSrcweir // loaded once.
121*cdf0e10cSrcweir //
122*cdf0e10cSrcweir // It may happen that the weak reference to the ClassLoader becomes null while
123*cdf0e10cSrcweir // the reference to the Class remains non-null (in case the Class was actually
124*cdf0e10cSrcweir // loaded by some parent of the ClassLoader), in which case the ClassLoader is
125*cdf0e10cSrcweir // resurrected (which cannot cause any classes to be loaded multiple times, as
126*cdf0e10cSrcweir // the ClassLoader is no longer reachable, so no classes it has ever loaded are
127*cdf0e10cSrcweir // still reachable).
128*cdf0e10cSrcweir //
129*cdf0e10cSrcweir // Similarly, it may happen that the weak reference to the Class becomes null
130*cdf0e10cSrcweir // while the reference to the ClassLoader remains non-null, in which case the
131*cdf0e10cSrcweir // Class is simply re-loaded.
132*cdf0e10cSrcweir //
133*cdf0e10cSrcweir // This code is close to the implementation of jvmaccess::ClassPath::loadClass
134*cdf0e10cSrcweir // in jvmaccess/classpath.hxx, but not close enough to avoid the duplication.
135*cdf0e10cSrcweir //
136*cdf0e10cSrcweir // If false is returned, a (still pending) JNI exception occurred.
137*cdf0e10cSrcweir bool loadClass(
138*cdf0e10cSrcweir     Reference< XComponentContext > const & context, JNIEnv& environment,
139*cdf0e10cSrcweir     rtl::OUString const & classPath, rtl::OUString const & name,
140*cdf0e10cSrcweir     LocalRef< jobject > * classLoaderPtr, LocalRef< jclass > * classPtr)
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir     OSL_ASSERT(classLoaderPtr != NULL);
143*cdf0e10cSrcweir     // For any jweak entries still present in the map upon destruction,
144*cdf0e10cSrcweir     // DeleteWeakGlobalRef is not called (which is a leak):
145*cdf0e10cSrcweir     ClassMapData * d =
146*cdf0e10cSrcweir         rtl_Instance< ClassMapData, ClassMapDataInit, osl::MutexGuard,
147*cdf0e10cSrcweir         osl::GetGlobalMutex >::create(
148*cdf0e10cSrcweir             ClassMapDataInit(), osl::GetGlobalMutex());
149*cdf0e10cSrcweir     osl::MutexGuard g(d->mutex);
150*cdf0e10cSrcweir     ClassMap::iterator i(d->map.begin());
151*cdf0e10cSrcweir     LocalRef< jobject > cloader(environment);
152*cdf0e10cSrcweir     LocalRef< jclass > cl(environment);
153*cdf0e10cSrcweir     // Prune dangling weak references from the list while searching for a match,
154*cdf0e10cSrcweir     // so that the list cannot grow unbounded:
155*cdf0e10cSrcweir     for (; i != d->map.end();)
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         LocalRef< jobject > classLoader( environment );
158*cdf0e10cSrcweir         if ( !getLocalFromWeakRef( i->classLoader, classLoader ) )
159*cdf0e10cSrcweir             return false;
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir         LocalRef< jclass > classObject( environment );
162*cdf0e10cSrcweir         if ( !getLocalFromWeakRef( i->classObject, classObject ) )
163*cdf0e10cSrcweir             return false;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         if ( !classLoader.is() && !classObject.is() )
166*cdf0e10cSrcweir         {
167*cdf0e10cSrcweir             i = d->map.erase(i);
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir         else if ( i->classPath == classPath && i->className == name )
170*cdf0e10cSrcweir         {
171*cdf0e10cSrcweir             cloader.set( classLoader.release() );
172*cdf0e10cSrcweir             cl.set( classObject.release() );
173*cdf0e10cSrcweir             break;
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir         else
176*cdf0e10cSrcweir         {
177*cdf0e10cSrcweir             ++i;
178*cdf0e10cSrcweir         }
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir     if ( !cloader.is() || !cl.is() )
181*cdf0e10cSrcweir     {
182*cdf0e10cSrcweir         if ( i == d->map.end() )
183*cdf0e10cSrcweir         {
184*cdf0e10cSrcweir             // Push a new ClassMapEntry (which can potentially fail) before
185*cdf0e10cSrcweir             // loading the class, so that it never happens that a class is
186*cdf0e10cSrcweir             // loaded but not added to the map (which could have effects on the
187*cdf0e10cSrcweir             // JVM that are not easily undone).  If the pushed ClassMapEntry is
188*cdf0e10cSrcweir             // not used after all (return false, etc.) it will be pruned on next
189*cdf0e10cSrcweir             // call because its classLoader/classObject are null:
190*cdf0e10cSrcweir             d->map.push_front( ClassMapEntry( classPath, name ) );
191*cdf0e10cSrcweir             i = d->map.begin();
192*cdf0e10cSrcweir         }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir         LocalRef< jclass > clClass( environment );
195*cdf0e10cSrcweir         clClass.set( environment.FindClass( "java/net/URLClassLoader" ) );
196*cdf0e10cSrcweir         if ( !clClass.is() )
197*cdf0e10cSrcweir             return false;
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         jweak wcloader = NULL;
200*cdf0e10cSrcweir         if (!cloader.is())
201*cdf0e10cSrcweir         {
202*cdf0e10cSrcweir             jmethodID ctorLoader( environment.GetMethodID( clClass.get(), "<init>", "([Ljava/net/URL;)V" ) );
203*cdf0e10cSrcweir             if (ctorLoader == NULL)
204*cdf0e10cSrcweir                 return false;
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir             LocalRef< jobjectArray > arr( environment );
207*cdf0e10cSrcweir             arr.set( jvmaccess::ClassPath::translateToUrls( context, &environment, classPath ) );
208*cdf0e10cSrcweir             if ( !arr.is() )
209*cdf0e10cSrcweir                 return false;
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir             jvalue arg;
212*cdf0e10cSrcweir             arg.l = arr.get();
213*cdf0e10cSrcweir             cloader.set( environment.NewObjectA( clClass.get(), ctorLoader, &arg ) );
214*cdf0e10cSrcweir             if ( !cloader.is() )
215*cdf0e10cSrcweir                 return false;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir             wcloader = environment.NewWeakGlobalRef( cloader.get() );
218*cdf0e10cSrcweir             if ( wcloader == NULL )
219*cdf0e10cSrcweir                 return false;
220*cdf0e10cSrcweir         }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         jweak wcl = NULL;
223*cdf0e10cSrcweir         if ( !cl.is() )
224*cdf0e10cSrcweir         {
225*cdf0e10cSrcweir             jmethodID methLoadClass( environment.GetMethodID( clClass.get(), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;" ) );
226*cdf0e10cSrcweir             if ( methLoadClass == NULL )
227*cdf0e10cSrcweir                 return false;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir             LocalRef< jstring > str( environment );
230*cdf0e10cSrcweir             str.set( convertwchar_tToJavaString( &environment, name ) );
231*cdf0e10cSrcweir             if ( !str.is() )
232*cdf0e10cSrcweir                 return false;
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir             jvalue arg;
235*cdf0e10cSrcweir             arg.l = str.get();
236*cdf0e10cSrcweir             cl.set( static_cast< jclass >( environment.CallObjectMethodA( cloader.get(), methLoadClass, &arg ) ) );
237*cdf0e10cSrcweir             if ( !cl.is() )
238*cdf0e10cSrcweir                 return false;
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir             wcl = environment.NewWeakGlobalRef( cl.get() );
241*cdf0e10cSrcweir             if ( wcl == NULL )
242*cdf0e10cSrcweir                 return false;
243*cdf0e10cSrcweir         }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir         if ( wcloader != NULL)
246*cdf0e10cSrcweir         {
247*cdf0e10cSrcweir             i->classLoader = wcloader;
248*cdf0e10cSrcweir         }
249*cdf0e10cSrcweir         if ( wcl != NULL )
250*cdf0e10cSrcweir         {
251*cdf0e10cSrcweir             i->classObject = wcl;
252*cdf0e10cSrcweir         }
253*cdf0e10cSrcweir     }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     classLoaderPtr->set( cloader.release() );
256*cdf0e10cSrcweir     classPtr->set( cl.release() );
257*cdf0e10cSrcweir     return true;
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir //------------------------------------------------------------------------------
263*cdf0e10cSrcweir IMPLEMENT_SERVICE_INFO(java_sql_Connection,"com.sun.star.sdbcx.JConnection","com.sun.star.sdbc.Connection");
264*cdf0e10cSrcweir //------------------------------------------------------------------------------
265*cdf0e10cSrcweir //**************************************************************
266*cdf0e10cSrcweir //************ Class: java.sql.Connection
267*cdf0e10cSrcweir //**************************************************************
268*cdf0e10cSrcweir jclass java_sql_Connection::theClass = 0;
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir java_sql_Connection::java_sql_Connection( const java_sql_Driver& _rDriver )
271*cdf0e10cSrcweir     :java_lang_Object( _rDriver.getContext().getLegacyServiceFactory() )
272*cdf0e10cSrcweir     ,OSubComponent<java_sql_Connection, java_sql_Connection_BASE>((::cppu::OWeakObject*)(&_rDriver), this)
273*cdf0e10cSrcweir     ,m_pDriver( &_rDriver )
274*cdf0e10cSrcweir     ,m_pDriverobject(NULL)
275*cdf0e10cSrcweir     ,m_pDriverClassLoader()
276*cdf0e10cSrcweir     ,m_Driver_theClass(NULL)
277*cdf0e10cSrcweir     ,m_aLogger( _rDriver.getLogger() )
278*cdf0e10cSrcweir     ,m_bParameterSubstitution(sal_False)
279*cdf0e10cSrcweir     ,m_bIgnoreDriverPrivileges(sal_True)
280*cdf0e10cSrcweir     ,m_bIgnoreCurrency(sal_False)
281*cdf0e10cSrcweir {
282*cdf0e10cSrcweir }
283*cdf0e10cSrcweir // -----------------------------------------------------------------------------
284*cdf0e10cSrcweir java_sql_Connection::~java_sql_Connection()
285*cdf0e10cSrcweir {
286*cdf0e10cSrcweir 	::rtl::Reference< jvmaccess::VirtualMachine > xTest = java_lang_Object::getVM();
287*cdf0e10cSrcweir 	if ( xTest.is() )
288*cdf0e10cSrcweir 	{
289*cdf0e10cSrcweir 		SDBThreadAttach t;
290*cdf0e10cSrcweir         clearObject(*t.pEnv);
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 		{
293*cdf0e10cSrcweir 			if ( m_pDriverobject )
294*cdf0e10cSrcweir 				t.pEnv->DeleteGlobalRef( m_pDriverobject );
295*cdf0e10cSrcweir 			m_pDriverobject = NULL;
296*cdf0e10cSrcweir 			if ( m_Driver_theClass )
297*cdf0e10cSrcweir 				t.pEnv->DeleteGlobalRef( m_Driver_theClass );
298*cdf0e10cSrcweir 			m_Driver_theClass = NULL;
299*cdf0e10cSrcweir 		}
300*cdf0e10cSrcweir 		t.releaseRef();
301*cdf0e10cSrcweir 	}
302*cdf0e10cSrcweir }
303*cdf0e10cSrcweir //-----------------------------------------------------------------------------
304*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::release() throw()
305*cdf0e10cSrcweir {
306*cdf0e10cSrcweir 	relase_ChildImpl();
307*cdf0e10cSrcweir }
308*cdf0e10cSrcweir //------------------------------------------------------------------------------
309*cdf0e10cSrcweir void java_sql_Connection::disposing()
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir 	::osl::MutexGuard aGuard(m_aMutex);
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir     m_aLogger.log( LogLevel::INFO, STR_LOG_SHUTDOWN_CONNECTION );
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir     dispose_ChildImpl();
316*cdf0e10cSrcweir 	java_sql_Connection_BASE::disposing();
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir 	if ( object )
319*cdf0e10cSrcweir 	{
320*cdf0e10cSrcweir         static jmethodID mID(NULL);
321*cdf0e10cSrcweir         callVoidMethod("close",mID);
322*cdf0e10cSrcweir 	}
323*cdf0e10cSrcweir }
324*cdf0e10cSrcweir // -------------------------------------------------------------------------
325*cdf0e10cSrcweir jclass java_sql_Connection::getMyClass() const
326*cdf0e10cSrcweir {
327*cdf0e10cSrcweir 	// die Klasse muss nur einmal geholt werden, daher statisch
328*cdf0e10cSrcweir 	if( !theClass )
329*cdf0e10cSrcweir         theClass = findMyClass("java/sql/Connection");
330*cdf0e10cSrcweir 	return theClass;
331*cdf0e10cSrcweir }
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir // -------------------------------------------------------------------------
334*cdf0e10cSrcweir ::rtl::OUString SAL_CALL java_sql_Connection::getCatalog(  ) throw(SQLException, RuntimeException)
335*cdf0e10cSrcweir {
336*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
337*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir     static jmethodID mID(NULL);
340*cdf0e10cSrcweir     return callStringMethod("getCatalog",mID);
341*cdf0e10cSrcweir }
342*cdf0e10cSrcweir // -------------------------------------------------------------------------
343*cdf0e10cSrcweir Reference< XDatabaseMetaData > SAL_CALL java_sql_Connection::getMetaData(  ) throw(SQLException, RuntimeException)
344*cdf0e10cSrcweir {
345*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
346*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir 	Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
350*cdf0e10cSrcweir 	if(!xMetaData.is())
351*cdf0e10cSrcweir 	{
352*cdf0e10cSrcweir         SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
353*cdf0e10cSrcweir         static jmethodID mID(NULL);
354*cdf0e10cSrcweir         jobject out = callObjectMethod(t.pEnv,"getMetaData","()Ljava/sql/DatabaseMetaData;", mID);
355*cdf0e10cSrcweir 		if(out)
356*cdf0e10cSrcweir 		{
357*cdf0e10cSrcweir 			xMetaData = new java_sql_DatabaseMetaData( t.pEnv, out, *this );
358*cdf0e10cSrcweir 			m_xMetaData = xMetaData;
359*cdf0e10cSrcweir 		}
360*cdf0e10cSrcweir 	}
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir 	return xMetaData;
363*cdf0e10cSrcweir }
364*cdf0e10cSrcweir // -------------------------------------------------------------------------
365*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::close(  ) throw(SQLException, RuntimeException)
366*cdf0e10cSrcweir {
367*cdf0e10cSrcweir 	dispose();
368*cdf0e10cSrcweir }
369*cdf0e10cSrcweir // -------------------------------------------------------------------------
370*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::commit(  ) throw(SQLException, RuntimeException)
371*cdf0e10cSrcweir {
372*cdf0e10cSrcweir     static jmethodID mID(NULL);
373*cdf0e10cSrcweir     callVoidMethod("commit",mID);
374*cdf0e10cSrcweir }
375*cdf0e10cSrcweir // -------------------------------------------------------------------------
376*cdf0e10cSrcweir sal_Bool SAL_CALL java_sql_Connection::isClosed(  ) throw(SQLException, RuntimeException)
377*cdf0e10cSrcweir {
378*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir     static jmethodID mID(NULL);
381*cdf0e10cSrcweir     return callBooleanMethod( "isClosed", mID ) && java_sql_Connection_BASE::rBHelper.bDisposed;
382*cdf0e10cSrcweir }
383*cdf0e10cSrcweir // -------------------------------------------------------------------------
384*cdf0e10cSrcweir sal_Bool SAL_CALL java_sql_Connection::isReadOnly(  ) throw(SQLException, RuntimeException)
385*cdf0e10cSrcweir {
386*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
387*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
388*cdf0e10cSrcweir     static jmethodID mID(NULL);
389*cdf0e10cSrcweir     return callBooleanMethod( "isReadOnly", mID );
390*cdf0e10cSrcweir }
391*cdf0e10cSrcweir // -------------------------------------------------------------------------
392*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::setCatalog( const ::rtl::OUString& catalog ) throw(SQLException, RuntimeException)
393*cdf0e10cSrcweir {
394*cdf0e10cSrcweir     static jmethodID mID(NULL);
395*cdf0e10cSrcweir     callVoidMethodWithStringArg("setCatalog",mID,catalog);
396*cdf0e10cSrcweir }
397*cdf0e10cSrcweir // -------------------------------------------------------------------------
398*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::rollback(  ) throw(SQLException, RuntimeException)
399*cdf0e10cSrcweir {
400*cdf0e10cSrcweir     static jmethodID mID(NULL);
401*cdf0e10cSrcweir     callVoidMethod("rollback",mID);
402*cdf0e10cSrcweir }
403*cdf0e10cSrcweir // -------------------------------------------------------------------------
404*cdf0e10cSrcweir sal_Bool SAL_CALL java_sql_Connection::getAutoCommit(  ) throw(SQLException, RuntimeException)
405*cdf0e10cSrcweir {
406*cdf0e10cSrcweir     static jmethodID mID(NULL);
407*cdf0e10cSrcweir     return callBooleanMethod( "getAutoCommit", mID );
408*cdf0e10cSrcweir }
409*cdf0e10cSrcweir // -------------------------------------------------------------------------
410*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::setReadOnly( sal_Bool readOnly ) throw(SQLException, RuntimeException)
411*cdf0e10cSrcweir {
412*cdf0e10cSrcweir     static jmethodID mID(NULL);
413*cdf0e10cSrcweir     callVoidMethodWithBoolArg("setReadOnly",mID,readOnly);
414*cdf0e10cSrcweir }
415*cdf0e10cSrcweir // -------------------------------------------------------------------------
416*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::setAutoCommit( sal_Bool autoCommit ) throw(SQLException, RuntimeException)
417*cdf0e10cSrcweir {
418*cdf0e10cSrcweir     static jmethodID mID(NULL);
419*cdf0e10cSrcweir     callVoidMethodWithBoolArg("setAutoCommit",mID,autoCommit);
420*cdf0e10cSrcweir }
421*cdf0e10cSrcweir // -------------------------------------------------------------------------
422*cdf0e10cSrcweir Reference< ::com::sun::star::container::XNameAccess > SAL_CALL java_sql_Connection::getTypeMap(  ) throw(SQLException, RuntimeException)
423*cdf0e10cSrcweir {
424*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
425*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
428*cdf0e10cSrcweir     static jmethodID mID(NULL);
429*cdf0e10cSrcweir     /*jobject out = */callObjectMethod(t.pEnv,"getTypeMap","()Ljava/util/Map;", mID);
430*cdf0e10cSrcweir 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
431*cdf0e10cSrcweir 	return 0;// ? 0 : Map2XNameAccess( t.pEnv, out );
432*cdf0e10cSrcweir }
433*cdf0e10cSrcweir // -------------------------------------------------------------------------
434*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::setTypeMap( const Reference< ::com::sun::star::container::XNameAccess >& /*typeMap*/ ) throw(SQLException, RuntimeException)
435*cdf0e10cSrcweir {
436*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
437*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir     ::dbtools::throwFeatureNotImplementedException( "XConnection::setTypeMap", *this );
440*cdf0e10cSrcweir }
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir // -------------------------------------------------------------------------
443*cdf0e10cSrcweir sal_Int32 SAL_CALL java_sql_Connection::getTransactionIsolation(  ) throw(SQLException, RuntimeException)
444*cdf0e10cSrcweir {
445*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
446*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir     static jmethodID mID(NULL);
449*cdf0e10cSrcweir 	return callIntMethod("getTransactionIsolation",mID);
450*cdf0e10cSrcweir }
451*cdf0e10cSrcweir // -------------------------------------------------------------------------
452*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::setTransactionIsolation( sal_Int32 level ) throw(SQLException, RuntimeException)
453*cdf0e10cSrcweir {
454*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
455*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir     static jmethodID mID(NULL);
458*cdf0e10cSrcweir     callVoidMethodWithIntArg("setTransactionIsolation",mID,level);
459*cdf0e10cSrcweir }
460*cdf0e10cSrcweir // -------------------------------------------------------------------------
461*cdf0e10cSrcweir Reference< XStatement > SAL_CALL java_sql_Connection::createStatement(  ) throw(SQLException, RuntimeException)
462*cdf0e10cSrcweir {
463*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
464*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
465*cdf0e10cSrcweir     m_aLogger.log( LogLevel::FINE, STR_LOG_CREATE_STATEMENT );
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir 	SDBThreadAttach t;
468*cdf0e10cSrcweir     java_sql_Statement* pStatement = new java_sql_Statement( t.pEnv, *this );
469*cdf0e10cSrcweir 	Reference< XStatement > xStmt = pStatement;
470*cdf0e10cSrcweir 	m_aStatements.push_back( WeakReferenceHelper( xStmt ) );
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir     m_aLogger.log( LogLevel::FINE, STR_LOG_CREATED_STATEMENT_ID, pStatement->getStatementObjectID() );
473*cdf0e10cSrcweir     return xStmt;
474*cdf0e10cSrcweir }
475*cdf0e10cSrcweir // -----------------------------------------------------------------------------
476*cdf0e10cSrcweir ::rtl::OUString java_sql_Connection::transFormPreparedStatement(const ::rtl::OUString& _sSQL)
477*cdf0e10cSrcweir {
478*cdf0e10cSrcweir 	::rtl::OUString sSqlStatement = _sSQL;
479*cdf0e10cSrcweir 	if ( m_bParameterSubstitution )
480*cdf0e10cSrcweir 	{
481*cdf0e10cSrcweir 		try
482*cdf0e10cSrcweir 		{
483*cdf0e10cSrcweir 			OSQLParser aParser( m_pDriver->getContext().getLegacyServiceFactory() );
484*cdf0e10cSrcweir 			::rtl::OUString sErrorMessage;
485*cdf0e10cSrcweir 			::rtl::OUString sNewSql;
486*cdf0e10cSrcweir 			OSQLParseNode* pNode = aParser.parseTree(sErrorMessage,_sSQL);
487*cdf0e10cSrcweir 			if(pNode)
488*cdf0e10cSrcweir 			{	// special handling for parameters
489*cdf0e10cSrcweir 				OSQLParseNode::substituteParameterNames(pNode);
490*cdf0e10cSrcweir 				pNode->parseNodeToStr( sNewSql, this );
491*cdf0e10cSrcweir 				delete pNode;
492*cdf0e10cSrcweir 				sSqlStatement = sNewSql;
493*cdf0e10cSrcweir 			}
494*cdf0e10cSrcweir 		}
495*cdf0e10cSrcweir 		catch(const Exception&)
496*cdf0e10cSrcweir 		{
497*cdf0e10cSrcweir 		}
498*cdf0e10cSrcweir 	}
499*cdf0e10cSrcweir 	return sSqlStatement;
500*cdf0e10cSrcweir }
501*cdf0e10cSrcweir // -------------------------------------------------------------------------
502*cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL java_sql_Connection::prepareStatement( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
503*cdf0e10cSrcweir {
504*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
505*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
506*cdf0e10cSrcweir     m_aLogger.log( LogLevel::FINE, STR_LOG_PREPARE_STATEMENT, sql );
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir     SDBThreadAttach t;
509*cdf0e10cSrcweir 	::rtl::OUString sSqlStatement = sql;
510*cdf0e10cSrcweir 	sSqlStatement = transFormPreparedStatement( sSqlStatement );
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir     java_sql_PreparedStatement* pStatement = new java_sql_PreparedStatement( t.pEnv, *this, sSqlStatement );
513*cdf0e10cSrcweir 	Reference< XPreparedStatement > xReturn( pStatement );
514*cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xReturn));
515*cdf0e10cSrcweir 
516*cdf0e10cSrcweir     m_aLogger.log( LogLevel::FINE, STR_LOG_PREPARED_STATEMENT_ID, pStatement->getStatementObjectID() );
517*cdf0e10cSrcweir 	return xReturn;
518*cdf0e10cSrcweir }
519*cdf0e10cSrcweir // -------------------------------------------------------------------------
520*cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL java_sql_Connection::prepareCall( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
521*cdf0e10cSrcweir {
522*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
523*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
524*cdf0e10cSrcweir     m_aLogger.log( LogLevel::FINE, STR_LOG_PREPARE_CALL, sql );
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir 	SDBThreadAttach t;
527*cdf0e10cSrcweir 	::rtl::OUString sSqlStatement = sql;
528*cdf0e10cSrcweir 	sSqlStatement = transFormPreparedStatement( sSqlStatement );
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir     java_sql_CallableStatement* pStatement = new java_sql_CallableStatement( t.pEnv, *this, sSqlStatement );
531*cdf0e10cSrcweir 	Reference< XPreparedStatement > xStmt( pStatement );
532*cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xStmt));
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir     m_aLogger.log( LogLevel::FINE, STR_LOG_PREPARED_CALL_ID, pStatement->getStatementObjectID() );
535*cdf0e10cSrcweir     return xStmt;
536*cdf0e10cSrcweir }
537*cdf0e10cSrcweir // -------------------------------------------------------------------------
538*cdf0e10cSrcweir ::rtl::OUString SAL_CALL java_sql_Connection::nativeSQL( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
539*cdf0e10cSrcweir {
540*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
541*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir 	::rtl::OUString aStr;
544*cdf0e10cSrcweir     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
545*cdf0e10cSrcweir 	{
546*cdf0e10cSrcweir 
547*cdf0e10cSrcweir 		// temporaere Variable initialisieren
548*cdf0e10cSrcweir 		static const char * cSignature = "(Ljava/lang/String;)Ljava/lang/String;";
549*cdf0e10cSrcweir 		static const char * cMethodName = "nativeSQL";
550*cdf0e10cSrcweir 		// Java-Call absetzen
551*cdf0e10cSrcweir 		static jmethodID mID(NULL);
552*cdf0e10cSrcweir         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
553*cdf0e10cSrcweir 		// Parameter konvertieren
554*cdf0e10cSrcweir 		jdbc::LocalRef< jstring > str( t.env(),convertwchar_tToJavaString(t.pEnv,sql));
555*cdf0e10cSrcweir 
556*cdf0e10cSrcweir 		jobject out = t.pEnv->CallObjectMethod( object, mID, str.get() );
557*cdf0e10cSrcweir 		aStr = JavaString2String(t.pEnv, (jstring)out );
558*cdf0e10cSrcweir 		ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
559*cdf0e10cSrcweir 	} //t.pEnv
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir     m_aLogger.log( LogLevel::FINER, STR_LOG_NATIVE_SQL, sql, aStr );
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir 	return aStr;
564*cdf0e10cSrcweir }
565*cdf0e10cSrcweir // -------------------------------------------------------------------------
566*cdf0e10cSrcweir void SAL_CALL java_sql_Connection::clearWarnings(  ) throw(SQLException, RuntimeException)
567*cdf0e10cSrcweir {
568*cdf0e10cSrcweir     static jmethodID mID(NULL);
569*cdf0e10cSrcweir     callVoidMethod("clearWarnings",mID);
570*cdf0e10cSrcweir }
571*cdf0e10cSrcweir // -------------------------------------------------------------------------
572*cdf0e10cSrcweir Any SAL_CALL java_sql_Connection::getWarnings(  ) throw(SQLException, RuntimeException)
573*cdf0e10cSrcweir {
574*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
575*cdf0e10cSrcweir 	checkDisposed(java_sql_Connection_BASE::rBHelper.bDisposed);
576*cdf0e10cSrcweir 
577*cdf0e10cSrcweir 	SDBThreadAttach t;
578*cdf0e10cSrcweir     static jmethodID mID(NULL);
579*cdf0e10cSrcweir     jobject out = callObjectMethod(t.pEnv,"getWarnings","()Ljava/sql/SQLWarning;", mID);
580*cdf0e10cSrcweir 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
581*cdf0e10cSrcweir 	if( out )
582*cdf0e10cSrcweir 	{
583*cdf0e10cSrcweir 		java_sql_SQLWarning_BASE		warn_base(t.pEnv, out);
584*cdf0e10cSrcweir         SQLException aAsException( static_cast< starsdbc::SQLException >( java_sql_SQLWarning( warn_base, *this ) ) );
585*cdf0e10cSrcweir 
586*cdf0e10cSrcweir         // translate to warning
587*cdf0e10cSrcweir         SQLWarning aWarning;
588*cdf0e10cSrcweir         aWarning.Context = aAsException.Context;
589*cdf0e10cSrcweir         aWarning.Message = aAsException.Message;
590*cdf0e10cSrcweir         aWarning.SQLState = aAsException.SQLState;
591*cdf0e10cSrcweir         aWarning.ErrorCode = aAsException.ErrorCode;
592*cdf0e10cSrcweir         aWarning.NextException = aAsException.NextException;
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir 		return makeAny( aWarning );
595*cdf0e10cSrcweir 	}
596*cdf0e10cSrcweir 
597*cdf0e10cSrcweir 	return Any();
598*cdf0e10cSrcweir }
599*cdf0e10cSrcweir 
600*cdf0e10cSrcweir // -----------------------------------------------------------------------------
601*cdf0e10cSrcweir namespace
602*cdf0e10cSrcweir {
603*cdf0e10cSrcweir     ::rtl::OUString lcl_getDriverLoadErrorMessage( const ::connectivity::SharedResources& _aResource,const ::rtl::OUString& _rDriverClass, const ::rtl::OUString& _rDriverClassPath )
604*cdf0e10cSrcweir     {
605*cdf0e10cSrcweir         ::rtl::OUString sError1( _aResource.getResourceStringWithSubstitution(
606*cdf0e10cSrcweir                 STR_NO_CLASSNAME,
607*cdf0e10cSrcweir                 "$classname$", _rDriverClass
608*cdf0e10cSrcweir              ) );
609*cdf0e10cSrcweir         if ( _rDriverClassPath.getLength() )
610*cdf0e10cSrcweir         {
611*cdf0e10cSrcweir             const ::rtl::OUString sError2( _aResource.getResourceStringWithSubstitution(
612*cdf0e10cSrcweir                 STR_NO_CLASSNAME_PATH,
613*cdf0e10cSrcweir                 "$classpath$", _rDriverClassPath
614*cdf0e10cSrcweir              ) );
615*cdf0e10cSrcweir             sError1 += sError2;
616*cdf0e10cSrcweir         } // if ( _rDriverClassPath.getLength() )
617*cdf0e10cSrcweir         return sError1;
618*cdf0e10cSrcweir     }
619*cdf0e10cSrcweir }
620*cdf0e10cSrcweir 
621*cdf0e10cSrcweir // -----------------------------------------------------------------------------
622*cdf0e10cSrcweir namespace
623*cdf0e10cSrcweir {
624*cdf0e10cSrcweir     bool lcl_setSystemProperties_nothrow( const java::sql::ConnectionLog& _rLogger,
625*cdf0e10cSrcweir         JNIEnv& _rEnv, const Sequence< NamedValue >& _rSystemProperties )
626*cdf0e10cSrcweir     {
627*cdf0e10cSrcweir         if ( _rSystemProperties.getLength() == 0 )
628*cdf0e10cSrcweir             // nothing to do
629*cdf0e10cSrcweir             return true;
630*cdf0e10cSrcweir 
631*cdf0e10cSrcweir         LocalRef< jclass > systemClass( _rEnv );
632*cdf0e10cSrcweir         jmethodID nSetPropertyMethodID = 0;
633*cdf0e10cSrcweir         // retrieve the java.lang.System class
634*cdf0e10cSrcweir         systemClass.set( _rEnv.FindClass( "java/lang/System" ) );
635*cdf0e10cSrcweir         if ( systemClass.is() )
636*cdf0e10cSrcweir         {
637*cdf0e10cSrcweir             nSetPropertyMethodID = _rEnv.GetStaticMethodID(
638*cdf0e10cSrcweir                 systemClass.get(), "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;" );
639*cdf0e10cSrcweir         }
640*cdf0e10cSrcweir 
641*cdf0e10cSrcweir         if ( nSetPropertyMethodID == 0 )
642*cdf0e10cSrcweir             return false;
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir         for (   const NamedValue* pSystemProp = _rSystemProperties.getConstArray();
645*cdf0e10cSrcweir                 pSystemProp != _rSystemProperties.getConstArray() + _rSystemProperties.getLength();
646*cdf0e10cSrcweir                 ++pSystemProp
647*cdf0e10cSrcweir             )
648*cdf0e10cSrcweir         {
649*cdf0e10cSrcweir             ::rtl::OUString sValue;
650*cdf0e10cSrcweir             OSL_VERIFY( pSystemProp->Value >>= sValue );
651*cdf0e10cSrcweir 
652*cdf0e10cSrcweir             _rLogger.log( LogLevel::FINER, STR_LOG_SETTING_SYSTEM_PROPERTY, pSystemProp->Name, sValue );
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir             LocalRef< jstring > jName( _rEnv, convertwchar_tToJavaString( &_rEnv, pSystemProp->Name ) );
655*cdf0e10cSrcweir             LocalRef< jstring > jValue( _rEnv, convertwchar_tToJavaString( &_rEnv, sValue ) );
656*cdf0e10cSrcweir 
657*cdf0e10cSrcweir             _rEnv.CallStaticObjectMethod( systemClass.get(), nSetPropertyMethodID, jName.get(), jValue.get() );
658*cdf0e10cSrcweir             LocalRef< jthrowable > throwable( _rEnv, _rEnv.ExceptionOccurred() );
659*cdf0e10cSrcweir             if ( throwable.is() )
660*cdf0e10cSrcweir                 return false;
661*cdf0e10cSrcweir         }
662*cdf0e10cSrcweir 
663*cdf0e10cSrcweir         return true;
664*cdf0e10cSrcweir     }
665*cdf0e10cSrcweir }
666*cdf0e10cSrcweir 
667*cdf0e10cSrcweir // -----------------------------------------------------------------------------
668*cdf0e10cSrcweir void java_sql_Connection::loadDriverFromProperties( const ::rtl::OUString& _sDriverClass, const ::rtl::OUString& _sDriverClassPath,
669*cdf0e10cSrcweir     const Sequence< NamedValue >& _rSystemProperties )
670*cdf0e10cSrcweir {
671*cdf0e10cSrcweir     // contains the statement which should be used when query for automatically generated values
672*cdf0e10cSrcweir 	::rtl::OUString		sGeneratedValueStatement;
673*cdf0e10cSrcweir     // set to <TRUE/> when we should allow to query for generated values
674*cdf0e10cSrcweir 	sal_Bool			bAutoRetrievingEnabled = sal_False;
675*cdf0e10cSrcweir 
676*cdf0e10cSrcweir     // first try if the jdbc driver is alraedy registered at the driver manager
677*cdf0e10cSrcweir     SDBThreadAttach t;
678*cdf0e10cSrcweir 	try
679*cdf0e10cSrcweir 	{
680*cdf0e10cSrcweir         if ( !object )
681*cdf0e10cSrcweir         {
682*cdf0e10cSrcweir             if ( !lcl_setSystemProperties_nothrow( getLogger(), *t.pEnv, _rSystemProperties ) )
683*cdf0e10cSrcweir                 ThrowLoggedSQLException( getLogger(), t.pEnv, *this );
684*cdf0e10cSrcweir 
685*cdf0e10cSrcweir             m_pDriverClassLoader.reset();
686*cdf0e10cSrcweir 
687*cdf0e10cSrcweir             // here I try to find the class for jdbc driver
688*cdf0e10cSrcweir             java_sql_SQLException_BASE::st_getMyClass();
689*cdf0e10cSrcweir             java_lang_Throwable::st_getMyClass();
690*cdf0e10cSrcweir 
691*cdf0e10cSrcweir             if ( !_sDriverClass.getLength() )
692*cdf0e10cSrcweir             {
693*cdf0e10cSrcweir                 m_aLogger.log( LogLevel::SEVERE, STR_LOG_NO_DRIVER_CLASS );
694*cdf0e10cSrcweir                 ::dbtools::throwGenericSQLException(
695*cdf0e10cSrcweir                     lcl_getDriverLoadErrorMessage( getResources(),_sDriverClass, _sDriverClassPath ),
696*cdf0e10cSrcweir                     *this
697*cdf0e10cSrcweir                 );
698*cdf0e10cSrcweir             }
699*cdf0e10cSrcweir             else
700*cdf0e10cSrcweir             {
701*cdf0e10cSrcweir                 m_aLogger.log( LogLevel::INFO, STR_LOG_LOADING_DRIVER, _sDriverClass );
702*cdf0e10cSrcweir                 // the driver manager holds the class of the driver for later use
703*cdf0e10cSrcweir                 ::std::auto_ptr< java_lang_Class > pDrvClass;
704*cdf0e10cSrcweir                 if ( !_sDriverClassPath.getLength() )
705*cdf0e10cSrcweir                 {
706*cdf0e10cSrcweir 					// if forName didn't find the class it will throw an exception
707*cdf0e10cSrcweir 					pDrvClass = ::std::auto_ptr< java_lang_Class >(java_lang_Class::forName(_sDriverClass));
708*cdf0e10cSrcweir                 }
709*cdf0e10cSrcweir                 else
710*cdf0e10cSrcweir                 {
711*cdf0e10cSrcweir                     LocalRef< jclass > driverClass(t.env());
712*cdf0e10cSrcweir                     LocalRef< jobject > driverClassLoader(t.env());
713*cdf0e10cSrcweir 
714*cdf0e10cSrcweir                     loadClass(
715*cdf0e10cSrcweir                         m_pDriver->getContext().getUNOContext(),
716*cdf0e10cSrcweir                         t.env(), _sDriverClassPath, _sDriverClass, &driverClassLoader, &driverClass );
717*cdf0e10cSrcweir 
718*cdf0e10cSrcweir                     m_pDriverClassLoader.set( driverClassLoader );
719*cdf0e10cSrcweir                     pDrvClass.reset( new java_lang_Class( t.pEnv, driverClass.release() ) );
720*cdf0e10cSrcweir 
721*cdf0e10cSrcweir                     ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
722*cdf0e10cSrcweir                 }
723*cdf0e10cSrcweir                 if ( pDrvClass.get() )
724*cdf0e10cSrcweir                 {
725*cdf0e10cSrcweir                     LocalRef< jobject > driverObject( t.env() );
726*cdf0e10cSrcweir                     driverObject.set( pDrvClass->newInstanceObject() );
727*cdf0e10cSrcweir                     ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
728*cdf0e10cSrcweir                     m_pDriverobject = driverObject.release();
729*cdf0e10cSrcweir 
730*cdf0e10cSrcweir                     if( t.pEnv && m_pDriverobject )
731*cdf0e10cSrcweir                         m_pDriverobject = t.pEnv->NewGlobalRef( m_pDriverobject );
732*cdf0e10cSrcweir 
733*cdf0e10cSrcweir                     {
734*cdf0e10cSrcweir                         jclass tempClass = t.pEnv->GetObjectClass(m_pDriverobject);
735*cdf0e10cSrcweir                         if ( m_pDriverobject )
736*cdf0e10cSrcweir                         {
737*cdf0e10cSrcweir                             m_Driver_theClass = (jclass)t.pEnv->NewGlobalRef( tempClass );
738*cdf0e10cSrcweir                             t.pEnv->DeleteLocalRef( tempClass );
739*cdf0e10cSrcweir                         }
740*cdf0e10cSrcweir                     }
741*cdf0e10cSrcweir                 }
742*cdf0e10cSrcweir                 m_aLogger.log( LogLevel::INFO, STR_LOG_CONN_SUCCESS );
743*cdf0e10cSrcweir             }
744*cdf0e10cSrcweir         }
745*cdf0e10cSrcweir 	}
746*cdf0e10cSrcweir     catch( const SQLException& e )
747*cdf0e10cSrcweir 	{
748*cdf0e10cSrcweir 		throw SQLException(
749*cdf0e10cSrcweir             lcl_getDriverLoadErrorMessage( getResources(),_sDriverClass, _sDriverClassPath ),
750*cdf0e10cSrcweir             *this,
751*cdf0e10cSrcweir             ::rtl::OUString(),
752*cdf0e10cSrcweir             1000,
753*cdf0e10cSrcweir             makeAny(e)
754*cdf0e10cSrcweir         );
755*cdf0e10cSrcweir 	}
756*cdf0e10cSrcweir 	catch( Exception& )
757*cdf0e10cSrcweir 	{
758*cdf0e10cSrcweir 		::dbtools::throwGenericSQLException(
759*cdf0e10cSrcweir             lcl_getDriverLoadErrorMessage( getResources(),_sDriverClass, _sDriverClassPath ),
760*cdf0e10cSrcweir             *this
761*cdf0e10cSrcweir         );
762*cdf0e10cSrcweir 	}
763*cdf0e10cSrcweir 
764*cdf0e10cSrcweir 	enableAutoRetrievingEnabled( bAutoRetrievingEnabled );
765*cdf0e10cSrcweir 	setAutoRetrievingStatement( sGeneratedValueStatement );
766*cdf0e10cSrcweir }
767*cdf0e10cSrcweir // -----------------------------------------------------------------------------
768*cdf0e10cSrcweir ::rtl::OUString java_sql_Connection::impl_getJavaDriverClassPath_nothrow(const ::rtl::OUString& _sDriverClass)
769*cdf0e10cSrcweir {
770*cdf0e10cSrcweir     static const ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.Office.DataAccess/JDBC/DriverClassPaths"));
771*cdf0e10cSrcweir     ::utl::OConfigurationTreeRoot aNamesRoot = ::utl::OConfigurationTreeRoot::createWithServiceFactory(
772*cdf0e10cSrcweir         m_pDriver->getContext().getLegacyServiceFactory(), s_sNodeName, -1, ::utl::OConfigurationTreeRoot::CM_READONLY);
773*cdf0e10cSrcweir     ::rtl::OUString sURL;
774*cdf0e10cSrcweir     if ( aNamesRoot.isValid() && aNamesRoot.hasByName( _sDriverClass ) )
775*cdf0e10cSrcweir     {
776*cdf0e10cSrcweir         ::utl::OConfigurationNode aRegisterObj = aNamesRoot.openNode( _sDriverClass );
777*cdf0e10cSrcweir         OSL_VERIFY( aRegisterObj.getNodeValue( "Path" ) >>= sURL );
778*cdf0e10cSrcweir     }
779*cdf0e10cSrcweir     return sURL;
780*cdf0e10cSrcweir }
781*cdf0e10cSrcweir // -----------------------------------------------------------------------------
782*cdf0e10cSrcweir sal_Bool java_sql_Connection::construct(const ::rtl::OUString& url,
783*cdf0e10cSrcweir 									const Sequence< PropertyValue >& info)
784*cdf0e10cSrcweir {
785*cdf0e10cSrcweir 	{ // initialize the java vm
786*cdf0e10cSrcweir 		::rtl::Reference< jvmaccess::VirtualMachine > xTest = java_lang_Object::getVM(getORB());
787*cdf0e10cSrcweir 		if ( !xTest.is() )
788*cdf0e10cSrcweir             throwGenericSQLException(STR_NO_JAVA,*this);
789*cdf0e10cSrcweir 	}
790*cdf0e10cSrcweir 	SDBThreadAttach t;
791*cdf0e10cSrcweir 	t.addRef();		 // will be released in dtor
792*cdf0e10cSrcweir 	if ( !t.pEnv )
793*cdf0e10cSrcweir 		throwGenericSQLException(STR_NO_JAVA,*this);
794*cdf0e10cSrcweir 
795*cdf0e10cSrcweir 	::rtl::OUString		sGeneratedValueStatement; // contains the statement which should be used when query for automatically generated values
796*cdf0e10cSrcweir 	sal_Bool			bAutoRetrievingEnabled = sal_False; // set to <TRUE/> when we should allow to query for generated values
797*cdf0e10cSrcweir     ::rtl::OUString sDriverClassPath,sDriverClass;
798*cdf0e10cSrcweir     Sequence< NamedValue > aSystemProperties;
799*cdf0e10cSrcweir 
800*cdf0e10cSrcweir     ::comphelper::NamedValueCollection aSettings( info );
801*cdf0e10cSrcweir     sDriverClass = aSettings.getOrDefault( "JavaDriverClass", sDriverClass );
802*cdf0e10cSrcweir     sDriverClassPath = aSettings.getOrDefault( "JavaDriverClassPath", sDriverClassPath);
803*cdf0e10cSrcweir     if ( !sDriverClassPath.getLength() )
804*cdf0e10cSrcweir         sDriverClassPath = impl_getJavaDriverClassPath_nothrow(sDriverClass);
805*cdf0e10cSrcweir     bAutoRetrievingEnabled = aSettings.getOrDefault( "IsAutoRetrievingEnabled", bAutoRetrievingEnabled );
806*cdf0e10cSrcweir     sGeneratedValueStatement = aSettings.getOrDefault( "AutoRetrievingStatement", sGeneratedValueStatement );
807*cdf0e10cSrcweir     m_bParameterSubstitution = aSettings.getOrDefault( "ParameterNameSubstitution", m_bParameterSubstitution );
808*cdf0e10cSrcweir     m_bIgnoreDriverPrivileges = aSettings.getOrDefault( "IgnoreDriverPrivileges", m_bIgnoreDriverPrivileges );
809*cdf0e10cSrcweir     m_bIgnoreCurrency = aSettings.getOrDefault( "IgnoreCurrency", m_bIgnoreCurrency );
810*cdf0e10cSrcweir     aSystemProperties = aSettings.getOrDefault( "SystemProperties", aSystemProperties );
811*cdf0e10cSrcweir     m_aCatalogRestriction = aSettings.getOrDefault( "ImplicitCatalogRestriction", Any() );
812*cdf0e10cSrcweir     m_aSchemaRestriction = aSettings.getOrDefault( "ImplicitSchemaRestriction", Any() );
813*cdf0e10cSrcweir 
814*cdf0e10cSrcweir     loadDriverFromProperties( sDriverClass, sDriverClassPath, aSystemProperties );
815*cdf0e10cSrcweir 
816*cdf0e10cSrcweir 	enableAutoRetrievingEnabled(bAutoRetrievingEnabled);
817*cdf0e10cSrcweir 	setAutoRetrievingStatement(sGeneratedValueStatement);
818*cdf0e10cSrcweir 
819*cdf0e10cSrcweir 	if ( t.pEnv && m_Driver_theClass && m_pDriverobject )
820*cdf0e10cSrcweir 	{
821*cdf0e10cSrcweir 		// temporaere Variable initialisieren
822*cdf0e10cSrcweir 		static const char * cSignature = "(Ljava/lang/String;Ljava/util/Properties;)Ljava/sql/Connection;";
823*cdf0e10cSrcweir 		static const char * cMethodName = "connect";
824*cdf0e10cSrcweir 		// Java-Call absetzen
825*cdf0e10cSrcweir 		jmethodID mID = NULL;
826*cdf0e10cSrcweir         if ( !mID  )
827*cdf0e10cSrcweir             mID  = t.pEnv->GetMethodID( m_Driver_theClass, cMethodName, cSignature );
828*cdf0e10cSrcweir         if ( mID )
829*cdf0e10cSrcweir 		{
830*cdf0e10cSrcweir 			jvalue args[2];
831*cdf0e10cSrcweir 			// Parameter konvertieren
832*cdf0e10cSrcweir 			args[0].l = convertwchar_tToJavaString(t.pEnv,url);
833*cdf0e10cSrcweir 			java_util_Properties* pProps = createStringPropertyArray(info);
834*cdf0e10cSrcweir 			args[1].l = pProps->getJavaObject();
835*cdf0e10cSrcweir 
836*cdf0e10cSrcweir             LocalRef< jobject > ensureDelete( t.env(), args[0].l );
837*cdf0e10cSrcweir 
838*cdf0e10cSrcweir             jobject out = NULL;
839*cdf0e10cSrcweir             // In some cases (e.g.,
840*cdf0e10cSrcweir             // connectivity/source/drivers/hsqldb/HDriver.cxx:1.24
841*cdf0e10cSrcweir             // l. 249) the JavaDriverClassPath contains multiple jars,
842*cdf0e10cSrcweir             // as creating the JavaDriverClass instance requires
843*cdf0e10cSrcweir             // (reflective) access to those other jars.  Now, if the
844*cdf0e10cSrcweir             // JavaDriverClass is actually loaded by some parent class
845*cdf0e10cSrcweir             // loader (e.g., because its jar is also on the global
846*cdf0e10cSrcweir             // class path), it would still not have access to the
847*cdf0e10cSrcweir             // additional jars on the JavaDriverClassPath.  Hence, the
848*cdf0e10cSrcweir             // JavaDriverClassPath class loader is pushed as context
849*cdf0e10cSrcweir             // class loader around the JavaDriverClass instance
850*cdf0e10cSrcweir             // creation:
851*cdf0e10cSrcweir             // #i82222# / 2007-10-15
852*cdf0e10cSrcweir             {
853*cdf0e10cSrcweir                 ContextClassLoaderScope ccl( t.env(), getDriverClassLoader(), getLogger(), *this );
854*cdf0e10cSrcweir                 out = t.pEnv->CallObjectMethod( m_pDriverobject, mID, args[0].l,args[1].l );
855*cdf0e10cSrcweir                 delete pProps, pProps = NULL;
856*cdf0e10cSrcweir                 ThrowLoggedSQLException( m_aLogger, t.pEnv, *this );
857*cdf0e10cSrcweir             }
858*cdf0e10cSrcweir 
859*cdf0e10cSrcweir             if ( !out )
860*cdf0e10cSrcweir                 m_aLogger.log( LogLevel::SEVERE, STR_LOG_NO_SYSTEM_CONNECTION );
861*cdf0e10cSrcweir 
862*cdf0e10cSrcweir 			if ( out )
863*cdf0e10cSrcweir 				object = t.pEnv->NewGlobalRef( out );
864*cdf0e10cSrcweir 
865*cdf0e10cSrcweir             if ( object )
866*cdf0e10cSrcweir                 m_aLogger.log( LogLevel::INFO, STR_LOG_GOT_JDBC_CONNECTION, url );
867*cdf0e10cSrcweir 
868*cdf0e10cSrcweir             m_aConnectionInfo = info;
869*cdf0e10cSrcweir 		} //mID
870*cdf0e10cSrcweir 	} //t.pEnv
871*cdf0e10cSrcweir 	 return object != NULL;
872*cdf0e10cSrcweir }
873*cdf0e10cSrcweir // -----------------------------------------------------------------------------
874