1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_connectivity.hxx" 30 31 #include "java/ContextClassLoader.hxx" 32 #include "java/lang/Object.hxx" 33 34 /** === begin UNO includes === **/ 35 /** === end UNO includes === **/ 36 37 //........................................................................ 38 namespace connectivity { namespace jdbc 39 { 40 //........................................................................ 41 42 /** === begin UNO using === **/ 43 using ::com::sun::star::uno::Reference; 44 using ::com::sun::star::uno::XInterface; 45 /** === end UNO using === **/ 46 47 using ::connectivity::java_lang_Object; 48 49 //==================================================================== 50 //= ContextClassLoaderScope 51 //==================================================================== 52 //-------------------------------------------------------------------- 53 ContextClassLoaderScope::ContextClassLoaderScope( JNIEnv& environment, const GlobalRef< jobject >& newClassLoader, 54 const ::comphelper::ResourceBasedEventLogger& _rLoggerForErrors, const Reference< XInterface >& _rxErrorContext ) 55 :m_environment( environment ) 56 ,m_currentThread( environment ) 57 ,m_oldContextClassLoader( environment ) 58 { 59 if ( !newClassLoader.is() ) 60 return; 61 62 do // artificial loop for easier flow control 63 { 64 65 LocalRef< jclass > threadClass( m_environment ); 66 threadClass.set( m_environment.FindClass( "java/lang/Thread" ) ); 67 if ( !threadClass.is() ) 68 break; 69 70 jmethodID currentThreadMethod( m_environment.GetStaticMethodID( 71 threadClass.get(), "currentThread", "()Ljava/lang/Thread;" ) ); 72 if ( currentThreadMethod == NULL ) 73 break; 74 75 m_currentThread.set( m_environment.CallStaticObjectMethod( threadClass.get(), currentThreadMethod ) ); 76 if ( !m_currentThread.is() ) 77 break; 78 79 jmethodID getContextClassLoaderMethod( m_environment.GetMethodID( 80 threadClass.get(), "getContextClassLoader", "()Ljava/lang/ClassLoader;" ) ); 81 if ( getContextClassLoaderMethod == NULL ) 82 break; 83 m_oldContextClassLoader.set( m_environment.CallObjectMethod( m_currentThread.get(), getContextClassLoaderMethod ) ); 84 LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() ); 85 if ( throwable.is() ) 86 break; 87 88 m_setContextClassLoaderMethod = m_environment.GetMethodID( 89 threadClass.get(), "setContextClassLoader", "(Ljava/lang/ClassLoader;)V" ); 90 if ( m_setContextClassLoaderMethod == NULL ) 91 break; 92 93 } 94 while ( false ); 95 96 if ( !isActive() ) 97 { 98 java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext ); 99 return; 100 } 101 102 // set the new class loader 103 m_environment.CallObjectMethod( m_currentThread.get(), m_setContextClassLoaderMethod, newClassLoader.get() ); 104 LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() ); 105 if ( throwable.is() ) 106 { 107 m_currentThread.reset(); 108 m_setContextClassLoaderMethod = NULL; 109 java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext ); 110 } 111 } 112 113 //-------------------------------------------------------------------- 114 void ContextClassLoaderScope::pop( bool clearExceptions ) 115 { 116 if ( isActive() ) 117 { 118 LocalRef< jobject > currentThread( m_currentThread.env(), m_currentThread.release() ); 119 jmethodID setContextClassLoaderMethod( m_setContextClassLoaderMethod ); 120 m_setContextClassLoaderMethod = NULL; 121 122 m_environment.CallObjectMethod( currentThread.get(), setContextClassLoaderMethod, m_oldContextClassLoader.get() ); 123 if ( clearExceptions ) 124 { 125 m_environment.ExceptionClear(); 126 } 127 } 128 } 129 //........................................................................ 130 } } // namespace connectivity::jdbc 131 //........................................................................ 132