xref: /AOO41X/main/connectivity/source/inc/java/lang/Object.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef _CONNECTIVITY_JAVA_LANG_OBJECT_HXX_
28*cdf0e10cSrcweir #define	_CONNECTIVITY_JAVA_LANG_OBJECT_HXX_
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #if STLPORT_VERSION>=321
31*cdf0e10cSrcweir // jni.h needs cstdarg for std::va_list
32*cdf0e10cSrcweir #include <cstdarg>
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir #include <osl/thread.h>
35*cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLException.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37*cdf0e10cSrcweir #include <osl/diagnose.h>
38*cdf0e10cSrcweir #include <jvmaccess/virtualmachine.hxx>
39*cdf0e10cSrcweir #include <memory>
40*cdf0e10cSrcweir #include <functional>
41*cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //=====================================================================
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #ifdef HAVE_64BIT_POINTERS
46*cdf0e10cSrcweir #error "no 64 bit pointer"
47*cdf0e10cSrcweir #else
48*cdf0e10cSrcweir #ifdef OS2
49*cdf0e10cSrcweir #define PVOID_TO_INT64(x) (jlong)(sal_Int32)x
50*cdf0e10cSrcweir #define INT64_TO_PVOID(x) (void *)x
51*cdf0e10cSrcweir #endif // OS2
52*cdf0e10cSrcweir #endif //HAVE_64BIT_POINTERS
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir namespace comphelper
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     class ResourceBasedEventLogger;
57*cdf0e10cSrcweir }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir namespace connectivity
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir 	typedef ::boost::shared_ptr< jvmaccess::VirtualMachine::AttachGuard> TGuard;
62*cdf0e10cSrcweir 	class SDBThreadAttach
63*cdf0e10cSrcweir 	{
64*cdf0e10cSrcweir 		jvmaccess::VirtualMachine::AttachGuard m_aGuard;
65*cdf0e10cSrcweir 		SDBThreadAttach(SDBThreadAttach&);
66*cdf0e10cSrcweir         SDBThreadAttach& operator= (SDBThreadAttach&);
67*cdf0e10cSrcweir 	public:
68*cdf0e10cSrcweir 		SDBThreadAttach();
69*cdf0e10cSrcweir 		~SDBThreadAttach();
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 		JNIEnv*	pEnv;
72*cdf0e10cSrcweir 		static void addRef();
73*cdf0e10cSrcweir 		static void releaseRef();
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     public:
76*cdf0e10cSrcweir         JNIEnv& env() const
77*cdf0e10cSrcweir         {
78*cdf0e10cSrcweir             // according to the documentation of jvmaccess::VirtualMachine::AttachGuard, our env is never
79*cdf0e10cSrcweir             // NULL, so why bothering with pointer checks?
80*cdf0e10cSrcweir             return *pEnv;
81*cdf0e10cSrcweir         }
82*cdf0e10cSrcweir 	};
83*cdf0e10cSrcweir 	//=====================================================================
84*cdf0e10cSrcweir     //=====================================================================
85*cdf0e10cSrcweir 	class java_lang_Class;
86*cdf0e10cSrcweir 	class  java_lang_Object
87*cdf0e10cSrcweir 	{
88*cdf0e10cSrcweir 		// Zuweisungsoperator und Copy Konstruktor sind verboten
89*cdf0e10cSrcweir 		java_lang_Object& operator= (java_lang_Object&);
90*cdf0e10cSrcweir 		java_lang_Object(java_lang_Object&);
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 		// nur zum Zerstoeren des C++ Pointers in vom JSbxObject
93*cdf0e10cSrcweir 		// abgeleiteten Java Objekten
94*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xFactory;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 	protected:
97*cdf0e10cSrcweir 		// der JAVA Handle zu dieser Klasse
98*cdf0e10cSrcweir 		jobject object;
99*cdf0e10cSrcweir 		// Klassendefinition
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 		// neu in SJ2:
102*cdf0e10cSrcweir 		static jclass theClass;				// die Klasse braucht nur einmal angefordert werden !
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         virtual jclass getMyClass() const;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 	public:
107*cdf0e10cSrcweir 		// der Konstruktor, der fuer die abgeleiteten Klassen verwendet
108*cdf0e10cSrcweir 		// werden soll.
109*cdf0e10cSrcweir 		java_lang_Object( JNIEnv * pEnv, jobject myObj );
110*cdf0e10cSrcweir 		// der eigentliche Konstruktor
111*cdf0e10cSrcweir         java_lang_Object(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory=NULL);
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		virtual ~java_lang_Object();
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 		void				saveRef( JNIEnv * pEnv, jobject myObj );
116*cdf0e10cSrcweir 		jobject				getJavaObject() const { return object; }
117*cdf0e10cSrcweir 		java_lang_Object *	GetWrapper() { return this; }
118*cdf0e10cSrcweir 		void clearObject(JNIEnv& rEnv);
119*cdf0e10cSrcweir 		void clearObject();
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 		virtual ::rtl::OUString toString() const;
122*cdf0e10cSrcweir 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > getORB() { return m_xFactory; }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir         static void ThrowSQLException(JNIEnv * pEnv,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> & _rContext);
125*cdf0e10cSrcweir         static void ThrowLoggedSQLException(
126*cdf0e10cSrcweir             const ::comphelper::ResourceBasedEventLogger& _rLogger,
127*cdf0e10cSrcweir             JNIEnv* pEnvironment,
128*cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext
129*cdf0e10cSrcweir         );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		static ::rtl::Reference< jvmaccess::VirtualMachine > getVM(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory=NULL);
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         static jclass   findMyClass(const char* _pClassName);
134*cdf0e10cSrcweir         void            obtainMethodId(JNIEnv* _pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID) const;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         sal_Bool        callBooleanMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const;
137*cdf0e10cSrcweir         sal_Bool        callBooleanMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const;
138*cdf0e10cSrcweir         jobject         callResultSetMethod( JNIEnv& _rEnv, const char* _pMethodName, jmethodID& _inout_MethodID ) const;
139*cdf0e10cSrcweir         sal_Int32       callIntMethod( const char* _pMethodName, jmethodID& _inout_MethodID,bool _bIgnoreException = false ) const;
140*cdf0e10cSrcweir         sal_Int32       callIntMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const;
141*cdf0e10cSrcweir         sal_Int32       callIntMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID,const ::rtl::OUString& _nArgument ) const;
142*cdf0e10cSrcweir         ::rtl::OUString callStringMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const;
143*cdf0e10cSrcweir         ::rtl::OUString callStringMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const;
144*cdf0e10cSrcweir         void            callVoidMethod( const char* _pMethodName, jmethodID& _inout_MethodID) const;
145*cdf0e10cSrcweir         void            callVoidMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument,bool _bIgnoreException = false ) const;
146*cdf0e10cSrcweir         void            callVoidMethodWithBoolArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument,bool _bIgnoreException = false ) const;
147*cdf0e10cSrcweir         void            callVoidMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID, const ::rtl::OUString& _nArgument ) const;
148*cdf0e10cSrcweir         jobject         callObjectMethod( JNIEnv * pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID ) const;
149*cdf0e10cSrcweir         jobject         callObjectMethodWithIntArg( JNIEnv * pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         template< typename T >
152*cdf0e10cSrcweir                         T callMethodWithIntArg(T (JNIEnv::*pCallMethod)( jobject obj, jmethodID methodID, ... ) ,const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             SDBThreadAttach t;
155*cdf0e10cSrcweir             obtainMethodId(t.pEnv, _pMethodName,_pSignature, _inout_MethodID);
156*cdf0e10cSrcweir             T out = (t.pEnv->*pCallMethod)( object, _inout_MethodID,_nArgument);
157*cdf0e10cSrcweir 			ThrowSQLException( t.pEnv, NULL );
158*cdf0e10cSrcweir             return out;
159*cdf0e10cSrcweir         }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir         template< typename T >
162*cdf0e10cSrcweir                         void callVoidMethod(const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID,sal_Int32 _nArgument, const T& _aValue) const
163*cdf0e10cSrcweir         {
164*cdf0e10cSrcweir             SDBThreadAttach t;
165*cdf0e10cSrcweir             obtainMethodId(t.pEnv, _pMethodName,_pSignature, _inout_MethodID);
166*cdf0e10cSrcweir             t.pEnv->CallVoidMethod( object, _inout_MethodID,_nArgument,_aValue);
167*cdf0e10cSrcweir 			ThrowSQLException( t.pEnv, NULL );
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 	};
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir #endif //_CONNECTIVITY_JAVA_LANG_OBJJECT_HXX_
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 
176