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