LibreOffice Module connectivity (master) 1
ContextClassLoader.cxx
Go to the documentation of this file.
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
21#include <java/lang/Object.hxx>
22
23
25{
26
27
28 using ::com::sun::star::uno::Reference;
29 using ::com::sun::star::uno::XInterface;
30
31 using ::connectivity::java_lang_Object;
32
33 ContextClassLoaderScope::ContextClassLoaderScope( JNIEnv& environment, const GlobalRef< jobject >& newClassLoader,
34 const ::comphelper::EventLogger& _rLoggerForErrors, const Reference< XInterface >& _rxErrorContext )
35 :m_environment( environment )
36 ,m_currentThread( environment )
37 ,m_oldContextClassLoader( environment )
38 ,m_setContextClassLoaderMethod( nullptr )
39 {
40 if ( !newClassLoader.is() )
41 return;
42
43 do // artificial loop for easier flow control
44 {
45
46 LocalRef< jclass > threadClass( m_environment );
47 threadClass.set( m_environment.FindClass( "java/lang/Thread" ) );
48 if ( !threadClass.is() )
49 break;
50
51 jmethodID currentThreadMethod( m_environment.GetStaticMethodID(
52 threadClass.get(), "currentThread", "()Ljava/lang/Thread;" ) );
53 if ( currentThreadMethod == nullptr )
54 break;
55
56 m_currentThread.set( m_environment.CallStaticObjectMethod( threadClass.get(), currentThreadMethod ) );
57 if ( !m_currentThread.is() )
58 break;
59
60 jmethodID getContextClassLoaderMethod( m_environment.GetMethodID(
61 threadClass.get(), "getContextClassLoader", "()Ljava/lang/ClassLoader;" ) );
62 if ( getContextClassLoaderMethod == nullptr )
63 break;
64 m_oldContextClassLoader.set( m_environment.CallObjectMethod( m_currentThread.get(), getContextClassLoaderMethod ) );
65 LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() );
66 if ( throwable.is() )
67 break;
68
69 m_setContextClassLoaderMethod = m_environment.GetMethodID(
70 threadClass.get(), "setContextClassLoader", "(Ljava/lang/ClassLoader;)V" );
71 if ( m_setContextClassLoaderMethod == nullptr )
72 break;
73
74 }
75 while ( false );
76
77 if ( !isActive() )
78 {
79 java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext );
80 return;
81 }
82
83 // set the new class loader
84 m_environment.CallObjectMethod( m_currentThread.get(), m_setContextClassLoaderMethod, newClassLoader.get() );
85 LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() );
86 if ( throwable.is() )
87 {
88 m_currentThread.reset();
89 m_setContextClassLoaderMethod = nullptr;
90 java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext );
91 }
92 }
93
94
96 {
97 if ( isActive() )
98 {
100 jmethodID setContextClassLoaderMethod( m_setContextClassLoaderMethod );
102
103 m_environment.CallObjectMethod( currentThread.get(), setContextClassLoaderMethod, m_oldContextClassLoader.get() );
104 m_environment.ExceptionClear();
105 }
106 }
107
108} // namespace connectivity::jdbc
109
110
111/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void ThrowLoggedSQLException(const ::comphelper::EventLogger &_rLogger, JNIEnv *pEnvironment, const css::uno::Reference< css::uno::XInterface > &_rxContext)
Definition: Object.cxx:197
ContextClassLoaderScope(JNIEnv &environment, const GlobalRef< jobject > &newClassLoader, const ::comphelper::EventLogger &_rLoggerForErrors, const css::uno::Reference< css::uno::XInterface > &_rxErrorContext)
creates the instance.
helper class to hold a local ref to a JNI object
Definition: LocalRef.hxx:42
JNIEnv & env() const
Definition: LocalRef.hxx:79
bool isActive()