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