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