Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / connectivity / source / drivers / jdbc / Object.cxx
blobf05eced9df9a7e41ba0884e0316beb21fb94d826
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <connectivity/CommonTools.hxx>
21 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
22 #include <com/sun/star/logging/LogLevel.hpp>
23 #include <java/tools.hxx>
24 #include <java/sql/SQLException.hxx>
25 #include <osl/diagnose.h>
26 #include <java/LocalRef.hxx>
27 #include <strings.hxx>
29 #include <comphelper/logging.hxx>
30 #include <cppuhelper/exc_hlp.hxx>
32 using namespace connectivity;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::beans;
35 using namespace ::com::sun::star::sdbc;
36 using namespace ::com::sun::star::container;
37 using namespace ::com::sun::star::lang;
40 static ::rtl::Reference< jvmaccess::VirtualMachine > const & getJavaVM2(const ::rtl::Reference< jvmaccess::VirtualMachine >& _rVM = ::rtl::Reference< jvmaccess::VirtualMachine >(),
41 bool _bSet = false)
43 static ::rtl::Reference< jvmaccess::VirtualMachine > s_VM;
44 if ( _rVM.is() || _bSet )
45 s_VM = _rVM;
46 return s_VM;
49 ::rtl::Reference< jvmaccess::VirtualMachine > java_lang_Object::getVM(const Reference<XComponentContext >& _rxContext)
51 ::rtl::Reference< jvmaccess::VirtualMachine > xVM = getJavaVM2();
52 if ( !xVM.is() && _rxContext.is() )
53 xVM = getJavaVM2(::connectivity::getJavaVM(_rxContext));
55 return xVM;
58 SDBThreadAttach::SDBThreadAttach()
59 : m_aGuard(java_lang_Object::getVM())
60 , pEnv(nullptr)
62 pEnv = m_aGuard.getEnvironment();
63 OSL_ENSURE(pEnv,"Environment is nULL!");
66 SDBThreadAttach::~SDBThreadAttach()
70 static oslInterlockedCount& getJavaVMRefCount()
72 static oslInterlockedCount s_nRefCount = 0;
73 return s_nRefCount;
76 void SDBThreadAttach::addRef()
78 osl_atomic_increment(&getJavaVMRefCount());
81 void SDBThreadAttach::releaseRef()
83 osl_atomic_decrement(&getJavaVMRefCount());
84 if ( getJavaVMRefCount() == 0 )
86 getJavaVM2(::rtl::Reference< jvmaccess::VirtualMachine >(),true);
90 // static variables of the class
91 jclass java_lang_Object::theClass = nullptr;
93 jclass java_lang_Object::getMyClass() const
95 if( !theClass )
96 theClass = findMyClass("java/lang/Object");
97 return theClass;
99 // the actual constructor
100 java_lang_Object::java_lang_Object()
101 : object( nullptr )
103 SDBThreadAttach::addRef();
106 // the protected-constructor for the derived classes
107 java_lang_Object::java_lang_Object( JNIEnv * pXEnv, jobject myObj )
108 : object( nullptr )
110 SDBThreadAttach::addRef();
111 if( pXEnv && myObj )
112 object = pXEnv->NewGlobalRef( myObj );
115 java_lang_Object::~java_lang_Object() COVERITY_NOEXCEPT_FALSE
117 if( object )
119 SDBThreadAttach t;
120 clearObject(*t.pEnv);
122 SDBThreadAttach::releaseRef();
124 void java_lang_Object::clearObject(JNIEnv& rEnv)
126 if( object )
128 rEnv.DeleteGlobalRef( object );
129 object = nullptr;
133 void java_lang_Object::clearObject()
135 if( object )
137 SDBThreadAttach t;
138 clearObject(*t.pEnv);
141 // the protected-constructor for the derived classes
142 void java_lang_Object::saveRef( JNIEnv * pXEnv, jobject myObj )
144 OSL_ENSURE( myObj, "object in c++ -> Java Wrapper" );
145 if( myObj )
146 object = pXEnv->NewGlobalRef( myObj );
150 OUString java_lang_Object::toString() const
152 static jmethodID mID(nullptr);
153 return callStringMethod("toString",mID);
157 namespace
159 bool lcl_translateJNIExceptionToUNOException(
160 JNIEnv* _pEnvironment, const Reference< XInterface >& _rxContext, SQLException& _out_rException )
162 jthrowable jThrow = _pEnvironment ? _pEnvironment->ExceptionOccurred() : nullptr;
163 if ( !jThrow )
164 return false;
166 _pEnvironment->ExceptionClear();
167 // we have to clear the exception here because we want to handle it itself
169 if ( _pEnvironment->IsInstanceOf( jThrow, java_sql_SQLException_BASE::st_getMyClass() ) )
171 java_sql_SQLException_BASE aException( _pEnvironment, jThrow );
172 _out_rException = SQLException( aException.getMessage(), _rxContext,
173 aException.getSQLState(), aException.getErrorCode(), Any() );
174 return true;
176 else if ( _pEnvironment->IsInstanceOf( jThrow, java_lang_Throwable::st_getMyClass() ) )
178 java_lang_Throwable aThrow( _pEnvironment, jThrow );
179 #if OSL_DEBUG_LEVEL > 0
180 aThrow.printStackTrace();
181 #endif
182 OUString sMessage = aThrow.getMessage();
183 if ( sMessage.isEmpty() )
184 sMessage = aThrow.getLocalizedMessage();
185 if( sMessage.isEmpty() )
186 sMessage = aThrow.toString();
187 _out_rException = SQLException( sMessage, _rxContext, OUString(), -1, Any() );
188 return true;
190 else
191 _pEnvironment->DeleteLocalRef( jThrow );
192 return false;
197 void java_lang_Object::ThrowLoggedSQLException( const ::comphelper::EventLogger& _rLogger, JNIEnv* _pEnvironment,
198 const Reference< XInterface >& _rxContext )
200 SQLException aException;
201 if ( lcl_translateJNIExceptionToUNOException( _pEnvironment, _rxContext, aException ) )
203 _rLogger.log( css::logging::LogLevel::SEVERE, STR_LOG_THROWING_EXCEPTION, aException.Message, aException.SQLState, aException.ErrorCode );
204 throw aException;
208 void java_lang_Object::ThrowSQLException( JNIEnv* _pEnvironment, const Reference< XInterface>& _rxContext )
210 SQLException aException;
211 if ( lcl_translateJNIExceptionToUNOException( _pEnvironment, _rxContext, aException ) )
212 throw aException;
215 void java_lang_Object::ThrowRuntimeException( JNIEnv* _pEnvironment, const Reference< XInterface>& _rxContext )
219 ThrowSQLException(_pEnvironment, _rxContext);
221 catch (const SQLException& e)
223 css::uno::Any anyEx = cppu::getCaughtException();
224 throw css::lang::WrappedTargetRuntimeException( e.Message,
225 e.Context, anyEx );
229 void java_lang_Object::obtainMethodId_throwSQL(JNIEnv* _pEnv,const char* _pMethodName, const char* _pSignature,jmethodID& _inout_MethodID) const
231 if ( !_inout_MethodID )
233 _inout_MethodID = _pEnv->GetMethodID( getMyClass(), _pMethodName, _pSignature );
234 OSL_ENSURE( _inout_MethodID, _pSignature );
235 if ( !_inout_MethodID )
236 throw SQLException();
237 } // if ( !_inout_MethodID )
240 void java_lang_Object::obtainMethodId_throwRuntime(JNIEnv* _pEnv,const char* _pMethodName, const char* _pSignature,jmethodID& _inout_MethodID) const
242 if ( !_inout_MethodID )
244 _inout_MethodID = _pEnv->GetMethodID( getMyClass(), _pMethodName, _pSignature );
245 OSL_ENSURE( _inout_MethodID, _pSignature );
246 if ( !_inout_MethodID )
247 throw RuntimeException();
248 } // if ( !_inout_MethodID )
252 bool java_lang_Object::callBooleanMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const
254 bool out( false );
256 SDBThreadAttach t;
257 OSL_ENSURE( t.pEnv, "java_lang_Object::callBooleanMethod: no Java environment anymore!" );
258 obtainMethodId_throwSQL(t.pEnv, _pMethodName,"()Z", _inout_MethodID);
259 // call method
260 out = t.pEnv->CallBooleanMethod( object, _inout_MethodID );
261 ThrowSQLException( t.pEnv, nullptr );
263 return out;
266 bool java_lang_Object::callBooleanMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const
268 bool out( false );
269 SDBThreadAttach t;
270 OSL_ENSURE( t.pEnv, "java_lang_Object::callBooleanMethodWithIntArg: no Java environment anymore!" );
271 obtainMethodId_throwSQL(t.pEnv, _pMethodName,"(I)Z", _inout_MethodID);
272 // call method
273 out = t.pEnv->CallBooleanMethod( object, _inout_MethodID, _nArgument );
274 ThrowSQLException( t.pEnv, nullptr );
276 return out;
279 jobject java_lang_Object::callResultSetMethod( JNIEnv& _rEnv,const char* _pMethodName, jmethodID& _inout_MethodID ) const
281 // call method
282 jobject out = callObjectMethod(&_rEnv,_pMethodName,"()Ljava/sql/ResultSet;", _inout_MethodID);
283 return out;
286 sal_Int32 java_lang_Object::callIntMethod_ThrowSQL(const char* _pMethodName, jmethodID& _inout_MethodID) const
288 SDBThreadAttach t;
289 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
290 obtainMethodId_throwSQL(t.pEnv, _pMethodName,"()I", _inout_MethodID);
291 // call method
292 jint out( t.pEnv->CallIntMethod( object, _inout_MethodID ) );
293 ThrowSQLException( t.pEnv, nullptr );
294 return static_cast<sal_Int32>(out);
297 sal_Int32 java_lang_Object::callIntMethod_ThrowRuntime(const char* _pMethodName, jmethodID& _inout_MethodID) const
299 SDBThreadAttach t;
300 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
301 obtainMethodId_throwRuntime(t.pEnv, _pMethodName,"()I", _inout_MethodID);
302 // call method
303 jint out( t.pEnv->CallIntMethod( object, _inout_MethodID ) );
304 ThrowRuntimeException(t.pEnv, nullptr);
305 return static_cast<sal_Int32>(out);
308 sal_Int32 java_lang_Object::callIntMethodWithIntArg_ThrowSQL( const char* _pMethodName, jmethodID& _inout_MethodID,sal_Int32 _nArgument ) const
310 SDBThreadAttach t;
311 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
312 obtainMethodId_throwSQL(t.pEnv, _pMethodName,"(I)I", _inout_MethodID);
313 // call method
314 jint out( t.pEnv->CallIntMethod( object, _inout_MethodID , _nArgument) );
315 ThrowSQLException( t.pEnv, nullptr );
316 return static_cast<sal_Int32>(out);
319 sal_Int32 java_lang_Object::callIntMethodWithIntArg_ThrowRuntime( const char* _pMethodName, jmethodID& _inout_MethodID,sal_Int32 _nArgument ) const
321 SDBThreadAttach t;
322 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
323 obtainMethodId_throwRuntime(t.pEnv, _pMethodName,"(I)I", _inout_MethodID);
324 // call method
325 jint out( t.pEnv->CallIntMethod( object, _inout_MethodID , _nArgument) );
326 ThrowRuntimeException(t.pEnv, nullptr);
327 return static_cast<sal_Int32>(out);
330 void java_lang_Object::callVoidMethod_ThrowSQL( const char* _pMethodName, jmethodID& _inout_MethodID) const
332 SDBThreadAttach t;
333 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
334 obtainMethodId_throwSQL(t.pEnv, _pMethodName,"()V", _inout_MethodID);
336 // call method
337 t.pEnv->CallVoidMethod( object, _inout_MethodID );
338 ThrowSQLException( t.pEnv, nullptr );
341 void java_lang_Object::callVoidMethod_ThrowRuntime( const char* _pMethodName, jmethodID& _inout_MethodID) const
343 SDBThreadAttach t;
344 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
345 obtainMethodId_throwRuntime(t.pEnv, _pMethodName,"()V", _inout_MethodID);
347 // call method
348 t.pEnv->CallVoidMethod( object, _inout_MethodID );
349 ThrowRuntimeException(t.pEnv, nullptr);
352 void java_lang_Object::callVoidMethodWithIntArg_ThrowSQL( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const
354 SDBThreadAttach t;
355 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
356 obtainMethodId_throwSQL(t.pEnv, _pMethodName,"(I)V", _inout_MethodID);
358 // call method
359 t.pEnv->CallVoidMethod( object, _inout_MethodID,_nArgument );
360 ThrowSQLException( t.pEnv, nullptr );
363 void java_lang_Object::callVoidMethodWithIntArg_ThrowRuntime( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const
365 SDBThreadAttach t;
366 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
367 obtainMethodId_throwRuntime(t.pEnv, _pMethodName,"(I)V", _inout_MethodID);
369 // call method
370 t.pEnv->CallVoidMethod( object, _inout_MethodID,_nArgument );
371 ThrowRuntimeException(t.pEnv, nullptr);
374 void java_lang_Object::callVoidMethodWithBoolArg_ThrowSQL( const char* _pMethodName, jmethodID& _inout_MethodID, bool _nArgument ) const
376 SDBThreadAttach t;
377 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
378 obtainMethodId_throwSQL(t.pEnv, _pMethodName,"(Z)V", _inout_MethodID);
379 // call method
380 t.pEnv->CallVoidMethod( object, _inout_MethodID,int(_nArgument) );
381 ThrowSQLException( t.pEnv, nullptr );
384 void java_lang_Object::callVoidMethodWithBoolArg_ThrowRuntime( const char* _pMethodName, jmethodID& _inout_MethodID, bool _nArgument ) const
386 SDBThreadAttach t;
387 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
388 obtainMethodId_throwRuntime(t.pEnv, _pMethodName,"(Z)V", _inout_MethodID);
389 // call method
390 t.pEnv->CallVoidMethod( object, _inout_MethodID,int(_nArgument) );
391 ThrowRuntimeException(t.pEnv, nullptr);
394 OUString java_lang_Object::callStringMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const
396 SDBThreadAttach t;
397 OSL_ENSURE( t.pEnv, "java_lang_Object::callStringMethod: no Java environment anymore!" );
399 // call method
400 jstring out = static_cast<jstring>(callObjectMethod(t.pEnv,_pMethodName,"()Ljava/lang/String;", _inout_MethodID));
401 return JavaString2String( t.pEnv, out );
404 jobject java_lang_Object::callObjectMethod( JNIEnv * _pEnv,const char* _pMethodName,const char* _pSignature, jmethodID& _inout_MethodID ) const
406 // obtain method ID
407 obtainMethodId_throwSQL(_pEnv, _pMethodName,_pSignature, _inout_MethodID);
408 // call method
409 jobject out = _pEnv->CallObjectMethod( object, _inout_MethodID);
410 ThrowSQLException( _pEnv, nullptr );
411 return out;
415 jobject java_lang_Object::callObjectMethodWithIntArg( JNIEnv * _pEnv,const char* _pMethodName,const char* _pSignature, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const
417 obtainMethodId_throwSQL(_pEnv, _pMethodName,_pSignature, _inout_MethodID);
418 // call method
419 jobject out = _pEnv->CallObjectMethod( object, _inout_MethodID,_nArgument );
420 ThrowSQLException( _pEnv, nullptr );
421 return out;
424 OUString java_lang_Object::callStringMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const
426 SDBThreadAttach t;
427 OSL_ENSURE( t.pEnv, "java_lang_Object::callStringMethod: no Java environment anymore!" );
428 jstring out = static_cast<jstring>(callObjectMethodWithIntArg(t.pEnv,_pMethodName,"(I)Ljava/lang/String;",_inout_MethodID,_nArgument));
429 return JavaString2String( t.pEnv, out );
432 void java_lang_Object::callVoidMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID,const OUString& _nArgument ) const
434 SDBThreadAttach t;
435 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethod: no Java environment anymore!" );
436 obtainMethodId_throwSQL(t.pEnv, _pMethodName,"(Ljava/lang/String;)V", _inout_MethodID);
438 jdbc::LocalRef< jstring > str( t.env(),convertwchar_tToJavaString(t.pEnv,_nArgument));
439 // call method
440 t.pEnv->CallVoidMethod( object, _inout_MethodID , str.get());
441 ThrowSQLException( t.pEnv, nullptr );
444 sal_Int32 java_lang_Object::callIntMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID,const OUString& _nArgument ) const
446 SDBThreadAttach t;
447 OSL_ENSURE( t.pEnv, "java_lang_Object::callIntMethodWithStringArg: no Java environment anymore!" );
448 obtainMethodId_throwSQL(t.pEnv, _pMethodName,"(Ljava/lang/String;)I", _inout_MethodID);
450 //TODO: Check if the code below is needed
451 //jdbc::LocalRef< jstring > str( t.env(), convertwchar_tToJavaString( t.pEnv, sql ) );
453 // jdbc::ContextClassLoaderScope ccl( t.env(),
454 // m_pConnection ? m_pConnection->getDriverClassLoader() : jdbc::GlobalRef< jobject >(),
455 // m_aLogger,
456 // *this
457 // );
459 jdbc::LocalRef< jstring > str( t.env(),convertwchar_tToJavaString(t.pEnv,_nArgument));
460 // call method
461 jint out = t.pEnv->CallIntMethod( object, _inout_MethodID , str.get());
462 ThrowSQLException( t.pEnv, nullptr );
463 return static_cast<sal_Int32>(out);
466 jclass java_lang_Object::findMyClass(const char* _pClassName)
468 // the class must be fetched only once, therefore static
469 SDBThreadAttach t;
470 jclass tempClass = t.pEnv->FindClass(_pClassName); OSL_ENSURE(tempClass,"Java : FindClass not successful!");
471 if(!tempClass)
473 t.pEnv->ExceptionDescribe();
474 t.pEnv->ExceptionClear();
476 jclass globClass = static_cast<jclass>(t.pEnv->NewGlobalRef( tempClass ));
477 t.pEnv->DeleteLocalRef( tempClass );
478 return globClass;
481 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */