cid#1636677 Uninitialized scalar field
[LibreOffice.git] / connectivity / source / drivers / jdbc / ContextClassLoader.cxx
blob3ab1b88eb33bf6bd94ab4943f90e740514f0647d
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 <java/ContextClassLoader.hxx>
21 #include <java/lang/Object.hxx>
24 namespace connectivity::jdbc
28 using ::com::sun::star::uno::Reference;
29 using ::com::sun::star::uno::XInterface;
31 using ::connectivity::java_lang_Object;
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 )
40 if ( !newClassLoader.is() )
41 return;
43 do // artificial loop for easier flow control
46 LocalRef< jclass > threadClass( m_environment );
47 threadClass.set( m_environment.FindClass( "java/lang/Thread" ) );
48 if ( !threadClass.is() )
49 break;
51 jmethodID currentThreadMethod( m_environment.GetStaticMethodID(
52 threadClass.get(), "currentThread", "()Ljava/lang/Thread;" ) );
53 if ( currentThreadMethod == nullptr )
54 break;
56 m_currentThread.set( m_environment.CallStaticObjectMethod( threadClass.get(), currentThreadMethod ) );
57 if ( !m_currentThread.is() )
58 break;
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;
69 m_setContextClassLoaderMethod = m_environment.GetMethodID(
70 threadClass.get(), "setContextClassLoader", "(Ljava/lang/ClassLoader;)V" );
71 if ( m_setContextClassLoaderMethod == nullptr )
72 break;
75 while ( false );
77 if ( !isActive() )
79 java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext );
80 return;
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() )
88 m_currentThread.reset();
89 m_setContextClassLoaderMethod = nullptr;
90 java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext );
95 ContextClassLoaderScope::~ContextClassLoaderScope()
97 if ( isActive() )
99 LocalRef< jobject > currentThread( m_currentThread.env(), m_currentThread.release() );
100 jmethodID setContextClassLoaderMethod( m_setContextClassLoaderMethod );
101 m_setContextClassLoaderMethod = nullptr;
103 m_environment.CallObjectMethod( currentThread.get(), setContextClassLoaderMethod, m_oldContextClassLoader.get() );
104 m_environment.ExceptionClear();
108 } // namespace connectivity::jdbc
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */