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_GLOBALREF_HXX
25 #define CONNECTIVITY_GLOBALREF_HXX
26 
27 #include "java/LocalRef.hxx"
28 #include "java/lang/Object.hxx"
29 
30 /** === begin UNO includes === **/
31 /** === end UNO includes === **/
32 
33 #include <jvmaccess/virtualmachine.hxx>
34 
35 //........................................................................
36 namespace connectivity { namespace jdbc
37 {
38 //........................................................................
39 
40 	//====================================================================
41 	//= GlobalRef
42 	//====================================================================
43     /** helper class to hold a local ref to a JNI object
44     */
45     template< typename T >
46     class GlobalRef
47     {
48     public:
GlobalRef()49         GlobalRef()
50             :m_object( NULL )
51         {
52         }
53 
GlobalRef(const GlobalRef & _source)54         GlobalRef( const GlobalRef& _source )
55             :m_object( NULL )
56         {
57             *this = _source;
58         }
59 
operator =(const GlobalRef & _source)60         GlobalRef& operator=( const GlobalRef& _source )
61         {
62             if ( &_source == this )
63                 return *this;
64 
65             SDBThreadAttach t;
66             set( t.env(), _source.get() );
67             return *this;
68         }
69 
~GlobalRef()70         ~GlobalRef()
71         {
72             reset();
73         }
74 
reset()75         void reset()
76         {
77             if ( m_object != NULL )
78             {
79                 SDBThreadAttach t;
80                 t.env().DeleteGlobalRef( m_object );
81                 m_object = NULL;
82             }
83         }
84 
set(JNIEnv & _environment,T _object)85         void set( JNIEnv& _environment, T _object )
86         {
87             if ( m_object != NULL )
88                 _environment.DeleteGlobalRef( m_object );
89             m_object = _object;
90             if ( m_object )
91                 m_object = static_cast< T >( _environment.NewGlobalRef( m_object ) );
92         }
93 
set(LocalRef<T> & _object)94         void set( LocalRef< T >& _object )
95         {
96             set( _object.env(), _object.release() );
97         }
98 
get() const99         T get() const
100         {
101             return m_object;
102         }
103 
is() const104         bool is() const
105         {
106             return m_object != NULL;
107         }
108 
109     private:
110         T   m_object;
111     };
112 
113 
114 //........................................................................
115 } } // namespace connectivity::jdbc
116 //........................................................................
117 
118 #endif // CONNECTIVITY_GLOBALREF_HXX
119