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