Line data Source code
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 :
20 : #include "java/ContextClassLoader.hxx"
21 : #include "java/lang/Object.hxx"
22 :
23 : //........................................................................
24 : namespace connectivity { namespace jdbc
25 : {
26 : //........................................................................
27 :
28 : /** === begin UNO using === **/
29 : using ::com::sun::star::uno::Reference;
30 : using ::com::sun::star::uno::XInterface;
31 : /** === end UNO using === **/
32 :
33 : using ::connectivity::java_lang_Object;
34 :
35 : //====================================================================
36 : //= ContextClassLoaderScope
37 : //====================================================================
38 : //--------------------------------------------------------------------
39 0 : ContextClassLoaderScope::ContextClassLoaderScope( JNIEnv& environment, const GlobalRef< jobject >& newClassLoader,
40 : const ::comphelper::ResourceBasedEventLogger& _rLoggerForErrors, const Reference< XInterface >& _rxErrorContext )
41 : :m_environment( environment )
42 : ,m_currentThread( environment )
43 0 : ,m_oldContextClassLoader( environment )
44 : {
45 0 : if ( !newClassLoader.is() )
46 : return;
47 :
48 : do // artificial loop for easier flow control
49 : {
50 :
51 0 : LocalRef< jclass > threadClass( m_environment );
52 0 : threadClass.set( m_environment.FindClass( "java/lang/Thread" ) );
53 0 : if ( !threadClass.is() )
54 : break;
55 :
56 : jmethodID currentThreadMethod( m_environment.GetStaticMethodID(
57 0 : threadClass.get(), "currentThread", "()Ljava/lang/Thread;" ) );
58 0 : if ( currentThreadMethod == NULL )
59 : break;
60 :
61 0 : m_currentThread.set( m_environment.CallStaticObjectMethod( threadClass.get(), currentThreadMethod ) );
62 0 : if ( !m_currentThread.is() )
63 : break;
64 :
65 : jmethodID getContextClassLoaderMethod( m_environment.GetMethodID(
66 0 : threadClass.get(), "getContextClassLoader", "()Ljava/lang/ClassLoader;" ) );
67 0 : if ( getContextClassLoaderMethod == NULL )
68 : break;
69 0 : m_oldContextClassLoader.set( m_environment.CallObjectMethod( m_currentThread.get(), getContextClassLoaderMethod ) );
70 0 : LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() );
71 0 : if ( throwable.is() )
72 : break;
73 :
74 : m_setContextClassLoaderMethod = m_environment.GetMethodID(
75 0 : threadClass.get(), "setContextClassLoader", "(Ljava/lang/ClassLoader;)V" );
76 0 : if ( m_setContextClassLoaderMethod == NULL )
77 0 : break;
78 :
79 : }
80 : while ( false );
81 :
82 0 : if ( !isActive() )
83 : {
84 0 : java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext );
85 : return;
86 : }
87 :
88 : // set the new class loader
89 0 : m_environment.CallObjectMethod( m_currentThread.get(), m_setContextClassLoaderMethod, newClassLoader.get() );
90 0 : LocalRef< jthrowable > throwable( m_environment, m_environment.ExceptionOccurred() );
91 0 : if ( throwable.is() )
92 : {
93 0 : m_currentThread.reset();
94 0 : m_setContextClassLoaderMethod = NULL;
95 0 : java_lang_Object::ThrowLoggedSQLException( _rLoggerForErrors, &environment, _rxErrorContext );
96 0 : }
97 : }
98 :
99 : //--------------------------------------------------------------------
100 0 : void ContextClassLoaderScope::pop( bool clearExceptions )
101 : {
102 0 : if ( isActive() )
103 : {
104 0 : LocalRef< jobject > currentThread( m_currentThread.env(), m_currentThread.release() );
105 0 : jmethodID setContextClassLoaderMethod( m_setContextClassLoaderMethod );
106 0 : m_setContextClassLoaderMethod = NULL;
107 :
108 0 : m_environment.CallObjectMethod( currentThread.get(), setContextClassLoaderMethod, m_oldContextClassLoader.get() );
109 0 : if ( clearExceptions )
110 : {
111 0 : m_environment.ExceptionClear();
112 0 : }
113 : }
114 0 : }
115 : //........................................................................
116 : } } // namespace connectivity::jdbc
117 : //........................................................................
118 :
119 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|