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 : #include "hsqldb/HStorageMap.hxx"
21 : #include <comphelper/types.hxx>
22 : #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
23 : #include <com/sun/star/embed/XTransactedObject.hpp>
24 : #include <com/sun/star/embed/ElementModes.hpp>
25 : #include <com/sun/star/lang/DisposedException.hpp>
26 : #include "diagnose_ex.h"
27 : #include <osl/thread.h>
28 :
29 : #include <o3tl/compat_functional.hxx>
30 :
31 : //........................................................................
32 : namespace connectivity
33 : {
34 : //........................................................................
35 : namespace hsqldb
36 : {
37 : //........................................................................
38 : using namespace ::com::sun::star::uno;
39 : using namespace ::com::sun::star::lang;
40 : using namespace ::com::sun::star::embed;
41 : using namespace ::com::sun::star::io;
42 :
43 : #define ThrowException(env, type, msg) { \
44 : env->ThrowNew(env->FindClass(type), msg); }
45 :
46 :
47 0 : StreamHelper::StreamHelper(const Reference< XStream>& _xStream)
48 0 : : m_xStream(_xStream)
49 : {
50 0 : }
51 : // -----------------------------------------------------------------------------
52 0 : StreamHelper::~StreamHelper()
53 : {
54 : try
55 : {
56 0 : m_xStream.clear();
57 0 : m_xSeek.clear();
58 0 : if ( m_xInputStream.is() )
59 : {
60 0 : m_xInputStream->closeInput();
61 0 : m_xInputStream.clear();
62 : }
63 : // this is done implicity by the closing of the input stream
64 0 : else if ( m_xOutputStream.is() )
65 : {
66 0 : m_xOutputStream->closeOutput();
67 : try
68 : {
69 0 : ::comphelper::disposeComponent(m_xOutputStream);
70 : }
71 0 : catch(const DisposedException&)
72 : {
73 : }
74 0 : catch(const Exception&)
75 : {
76 : OSL_FAIL("Could not dispose OutputStream");
77 : }
78 0 : m_xOutputStream.clear();
79 : }
80 : }
81 0 : catch(const Exception&)
82 : {
83 : OSL_FAIL("Exception caught!");
84 : }
85 0 : }
86 : // -----------------------------------------------------------------------------
87 0 : Reference< XInputStream> StreamHelper::getInputStream()
88 : {
89 0 : if ( !m_xInputStream.is() )
90 0 : m_xInputStream = m_xStream->getInputStream();
91 0 : return m_xInputStream;
92 : }
93 : // -----------------------------------------------------------------------------
94 0 : Reference< XOutputStream> StreamHelper::getOutputStream()
95 : {
96 0 : if ( !m_xOutputStream.is() )
97 0 : m_xOutputStream = m_xStream->getOutputStream();
98 0 : return m_xOutputStream;
99 : }
100 : // -----------------------------------------------------------------------------
101 0 : Reference< XSeekable> StreamHelper::getSeek()
102 : {
103 0 : if ( !m_xSeek.is() )
104 0 : m_xSeek.set(m_xStream,UNO_QUERY);
105 0 : return m_xSeek;
106 : }
107 : // -----------------------------------------------------------------------------
108 0 : TStorages& lcl_getStorageMap()
109 : {
110 0 : static TStorages s_aMap;
111 0 : return s_aMap;
112 : }
113 : // -----------------------------------------------------------------------------
114 0 : ::rtl::OUString lcl_getNextCount()
115 : {
116 : static sal_Int32 s_nCount = 0;
117 0 : return ::rtl::OUString::valueOf(s_nCount++);
118 : }
119 : // -----------------------------------------------------------------------------
120 0 : ::rtl::OUString StorageContainer::removeURLPrefix(const ::rtl::OUString& _sURL,const ::rtl::OUString& _sFileURL)
121 : {
122 0 : return _sURL.copy(_sFileURL.getLength()+1);
123 : }
124 : // -----------------------------------------------------------------------------
125 0 : ::rtl::OUString StorageContainer::removeOldURLPrefix(const ::rtl::OUString& _sURL)
126 : {
127 0 : ::rtl::OUString sRet = _sURL;
128 : #if defined(WNT)
129 : sal_Int32 nIndex = sRet.lastIndexOf('\\');
130 : #else
131 0 : sal_Int32 nIndex = sRet.lastIndexOf('/');
132 : #endif
133 0 : if ( nIndex != -1 )
134 : {
135 0 : sRet = _sURL.copy(nIndex+1);
136 : }
137 0 : return sRet;
138 :
139 : }
140 : /*****************************************************************************/
141 : /* convert jstring to rtl_uString */
142 :
143 0 : ::rtl::OUString StorageContainer::jstring2ustring(JNIEnv * env, jstring jstr)
144 : {
145 0 : if (JNI_FALSE != env->ExceptionCheck())
146 : {
147 0 : env->ExceptionClear();
148 : OSL_FAIL("ExceptionClear");
149 : }
150 0 : ::rtl::OUString aStr;
151 0 : if ( jstr )
152 : {
153 0 : jboolean bCopy(sal_True);
154 0 : const jchar* pChar = env->GetStringChars(jstr,&bCopy);
155 0 : jsize len = env->GetStringLength(jstr);
156 0 : aStr = ::rtl::OUString(pChar,len);
157 :
158 0 : if(bCopy)
159 0 : env->ReleaseStringChars(jstr,pChar);
160 : }
161 :
162 0 : if (JNI_FALSE != env->ExceptionCheck())
163 : {
164 0 : env->ExceptionClear();
165 : OSL_FAIL("ExceptionClear");
166 : }
167 0 : return aStr;
168 : }
169 :
170 : // -----------------------------------------------------------------------------
171 0 : ::rtl::OUString StorageContainer::registerStorage(const Reference< XStorage>& _xStorage,const ::rtl::OUString& _sURL)
172 : {
173 : OSL_ENSURE(_xStorage.is(),"Storage is NULL!");
174 0 : TStorages& rMap = lcl_getStorageMap();
175 : // check if the storage is already in our map
176 : TStorages::iterator aFind = ::std::find_if(rMap.begin(),rMap.end(),
177 : ::o3tl::compose1(
178 : ::std::bind2nd(::std::equal_to<Reference<XStorage> >(),_xStorage)
179 0 : ,::o3tl::compose1(::o3tl::select1st<TStorageURLPair>(),::o3tl::compose1(::o3tl::select1st<TStorages::mapped_type>(),::o3tl::select2nd<TStorages::value_type>())))
180 0 : );
181 0 : if ( aFind == rMap.end() )
182 : {
183 0 : aFind = rMap.insert(TStorages::value_type(lcl_getNextCount(),TStorages::mapped_type(TStorageURLPair(_xStorage,_sURL),TStreamMap()))).first;
184 : }
185 :
186 0 : return aFind->first;
187 : }
188 : // -----------------------------------------------------------------------------
189 0 : TStorages::mapped_type StorageContainer::getRegisteredStorage(const ::rtl::OUString& _sKey)
190 : {
191 0 : TStorages::mapped_type aRet;
192 0 : TStorages& rMap = lcl_getStorageMap();
193 0 : TStorages::iterator aFind = rMap.find(_sKey);
194 : OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
195 0 : if ( aFind != rMap.end() )
196 0 : aRet = aFind->second;
197 :
198 0 : return aRet;
199 : }
200 : // -----------------------------------------------------------------------------
201 0 : ::rtl::OUString StorageContainer::getRegisteredKey(const Reference< XStorage>& _xStorage)
202 : {
203 0 : ::rtl::OUString sKey;
204 : OSL_ENSURE(_xStorage.is(),"Storage is NULL!");
205 0 : TStorages& rMap = lcl_getStorageMap();
206 : // check if the storage is already in our map
207 : TStorages::iterator aFind = ::std::find_if(rMap.begin(),rMap.end(),
208 : ::o3tl::compose1(
209 : ::std::bind2nd(::std::equal_to<Reference<XStorage> >(),_xStorage)
210 0 : ,::o3tl::compose1(::o3tl::select1st<TStorageURLPair>(),::o3tl::compose1(::o3tl::select1st<TStorages::mapped_type>(),::o3tl::select2nd<TStorages::value_type>())))
211 0 : );
212 0 : if ( aFind != rMap.end() )
213 0 : sKey = aFind->first;
214 0 : return sKey;
215 : }
216 : // -----------------------------------------------------------------------------
217 0 : void StorageContainer::revokeStorage(const ::rtl::OUString& _sKey,const Reference<XTransactionListener>& _xListener)
218 : {
219 0 : TStorages& rMap = lcl_getStorageMap();
220 0 : TStorages::iterator aFind = rMap.find(_sKey);
221 0 : if ( aFind != rMap.end() )
222 : {
223 : try
224 : {
225 0 : if ( _xListener.is() )
226 : {
227 0 : Reference<XTransactionBroadcaster> xBroad(aFind->second.first.first,UNO_QUERY);
228 0 : if ( xBroad.is() )
229 0 : xBroad->removeTransactionListener(_xListener);
230 0 : Reference<XTransactedObject> xTrans(aFind->second.first.first,UNO_QUERY);
231 0 : if ( xTrans.is() )
232 0 : xTrans->commit();
233 : }
234 : }
235 0 : catch(const Exception&)
236 : {
237 : }
238 0 : rMap.erase(aFind);
239 : }
240 0 : }
241 : // -----------------------------------------------------------------------------
242 0 : TStreamMap::mapped_type StorageContainer::registerStream(JNIEnv * env,jstring name, jstring key,sal_Int32 _nMode)
243 : {
244 0 : TStreamMap::mapped_type pHelper;
245 0 : TStorages& rMap = lcl_getStorageMap();
246 0 : ::rtl::OUString sKey = jstring2ustring(env,key);
247 0 : TStorages::iterator aFind = rMap.find(sKey);
248 : OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
249 0 : if ( aFind != rMap.end() )
250 : {
251 0 : TStorages::mapped_type aStoragePair = StorageContainer::getRegisteredStorage(sKey);
252 : OSL_ENSURE(aStoragePair.first.first.is(),"No Storage available!");
253 0 : if ( aStoragePair.first.first.is() )
254 : {
255 0 : ::rtl::OUString sOrgName = StorageContainer::jstring2ustring(env,name);
256 0 : ::rtl::OUString sName = removeURLPrefix(sOrgName,aStoragePair.first.second);
257 0 : TStreamMap::iterator aStreamFind = aFind->second.second.find(sName);
258 : OSL_ENSURE( aStreamFind == aFind->second.second.end(),"A Stream was already registered for this object!");
259 0 : if ( aStreamFind != aFind->second.second.end() )
260 : {
261 0 : pHelper = aStreamFind->second;
262 : }
263 : else
264 : {
265 : try
266 : {
267 : try
268 : {
269 0 : pHelper.reset(new StreamHelper(aStoragePair.first.first->openStreamElement(sName,_nMode)));
270 : }
271 0 : catch(const Exception&)
272 : {
273 0 : ::rtl::OUString sStrippedName = removeOldURLPrefix(sOrgName);
274 :
275 0 : if ( ((_nMode & ElementModes::WRITE) != ElementModes::WRITE ) )
276 : {
277 0 : sal_Bool bIsStream = sal_True;
278 : try
279 : {
280 0 : bIsStream = aStoragePair.first.first->isStreamElement(sStrippedName);
281 : }
282 0 : catch(const Exception&)
283 : {
284 0 : bIsStream = sal_False;
285 : }
286 0 : if ( !bIsStream )
287 : return pHelper; // readonly file without data stream
288 : }
289 0 : pHelper.reset( new StreamHelper(aStoragePair.first.first->openStreamElement( sStrippedName, _nMode ) ) );
290 : }
291 0 : aFind->second.second.insert(TStreamMap::value_type(sName,pHelper));
292 : }
293 0 : catch(const Exception& e)
294 : {
295 : #if OSL_DEBUG_LEVEL > 0
296 : ::rtl::OString sMessage( "[HSQLDB-SDBC] caught an exception while opening a stream\n" );
297 : sMessage += "Name: ";
298 : sMessage += ::rtl::OString( sName.getStr(), sName.getLength(), osl_getThreadTextEncoding() );
299 : sMessage += "\nMode: 0x";
300 : if ( _nMode < 16 )
301 : sMessage += "0";
302 : sMessage += ::rtl::OString::valueOf( _nMode, 16 ).toAsciiUpperCase();
303 : OSL_FAIL( sMessage.getStr() );
304 : #endif
305 0 : StorageContainer::throwJavaException(e,env);
306 : }
307 0 : }
308 0 : }
309 : }
310 0 : return pHelper;
311 : }
312 : // -----------------------------------------------------------------------------
313 0 : void StorageContainer::revokeStream( JNIEnv * env,jstring name, jstring key)
314 : {
315 0 : TStorages& rMap = lcl_getStorageMap();
316 0 : TStorages::iterator aFind = rMap.find(jstring2ustring(env,key));
317 : OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
318 0 : if ( aFind != rMap.end() )
319 0 : aFind->second.second.erase(removeURLPrefix(jstring2ustring(env,name),aFind->second.first.second));
320 0 : }
321 : // -----------------------------------------------------------------------------
322 0 : TStreamMap::mapped_type StorageContainer::getRegisteredStream( JNIEnv * env,jstring name, jstring key)
323 : {
324 0 : TStreamMap::mapped_type pRet;
325 0 : TStorages& rMap = lcl_getStorageMap();
326 0 : TStorages::iterator aFind = rMap.find(jstring2ustring(env,key));
327 : OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
328 0 : if ( aFind != rMap.end() )
329 : {
330 0 : TStreamMap::iterator aStreamFind = aFind->second.second.find(removeURLPrefix(jstring2ustring(env,name),aFind->second.first.second));
331 0 : if ( aStreamFind != aFind->second.second.end() )
332 0 : pRet = aStreamFind->second;
333 : }
334 :
335 0 : return pRet;
336 : }
337 : // -----------------------------------------------------------------------------
338 0 : void StorageContainer::throwJavaException(const Exception& _aException,JNIEnv * env)
339 : {
340 0 : if (JNI_FALSE != env->ExceptionCheck())
341 0 : env->ExceptionClear();
342 0 : ::rtl::OString cstr( ::rtl::OUStringToOString(_aException.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
343 : OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
344 0 : env->ThrowNew(env->FindClass("java/io/IOException"), cstr.getStr());
345 0 : }
346 : //........................................................................
347 : } // namespace hsqldb
348 : //........................................................................
349 : //........................................................................
350 : }
351 : // namespace connectivity
352 : //........................................................................
353 :
354 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|