merge the formfield patch from ooo-build
[ooovba.git] / connectivity / source / drivers / hsqldb / HStorageMap.cxx
blob712c38eea0342c36e3185625403fb14f6c7cecaf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: HStorageMap.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "hsqldb/HStorageMap.hxx"
34 #include <comphelper/types.hxx>
35 #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
36 #include <com/sun/star/embed/XTransactedObject.hpp>
37 #include <com/sun/star/embed/ElementModes.hpp>
38 #include <com/sun/star/lang/DisposedException.hpp>
39 #include "diagnose_ex.h"
40 #include <osl/thread.h>
42 //........................................................................
43 namespace connectivity
45 //........................................................................
46 namespace hsqldb
48 //........................................................................
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::embed;
52 using namespace ::com::sun::star::io;
54 #define ThrowException(env, type, msg) { \
55 env->ThrowNew(env->FindClass(type), msg); }
58 StreamHelper::StreamHelper(const Reference< XStream>& _xStream)
59 : m_xStream(_xStream)
62 // -----------------------------------------------------------------------------
63 StreamHelper::~StreamHelper()
65 try
67 m_xStream.clear();
68 m_xSeek.clear();
69 if ( m_xInputStream.is() )
71 m_xInputStream->closeInput();
72 m_xInputStream.clear();
74 if ( m_xOutputStream.is() )
76 m_xOutputStream->closeOutput();
77 try
79 ::comphelper::disposeComponent(m_xOutputStream);
81 catch(DisposedException&)
84 catch(const Exception& e)
86 OSL_UNUSED( e );
87 OSL_ENSURE(0,"Could not dispose OutputStream");
89 m_xOutputStream.clear();
92 catch(Exception& )
94 OSL_ENSURE(0,"Exception catched!");
97 // -----------------------------------------------------------------------------
98 Reference< XInputStream> StreamHelper::getInputStream()
100 if ( !m_xInputStream.is() )
101 m_xInputStream = m_xStream->getInputStream();
102 return m_xInputStream;
104 // -----------------------------------------------------------------------------
105 Reference< XOutputStream> StreamHelper::getOutputStream()
107 if ( !m_xOutputStream.is() )
108 m_xOutputStream = m_xStream->getOutputStream();
109 return m_xOutputStream;
111 // -----------------------------------------------------------------------------
112 Reference< XSeekable> StreamHelper::getSeek()
114 if ( !m_xSeek.is() )
115 m_xSeek.set(m_xStream,UNO_QUERY);
116 return m_xSeek;
118 // -----------------------------------------------------------------------------
119 TStorages& lcl_getStorageMap()
121 static TStorages s_aMap;
122 return s_aMap;
124 // -----------------------------------------------------------------------------
125 ::rtl::OUString lcl_getNextCount()
127 static sal_Int32 s_nCount = 0;
128 return ::rtl::OUString::valueOf(s_nCount++);
130 // -----------------------------------------------------------------------------
131 ::rtl::OUString StorageContainer::removeURLPrefix(const ::rtl::OUString& _sURL,const ::rtl::OUString& _sFileURL)
133 return _sURL.copy(_sFileURL.getLength()+1);
135 // -----------------------------------------------------------------------------
136 ::rtl::OUString StorageContainer::removeOldURLPrefix(const ::rtl::OUString& _sURL)
138 ::rtl::OUString sRet = _sURL;
139 #if defined(WIN) || defined(WNT)
140 sal_Int32 nIndex = sRet.lastIndexOf('\\');
141 #else
142 sal_Int32 nIndex = sRet.lastIndexOf('/');
143 #endif
144 if ( nIndex != -1 )
146 sRet = _sURL.copy(nIndex+1);
148 return sRet;
151 /*****************************************************************************/
152 /* convert jstring to rtl_uString */
154 ::rtl::OUString StorageContainer::jstring2ustring(JNIEnv * env, jstring jstr)
156 if (JNI_FALSE != env->ExceptionCheck())
158 env->ExceptionClear();
159 OSL_ENSURE(0,"ExceptionClear");
161 ::rtl::OUString aStr;
162 if ( jstr )
164 jboolean bCopy(sal_True);
165 const jchar* pChar = env->GetStringChars(jstr,&bCopy);
166 jsize len = env->GetStringLength(jstr);
167 aStr = ::rtl::OUString(pChar,len);
169 if(bCopy)
170 env->ReleaseStringChars(jstr,pChar);
173 if (JNI_FALSE != env->ExceptionCheck())
175 env->ExceptionClear();
176 OSL_ENSURE(0,"ExceptionClear");
178 return aStr;
181 // -----------------------------------------------------------------------------
182 ::rtl::OUString StorageContainer::registerStorage(const Reference< XStorage>& _xStorage,const ::rtl::OUString& _sURL)
184 OSL_ENSURE(_xStorage.is(),"Storage is NULL!");
185 TStorages& rMap = lcl_getStorageMap();
186 // check if the storage is already in our map
187 TStorages::iterator aFind = ::std::find_if(rMap.begin(),rMap.end(),
188 ::std::compose1(
189 ::std::bind2nd(::std::equal_to<Reference<XStorage> >(),_xStorage)
190 ,::std::compose1(::std::select1st<TStorageURLPair>(),::std::compose1(::std::select1st<TStorages::mapped_type>(),::std::select2nd<TStorages::value_type>())))
192 if ( aFind == rMap.end() )
194 aFind = rMap.insert(TStorages::value_type(lcl_getNextCount(),TStorages::mapped_type(TStorageURLPair(_xStorage,_sURL),TStreamMap()))).first;
197 return aFind->first;
199 // -----------------------------------------------------------------------------
200 TStorages::mapped_type StorageContainer::getRegisteredStorage(const ::rtl::OUString& _sKey)
202 TStorages::mapped_type aRet;
203 TStorages& rMap = lcl_getStorageMap();
204 TStorages::iterator aFind = rMap.find(_sKey);
205 OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
206 if ( aFind != rMap.end() )
207 aRet = aFind->second;
209 return aRet;
211 // -----------------------------------------------------------------------------
212 ::rtl::OUString StorageContainer::getRegisteredKey(const Reference< XStorage>& _xStorage)
214 ::rtl::OUString sKey;
215 OSL_ENSURE(_xStorage.is(),"Storage is NULL!");
216 TStorages& rMap = lcl_getStorageMap();
217 // check if the storage is already in our map
218 TStorages::iterator aFind = ::std::find_if(rMap.begin(),rMap.end(),
219 ::std::compose1(
220 ::std::bind2nd(::std::equal_to<Reference<XStorage> >(),_xStorage)
221 ,::std::compose1(::std::select1st<TStorageURLPair>(),::std::compose1(::std::select1st<TStorages::mapped_type>(),::std::select2nd<TStorages::value_type>())))
223 if ( aFind != rMap.end() )
224 sKey = aFind->first;
225 return sKey;
227 // -----------------------------------------------------------------------------
228 void StorageContainer::revokeStorage(const ::rtl::OUString& _sKey,const Reference<XTransactionListener>& _xListener)
230 TStorages& rMap = lcl_getStorageMap();
231 TStorages::iterator aFind = rMap.find(_sKey);
232 if ( aFind != rMap.end() )
236 if ( _xListener.is() )
238 Reference<XTransactionBroadcaster> xBroad(aFind->second.first.first,UNO_QUERY);
239 if ( xBroad.is() )
240 xBroad->removeTransactionListener(_xListener);
241 Reference<XTransactedObject> xTrans(aFind->second.first.first,UNO_QUERY);
242 if ( xTrans.is() )
243 xTrans->commit();
246 catch(Exception&)
249 rMap.erase(aFind);
252 // -----------------------------------------------------------------------------
253 TStreamMap::mapped_type StorageContainer::registerStream(JNIEnv * env,jstring name, jstring key,sal_Int32 _nMode)
255 TStreamMap::mapped_type pHelper;
256 TStorages& rMap = lcl_getStorageMap();
257 ::rtl::OUString sKey = jstring2ustring(env,key);
258 TStorages::iterator aFind = rMap.find(sKey);
259 OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
260 if ( aFind != rMap.end() )
262 TStorages::mapped_type aStoragePair = StorageContainer::getRegisteredStorage(sKey);
263 OSL_ENSURE(aStoragePair.first.first.is(),"No Storage available!");
264 if ( aStoragePair.first.first.is() )
266 ::rtl::OUString sOrgName = StorageContainer::jstring2ustring(env,name);
267 ::rtl::OUString sName = removeURLPrefix(sOrgName,aStoragePair.first.second);
268 TStreamMap::iterator aStreamFind = aFind->second.second.find(sName);
269 OSL_ENSURE( aStreamFind == aFind->second.second.end(),"A Stream was already registered for this object!");
270 if ( aStreamFind != aFind->second.second.end() )
272 pHelper = aStreamFind->second;
274 else
280 pHelper.reset(new StreamHelper(aStoragePair.first.first->openStreamElement(sName,_nMode)));
282 catch(Exception& )
284 ::rtl::OUString sStrippedName = removeOldURLPrefix(sOrgName);
286 if ( ((_nMode & ElementModes::WRITE) != ElementModes::WRITE ) )
288 sal_Bool bIsStream = sal_True;
291 bIsStream = aStoragePair.first.first->isStreamElement(sStrippedName);
293 catch(Exception& )
295 bIsStream = sal_False;
297 if ( !bIsStream )
298 return pHelper; // readonly file without data stream
300 pHelper.reset( new StreamHelper(aStoragePair.first.first->openStreamElement( sStrippedName, _nMode ) ) );
302 aFind->second.second.insert(TStreamMap::value_type(sName,pHelper));
304 catch(Exception& e)
306 #if OSL_DEBUG_LEVEL > 0
307 ::rtl::OString sMessage( "[HSQLDB-SDBC] caught an exception while opening a stream\n" );
308 sMessage += "Name: ";
309 sMessage += ::rtl::OString( sName.getStr(), sName.getLength(), osl_getThreadTextEncoding() );
310 sMessage += "\nMode: 0x";
311 if ( _nMode < 16 )
312 sMessage += "0";
313 sMessage += ::rtl::OString::valueOf( _nMode, 16 ).toAsciiUpperCase();
314 OSL_ENSURE( false, sMessage.getStr() );
315 #endif
316 StorageContainer::throwJavaException(e,env);
321 return pHelper;
323 // -----------------------------------------------------------------------------
324 void StorageContainer::revokeStream( JNIEnv * env,jstring name, jstring key)
326 TStorages& rMap = lcl_getStorageMap();
327 TStorages::iterator aFind = rMap.find(jstring2ustring(env,key));
328 OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
329 if ( aFind != rMap.end() )
330 aFind->second.second.erase(removeURLPrefix(jstring2ustring(env,name),aFind->second.first.second));
332 // -----------------------------------------------------------------------------
333 TStreamMap::mapped_type StorageContainer::getRegisteredStream( JNIEnv * env,jstring name, jstring key)
335 TStreamMap::mapped_type pRet;
336 TStorages& rMap = lcl_getStorageMap();
337 TStorages::iterator aFind = rMap.find(jstring2ustring(env,key));
338 OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
339 if ( aFind != rMap.end() )
341 TStreamMap::iterator aStreamFind = aFind->second.second.find(removeURLPrefix(jstring2ustring(env,name),aFind->second.first.second));
342 if ( aStreamFind != aFind->second.second.end() )
343 pRet = aStreamFind->second;
346 return pRet;
348 // -----------------------------------------------------------------------------
349 void StorageContainer::throwJavaException(const Exception& _aException,JNIEnv * env)
351 if (JNI_FALSE != env->ExceptionCheck())
352 env->ExceptionClear();
353 ::rtl::OString cstr( ::rtl::OUStringToOString(_aException.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
354 OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
355 env->ThrowNew(env->FindClass("java/io/IOException"), cstr.getStr());
357 //........................................................................
358 } // namespace hsqldb
359 //........................................................................
360 //........................................................................
362 // namespace connectivity
363 //........................................................................