1*caf5cd79SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*caf5cd79SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*caf5cd79SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*caf5cd79SAndrew Rist  * distributed with this work for additional information
6*caf5cd79SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*caf5cd79SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*caf5cd79SAndrew Rist  * "License"); you may not use this file except in compliance
9*caf5cd79SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*caf5cd79SAndrew Rist  *
11*caf5cd79SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*caf5cd79SAndrew Rist  *
13*caf5cd79SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*caf5cd79SAndrew Rist  * software distributed under the License is distributed on an
15*caf5cd79SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*caf5cd79SAndrew Rist  * KIND, either express or implied.  See the License for the
17*caf5cd79SAndrew Rist  * specific language governing permissions and limitations
18*caf5cd79SAndrew Rist  * under the License.
19*caf5cd79SAndrew Rist  *
20*caf5cd79SAndrew Rist  *************************************************************/
21*caf5cd79SAndrew Rist 
22*caf5cd79SAndrew Rist 
23cdf0e10cSrcweir #ifndef _CONNECTIVITY_JAVA_LANG_OBJECT_HXX_
24cdf0e10cSrcweir #define	_CONNECTIVITY_JAVA_LANG_OBJECT_HXX_
25cdf0e10cSrcweir 
26cdf0e10cSrcweir // jni.h needs cstdarg for std::va_list
27cdf0e10cSrcweir #include <cstdarg>
28cdf0e10cSrcweir #include <osl/thread.h>
29cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLException.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31cdf0e10cSrcweir #include <osl/diagnose.h>
32cdf0e10cSrcweir #include <jvmaccess/virtualmachine.hxx>
33cdf0e10cSrcweir #include <memory>
34cdf0e10cSrcweir #include <functional>
35cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //=====================================================================
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #ifdef HAVE_64BIT_POINTERS
40cdf0e10cSrcweir #error "no 64 bit pointer"
41cdf0e10cSrcweir #else
42cdf0e10cSrcweir #ifdef OS2
43cdf0e10cSrcweir #define PVOID_TO_INT64(x) (jlong)(sal_Int32)x
44cdf0e10cSrcweir #define INT64_TO_PVOID(x) (void *)x
45cdf0e10cSrcweir #endif // OS2
46cdf0e10cSrcweir #endif //HAVE_64BIT_POINTERS
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace comphelper
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     class ResourceBasedEventLogger;
51cdf0e10cSrcweir }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir namespace connectivity
54cdf0e10cSrcweir {
55cdf0e10cSrcweir 	typedef ::boost::shared_ptr< jvmaccess::VirtualMachine::AttachGuard> TGuard;
56cdf0e10cSrcweir 	class SDBThreadAttach
57cdf0e10cSrcweir 	{
58cdf0e10cSrcweir 		jvmaccess::VirtualMachine::AttachGuard m_aGuard;
59cdf0e10cSrcweir 		SDBThreadAttach(SDBThreadAttach&);
60cdf0e10cSrcweir         SDBThreadAttach& operator= (SDBThreadAttach&);
61cdf0e10cSrcweir 	public:
62cdf0e10cSrcweir 		SDBThreadAttach();
63cdf0e10cSrcweir 		~SDBThreadAttach();
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 		JNIEnv*	pEnv;
66cdf0e10cSrcweir 		static void addRef();
67cdf0e10cSrcweir 		static void releaseRef();
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     public:
env() const70cdf0e10cSrcweir         JNIEnv& env() const
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir             // according to the documentation of jvmaccess::VirtualMachine::AttachGuard, our env is never
73cdf0e10cSrcweir             // NULL, so why bothering with pointer checks?
74cdf0e10cSrcweir             return *pEnv;
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir 	};
77cdf0e10cSrcweir 	//=====================================================================
78cdf0e10cSrcweir     //=====================================================================
79cdf0e10cSrcweir 	class java_lang_Class;
80cdf0e10cSrcweir 	class  java_lang_Object
81cdf0e10cSrcweir 	{
82cdf0e10cSrcweir 		// Zuweisungsoperator und Copy Konstruktor sind verboten
83cdf0e10cSrcweir 		java_lang_Object& operator= (java_lang_Object&);
84cdf0e10cSrcweir 		java_lang_Object(java_lang_Object&);
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 		// nur zum Zerstoeren des C++ Pointers in vom JSbxObject
87cdf0e10cSrcweir 		// abgeleiteten Java Objekten
88cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xFactory;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	protected:
91cdf0e10cSrcweir 		// der JAVA Handle zu dieser Klasse
92cdf0e10cSrcweir 		jobject object;
93cdf0e10cSrcweir 		// Klassendefinition
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 		// neu in SJ2:
96cdf0e10cSrcweir 		static jclass theClass;				// die Klasse braucht nur einmal angefordert werden !
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         virtual jclass getMyClass() const;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	public:
101cdf0e10cSrcweir 		// der Konstruktor, der fuer die abgeleiteten Klassen verwendet
102cdf0e10cSrcweir 		// werden soll.
103cdf0e10cSrcweir 		java_lang_Object( JNIEnv * pEnv, jobject myObj );
104cdf0e10cSrcweir 		// der eigentliche Konstruktor
105cdf0e10cSrcweir         java_lang_Object(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory=NULL);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 		virtual ~java_lang_Object();
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		void				saveRef( JNIEnv * pEnv, jobject myObj );
getJavaObject() const110cdf0e10cSrcweir 		jobject				getJavaObject() const { return object; }
GetWrapper()111cdf0e10cSrcweir 		java_lang_Object *	GetWrapper() { return this; }
112cdf0e10cSrcweir 		void clearObject(JNIEnv& rEnv);
113cdf0e10cSrcweir 		void clearObject();
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 		virtual ::rtl::OUString toString() const;
getORB()116cdf0e10cSrcweir 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > getORB() { return m_xFactory; }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         static void ThrowSQLException(JNIEnv * pEnv,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> & _rContext);
119cdf0e10cSrcweir         static void ThrowLoggedSQLException(
120cdf0e10cSrcweir             const ::comphelper::ResourceBasedEventLogger& _rLogger,
121cdf0e10cSrcweir             JNIEnv* pEnvironment,
122cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext
123cdf0e10cSrcweir         );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 		static ::rtl::Reference< jvmaccess::VirtualMachine > getVM(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory=NULL);
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         static jclass   findMyClass(const char* _pClassName);
128cdf0e10cSrcweir         void            obtainMethodId(JNIEnv* _pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID) const;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         sal_Bool        callBooleanMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const;
131cdf0e10cSrcweir         sal_Bool        callBooleanMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const;
132cdf0e10cSrcweir         jobject         callResultSetMethod( JNIEnv& _rEnv, const char* _pMethodName, jmethodID& _inout_MethodID ) const;
133cdf0e10cSrcweir         sal_Int32       callIntMethod( const char* _pMethodName, jmethodID& _inout_MethodID,bool _bIgnoreException = false ) const;
134cdf0e10cSrcweir         sal_Int32       callIntMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const;
135cdf0e10cSrcweir         sal_Int32       callIntMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID,const ::rtl::OUString& _nArgument ) const;
136cdf0e10cSrcweir         ::rtl::OUString callStringMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const;
137cdf0e10cSrcweir         ::rtl::OUString callStringMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const;
138cdf0e10cSrcweir         void            callVoidMethod( const char* _pMethodName, jmethodID& _inout_MethodID) const;
139cdf0e10cSrcweir         void            callVoidMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument,bool _bIgnoreException = false ) const;
140cdf0e10cSrcweir         void            callVoidMethodWithBoolArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument,bool _bIgnoreException = false ) const;
141cdf0e10cSrcweir         void            callVoidMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID, const ::rtl::OUString& _nArgument ) const;
142cdf0e10cSrcweir         jobject         callObjectMethod( JNIEnv * pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID ) const;
143cdf0e10cSrcweir         jobject         callObjectMethodWithIntArg( JNIEnv * pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         template< typename T >
callMethodWithIntArg(T (JNIEnv::* pCallMethod)(jobject obj,jmethodID methodID,...),const char * _pMethodName,const char * _pSignature,jmethodID & _inout_MethodID,sal_Int32 _nArgument) const146cdf0e10cSrcweir                         T callMethodWithIntArg(T (JNIEnv::*pCallMethod)( jobject obj, jmethodID methodID, ... ) ,const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             SDBThreadAttach t;
149cdf0e10cSrcweir             obtainMethodId(t.pEnv, _pMethodName,_pSignature, _inout_MethodID);
150cdf0e10cSrcweir             T out = (t.pEnv->*pCallMethod)( object, _inout_MethodID,_nArgument);
151cdf0e10cSrcweir 			ThrowSQLException( t.pEnv, NULL );
152cdf0e10cSrcweir             return out;
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         template< typename T >
callVoidMethod(const char * _pMethodName,const char * _pSignature,jmethodID & _inout_MethodID,sal_Int32 _nArgument,const T & _aValue) const156cdf0e10cSrcweir                         void callVoidMethod(const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID,sal_Int32 _nArgument, const T& _aValue) const
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             SDBThreadAttach t;
159cdf0e10cSrcweir             obtainMethodId(t.pEnv, _pMethodName,_pSignature, _inout_MethodID);
160cdf0e10cSrcweir             t.pEnv->CallVoidMethod( object, _inout_MethodID,_nArgument,_aValue);
161cdf0e10cSrcweir 			ThrowSQLException( t.pEnv, NULL );
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	};
166cdf0e10cSrcweir }
167cdf0e10cSrcweir #endif //_CONNECTIVITY_JAVA_LANG_OBJJECT_HXX_
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 
170