LCOV - code coverage report
Current view: top level - connectivity/source/drivers/hsqldb - HStorageMap.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 119 144 82.6 %
Date: 2014-04-11 Functions: 17 18 94.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10