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 #ifndef _CONNECTIVITY_JAVA_LANG_OBJECT_HXX_
24 #define	_CONNECTIVITY_JAVA_LANG_OBJECT_HXX_
25 
26 // jni.h needs cstdarg for std::va_list
27 #include <cstdarg>
28 #include <osl/thread.h>
29 #include <com/sun/star/sdbc/SQLException.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <osl/diagnose.h>
32 #include <jvmaccess/virtualmachine.hxx>
33 #include <memory>
34 #include <functional>
35 #include <boost/shared_ptr.hpp>
36 
37 //=====================================================================
38 
39 #ifdef HAVE_64BIT_POINTERS
40 #error "no 64 bit pointer"
41 #else
42 #ifdef OS2
43 #define PVOID_TO_INT64(x) (jlong)(sal_Int32)x
44 #define INT64_TO_PVOID(x) (void *)x
45 #endif // OS2
46 #endif //HAVE_64BIT_POINTERS
47 
48 namespace comphelper
49 {
50     class ResourceBasedEventLogger;
51 }
52 
53 namespace connectivity
54 {
55 	typedef ::boost::shared_ptr< jvmaccess::VirtualMachine::AttachGuard> TGuard;
56 	class SDBThreadAttach
57 	{
58 		jvmaccess::VirtualMachine::AttachGuard m_aGuard;
59 		SDBThreadAttach(SDBThreadAttach&);
60         SDBThreadAttach& operator= (SDBThreadAttach&);
61 	public:
62 		SDBThreadAttach();
63 		~SDBThreadAttach();
64 
65 		JNIEnv*	pEnv;
66 		static void addRef();
67 		static void releaseRef();
68 
69     public:
env() const70         JNIEnv& env() const
71         {
72             // according to the documentation of jvmaccess::VirtualMachine::AttachGuard, our env is never
73             // NULL, so why bothering with pointer checks?
74             return *pEnv;
75         }
76 	};
77 	//=====================================================================
78     //=====================================================================
79 	class java_lang_Class;
80 	class  java_lang_Object
81 	{
82 		// Zuweisungsoperator und Copy Konstruktor sind verboten
83 		java_lang_Object& operator= (java_lang_Object&);
84 		java_lang_Object(java_lang_Object&);
85 
86 		// nur zum Zerstoeren des C++ Pointers in vom JSbxObject
87 		// abgeleiteten Java Objekten
88         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xFactory;
89 
90 	protected:
91 		// der JAVA Handle zu dieser Klasse
92 		jobject object;
93 		// Klassendefinition
94 
95 		// neu in SJ2:
96 		static jclass theClass;				// die Klasse braucht nur einmal angefordert werden !
97 
98         virtual jclass getMyClass() const;
99 
100 	public:
101 		// der Konstruktor, der fuer die abgeleiteten Klassen verwendet
102 		// werden soll.
103 		java_lang_Object( JNIEnv * pEnv, jobject myObj );
104 		// der eigentliche Konstruktor
105         java_lang_Object(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory=NULL);
106 
107 		virtual ~java_lang_Object();
108 
109 		void				saveRef( JNIEnv * pEnv, jobject myObj );
getJavaObject() const110 		jobject				getJavaObject() const { return object; }
GetWrapper()111 		java_lang_Object *	GetWrapper() { return this; }
112 		void clearObject(JNIEnv& rEnv);
113 		void clearObject();
114 
115 		virtual ::rtl::OUString toString() const;
getORB()116 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > getORB() { return m_xFactory; }
117 
118         static void ThrowSQLException(JNIEnv * pEnv,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> & _rContext);
119         static void ThrowLoggedSQLException(
120             const ::comphelper::ResourceBasedEventLogger& _rLogger,
121             JNIEnv* pEnvironment,
122             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext
123         );
124 
125 		static ::rtl::Reference< jvmaccess::VirtualMachine > getVM(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory=NULL);
126 
127         static jclass   findMyClass(const char* _pClassName);
128         void            obtainMethodId(JNIEnv* _pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID) const;
129 
130         sal_Bool        callBooleanMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const;
131         sal_Bool        callBooleanMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const;
132         jobject         callResultSetMethod( JNIEnv& _rEnv, const char* _pMethodName, jmethodID& _inout_MethodID ) const;
133         sal_Int32       callIntMethod( const char* _pMethodName, jmethodID& _inout_MethodID,bool _bIgnoreException = false ) const;
134         sal_Int32       callIntMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const;
135         sal_Int32       callIntMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID,const ::rtl::OUString& _nArgument ) const;
136         ::rtl::OUString callStringMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const;
137         ::rtl::OUString callStringMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const;
138         void            callVoidMethod( const char* _pMethodName, jmethodID& _inout_MethodID) const;
139         void            callVoidMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument,bool _bIgnoreException = false ) const;
140         void            callVoidMethodWithBoolArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument,bool _bIgnoreException = false ) const;
141         void            callVoidMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID, const ::rtl::OUString& _nArgument ) const;
142         jobject         callObjectMethod( JNIEnv * pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID ) const;
143         jobject         callObjectMethodWithIntArg( JNIEnv * pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const;
144 
145         template< typename T >
callMethodWithIntArg(T (JNIEnv::* pCallMethod)(jobject obj,jmethodID methodID,...),const char * _pMethodName,const char * _pSignature,jmethodID & _inout_MethodID,sal_Int32 _nArgument) const146                         T callMethodWithIntArg(T (JNIEnv::*pCallMethod)( jobject obj, jmethodID methodID, ... ) ,const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const
147         {
148             SDBThreadAttach t;
149             obtainMethodId(t.pEnv, _pMethodName,_pSignature, _inout_MethodID);
150             T out = (t.pEnv->*pCallMethod)( object, _inout_MethodID,_nArgument);
151 			ThrowSQLException( t.pEnv, NULL );
152             return out;
153         }
154 
155         template< typename T >
callVoidMethod(const char * _pMethodName,const char * _pSignature,jmethodID & _inout_MethodID,sal_Int32 _nArgument,const T & _aValue) const156                         void callVoidMethod(const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID,sal_Int32 _nArgument, const T& _aValue) const
157         {
158             SDBThreadAttach t;
159             obtainMethodId(t.pEnv, _pMethodName,_pSignature, _inout_MethodID);
160             t.pEnv->CallVoidMethod( object, _inout_MethodID,_nArgument,_aValue);
161 			ThrowSQLException( t.pEnv, NULL );
162         }
163 
164 
165 	};
166 }
167 #endif //_CONNECTIVITY_JAVA_LANG_OBJJECT_HXX_
168 
169 
170