1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef CONNECTIVITY_CONTEXTCLASSLOADER_HXX
25 #define CONNECTIVITY_CONTEXTCLASSLOADER_HXX
26 
27 #include "java/GlobalRef.hxx"
28 
29 /** === begin UNO includes === **/
30 /** === end UNO includes === **/
31 
32 namespace comphelper
33 {
34     class ResourceBasedEventLogger;
35 }
36 
37 //........................................................................
38 namespace connectivity { namespace jdbc
39 {
40 //........................................................................
41 
42 	//====================================================================
43 	//= ContextClassLoaderScope
44 	//====================================================================
45     /**
46     */
47     class ContextClassLoaderScope
48     {
49     public:
50         /** creates the instance. If isActive returns <FALSE/> afterwards, then an exception
51             happened in the JVM, which should be raised as UNO exception by the caller
52 
53             @param  environment
54                 the current JNI environment
55             @param  newClassLoader
56                 the new class loader to set at the current thread
57             @param  _rLoggerForErrors
58                 the logger which should be passed to java_lang_object::ThrowLoggedSQLException in case
59                 an error occurs
60             @param  _rxErrorContext
61                 the context which should be passed to java_lang_object::ThrowLoggedSQLException in case
62                 an error occurs
63 
64         */
65         ContextClassLoaderScope(
66             JNIEnv& environment,
67             const GlobalRef< jobject >& newClassLoader,
68             const ::comphelper::ResourceBasedEventLogger& _rLoggerForErrors,
69             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxErrorContext
70         );
71 
~ContextClassLoaderScope()72         ~ContextClassLoaderScope() { pop(true); }
73 
pop()74         void pop() { pop(false); }
75 
isActive() const76         bool isActive() const
77         {
78             return  ( m_currentThread.is() )
79                 &&  ( m_setContextClassLoaderMethod != NULL );
80         }
81 
82     private:
83         ContextClassLoaderScope(ContextClassLoaderScope &); // not defined
84         void operator =(ContextClassLoaderScope &); // not defined
85 
86         void pop( bool clearExceptions );
87 
88         JNIEnv&                             m_environment;
89         LocalRef< jobject >                 m_currentThread;
90         LocalRef< jobject >                 m_oldContextClassLoader;
91         jmethodID                           m_setContextClassLoaderMethod;
92     };
93 
94 
95 //........................................................................
96 } } // namespace connectivity::jdbc
97 //........................................................................
98 
99 #endif // CONNECTIVITY_CONTEXTCLASSLOADER_HXX
100