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