1*9b5730f6SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*9b5730f6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*9b5730f6SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*9b5730f6SAndrew Rist * distributed with this work for additional information 6*9b5730f6SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*9b5730f6SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*9b5730f6SAndrew Rist * "License"); you may not use this file except in compliance 9*9b5730f6SAndrew Rist * with the License. You may obtain a copy of the License at 10*9b5730f6SAndrew Rist * 11*9b5730f6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*9b5730f6SAndrew Rist * 13*9b5730f6SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*9b5730f6SAndrew Rist * software distributed under the License is distributed on an 15*9b5730f6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*9b5730f6SAndrew Rist * KIND, either express or implied. See the License for the 17*9b5730f6SAndrew Rist * specific language governing permissions and limitations 18*9b5730f6SAndrew Rist * under the License. 19*9b5730f6SAndrew Rist * 20*9b5730f6SAndrew Rist *************************************************************/ 21*9b5730f6SAndrew Rist 22*9b5730f6SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "java/ContextClassLoader.hxx" 28cdf0e10cSrcweir #include "java/lang/Object.hxx" 29cdf0e10cSrcweir 30cdf0e10cSrcweir /** === begin UNO includes === **/ 31cdf0e10cSrcweir /** === end UNO includes === **/ 32cdf0e10cSrcweir 33cdf0e10cSrcweir //........................................................................ 34cdf0e10cSrcweir namespace connectivity { namespace jdbc 35cdf0e10cSrcweir { 36cdf0e10cSrcweir //........................................................................ 37cdf0e10cSrcweir 38cdf0e10cSrcweir /** === begin UNO using === **/ 39cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 40cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 41cdf0e10cSrcweir /** === end UNO using === **/ 42cdf0e10cSrcweir 43cdf0e10cSrcweir using ::connectivity::java_lang_Object; 44cdf0e10cSrcweir 45cdf0e10cSrcweir //==================================================================== 46cdf0e10cSrcweir //= ContextClassLoaderScope 47cdf0e10cSrcweir //==================================================================== 48cdf0e10cSrcweir //-------------------------------------------------------------------- ContextClassLoaderScope(JNIEnv & environment,const GlobalRef<jobject> & newClassLoader,const::comphelper::ResourceBasedEventLogger & _rLoggerForErrors,const Reference<XInterface> & _rxErrorContext)49cdf0e10cSrcweir ContextClassLoaderScope::ContextClassLoaderScope( JNIEnv& environment, const GlobalRef< jobject >& newClassLoader, 50cdf0e10cSrcweir const ::comphelper::ResourceBasedEventLogger& _rLoggerForErrors, const Reference< XInterface >& _rxErrorContext ) 51cdf0e10cSrcweir :m_environment( environment ) 52cdf0e10cSrcweir ,m_currentThread( environment ) 53cdf0e10cSrcweir ,m_oldContextClassLoader( environment ) 54cdf0e10cSrcweir { 55cdf0e10cSrcweir if ( !newClassLoader.is() ) 56cdf0e10cSrcweir return; 57cdf0e10cSrcweir 58cdf0e10cSrcweir do // artificial loop for easier flow control 59cdf0e10cSrcweir { 60cdf0e10cSrcweir 61cdf0e10cSrcweir LocalRef< jclass > threadClass( m_environment ); 62cdf0e10cSrcweir threadClass.set( m_environment.FindClass( "java/lang/Thread" ) ); 63cdf0e10cSrcweir if ( !threadClass.is() ) 64cdf0e10cSrcweir break; 65cdf0e10cSrcweir 66cdf0e10cSrcweir jmethodID currentThreadMethod( m_environment.GetStaticMethodID( 67cdf0e10cSrcweir threadClass.get(), "currentThread", "()Ljava/lang/Thread;" ) ); 68cdf0e10cSrcweir if ( currentThreadMethod == NULL ) 69cdf0e10cSrcweir break; 70cdf0e10cSrcweir 71cdf0e10cSrcweir m_currentThread.set( m_environment.CallStaticObjectMethod( threadClass.get(), currentThreadMethod ) ); 72cdf0e10cSrcweir if ( !m_currentThread.is() ) 73cdf0e10cSrcweir break; 74cdf0e10cSrcweir 75cdf0e10cSrcweir jmethodID getContextClassLoaderMethod( m_environment.GetMethodID( 76cdf0e10cSrcweir threadClass.get(), "getContextClassLoader", "()Ljava/lang/ClassLoader;" ) ); 77cdf0e10cSrcweir if ( getContextClassLoaderMethod == NULL ) 78cdf0e10cSrcweir break; 79cdf0e10cSrcweir m_oldContextClassLoader.set( m_environment.CallObjectMethod( m_currentThread.get(), getContextClassLoaderMethod ) ); 80cdf0e10cSrcweir LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() ); 81cdf0e10cSrcweir if ( throwable.is() ) 82cdf0e10cSrcweir break; 83cdf0e10cSrcweir 84cdf0e10cSrcweir m_setContextClassLoaderMethod = m_environment.GetMethodID( 85cdf0e10cSrcweir threadClass.get(), "setContextClassLoader", "(Ljava/lang/ClassLoader;)V" ); 86cdf0e10cSrcweir if ( m_setContextClassLoaderMethod == NULL ) 87cdf0e10cSrcweir break; 88cdf0e10cSrcweir 89cdf0e10cSrcweir } 90cdf0e10cSrcweir while ( false ); 91cdf0e10cSrcweir 92cdf0e10cSrcweir if ( !isActive() ) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext ); 95cdf0e10cSrcweir return; 96cdf0e10cSrcweir } 97cdf0e10cSrcweir 98cdf0e10cSrcweir // set the new class loader 99cdf0e10cSrcweir m_environment.CallObjectMethod( m_currentThread.get(), m_setContextClassLoaderMethod, newClassLoader.get() ); 100cdf0e10cSrcweir LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() ); 101cdf0e10cSrcweir if ( throwable.is() ) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir m_currentThread.reset(); 104cdf0e10cSrcweir m_setContextClassLoaderMethod = NULL; 105cdf0e10cSrcweir java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext ); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir } 108cdf0e10cSrcweir 109cdf0e10cSrcweir //-------------------------------------------------------------------- pop(bool clearExceptions)110cdf0e10cSrcweir void ContextClassLoaderScope::pop( bool clearExceptions ) 111cdf0e10cSrcweir { 112cdf0e10cSrcweir if ( isActive() ) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir LocalRef< jobject > currentThread( m_currentThread.env(), m_currentThread.release() ); 115cdf0e10cSrcweir jmethodID setContextClassLoaderMethod( m_setContextClassLoaderMethod ); 116cdf0e10cSrcweir m_setContextClassLoaderMethod = NULL; 117cdf0e10cSrcweir 118cdf0e10cSrcweir m_environment.CallObjectMethod( currentThread.get(), setContextClassLoaderMethod, m_oldContextClassLoader.get() ); 119cdf0e10cSrcweir if ( clearExceptions ) 120cdf0e10cSrcweir { 121cdf0e10cSrcweir m_environment.ExceptionClear(); 122cdf0e10cSrcweir } 123cdf0e10cSrcweir } 124cdf0e10cSrcweir } 125cdf0e10cSrcweir //........................................................................ 126cdf0e10cSrcweir } } // namespace connectivity::jdbc 127cdf0e10cSrcweir //........................................................................ 128