1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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 <osl/diagnose.h>
27 #include <osl/thread.h>
28 #include <uno/mapping.hxx>
31 namespace connectivity
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::com::sun::star::embed
;
40 using namespace ::com::sun::star::io
;
42 StreamHelper::StreamHelper(const Reference
< XStream
>& _xStream
)
47 StreamHelper::~StreamHelper()
53 if ( m_xInputStream
.is() )
55 m_xInputStream
->closeInput();
56 m_xInputStream
.clear();
58 // this is done implicity by the closing of the input stream
59 else if ( m_xOutputStream
.is() )
61 m_xOutputStream
->closeOutput();
64 ::comphelper::disposeComponent(m_xOutputStream
);
66 catch(const DisposedException
&)
69 catch(const Exception
&)
71 OSL_FAIL("Could not dispose OutputStream");
73 m_xOutputStream
.clear();
76 catch(const Exception
&)
78 OSL_FAIL("Exception caught!");
82 Reference
< XInputStream
> const & StreamHelper::getInputStream()
84 if ( !m_xInputStream
.is() )
85 m_xInputStream
= m_xStream
->getInputStream();
86 return m_xInputStream
;
89 Reference
< XOutputStream
> const & StreamHelper::getOutputStream()
91 if ( !m_xOutputStream
.is() )
92 m_xOutputStream
= m_xStream
->getOutputStream();
93 return m_xOutputStream
;
96 Reference
< XSeekable
> const & StreamHelper::getSeek()
99 m_xSeek
.set(m_xStream
,UNO_QUERY
);
103 css::uno::Reference
<css::embed::XStorage
> StorageData::mapStorage()
106 css::uno::Environment
env(css::uno::Environment::getCurrent());
107 if (!(env
.is() && storageEnvironment
.is())) {
108 throw css::uno::RuntimeException("cannot get environments");
110 if (env
.get() == storageEnvironment
.get()) {
113 css::uno::Mapping
map(storageEnvironment
, env
);
115 throw css::uno::RuntimeException("cannot get mapping");
117 css::uno::Reference
<css::embed::XStorage
> mapped
;
119 reinterpret_cast<void **>(&mapped
), storage
.get(),
120 cppu::UnoType
<css::embed::XStorage
>::get());
125 TStorages
& lcl_getStorageMap()
127 static TStorages s_aMap
;
131 OUString
lcl_getNextCount()
133 static sal_Int32 s_nCount
= 0;
134 return OUString::number(s_nCount
++);
137 OUString
StorageContainer::removeURLPrefix(const OUString
& _sURL
,const OUString
& _sFileURL
)
139 return _sURL
.copy(_sFileURL
.getLength()+1);
142 OUString
StorageContainer::removeOldURLPrefix(const OUString
& _sURL
)
144 OUString sRet
= _sURL
;
146 sal_Int32 nIndex
= sRet
.lastIndexOf('\\');
148 sal_Int32 nIndex
= sRet
.lastIndexOf('/');
152 sRet
= _sURL
.copy(nIndex
+1);
157 /*****************************************************************************/
158 /* convert jstring to rtl_uString */
160 OUString
StorageContainer::jstring2ustring(JNIEnv
* env
, jstring jstr
)
162 if (env
->ExceptionCheck())
164 env
->ExceptionClear();
165 OSL_FAIL("ExceptionClear");
170 jboolean
bCopy(true);
171 const jchar
* pChar
= env
->GetStringChars(jstr
,&bCopy
);
172 jsize len
= env
->GetStringLength(jstr
);
174 reinterpret_cast<sal_Unicode
const *>(pChar
), len
);
177 env
->ReleaseStringChars(jstr
,pChar
);
180 if (env
->ExceptionCheck())
182 env
->ExceptionClear();
183 OSL_FAIL("ExceptionClear");
189 OUString
StorageContainer::registerStorage(const Reference
< XStorage
>& _xStorage
,const OUString
& _sURL
)
191 OSL_ENSURE(_xStorage
.is(),"Storage is NULL!");
192 TStorages
& rMap
= lcl_getStorageMap();
193 // check if the storage is already in our map
194 TStorages::const_iterator aFind
= ::std::find_if(rMap
.begin(),rMap
.end(),
195 [&_xStorage
] (const TStorages::value_type
& storage
) {
196 return storage
.second
.mapStorage() == _xStorage
;
199 if ( aFind
== rMap
.end() )
201 aFind
= rMap
.insert(TStorages::value_type(lcl_getNextCount(), {_xStorage
, css::uno::Environment::getCurrent(), _sURL
, TStreamMap()})).first
;
207 TStorages::mapped_type
StorageContainer::getRegisteredStorage(const OUString
& _sKey
)
209 TStorages::mapped_type aRet
;
210 TStorages
& rMap
= lcl_getStorageMap();
211 TStorages::const_iterator aFind
= rMap
.find(_sKey
);
212 OSL_ENSURE(aFind
!= rMap
.end(),"Storage could not be found in list!");
213 if ( aFind
!= rMap
.end() )
214 aRet
= aFind
->second
;
219 OUString
StorageContainer::getRegisteredKey(const Reference
< XStorage
>& _xStorage
)
222 OSL_ENSURE(_xStorage
.is(),"Storage is NULL!");
223 TStorages
& rMap
= lcl_getStorageMap();
224 // check if the storage is already in our map
225 TStorages::const_iterator aFind
= ::std::find_if(rMap
.begin(),rMap
.end(),
226 [&_xStorage
] (const TStorages::value_type
& storage
) {
227 return storage
.second
.mapStorage() == _xStorage
;
230 if ( aFind
!= rMap
.end() )
235 void StorageContainer::revokeStorage(const OUString
& _sKey
,const Reference
<XTransactionListener
>& _xListener
)
237 TStorages
& rMap
= lcl_getStorageMap();
238 TStorages::iterator aFind
= rMap
.find(_sKey
);
239 if ( aFind
!= rMap
.end() )
243 if ( _xListener
.is() )
245 Reference
<XTransactionBroadcaster
> xBroad(aFind
->second
.mapStorage(),UNO_QUERY
);
247 xBroad
->removeTransactionListener(_xListener
);
248 Reference
<XTransactedObject
> xTrans(aFind
->second
.mapStorage(),UNO_QUERY
);
253 catch(const Exception
&)
260 TStreamMap::mapped_type
StorageContainer::registerStream(JNIEnv
* env
,jstring name
, jstring key
,sal_Int32 _nMode
)
262 TStreamMap::mapped_type pHelper
;
263 TStorages
& rMap
= lcl_getStorageMap();
264 OUString sKey
= jstring2ustring(env
,key
);
265 TStorages::iterator aFind
= rMap
.find(sKey
);
266 OSL_ENSURE(aFind
!= rMap
.end(),"Storage could not be found in list!");
267 if ( aFind
!= rMap
.end() )
269 TStorages::mapped_type aStoragePair
= StorageContainer::getRegisteredStorage(sKey
);
270 auto storage
= aStoragePair
.mapStorage();
271 OSL_ENSURE(storage
.is(),"No Storage available!");
274 OUString sOrgName
= StorageContainer::jstring2ustring(env
,name
);
275 OUString sName
= removeURLPrefix(sOrgName
,aStoragePair
.url
);
276 TStreamMap::iterator aStreamFind
= aFind
->second
.streams
.find(sName
);
277 OSL_ENSURE( aStreamFind
== aFind
->second
.streams
.end(),"A Stream was already registered for this object!");
278 if ( aStreamFind
!= aFind
->second
.streams
.end() )
280 pHelper
= aStreamFind
->second
;
288 pHelper
.reset(new StreamHelper(storage
->openStreamElement(sName
,_nMode
)));
290 catch(const Exception
&)
292 OUString sStrippedName
= removeOldURLPrefix(sOrgName
);
294 if ( ((_nMode
& ElementModes::WRITE
) != ElementModes::WRITE
) )
296 bool bIsStream
= true;
299 bIsStream
= storage
->isStreamElement(sStrippedName
);
301 catch(const Exception
&)
306 return pHelper
; // readonly file without data stream
308 pHelper
.reset( new StreamHelper(storage
->openStreamElement( sStrippedName
, _nMode
) ) );
310 aFind
->second
.streams
.insert(TStreamMap::value_type(sName
,pHelper
));
312 catch(const Exception
& e
)
314 #if OSL_DEBUG_LEVEL > 0
315 OString
sMessage( "[HSQLDB-SDBC] caught an exception while opening a stream\n" );
316 sMessage
+= "Name: ";
317 sMessage
+= OString( sName
.getStr(), sName
.getLength(), osl_getThreadTextEncoding() );
318 sMessage
+= "\nMode: 0x";
321 sMessage
+= OString::number( _nMode
, 16 ).toAsciiUpperCase();
322 OSL_FAIL( sMessage
.getStr() );
324 StorageContainer::throwJavaException(e
,env
);
332 void StorageContainer::revokeStream( JNIEnv
* env
,jstring name
, jstring key
)
334 TStorages
& rMap
= lcl_getStorageMap();
335 TStorages::iterator aFind
= rMap
.find(jstring2ustring(env
,key
));
336 OSL_ENSURE(aFind
!= rMap
.end(),"Storage could not be found in list!");
337 if ( aFind
!= rMap
.end() )
338 aFind
->second
.streams
.erase(removeURLPrefix(jstring2ustring(env
,name
),aFind
->second
.url
));
341 TStreamMap::mapped_type
StorageContainer::getRegisteredStream( JNIEnv
* env
,jstring name
, jstring key
)
343 TStreamMap::mapped_type pRet
;
344 TStorages
& rMap
= lcl_getStorageMap();
345 TStorages::const_iterator aFind
= rMap
.find(jstring2ustring(env
,key
));
346 OSL_ENSURE(aFind
!= rMap
.end(),"Storage could not be found in list!");
347 if ( aFind
!= rMap
.end() )
349 TStreamMap::const_iterator aStreamFind
= aFind
->second
.streams
.find(removeURLPrefix(jstring2ustring(env
,name
),aFind
->second
.url
));
350 if ( aStreamFind
!= aFind
->second
.streams
.end() )
351 pRet
= aStreamFind
->second
;
357 void StorageContainer::throwJavaException(const Exception
& _aException
,JNIEnv
* env
)
359 if (env
->ExceptionCheck())
360 env
->ExceptionClear();
361 OString
cstr( OUStringToOString(_aException
.Message
, RTL_TEXTENCODING_JAVA_UTF8
) );
362 OSL_TRACE( __FILE__
": forwarding Exception: %s", cstr
.getStr() );
363 env
->ThrowNew(env
->FindClass("java/io/IOException"), cstr
.getStr());
366 } // namespace hsqldb
370 // namespace connectivity
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */