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 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 
31 #include <cstdarg>
32 #include "java/tools.hxx"
33 #include "java/lang/String.hxx"
34 #include "java/lang/Class.hxx"
35 #include "java/util/Property.hxx"
36 #include <com/sun/star/sdbc/DriverPropertyInfo.hpp>
37 #include <com/sun/star/container/XNameAccess.hpp>
38 #include <connectivity/dbexception.hxx>
39 
40 using namespace connectivity;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 //	using namespace ::com::sun::star::sdbcx;
44 using namespace ::com::sun::star::sdbc;
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::lang;
47 
48 void java_util_Properties::setProperty(const ::rtl::OUString key, const ::rtl::OUString& value)
49 {
50     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
51 	jobject out(0);
52 
53 	{
54 		jvalue args[2];
55 		// Parameter konvertieren
56 		args[0].l = convertwchar_tToJavaString(t.pEnv,key);
57 		args[1].l = convertwchar_tToJavaString(t.pEnv,value);
58 		// temporaere Variable initialisieren
59 		static const char * cSignature = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;";
60 		static const char * cMethodName = "setProperty";
61 		// Java-Call absetzen
62 		static jmethodID mID(NULL);
63         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
64 		out = t.pEnv->CallObjectMethod(object, mID, args[0].l,args[1].l);
65 		ThrowSQLException(t.pEnv,NULL);
66 		t.pEnv->DeleteLocalRef((jstring)args[1].l);
67 		t.pEnv->DeleteLocalRef((jstring)args[0].l);
68 		ThrowSQLException(t.pEnv,0);
69 		if(out)
70 			t.pEnv->DeleteLocalRef(out);
71 	} //t.pEnv
72 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
73 }
74 jclass java_util_Properties::theClass = 0;
75 
76 java_util_Properties::~java_util_Properties()
77 {}
78 
79 jclass java_util_Properties::getMyClass() const
80 {
81 	// die Klasse muss nur einmal geholt werden, daher statisch
82 	if( !theClass )
83         theClass = findMyClass("java/util/Properties");
84 	return theClass;
85 }
86 
87 //--------------------------------------------------------------------------
88 java_util_Properties::java_util_Properties( ): java_lang_Object( NULL, (jobject)NULL )
89 {
90 	SDBThreadAttach t;
91 	if( !t.pEnv )
92 		return;
93 	// Java-Call fuer den Konstruktor absetzen
94 	// temporaere Variable initialisieren
95 	static const char * cSignature = "()V";
96 	jobject tempObj;
97 	static jmethodID mID(NULL);
98     obtainMethodId(t.pEnv, "<init>",cSignature, mID);
99 	tempObj = t.pEnv->NewObject( getMyClass(), mID);
100 	saveRef( t.pEnv, tempObj );
101 	t.pEnv->DeleteLocalRef( tempObj );
102 }
103 
104 // --------------------------------------------------------------------------------
105 jstring connectivity::convertwchar_tToJavaString(JNIEnv *pEnv,const ::rtl::OUString& _rTemp)
106 {
107 	OSL_ENSURE(pEnv,"Environment is NULL!");
108 	jstring pStr = pEnv->NewString(_rTemp.getStr(), _rTemp.getLength());
109 	pEnv->ExceptionClear();
110 	OSL_ENSURE(pStr,"Could not create a jsstring object!");
111 	return pStr;
112 }
113 
114 // --------------------------------------------------------------------------------
115 java_util_Properties* connectivity::createStringPropertyArray(const Sequence< PropertyValue >& info )  throw(SQLException, RuntimeException)
116 {
117 	java_util_Properties* pProps = new java_util_Properties();
118 	const PropertyValue* pBegin	= info.getConstArray();
119 	const PropertyValue* pEnd	= pBegin + info.getLength();
120 
121 	for(;pBegin != pEnd;++pBegin)
122 	{
123 		// this is a special property to find the jdbc driver
124 		if  (   pBegin->Name.compareToAscii( "JavaDriverClass" )
125             &&  pBegin->Name.compareToAscii( "JavaDriverClassPath" )
126             &&  pBegin->Name.compareToAscii( "SystemProperties" )
127 			&&  pBegin->Name.compareToAscii( "CharSet" )
128 			&&  pBegin->Name.compareToAscii( "AppendTableAliasName" )
129 			&&  pBegin->Name.compareToAscii( "AddIndexAppendix" )
130 			&&  pBegin->Name.compareToAscii( "FormsCheckRequiredFields" )
131 			&&  pBegin->Name.compareToAscii( "GenerateASBeforeCorrelationName" )
132 			&&  pBegin->Name.compareToAscii( "EscapeDateTime" )
133 			&&  pBegin->Name.compareToAscii( "ParameterNameSubstitution" )
134 			&&  pBegin->Name.compareToAscii( "IsPasswordRequired" )
135 			&&  pBegin->Name.compareToAscii( "IsAutoRetrievingEnabled" )
136 			&&  pBegin->Name.compareToAscii( "AutoRetrievingStatement" )
137 			&&  pBegin->Name.compareToAscii( "UseCatalogInSelect" )
138 			&&  pBegin->Name.compareToAscii( "UseSchemaInSelect" )
139 			&&  pBegin->Name.compareToAscii( "AutoIncrementCreation" )
140 			&&  pBegin->Name.compareToAscii( "Extension" )
141 			&&  pBegin->Name.compareToAscii( "NoNameLengthLimit" )
142 			&&  pBegin->Name.compareToAscii( "EnableSQL92Check" )
143 			&&  pBegin->Name.compareToAscii( "EnableOuterJoinEscape" )
144 			&&  pBegin->Name.compareToAscii( "BooleanComparisonMode" )
145             &&  pBegin->Name.compareToAscii( "IgnoreCurrency" )
146             &&  pBegin->Name.compareToAscii( "TypeInfoSettings" )
147 			&&  pBegin->Name.compareToAscii( "IgnoreDriverPrivileges" )
148 			&&  pBegin->Name.compareToAscii( "ImplicitCatalogRestriction" )
149 			&&  pBegin->Name.compareToAscii( "ImplicitSchemaRestriction" )
150             &&  pBegin->Name.compareToAscii( "SupportsTableCreation" )
151             &&  pBegin->Name.compareToAscii( "UseJava" )
152             &&  pBegin->Name.compareToAscii( "Authentication" )
153             &&  pBegin->Name.compareToAscii( "PreferDosLikeLineEnds" )
154             &&  pBegin->Name.compareToAscii( "PrimaryKeySupport" )
155             &&  pBegin->Name.compareToAscii( "RespectDriverResultSetType" )
156             )
157 		{
158 			::rtl::OUString aStr;
159 			OSL_VERIFY( pBegin->Value >>= aStr );
160 			pProps->setProperty(pBegin->Name,aStr);
161 		}
162 	}
163 	return pProps;
164 }
165 // --------------------------------------------------------------------------------
166 ::rtl::OUString connectivity::JavaString2String(JNIEnv *pEnv,jstring _Str)
167 {
168 	::rtl::OUString aStr;
169 	if(_Str)
170 	{
171 		jboolean bCopy(sal_True);
172 		const jchar* pChar = pEnv->GetStringChars(_Str,&bCopy);
173 		jsize len = pEnv->GetStringLength(_Str);
174 		aStr = ::rtl::OUString(pChar,len);
175 
176 		if(bCopy)
177 			pEnv->ReleaseStringChars(_Str,pChar);
178 		pEnv->DeleteLocalRef(_Str);
179 	}
180 	return aStr;
181 }
182 // --------------------------------------------------------------------------------
183 jobject connectivity::convertTypeMapToJavaMap(JNIEnv* /*pEnv*/,const Reference< ::com::sun::star::container::XNameAccess > & _rMap)
184 {
185     if ( _rMap.is() )
186     {
187 	    ::com::sun::star::uno::Sequence< ::rtl::OUString > aNames = _rMap->getElementNames();
188         if ( aNames.getLength() > 0 )
189             ::dbtools::throwFeatureNotImplementedException( "Type maps", NULL );
190     }
191 	return 0;
192 }
193 // -----------------------------------------------------------------------------
194 sal_Bool connectivity::isExceptionOccured(JNIEnv *pEnv,sal_Bool _bClear)
195 {
196 	if ( !pEnv )
197 		return sal_False;
198 
199 	jthrowable pThrowable = pEnv->ExceptionOccurred();
200 	sal_Bool bRet = pThrowable != NULL;
201 	if ( pThrowable )
202 	{
203 		if ( _bClear )
204 			pEnv->ExceptionClear();
205 #if OSL_DEBUG_LEVEL > 1
206 		if(pEnv->IsInstanceOf(pThrowable,java_sql_SQLException_BASE::st_getMyClass()))
207 		{
208 
209 			java_sql_SQLException_BASE* pException = new java_sql_SQLException_BASE(pEnv,pThrowable);
210 			::rtl::OUString sError = pException->getMessage();
211 			delete pException;
212 		}
213 #else
214 		pEnv->DeleteLocalRef(pThrowable);
215 #endif
216 
217 	}
218 
219 	return bRet;
220 }
221 // -----------------------------------------------------------------------------
222 jobject connectivity::createByteInputStream(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x,sal_Int32 length)
223 {
224     SDBThreadAttach t;
225 	if( !t.pEnv || !x.is() )
226 		return NULL;
227 	// Java-Call fuer den Konstruktor absetzen
228 	// temporaere Variable initialisieren
229     jclass clazz = java_lang_Object::findMyClass("java/io/ByteArrayInputStream");
230 	static jmethodID mID(NULL);
231     if  ( !mID )
232     {
233         static const char * cSignature = "([B)V";
234         mID  = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
235         OSL_ENSURE( mID, cSignature );
236         if  ( !mID )
237             throw SQLException();
238     } // if  ( !_inout_MethodID )
239     jbyteArray pByteArray = t.pEnv->NewByteArray(length);
240     Sequence< sal_Int8 > aData;
241     x->readBytes(aData,length);
242     jboolean p = sal_False;
243     rtl_copyMemory(t.pEnv->GetByteArrayElements(pByteArray,&p),aData.getArray(),aData.getLength());
244 	jobject out = t.pEnv->NewObject( clazz, mID,pByteArray);
245     t.pEnv->DeleteLocalRef((jbyteArray)pByteArray);
246     return out;
247 }
248 // -----------------------------------------------------------------------------
249 jobject connectivity::createCharArrayReader(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x,sal_Int32 length)
250 {
251     SDBThreadAttach t;
252 	if( !t.pEnv || !x.is() )
253 		return NULL;
254 	// Java-Call fuer den Konstruktor absetzen
255 	// temporaere Variable initialisieren
256     jclass clazz = java_lang_Object::findMyClass("java/io/CharArrayReader");
257 	static jmethodID mID(NULL);
258     if  ( !mID )
259     {
260         static const char * cSignature = "([C)V";
261         mID  = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
262         OSL_ENSURE( mID, cSignature );
263         if  ( !mID )
264             throw SQLException();
265     } // if  ( !_inout_MethodID )
266     jcharArray pCharArray = t.pEnv->NewCharArray(length);
267     Sequence< sal_Int8 > aData;
268     x->readBytes(aData,length);
269     jboolean p = sal_False;
270     rtl_copyMemory(t.pEnv->GetCharArrayElements(pCharArray,&p),aData.getArray(),aData.getLength());
271 	jobject out = t.pEnv->NewObject( clazz, mID,pCharArray);
272     t.pEnv->DeleteLocalRef((jcharArray)pCharArray);
273     return out;
274 }
275 // -----------------------------------------------------------------------------
276