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 "diagnose_ex.h"
27 #include <osl/thread.h>
29 #include <o3tl/compat_functional.hxx>
32 namespace connectivity
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
;
43 StreamHelper::StreamHelper(const Reference
< XStream
>& _xStream
)
48 StreamHelper::~StreamHelper()
54 if ( m_xInputStream
.is() )
56 m_xInputStream
->closeInput();
57 m_xInputStream
.clear();
59 // this is done implicity by the closing of the input stream
60 else if ( m_xOutputStream
.is() )
62 m_xOutputStream
->closeOutput();
65 ::comphelper::disposeComponent(m_xOutputStream
);
67 catch(const DisposedException
&)
70 catch(const Exception
&)
72 OSL_FAIL("Could not dispose OutputStream");
74 m_xOutputStream
.clear();
77 catch(const Exception
&)
79 OSL_FAIL("Exception caught!");
83 Reference
< XInputStream
> StreamHelper::getInputStream()
85 if ( !m_xInputStream
.is() )
86 m_xInputStream
= m_xStream
->getInputStream();
87 return m_xInputStream
;
90 Reference
< XOutputStream
> StreamHelper::getOutputStream()
92 if ( !m_xOutputStream
.is() )
93 m_xOutputStream
= m_xStream
->getOutputStream();
94 return m_xOutputStream
;
97 Reference
< XSeekable
> StreamHelper::getSeek()
100 m_xSeek
.set(m_xStream
,UNO_QUERY
);
104 TStorages
& lcl_getStorageMap()
106 static TStorages s_aMap
;
110 OUString
lcl_getNextCount()
112 static sal_Int32 s_nCount
= 0;
113 return OUString::number(s_nCount
++);
116 OUString
StorageContainer::removeURLPrefix(const OUString
& _sURL
,const OUString
& _sFileURL
)
118 return _sURL
.copy(_sFileURL
.getLength()+1);
121 OUString
StorageContainer::removeOldURLPrefix(const OUString
& _sURL
)
123 OUString sRet
= _sURL
;
125 sal_Int32 nIndex
= sRet
.lastIndexOf('\\');
127 sal_Int32 nIndex
= sRet
.lastIndexOf('/');
131 sRet
= _sURL
.copy(nIndex
+1);
136 /*****************************************************************************/
137 /* convert jstring to rtl_uString */
139 OUString
StorageContainer::jstring2ustring(JNIEnv
* env
, jstring jstr
)
141 if (JNI_FALSE
!= env
->ExceptionCheck())
143 env
->ExceptionClear();
144 OSL_FAIL("ExceptionClear");
149 jboolean
bCopy(sal_True
);
150 const jchar
* pChar
= env
->GetStringChars(jstr
,&bCopy
);
151 jsize len
= env
->GetStringLength(jstr
);
152 aStr
= OUString(pChar
,len
);
155 env
->ReleaseStringChars(jstr
,pChar
);
158 if (JNI_FALSE
!= env
->ExceptionCheck())
160 env
->ExceptionClear();
161 OSL_FAIL("ExceptionClear");
167 OUString
StorageContainer::registerStorage(const Reference
< XStorage
>& _xStorage
,const OUString
& _sURL
)
169 OSL_ENSURE(_xStorage
.is(),"Storage is NULL!");
170 TStorages
& rMap
= lcl_getStorageMap();
171 // check if the storage is already in our map
172 TStorages::iterator aFind
= ::std::find_if(rMap
.begin(),rMap
.end(),
174 ::std::bind2nd(::std::equal_to
<Reference
<XStorage
> >(),_xStorage
)
175 ,::o3tl::compose1(::o3tl::select1st
<TStorageURLPair
>(),::o3tl::compose1(::o3tl::select1st
<TStorages::mapped_type
>(),::o3tl::select2nd
<TStorages::value_type
>())))
177 if ( aFind
== rMap
.end() )
179 aFind
= rMap
.insert(TStorages::value_type(lcl_getNextCount(),TStorages::mapped_type(TStorageURLPair(_xStorage
,_sURL
),TStreamMap()))).first
;
185 TStorages::mapped_type
StorageContainer::getRegisteredStorage(const OUString
& _sKey
)
187 TStorages::mapped_type aRet
;
188 TStorages
& rMap
= lcl_getStorageMap();
189 TStorages::iterator aFind
= rMap
.find(_sKey
);
190 OSL_ENSURE(aFind
!= rMap
.end(),"Storage could not be found in list!");
191 if ( aFind
!= rMap
.end() )
192 aRet
= aFind
->second
;
197 OUString
StorageContainer::getRegisteredKey(const Reference
< XStorage
>& _xStorage
)
200 OSL_ENSURE(_xStorage
.is(),"Storage is NULL!");
201 TStorages
& rMap
= lcl_getStorageMap();
202 // check if the storage is already in our map
203 TStorages::iterator aFind
= ::std::find_if(rMap
.begin(),rMap
.end(),
205 ::std::bind2nd(::std::equal_to
<Reference
<XStorage
> >(),_xStorage
)
206 ,::o3tl::compose1(::o3tl::select1st
<TStorageURLPair
>(),::o3tl::compose1(::o3tl::select1st
<TStorages::mapped_type
>(),::o3tl::select2nd
<TStorages::value_type
>())))
208 if ( aFind
!= rMap
.end() )
213 void StorageContainer::revokeStorage(const OUString
& _sKey
,const Reference
<XTransactionListener
>& _xListener
)
215 TStorages
& rMap
= lcl_getStorageMap();
216 TStorages::iterator aFind
= rMap
.find(_sKey
);
217 if ( aFind
!= rMap
.end() )
221 if ( _xListener
.is() )
223 Reference
<XTransactionBroadcaster
> xBroad(aFind
->second
.first
.first
,UNO_QUERY
);
225 xBroad
->removeTransactionListener(_xListener
);
226 Reference
<XTransactedObject
> xTrans(aFind
->second
.first
.first
,UNO_QUERY
);
231 catch(const Exception
&)
238 TStreamMap::mapped_type
StorageContainer::registerStream(JNIEnv
* env
,jstring name
, jstring key
,sal_Int32 _nMode
)
240 TStreamMap::mapped_type pHelper
;
241 TStorages
& rMap
= lcl_getStorageMap();
242 OUString sKey
= jstring2ustring(env
,key
);
243 TStorages::iterator aFind
= rMap
.find(sKey
);
244 OSL_ENSURE(aFind
!= rMap
.end(),"Storage could not be found in list!");
245 if ( aFind
!= rMap
.end() )
247 TStorages::mapped_type aStoragePair
= StorageContainer::getRegisteredStorage(sKey
);
248 OSL_ENSURE(aStoragePair
.first
.first
.is(),"No Storage available!");
249 if ( aStoragePair
.first
.first
.is() )
251 OUString sOrgName
= StorageContainer::jstring2ustring(env
,name
);
252 OUString sName
= removeURLPrefix(sOrgName
,aStoragePair
.first
.second
);
253 TStreamMap::iterator aStreamFind
= aFind
->second
.second
.find(sName
);
254 OSL_ENSURE( aStreamFind
== aFind
->second
.second
.end(),"A Stream was already registered for this object!");
255 if ( aStreamFind
!= aFind
->second
.second
.end() )
257 pHelper
= aStreamFind
->second
;
265 pHelper
.reset(new StreamHelper(aStoragePair
.first
.first
->openStreamElement(sName
,_nMode
)));
267 catch(const Exception
&)
269 OUString sStrippedName
= removeOldURLPrefix(sOrgName
);
271 if ( ((_nMode
& ElementModes::WRITE
) != ElementModes::WRITE
) )
273 bool bIsStream
= true;
276 bIsStream
= aStoragePair
.first
.first
->isStreamElement(sStrippedName
);
278 catch(const Exception
&)
283 return pHelper
; // readonly file without data stream
285 pHelper
.reset( new StreamHelper(aStoragePair
.first
.first
->openStreamElement( sStrippedName
, _nMode
) ) );
287 aFind
->second
.second
.insert(TStreamMap::value_type(sName
,pHelper
));
289 catch(const Exception
& e
)
291 #if OSL_DEBUG_LEVEL > 0
292 OString
sMessage( "[HSQLDB-SDBC] caught an exception while opening a stream\n" );
293 sMessage
+= "Name: ";
294 sMessage
+= OString( sName
.getStr(), sName
.getLength(), osl_getThreadTextEncoding() );
295 sMessage
+= "\nMode: 0x";
298 sMessage
+= OString::number( _nMode
, 16 ).toAsciiUpperCase();
299 OSL_FAIL( sMessage
.getStr() );
301 StorageContainer::throwJavaException(e
,env
);
309 void StorageContainer::revokeStream( JNIEnv
* env
,jstring name
, jstring key
)
311 TStorages
& rMap
= lcl_getStorageMap();
312 TStorages::iterator aFind
= rMap
.find(jstring2ustring(env
,key
));
313 OSL_ENSURE(aFind
!= rMap
.end(),"Storage could not be found in list!");
314 if ( aFind
!= rMap
.end() )
315 aFind
->second
.second
.erase(removeURLPrefix(jstring2ustring(env
,name
),aFind
->second
.first
.second
));
318 TStreamMap::mapped_type
StorageContainer::getRegisteredStream( JNIEnv
* env
,jstring name
, jstring key
)
320 TStreamMap::mapped_type pRet
;
321 TStorages
& rMap
= lcl_getStorageMap();
322 TStorages::iterator aFind
= rMap
.find(jstring2ustring(env
,key
));
323 OSL_ENSURE(aFind
!= rMap
.end(),"Storage could not be found in list!");
324 if ( aFind
!= rMap
.end() )
326 TStreamMap::iterator aStreamFind
= aFind
->second
.second
.find(removeURLPrefix(jstring2ustring(env
,name
),aFind
->second
.first
.second
));
327 if ( aStreamFind
!= aFind
->second
.second
.end() )
328 pRet
= aStreamFind
->second
;
334 void StorageContainer::throwJavaException(const Exception
& _aException
,JNIEnv
* env
)
336 if (JNI_FALSE
!= env
->ExceptionCheck())
337 env
->ExceptionClear();
338 OString
cstr( OUStringToOString(_aException
.Message
, RTL_TEXTENCODING_JAVA_UTF8
) );
339 OSL_TRACE( __FILE__
": forwarding Exception: %s", cstr
.getStr() );
340 env
->ThrowNew(env
->FindClass("java/io/IOException"), cstr
.getStr());
343 } // namespace hsqldb
347 // namespace connectivity
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */