summaryrefslogtreecommitdiffstats
path: root/connectivity/source/drivers/jdbc/ContextClassLoader.cxx
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /connectivity/source/drivers/jdbc/ContextClassLoader.cxx
parentInitial commit. (diff)
downloadlibreoffice-cb75148ebd0135178ff46f89a30139c44f8d2040.tar.xz
libreoffice-cb75148ebd0135178ff46f89a30139c44f8d2040.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'connectivity/source/drivers/jdbc/ContextClassLoader.cxx')
-rw-r--r--connectivity/source/drivers/jdbc/ContextClassLoader.cxx111
1 files changed, 111 insertions, 0 deletions
diff --git a/connectivity/source/drivers/jdbc/ContextClassLoader.cxx b/connectivity/source/drivers/jdbc/ContextClassLoader.cxx
new file mode 100644
index 000000000..3ab1b88eb
--- /dev/null
+++ b/connectivity/source/drivers/jdbc/ContextClassLoader.cxx
@@ -0,0 +1,111 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <java/ContextClassLoader.hxx>
+#include <java/lang/Object.hxx>
+
+
+namespace connectivity::jdbc
+{
+
+
+ using ::com::sun::star::uno::Reference;
+ using ::com::sun::star::uno::XInterface;
+
+ using ::connectivity::java_lang_Object;
+
+ ContextClassLoaderScope::ContextClassLoaderScope( JNIEnv& environment, const GlobalRef< jobject >& newClassLoader,
+ const ::comphelper::EventLogger& _rLoggerForErrors, const Reference< XInterface >& _rxErrorContext )
+ :m_environment( environment )
+ ,m_currentThread( environment )
+ ,m_oldContextClassLoader( environment )
+ ,m_setContextClassLoaderMethod( nullptr )
+ {
+ if ( !newClassLoader.is() )
+ return;
+
+ do // artificial loop for easier flow control
+ {
+
+ LocalRef< jclass > threadClass( m_environment );
+ threadClass.set( m_environment.FindClass( "java/lang/Thread" ) );
+ if ( !threadClass.is() )
+ break;
+
+ jmethodID currentThreadMethod( m_environment.GetStaticMethodID(
+ threadClass.get(), "currentThread", "()Ljava/lang/Thread;" ) );
+ if ( currentThreadMethod == nullptr )
+ break;
+
+ m_currentThread.set( m_environment.CallStaticObjectMethod( threadClass.get(), currentThreadMethod ) );
+ if ( !m_currentThread.is() )
+ break;
+
+ jmethodID getContextClassLoaderMethod( m_environment.GetMethodID(
+ threadClass.get(), "getContextClassLoader", "()Ljava/lang/ClassLoader;" ) );
+ if ( getContextClassLoaderMethod == nullptr )
+ break;
+ m_oldContextClassLoader.set( m_environment.CallObjectMethod( m_currentThread.get(), getContextClassLoaderMethod ) );
+ LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() );
+ if ( throwable.is() )
+ break;
+
+ m_setContextClassLoaderMethod = m_environment.GetMethodID(
+ threadClass.get(), "setContextClassLoader", "(Ljava/lang/ClassLoader;)V" );
+ if ( m_setContextClassLoaderMethod == nullptr )
+ break;
+
+ }
+ while ( false );
+
+ if ( !isActive() )
+ {
+ java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext );
+ return;
+ }
+
+ // set the new class loader
+ m_environment.CallObjectMethod( m_currentThread.get(), m_setContextClassLoaderMethod, newClassLoader.get() );
+ LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() );
+ if ( throwable.is() )
+ {
+ m_currentThread.reset();
+ m_setContextClassLoaderMethod = nullptr;
+ java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext );
+ }
+ }
+
+
+ ContextClassLoaderScope::~ContextClassLoaderScope()
+ {
+ if ( isActive() )
+ {
+ LocalRef< jobject > currentThread( m_currentThread.env(), m_currentThread.release() );
+ jmethodID setContextClassLoaderMethod( m_setContextClassLoaderMethod );
+ m_setContextClassLoaderMethod = nullptr;
+
+ m_environment.CallObjectMethod( currentThread.get(), setContextClassLoaderMethod, m_oldContextClassLoader.get() );
+ m_environment.ExceptionClear();
+ }
+ }
+
+} // namespace connectivity::jdbc
+
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */