Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <string.h>
22 : #include <cstdarg>
23 : #include "java/tools.hxx"
24 : #include "java/lang/String.hxx"
25 : #include "java/lang/Class.hxx"
26 : #include "java/util/Property.hxx"
27 : #include <com/sun/star/sdbc/DriverPropertyInfo.hpp>
28 : #include <com/sun/star/container/XNameAccess.hpp>
29 : #include <connectivity/dbexception.hxx>
30 :
31 : using namespace connectivity;
32 : using namespace ::com::sun::star::uno;
33 : using namespace ::com::sun::star::beans;
34 : using namespace ::com::sun::star::sdbc;
35 : using namespace ::com::sun::star::container;
36 : using namespace ::com::sun::star::lang;
37 :
38 36 : void java_util_Properties::setProperty(const OUString& key, const OUString& value)
39 : {
40 36 : SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
41 36 : jobject out(0);
42 :
43 : {
44 : jvalue args[2];
45 : // Convert Parameter
46 36 : args[0].l = convertwchar_tToJavaString(t.pEnv,key);
47 36 : args[1].l = convertwchar_tToJavaString(t.pEnv,value);
48 : // Initialize temporary Variables
49 : static const char * cSignature = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;";
50 : static const char * cMethodName = "setProperty";
51 : // Turn off Java-Call
52 : static jmethodID mID(NULL);
53 36 : obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
54 36 : out = t.pEnv->CallObjectMethod(object, mID, args[0].l,args[1].l);
55 36 : ThrowSQLException(t.pEnv,NULL);
56 36 : t.pEnv->DeleteLocalRef(static_cast<jstring>(args[1].l));
57 36 : t.pEnv->DeleteLocalRef(static_cast<jstring>(args[0].l));
58 36 : ThrowSQLException(t.pEnv,0);
59 36 : if(out)
60 0 : t.pEnv->DeleteLocalRef(out);
61 36 : } //t.pEnv
62 : // WARNING: The caller will be owner of the returned pointers!!!
63 36 : }
64 : jclass java_util_Properties::theClass = 0;
65 :
66 18 : java_util_Properties::~java_util_Properties()
67 18 : {}
68 :
69 17 : jclass java_util_Properties::getMyClass() const
70 : {
71 : // the class needs only be called once, that is why it is static
72 17 : if( !theClass )
73 4 : theClass = findMyClass("java/util/Properties");
74 17 : return theClass;
75 : }
76 :
77 :
78 9 : java_util_Properties::java_util_Properties( ): java_lang_Object( NULL, nullptr )
79 : {
80 9 : SDBThreadAttach t;
81 9 : if( !t.pEnv )
82 9 : return;
83 : // Turn off Java-Call for the constructor
84 : // Initialize temperary Variables
85 : static const char * cSignature = "()V";
86 : jobject tempObj;
87 : static jmethodID mID(NULL);
88 9 : obtainMethodId_throwSQL(t.pEnv, "<init>",cSignature, mID);
89 9 : tempObj = t.pEnv->NewObject( getMyClass(), mID);
90 9 : saveRef( t.pEnv, tempObj );
91 9 : t.pEnv->DeleteLocalRef( tempObj );
92 : }
93 :
94 :
95 1585 : jstring connectivity::convertwchar_tToJavaString(JNIEnv *pEnv,const OUString& _rTemp)
96 : {
97 : OSL_ENSURE(pEnv,"Environment is NULL!");
98 1585 : jstring pStr = pEnv->NewString(_rTemp.getStr(), _rTemp.getLength());
99 1585 : pEnv->ExceptionClear();
100 : OSL_ENSURE(pStr,"Could not create a jsstring object!");
101 1585 : return pStr;
102 : }
103 :
104 :
105 9 : java_util_Properties* connectivity::createStringPropertyArray(const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
106 : {
107 9 : java_util_Properties* pProps = new java_util_Properties();
108 9 : const PropertyValue* pBegin = info.getConstArray();
109 9 : const PropertyValue* pEnd = pBegin + info.getLength();
110 :
111 99 : for(;pBegin != pEnd;++pBegin)
112 : {
113 : // these are properties used internally by LibreOffice,
114 : // and should not be passed to the JDBC driver
115 : // (which probably does not know anything about them anyway).
116 180 : if ( pBegin->Name != "JavaDriverClass"
117 81 : && pBegin->Name != "JavaDriverClassPath"
118 72 : && pBegin->Name != "SystemProperties"
119 63 : && pBegin->Name != "CharSet"
120 63 : && pBegin->Name != "AppendTableAliasName"
121 63 : && pBegin->Name != "AddIndexAppendix"
122 63 : && pBegin->Name != "FormsCheckRequiredFields"
123 63 : && pBegin->Name != "GenerateASBeforeCorrelationName"
124 63 : && pBegin->Name != "EscapeDateTime"
125 63 : && pBegin->Name != "ParameterNameSubstitution"
126 63 : && pBegin->Name != "IsPasswordRequired"
127 63 : && pBegin->Name != "IsAutoRetrievingEnabled"
128 54 : && pBegin->Name != "AutoRetrievingStatement"
129 45 : && pBegin->Name != "UseCatalogInSelect"
130 45 : && pBegin->Name != "UseSchemaInSelect"
131 45 : && pBegin->Name != "AutoIncrementCreation"
132 45 : && pBegin->Name != "Extension"
133 45 : && pBegin->Name != "NoNameLengthLimit"
134 45 : && pBegin->Name != "EnableSQL92Check"
135 45 : && pBegin->Name != "EnableOuterJoinEscape"
136 45 : && pBegin->Name != "BooleanComparisonMode"
137 45 : && pBegin->Name != "IgnoreCurrency"
138 45 : && pBegin->Name != "TypeInfoSettings"
139 45 : && pBegin->Name != "IgnoreDriverPrivileges"
140 36 : && pBegin->Name != "ImplicitCatalogRestriction"
141 36 : && pBegin->Name != "ImplicitSchemaRestriction"
142 36 : && pBegin->Name != "SupportsTableCreation"
143 36 : && pBegin->Name != "UseJava"
144 36 : && pBegin->Name != "Authentication"
145 36 : && pBegin->Name != "PreferDosLikeLineEnds"
146 36 : && pBegin->Name != "PrimaryKeySupport"
147 126 : && pBegin->Name != "RespectDriverResultSetType"
148 : )
149 : {
150 36 : OUString aStr;
151 36 : OSL_VERIFY( pBegin->Value >>= aStr );
152 36 : pProps->setProperty(pBegin->Name,aStr);
153 : }
154 : }
155 9 : return pProps;
156 : }
157 :
158 48665 : OUString connectivity::JavaString2String(JNIEnv *pEnv,jstring _Str)
159 : {
160 48665 : OUString aStr;
161 48665 : if(_Str)
162 : {
163 48274 : jboolean bCopy(sal_True);
164 48274 : const jchar* pChar = pEnv->GetStringChars(_Str,&bCopy);
165 48274 : jsize len = pEnv->GetStringLength(_Str);
166 48274 : aStr = OUString(pChar,len);
167 :
168 48274 : if(bCopy)
169 48274 : pEnv->ReleaseStringChars(_Str,pChar);
170 48274 : pEnv->DeleteLocalRef(_Str);
171 : }
172 48665 : return aStr;
173 : }
174 :
175 0 : jobject connectivity::convertTypeMapToJavaMap(JNIEnv* /*pEnv*/,const Reference< ::com::sun::star::container::XNameAccess > & _rMap)
176 : {
177 0 : if ( _rMap.is() )
178 : {
179 0 : ::com::sun::star::uno::Sequence< OUString > aNames = _rMap->getElementNames();
180 0 : if ( aNames.getLength() > 0 )
181 0 : ::dbtools::throwFeatureNotImplementedSQLException( "Type maps", NULL );
182 : }
183 0 : return 0;
184 : }
185 :
186 900 : bool connectivity::isExceptionOccurred(JNIEnv *pEnv,bool _bClear)
187 : {
188 900 : if ( !pEnv )
189 0 : return false;
190 :
191 900 : jthrowable pThrowable = pEnv->ExceptionOccurred();
192 900 : bool bRet = pThrowable != NULL;
193 900 : if ( pThrowable )
194 : {
195 0 : if ( _bClear )
196 0 : pEnv->ExceptionClear();
197 0 : pEnv->DeleteLocalRef(pThrowable);
198 : }
199 :
200 900 : return bRet;
201 : }
202 :
203 0 : jobject connectivity::createByteInputStream(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x,sal_Int32 length)
204 : {
205 0 : SDBThreadAttach t;
206 0 : if( !t.pEnv || !x.is() )
207 0 : return NULL;
208 : // Turn off Java-Call for the constructor
209 : // Initialize temperary variables
210 0 : jclass clazz = java_lang_Object::findMyClass("java/io/ByteArrayInputStream");
211 : static jmethodID mID(NULL);
212 0 : if ( !mID )
213 : {
214 : static const char * cSignature = "([B)V";
215 0 : mID = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
216 : OSL_ENSURE( mID, cSignature );
217 0 : if ( !mID )
218 0 : throw SQLException();
219 : } // if ( !_inout_MethodID )
220 0 : jbyteArray pByteArray = t.pEnv->NewByteArray(length);
221 0 : Sequence< sal_Int8 > aData;
222 0 : x->readBytes(aData,length);
223 0 : jboolean p = sal_False;
224 0 : memcpy(t.pEnv->GetByteArrayElements(pByteArray,&p),aData.getArray(),aData.getLength());
225 0 : jobject out = t.pEnv->NewObject( clazz, mID,pByteArray);
226 0 : t.pEnv->DeleteLocalRef(pByteArray);
227 0 : return out;
228 : }
229 :
230 0 : jobject connectivity::createCharArrayReader(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x,sal_Int32 length)
231 : {
232 0 : SDBThreadAttach t;
233 0 : if( !t.pEnv || !x.is() )
234 0 : return NULL;
235 : // Turn off Java-Call for the constructor
236 : // Initialize temperary Variables
237 0 : jclass clazz = java_lang_Object::findMyClass("java/io/CharArrayReader");
238 : static jmethodID mID(NULL);
239 0 : if ( !mID )
240 : {
241 : static const char * cSignature = "([C)V";
242 0 : mID = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
243 : OSL_ENSURE( mID, cSignature );
244 0 : if ( !mID )
245 0 : throw SQLException();
246 : } // if ( !_inout_MethodID )
247 0 : jcharArray pCharArray = t.pEnv->NewCharArray(length);
248 0 : Sequence< sal_Int8 > aData;
249 0 : x->readBytes(aData,length);
250 0 : jboolean p = sal_False;
251 0 : memcpy(t.pEnv->GetCharArrayElements(pCharArray,&p),aData.getArray(),aData.getLength());
252 0 : jobject out = t.pEnv->NewObject( clazz, mID,pCharArray);
253 0 : t.pEnv->DeleteLocalRef(pCharArray);
254 0 : return out;
255 : }
256 :
257 :
258 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|