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