LibreOffice Module connectivity (master) 1
tools.cxx
Go to the documentation of this file.
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 <java/tools.hxx>
24#include <com/sun/star/sdbc/DriverPropertyInfo.hpp>
25#include <com/sun/star/sdbc/SQLException.hpp>
26#include <com/sun/star/container/XNameAccess.hpp>
28#include <osl/diagnose.h>
29
30using namespace connectivity;
31using namespace ::com::sun::star::uno;
32using namespace ::com::sun::star::beans;
33using namespace ::com::sun::star::sdbc;
34using namespace ::com::sun::star::container;
35using namespace ::com::sun::star::lang;
36
37void java_util_Properties::setProperty(const OUString& key, const OUString& value)
38{
39 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
40
41 {
42 jvalue args[2];
43 // Convert Parameter
44 args[0].l = convertwchar_tToJavaString(t.pEnv,key);
46 // Initialize temporary Variables
47 static const char * const cSignature = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;";
48 static const char * const cMethodName = "setProperty";
49 // Turn off Java-Call
50 static jmethodID mID(nullptr);
51 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
52 jobject out = t.pEnv->CallObjectMethod(object, mID, args[0].l,args[1].l);
53 ThrowSQLException(t.pEnv,nullptr);
54 t.pEnv->DeleteLocalRef(static_cast<jstring>(args[1].l));
55 t.pEnv->DeleteLocalRef(static_cast<jstring>(args[0].l));
56 ThrowSQLException(t.pEnv,nullptr);
57 if(out)
58 t.pEnv->DeleteLocalRef(out);
59 } //t.pEnv
60 // WARNING: The caller will be owner of the returned pointers!!!
61}
62jclass java_util_Properties::theClass = nullptr;
63
65{}
66
68{
69 // the class needs only be called once, that is why it is static
70 if( !theClass )
71 theClass = findMyClass("java/util/Properties");
72 return theClass;
73}
74
75
77{
79 if( !t.pEnv )
80 return;
81 // Turn off Java-Call for the constructor
82 // Initialize temporary Variables
83 static const char * const cSignature = "()V";
84 jobject tempObj;
85 static jmethodID mID(nullptr);
86 obtainMethodId_throwSQL(t.pEnv, "<init>",cSignature, mID);
87 tempObj = t.pEnv->NewObject( getMyClass(), mID);
88 saveRef( t.pEnv, tempObj );
89 t.pEnv->DeleteLocalRef( tempObj );
90}
91
92
93jstring connectivity::convertwchar_tToJavaString(JNIEnv *pEnv,const OUString& _rTemp)
94{
95 OSL_ENSURE(pEnv,"Environment is NULL!");
96 jstring pStr = pEnv->NewString(
97 reinterpret_cast<jchar const *>(_rTemp.getStr()), _rTemp.getLength());
98 pEnv->ExceptionClear();
99 OSL_ENSURE(pStr,"Could not create a jsstring object!");
100 return pStr;
101}
102
103
104std::unique_ptr<java_util_Properties> connectivity::createStringPropertyArray(const Sequence< PropertyValue >& info )
105{
106 std::unique_ptr<java_util_Properties> pProps(new java_util_Properties());
107 const PropertyValue* pBegin = info.getConstArray();
108 const PropertyValue* pEnd = pBegin + info.getLength();
109
110 for(;pBegin != pEnd;++pBegin)
111 {
112 // these are properties used internally by LibreOffice,
113 // and should not be passed to the JDBC driver
114 // (which probably does not know anything about them anyway).
115 if ( pBegin->Name != "JavaDriverClass"
116 && pBegin->Name != "JavaDriverClassPath"
117 && pBegin->Name != "SystemProperties"
118 && pBegin->Name != "CharSet"
119 && pBegin->Name != "AppendTableAliasName"
120 && pBegin->Name != "AppendTableAliasInSelect"
121 && pBegin->Name != "DisplayVersionColumns"
122 && pBegin->Name != "GeneratedValues"
123 && pBegin->Name != "UseIndexDirectionKeyword"
124 && pBegin->Name != "UseKeywordAsBeforeAlias"
125 && pBegin->Name != "AddIndexAppendix"
126 && pBegin->Name != "FormsCheckRequiredFields"
127 && pBegin->Name != "GenerateASBeforeCorrelationName"
128 && pBegin->Name != "EscapeDateTime"
129 && pBegin->Name != "ParameterNameSubstitution"
130 && pBegin->Name != "IsPasswordRequired"
131 && pBegin->Name != "IsAutoRetrievingEnabled"
132 && pBegin->Name != "AutoRetrievingStatement"
133 && pBegin->Name != "UseCatalogInSelect"
134 && pBegin->Name != "UseSchemaInSelect"
135 && pBegin->Name != "AutoIncrementCreation"
136 && pBegin->Name != "Extension"
137 && pBegin->Name != "NoNameLengthLimit"
138 && pBegin->Name != "EnableSQL92Check"
139 && pBegin->Name != "EnableOuterJoinEscape"
140 && pBegin->Name != "BooleanComparisonMode"
141 && pBegin->Name != "IgnoreCurrency"
142 && pBegin->Name != "TypeInfoSettings"
143 && pBegin->Name != "IgnoreDriverPrivileges"
144 && pBegin->Name != "ImplicitCatalogRestriction"
145 && pBegin->Name != "ImplicitSchemaRestriction"
146 && pBegin->Name != "SupportsTableCreation"
147 && pBegin->Name != "UseJava"
148 && pBegin->Name != "Authentication"
149 && pBegin->Name != "PreferDosLikeLineEnds"
150 && pBegin->Name != "PrimaryKeySupport"
151 && pBegin->Name != "RespectDriverResultSetType"
152 )
153 {
154 OUString aStr;
155 OSL_VERIFY( pBegin->Value >>= aStr );
156 pProps->setProperty(pBegin->Name,aStr);
157 }
158 }
159 return pProps;
160}
161
162OUString connectivity::JavaString2String(JNIEnv *pEnv,jstring Str)
163{
164 OUString aStr;
165 if(Str)
166 {
167 jboolean bCopy(true);
168 const jchar* pChar = pEnv->GetStringChars(Str,&bCopy);
169 jsize len = pEnv->GetStringLength(Str);
170 aStr = OUString(reinterpret_cast<sal_Unicode const *>(pChar), len);
171
172 if(bCopy)
173 pEnv->ReleaseStringChars(Str,pChar);
174 pEnv->DeleteLocalRef(Str);
175 }
176 return aStr;
177}
178
179jobject connectivity::convertTypeMapToJavaMap(const Reference< css::container::XNameAccess > & _rMap)
180{
181 if ( _rMap.is() )
182 {
183 css::uno::Sequence< OUString > aNames = _rMap->getElementNames();
184 if ( aNames.hasElements() )
186 }
187 return nullptr;
188}
189
191{
192 if ( !pEnv )
193 return false;
194
195 jthrowable pThrowable = pEnv->ExceptionOccurred();
196 bool bRet = pThrowable != nullptr;
197 if ( pThrowable )
198 {
199 pEnv->ExceptionClear();
200 pEnv->DeleteLocalRef(pThrowable);
201 }
202
203 return bRet;
204}
205
206jobject connectivity::createByteInputStream(const css::uno::Reference< css::io::XInputStream >& x,sal_Int32 length)
207{
209 if( !t.pEnv || !x.is() )
210 return nullptr;
211 // Turn off Java-Call for the constructor
212 // Initialize temporary variables
213 jclass clazz = java_lang_Object::findMyClass("java/io/ByteArrayInputStream");
214 static jmethodID mID(nullptr);
215 if ( !mID )
216 {
217 static const char * const cSignature = "([B)V";
218 mID = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
219 OSL_ENSURE( mID, cSignature );
220 if ( !mID )
221 throw SQLException();
222 } // if ( !_inout_MethodID )
223 jbyteArray pByteArray = t.pEnv->NewByteArray(length);
224 Sequence< sal_Int8 > aData;
225 x->readBytes(aData,length);
226 jboolean p = false;
227 memcpy(t.pEnv->GetByteArrayElements(pByteArray,&p),aData.getArray(),aData.getLength());
228 jobject out = t.pEnv->NewObject( clazz, mID,pByteArray);
229 t.pEnv->DeleteLocalRef(pByteArray);
230 return out;
231}
232
233jobject connectivity::createCharArrayReader(const css::uno::Reference< css::io::XInputStream >& x,sal_Int32 length)
234{
236 if( !t.pEnv || !x.is() )
237 return nullptr;
238 // Turn off Java-Call for the constructor
239 // Initialize temporary Variables
240 jclass clazz = java_lang_Object::findMyClass("java/io/CharArrayReader");
241 static jmethodID mID(nullptr);
242 if ( !mID )
243 {
244 static const char * const cSignature = "([C)V";
245 mID = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
246 OSL_ENSURE( mID, cSignature );
247 if ( !mID )
248 throw SQLException();
249 } // if ( !_inout_MethodID )
250 jcharArray pCharArray = t.pEnv->NewCharArray(length);
251 Sequence< sal_Int8 > aData;
252 x->readBytes(aData,length);
253 jboolean p = false;
254 memcpy(t.pEnv->GetCharArrayElements(pCharArray,&p),aData.getArray(),aData.getLength());
255 jobject out = t.pEnv->NewObject( clazz, mID,pCharArray);
256 t.pEnv->DeleteLocalRef(pCharArray);
257 return out;
258}
259
260
261/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
void saveRef(JNIEnv *pEnv, jobject myObj)
Definition: Object.cxx:142
static void ThrowSQLException(JNIEnv *pEnv, const css::uno::Reference< css::uno::XInterface > &_rContext)
Definition: Object.cxx:208
static jclass findMyClass(const char *_pClassName)
Definition: Object.cxx:466
void obtainMethodId_throwSQL(JNIEnv *_pEnv, const char *_pMethodName, const char *_pSignature, jmethodID &_inout_MethodID) const
Definition: Object.cxx:229
virtual jclass getMyClass() const override
Definition: tools.cxx:67
virtual ~java_util_Properties() override
Definition: tools.cxx:64
Any value
float x
void * p
aStr
constexpr OUStringLiteral aData
jobject convertTypeMapToJavaMap(const css::uno::Reference< css::container::XNameAccess > &_rMap)
bool isExceptionOccurred(JNIEnv *pEnv)
return if an exception occurred the exception will be cleared.
Definition: tools.cxx:190
jobject createCharArrayReader(const css::uno::Reference< css::io::XInputStream > &x, sal_Int32 length)
Definition: tools.cxx:233
OUString JavaString2String(JNIEnv *pEnv, jstring Str)
Definition: tools.cxx:162
jstring convertwchar_tToJavaString(JNIEnv *pEnv, const OUString &Temp)
Definition: tools.cxx:93
jobject createByteInputStream(const css::uno::Reference< css::io::XInputStream > &x, sal_Int32 length)
Definition: tools.cxx:206
std::unique_ptr< java_util_Properties > createStringPropertyArray(const css::uno::Sequence< css::beans::PropertyValue > &info)
void throwFeatureNotImplementedSQLException(const OUString &_rFeatureName, const Reference< XInterface > &_rxContext, const Any &_rNextException)
args
sal_Int32 value
Definition: pq_statics.cxx:68
sal_uInt16 sal_Unicode
const char * pChar