32#include <com/sun/star/sdbc/SQLWarning.hpp>
33#include <com/sun/star/beans/NamedValue.hpp>
37#include <com/sun/star/uno/XComponentContext.hpp>
62 OUString theClassPath, OUString theClassName):
63 classPath(
std::move(theClassPath)), className(
std::move(theClassName)), classLoader(nullptr),
64 classObject(nullptr) {}
72typedef std::vector< ClassMapEntry > ClassMap;
80template <
typename T >
81bool getLocalFromWeakRef( jweak& _weak,
LocalRef< T >& _inout_local )
83 _inout_local.
set(
static_cast< T
>( _inout_local.
env().NewLocalRef( _weak ) ) );
85 if ( !_inout_local.
is() )
87 if ( _inout_local.
env().ExceptionCheck())
91 else if ( _weak !=
nullptr )
93 _inout_local.
env().DeleteWeakGlobalRef( _weak );
120 Reference< XComponentContext >
const & context, JNIEnv& environment,
121 OUString
const & classPath, OUString
const &
name,
124 OSL_ASSERT(classLoaderPtr !=
nullptr);
127 static ClassMapData classMapData;
128 osl::MutexGuard g(classMapData.mutex);
129 ClassMap::iterator
i(classMapData.map.begin());
134 for (;
i != classMapData.map.end();)
137 if ( !getLocalFromWeakRef(
i->classLoader, classLoader ) )
141 if ( !getLocalFromWeakRef(
i->classObject, classObject ) )
144 if ( !classLoader.is() && !classObject.is() )
146 i = classMapData.map.erase(i);
148 else if (
i->classPath == classPath &&
i->className ==
name )
150 cloader.set( classLoader.release() );
151 cl.set( classObject.release() );
159 if ( !cloader.is() || !cl.is() )
161 if ( i == classMapData.map.end() )
169 classMapData.map.push_back( ClassMapEntry( classPath,
name ) );
170 i = std::prev(classMapData.map.end());
174 clClass.set( environment.FindClass(
"java/net/URLClassLoader" ) );
178 jweak wcloader =
nullptr;
181 jmethodID ctorLoader( environment.GetMethodID( clClass.get(),
"<init>",
"([Ljava/net/URL;)V" ) );
182 if (ctorLoader ==
nullptr)
192 cloader.set( environment.NewObjectA( clClass.get(), ctorLoader, &arg ) );
196 wcloader = environment.NewWeakGlobalRef( cloader.get() );
197 if ( wcloader ==
nullptr )
204 jmethodID methLoadClass( environment.GetMethodID( clClass.get(),
"loadClass",
"(Ljava/lang/String;)Ljava/lang/Class;" ) );
205 if ( methLoadClass ==
nullptr )
215 cl.set(
static_cast< jclass
>( environment.CallObjectMethodA( cloader.get(), methLoadClass, &arg ) ) );
219 wcl = environment.NewWeakGlobalRef( cl.get() );
220 if ( wcl ==
nullptr )
224 if ( wcloader !=
nullptr)
226 i->classLoader = wcloader;
228 if ( wcl !=
nullptr )
230 i->classObject = wcl;
234 classLoaderPtr->
set( cloader.release() );
235 classPtr->
set( cl.release() );
247jclass java_sql_Connection::theClass =
nullptr;
251 ,m_pDriver( &_rDriver )
252 ,m_pDriverobject(nullptr)
253 ,m_Driver_theClass(nullptr)
254 ,m_aLogger( _rDriver.getLogger() )
255 ,m_bIgnoreDriverPrivileges(true)
256 ,m_bIgnoreCurrency(false)
290 static jmethodID mID(
nullptr);
306 ::osl::MutexGuard aGuard(
m_aMutex );
309 static jmethodID mID(
nullptr);
315 ::osl::MutexGuard aGuard(
m_aMutex );
319 Reference< XDatabaseMetaData > xMetaData =
m_xMetaData;
323 static jmethodID mID(
nullptr);
324 jobject out =
callObjectMethod(
t.pEnv,
"getMetaData",
"()Ljava/sql/DatabaseMetaData;", mID);
342 static jmethodID mID(
nullptr);
348 ::osl::MutexGuard aGuard(
m_aMutex );
350 static jmethodID mID(
nullptr);
351 return callBooleanMethod(
"isClosed", mID ) && java_sql_Connection_BASE::rBHelper.bDisposed;
356 ::osl::MutexGuard aGuard(
m_aMutex );
358 static jmethodID mID(
nullptr);
364 static jmethodID mID(
nullptr);
370 static jmethodID mID(
nullptr);
376 static jmethodID mID(
nullptr);
382 static jmethodID mID(
nullptr);
388 static jmethodID mID(
nullptr);
394 ::osl::MutexGuard aGuard(
m_aMutex );
398 static jmethodID mID(
nullptr);
406 ::osl::MutexGuard aGuard(
m_aMutex );
415 ::osl::MutexGuard aGuard(
m_aMutex );
418 static jmethodID mID(
nullptr);
424 ::osl::MutexGuard aGuard(
m_aMutex );
427 static jmethodID mID(
nullptr);
433 ::osl::MutexGuard aGuard(
m_aMutex );
439 Reference< XStatement > xStmt = pStatement;
448 ::osl::MutexGuard aGuard(
m_aMutex );
455 Reference< XPreparedStatement > xReturn( pStatement );
464 ::osl::MutexGuard aGuard(
m_aMutex );
471 Reference< XPreparedStatement > xStmt( pStatement );
480 ::osl::MutexGuard aGuard(
m_aMutex );
488 static const char *
const cSignature =
"(Ljava/lang/String;)Ljava/lang/String;";
489 static const char *
const cMethodName =
"nativeSQL";
491 static jmethodID mID(
nullptr);
496 jobject out =
t.pEnv->CallObjectMethod(
object, mID, str.
get() );
508 static jmethodID mID(
nullptr);
514 ::osl::MutexGuard aGuard(
m_aMutex );
518 static jmethodID mID(
nullptr);
519 jobject out =
callObjectMethod(
t.pEnv,
"getWarnings",
"()Ljava/sql/SQLWarning;", mID);
528 aWarning.Context = aAsException.Context;
529 aWarning.Message = aAsException.Message;
530 aWarning.SQLState = aAsException.SQLState;
531 aWarning.ErrorCode = aAsException.ErrorCode;
532 aWarning.NextException = aAsException.NextException;
534 return Any( aWarning );
543 OUString lcl_getDriverLoadErrorMessage( const ::connectivity::SharedResources& _aResource,
const OUString& _rDriverClass,
const OUString& _rDriverClassPath )
545 OUString sError1( _aResource.getResourceStringWithSubstitution(
547 "$classname$", _rDriverClass
549 if ( !_rDriverClassPath.isEmpty() )
551 const OUString sError2( _aResource.getResourceStringWithSubstitution(
552 STR_NO_CLASSNAME_PATH,
553 "$classpath$", _rDriverClassPath
565 JNIEnv& _rEnv,
const Sequence< NamedValue >& _rSystemProperties )
567 if ( !_rSystemProperties.hasElements() )
572 jmethodID nSetPropertyMethodID =
nullptr;
574 systemClass.set( _rEnv.FindClass(
"java/lang/System" ) );
575 if ( systemClass.is() )
577 nSetPropertyMethodID = _rEnv.GetStaticMethodID(
578 systemClass.get(),
"setProperty",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;" );
581 if ( nSetPropertyMethodID ==
nullptr )
584 for (
auto const & systemProp : _rSystemProperties )
587 OSL_VERIFY( systemProp.Value >>= sValue );
594 _rEnv.CallStaticObjectMethod( systemClass.get(), nSetPropertyMethodID, jName.get(), jValue.get() );
596 if ( throwable.is() )
606 const Sequence< NamedValue >& _rSystemProperties )
614 if ( !lcl_setSystemProperties_nothrow(
getLogger(), *
t.pEnv, _rSystemProperties ) )
623 if ( _sDriverClass.isEmpty() )
627 lcl_getDriverLoadErrorMessage(
getResources(),_sDriverClass, _sDriverClassPath ),
635 std::unique_ptr< java_lang_Class > pDrvClass;
636 if ( _sDriverClassPath.isEmpty() )
648 t.env(), _sDriverClassPath, _sDriverClass, &driverClassLoader, &driverClass );
658 driverObject.
set( pDrvClass->newInstanceObject() );
670 t.pEnv->DeleteLocalRef( tempClass );
678 catch(
const SQLException& )
682 lcl_getDriverLoadErrorMessage(
getResources(),_sDriverClass, _sDriverClassPath ),
692 lcl_getDriverLoadErrorMessage(
getResources(),_sDriverClass, _sDriverClassPath ),
701 static constexpr OUStringLiteral s_sNodeName
702 =
u"org.openoffice.Office.DataAccess/JDBC/DriverClassPaths";
709 OSL_VERIFY( aRegisterObj.
getNodeValue(
"Path" ) >>= sURL );
715 const Sequence< PropertyValue >& info)
727 OUString sGeneratedValueStatement;
728 bool bAutoRetrievingEnabled =
false;
729 OUString sDriverClassPath,sDriverClass;
730 Sequence< NamedValue > aSystemProperties;
733 sDriverClass = aSettings.
getOrDefault(
"JavaDriverClass", sDriverClass );
734 sDriverClassPath = aSettings.
getOrDefault(
"JavaDriverClassPath", sDriverClassPath);
735 if ( sDriverClassPath.isEmpty() )
737 bAutoRetrievingEnabled = aSettings.
getOrDefault(
"IsAutoRetrievingEnabled", bAutoRetrievingEnabled );
738 sGeneratedValueStatement = aSettings.
getOrDefault(
"AutoRetrievingStatement", sGeneratedValueStatement );
741 aSystemProperties = aSettings.
getOrDefault(
"SystemProperties", aSystemProperties );
753 static const char *
const cSignature =
"(Ljava/lang/String;Ljava/util/Properties;)Ljava/sql/Connection;";
754 static const char *
const cMethodName =
"connect";
763 args[1].l = pProps->getJavaObject();
767 jobject out =
nullptr;
792 object =
t.pEnv->NewGlobalRef( out );
800 return object !=
nullptr;
IMPLEMENT_SERVICE_INFO(java_sql_Connection,"com.sun.star.sdbcx.JConnection","com.sun.star.sdbc.Connection")
Reference< XComponentContext > m_xContext
VALUE_TYPE getOrDefault(const OUString &_rValueName, const VALUE_TYPE &_rDefault) const
void setAutoRetrievingStatement(const OUString &_sStmt)
void enableAutoRetrievingEnabled(bool _bAutoEnable)
void log(const sal_Int32 _nLogLevel, const OUString &rMessage)
logs a given message, without any arguments, or source class/method names
static java_lang_Class * forName(std::u16string_view _par0)
jobject callObjectMethod(JNIEnv *pEnv, const char *_pMethodName, const char *_pSignature, jmethodID &_inout_MethodID) const
sal_Int32 callIntMethod_ThrowSQL(const char *_pMethodName, jmethodID &_inout_MethodID) const
bool callBooleanMethod(const char *_pMethodName, jmethodID &_inout_MethodID) const
static ::rtl::Reference< jvmaccess::VirtualMachine > getVM(const css::uno::Reference< css::uno::XComponentContext > &_rxContext=nullptr)
void callVoidMethodWithStringArg(const char *_pMethodName, jmethodID &_inout_MethodID, const OUString &_nArgument) const
void callVoidMethod_ThrowSQL(const char *_pMethodName, jmethodID &_inout_MethodID) const
void callVoidMethodWithIntArg_ThrowSQL(const char *_pMethodName, jmethodID &_inout_MethodID, sal_Int32 _nArgument) const
OUString callStringMethod(const char *_pMethodName, jmethodID &_inout_MethodID) const
static void ThrowLoggedSQLException(const ::comphelper::EventLogger &_rLogger, JNIEnv *pEnvironment, const css::uno::Reference< css::uno::XInterface > &_rxContext)
static jclass findMyClass(const char *_pClassName)
void callVoidMethodWithBoolArg_ThrowSQL(const char *_pMethodName, jmethodID &_inout_MethodID, bool _nArgument) const
void obtainMethodId_throwSQL(JNIEnv *_pEnv, const char *_pMethodName, const char *_pSignature, jmethodID &_inout_MethodID) const
static jclass st_getMyClass()
css::uno::Any m_aSchemaRestriction
virtual void SAL_CALL rollback() override
virtual void SAL_CALL setTypeMap(const css::uno::Reference< css::container::XNameAccess > &typeMap) override
virtual void SAL_CALL setAutoCommit(sal_Bool autoCommit) override
java::sql::ConnectionLog m_aLogger
virtual sal_Bool SAL_CALL isClosed() override
virtual void SAL_CALL commit() override
virtual css::uno::Reference< css::sdbc::XStatement > SAL_CALL createStatement() override
virtual void SAL_CALL setTransactionIsolation(sal_Int32 level) override
virtual void SAL_CALL setCatalog(const OUString &catalog) override
virtual css::uno::Any SAL_CALL getWarnings() override
virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareStatement(const OUString &sql) override
virtual void SAL_CALL disposing() override
virtual void SAL_CALL close() override
virtual ~java_sql_Connection() override
jdbc::GlobalRef< jobject > m_pDriverClassLoader
virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareCall(const OUString &sql) override
virtual sal_Int32 SAL_CALL getTransactionIsolation() override
bool construct(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info)
virtual css::uno::Reference< css::sdbc::XDatabaseMetaData > SAL_CALL getMetaData() override
const jdbc::GlobalRef< jobject > & getDriverClassLoader() const
returns the class loader which was used to load the driver class
virtual sal_Bool SAL_CALL getAutoCommit() override
virtual void SAL_CALL clearWarnings() override
const java_sql_Driver * m_pDriver
const java::sql::ConnectionLog & getLogger() const
returns the instance used for logging events related to this connection
bool m_bIgnoreDriverPrivileges
virtual void SAL_CALL setReadOnly(sal_Bool readOnly) override
void loadDriverFromProperties(const OUString &_sDriverClass, const OUString &_sDriverClassPath, const css::uno::Sequence< css::beans::NamedValue > &_rSystemProperties)
virtual OUString SAL_CALL nativeSQL(const OUString &sql) override
virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getTypeMap() override
virtual sal_Bool SAL_CALL isReadOnly() override
virtual OUString SAL_CALL getCatalog() override
css::uno::Any m_aCatalogRestriction
OUString impl_getJavaDriverClassPath_nothrow(const OUString &_sDriverClass)
load driver class path from system configuration.
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual jclass getMyClass() const override
const css::uno::Reference< css::uno::XComponentContext > & getContext() const
static jclass st_getMyClass()
void set(JNIEnv &_environment, T _object)
helper class to hold a local ref to a JNI object
css::uno::Any getNodeValue(const OUString &_rPath) const noexcept
bool hasByName(const OUString &_rName) const noexcept
OConfigurationNode openNode(const OUString &_rPath) const noexcept
static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const OUString &_rPath, sal_Int32 _nDepth=-1, CREATION_MODE _eMode=CM_UPDATABLE)
OUString JavaString2String(JNIEnv *pEnv, jstring Str)
jstring convertwchar_tToJavaString(JNIEnv *pEnv, const OUString &Temp)
std::unique_ptr< java_util_Properties > createStringPropertyArray(const css::uno::Sequence< css::beans::PropertyValue > &info)
void checkDisposed(bool _bThrow)
Any SAL_CALL getCaughtException()
JVMACCESS_DLLPUBLIC jobjectArray translateToUrls(css::uno::Reference< css::uno::XComponentContext > const &context, JNIEnv *environment, std::u16string_view classPath)
std::map< OUString, rtl::Reference< Entity > > map
constexpr OUStringLiteral STR_LOG_GOT_JDBC_CONNECTION
constexpr OUStringLiteral STR_LOG_PREPARED_CALL_ID
constexpr OUStringLiteral STR_LOG_SETTING_SYSTEM_PROPERTY
constexpr OUStringLiteral STR_LOG_PREPARE_CALL
constexpr OUStringLiteral STR_LOG_PREPARE_STATEMENT
constexpr OUStringLiteral STR_LOG_NO_SYSTEM_CONNECTION
constexpr OUStringLiteral STR_LOG_CONN_SUCCESS
constexpr OUStringLiteral STR_LOG_CREATED_STATEMENT_ID
constexpr OUStringLiteral STR_LOG_NO_DRIVER_CLASS
constexpr OUStringLiteral STR_LOG_SHUTDOWN_CONNECTION
constexpr OUStringLiteral STR_LOG_NATIVE_SQL
constexpr OUStringLiteral STR_LOG_PREPARED_STATEMENT_ID
constexpr OUStringLiteral STR_LOG_CREATE_STATEMENT
constexpr OUStringLiteral STR_LOG_LOADING_DRIVER