1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef CONNECTIVITY_CONTEXTCLASSLOADER_HXX
29 #define CONNECTIVITY_CONTEXTCLASSLOADER_HXX
30 
31 #include "java/GlobalRef.hxx"
32 
33 /** === begin UNO includes === **/
34 /** === end UNO includes === **/
35 
36 namespace comphelper
37 {
38     class ResourceBasedEventLogger;
39 }
40 
41 //........................................................................
42 namespace connectivity { namespace jdbc
43 {
44 //........................................................................
45 
46 	//====================================================================
47 	//= ContextClassLoaderScope
48 	//====================================================================
49     /**
50     */
51     class ContextClassLoaderScope
52     {
53     public:
54         /** creates the instance. If isActive returns <FALSE/> afterwards, then an exception
55             happend in the JVM, which should be raised as UNO exception by the caller
56 
57             @param  environment
58                 the current JNI environment
59             @param  newClassLoader
60                 the new class loader to set at the current thread
61             @param  _rLoggerForErrors
62                 the logger which should be passed to java_lang_object::ThrowLoggedSQLException in case
63                 an error occurs
64             @param  _rxErrorContext
65                 the context which should be passed to java_lang_object::ThrowLoggedSQLException in case
66                 an error occurs
67 
68         */
69         ContextClassLoaderScope(
70             JNIEnv& environment,
71             const GlobalRef< jobject >& newClassLoader,
72             const ::comphelper::ResourceBasedEventLogger& _rLoggerForErrors,
73             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxErrorContext
74         );
75 
76         ~ContextClassLoaderScope() { pop(true); }
77 
78         void pop() { pop(false); }
79 
80         bool isActive() const
81         {
82             return  ( m_currentThread.is() )
83                 &&  ( m_setContextClassLoaderMethod != NULL );
84         }
85 
86     private:
87         ContextClassLoaderScope(ContextClassLoaderScope &); // not defined
88         void operator =(ContextClassLoaderScope &); // not defined
89 
90         void pop( bool clearExceptions );
91 
92         JNIEnv&                             m_environment;
93         LocalRef< jobject >                 m_currentThread;
94         LocalRef< jobject >                 m_oldContextClassLoader;
95         jmethodID                           m_setContextClassLoaderMethod;
96     };
97 
98 
99 //........................................................................
100 } } // namespace connectivity::jdbc
101 //........................................................................
102 
103 #endif // CONNECTIVITY_CONTEXTCLASSLOADER_HXX
104