LCOV - code coverage report
Current view: top level - connectivity/source/drivers/hsqldb - HStorageMap.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 144 0.0 %
Date: 2014-04-14 Functions: 0 18 0.0 %
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           0 :         StreamHelper::StreamHelper(const Reference< XStream>& _xStream)
      44           0 :             : m_xStream(_xStream)
      45             :         {
      46           0 :         }
      47             : 
      48           0 :         StreamHelper::~StreamHelper()
      49             :         {
      50             :             try
      51             :             {
      52           0 :                 m_xStream.clear();
      53           0 :                 m_xSeek.clear();
      54           0 :                 if ( m_xInputStream.is() )
      55             :                 {
      56           0 :                     m_xInputStream->closeInput();
      57           0 :                     m_xInputStream.clear();
      58             :                 }
      59             :                 // this is done implicity by the closing of the input stream
      60           0 :                 else if ( m_xOutputStream.is() )
      61             :                 {
      62           0 :                     m_xOutputStream->closeOutput();
      63             :                     try
      64             :                     {
      65           0 :                         ::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           0 :                     m_xOutputStream.clear();
      75             :                 }
      76             :             }
      77           0 :             catch(const Exception&)
      78             :             {
      79             :                 OSL_FAIL("Exception caught!");
      80             :             }
      81           0 :         }
      82             : 
      83           0 :         Reference< XInputStream> StreamHelper::getInputStream()
      84             :         {
      85           0 :             if ( !m_xInputStream.is() )
      86           0 :                 m_xInputStream = m_xStream->getInputStream();
      87           0 :             return m_xInputStream;
      88             :         }
      89             : 
      90           0 :         Reference< XOutputStream> StreamHelper::getOutputStream()
      91             :         {
      92           0 :             if ( !m_xOutputStream.is() )
      93           0 :                 m_xOutputStream = m_xStream->getOutputStream();
      94           0 :             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           0 :         TStorages& lcl_getStorageMap()
     105             :         {
     106           0 :             static TStorages s_aMap;
     107           0 :             return s_aMap;
     108             :         }
     109             : 
     110           0 :         OUString lcl_getNextCount()
     111             :         {
     112             :             static sal_Int32 s_nCount = 0;
     113           0 :             return OUString::number(s_nCount++);
     114             :         }
     115             : 
     116           0 :         OUString StorageContainer::removeURLPrefix(const OUString& _sURL,const OUString& _sFileURL)
     117             :         {
     118           0 :             return _sURL.copy(_sFileURL.getLength()+1);
     119             :         }
     120             : 
     121           0 :         OUString StorageContainer::removeOldURLPrefix(const OUString& _sURL)
     122             :         {
     123           0 :             OUString sRet = _sURL;
     124             : #if defined(WNT)
     125             :             sal_Int32 nIndex = sRet.lastIndexOf('\\');
     126             : #else
     127           0 :             sal_Int32 nIndex = sRet.lastIndexOf('/');
     128             : #endif
     129           0 :             if ( nIndex != -1 )
     130             :             {
     131           0 :                 sRet = _sURL.copy(nIndex+1);
     132             :             }
     133           0 :             return sRet;
     134             : 
     135             :         }
     136             :         /*****************************************************************************/
     137             :         /* convert jstring to rtl_uString */
     138             : 
     139           0 :         OUString StorageContainer::jstring2ustring(JNIEnv * env, jstring jstr)
     140             :         {
     141           0 :             if (JNI_FALSE != env->ExceptionCheck())
     142             :             {
     143           0 :                 env->ExceptionClear();
     144             :                 OSL_FAIL("ExceptionClear");
     145             :             }
     146           0 :             OUString aStr;
     147           0 :             if ( jstr )
     148             :             {
     149           0 :                 jboolean bCopy(sal_True);
     150           0 :                 const jchar* pChar = env->GetStringChars(jstr,&bCopy);
     151           0 :                 jsize len = env->GetStringLength(jstr);
     152           0 :                 aStr = OUString(pChar,len);
     153             : 
     154           0 :                 if(bCopy)
     155           0 :                     env->ReleaseStringChars(jstr,pChar);
     156             :             }
     157             : 
     158           0 :             if (JNI_FALSE != env->ExceptionCheck())
     159             :             {
     160           0 :                 env->ExceptionClear();
     161             :                 OSL_FAIL("ExceptionClear");
     162             :             }
     163           0 :             return aStr;
     164             :         }
     165             : 
     166             : 
     167           0 :         OUString StorageContainer::registerStorage(const Reference< XStorage>& _xStorage,const OUString& _sURL)
     168             :         {
     169             :             OSL_ENSURE(_xStorage.is(),"Storage is NULL!");
     170           0 :             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           0 :                                             ,::o3tl::compose1(::o3tl::select1st<TStorageURLPair>(),::o3tl::compose1(::o3tl::select1st<TStorages::mapped_type>(),::o3tl::select2nd<TStorages::value_type>())))
     176           0 :                     );
     177           0 :             if ( aFind == rMap.end() )
     178             :             {
     179           0 :                 aFind = rMap.insert(TStorages::value_type(lcl_getNextCount(),TStorages::mapped_type(TStorageURLPair(_xStorage,_sURL),TStreamMap()))).first;
     180             :             }
     181             : 
     182           0 :             return aFind->first;
     183             :         }
     184             : 
     185           0 :         TStorages::mapped_type StorageContainer::getRegisteredStorage(const OUString& _sKey)
     186             :         {
     187           0 :             TStorages::mapped_type aRet;
     188           0 :             TStorages& rMap = lcl_getStorageMap();
     189           0 :             TStorages::iterator aFind = rMap.find(_sKey);
     190             :             OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
     191           0 :             if ( aFind != rMap.end() )
     192           0 :                 aRet = aFind->second;
     193             : 
     194           0 :             return aRet;
     195             :         }
     196             : 
     197           0 :         OUString StorageContainer::getRegisteredKey(const Reference< XStorage>& _xStorage)
     198             :         {
     199           0 :             OUString sKey;
     200             :             OSL_ENSURE(_xStorage.is(),"Storage is NULL!");
     201           0 :             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           0 :                                             ,::o3tl::compose1(::o3tl::select1st<TStorageURLPair>(),::o3tl::compose1(::o3tl::select1st<TStorages::mapped_type>(),::o3tl::select2nd<TStorages::value_type>())))
     207           0 :                     );
     208           0 :             if ( aFind != rMap.end() )
     209           0 :                 sKey = aFind->first;
     210           0 :             return sKey;
     211             :         }
     212             : 
     213           0 :         void StorageContainer::revokeStorage(const OUString& _sKey,const Reference<XTransactionListener>& _xListener)
     214             :         {
     215           0 :             TStorages& rMap = lcl_getStorageMap();
     216           0 :             TStorages::iterator aFind = rMap.find(_sKey);
     217           0 :             if ( aFind != rMap.end() )
     218             :             {
     219             :                 try
     220             :                 {
     221           0 :                     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           0 :                 rMap.erase(aFind);
     235             :             }
     236           0 :         }
     237             : 
     238           0 :         TStreamMap::mapped_type StorageContainer::registerStream(JNIEnv * env,jstring name, jstring key,sal_Int32 _nMode)
     239             :         {
     240           0 :             TStreamMap::mapped_type pHelper;
     241           0 :             TStorages& rMap = lcl_getStorageMap();
     242           0 :             OUString sKey = jstring2ustring(env,key);
     243           0 :             TStorages::iterator aFind = rMap.find(sKey);
     244             :             OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
     245           0 :             if ( aFind != rMap.end() )
     246             :             {
     247           0 :                 TStorages::mapped_type aStoragePair = StorageContainer::getRegisteredStorage(sKey);
     248             :                 OSL_ENSURE(aStoragePair.first.first.is(),"No Storage available!");
     249           0 :                 if ( aStoragePair.first.first.is() )
     250             :                 {
     251           0 :                     OUString sOrgName = StorageContainer::jstring2ustring(env,name);
     252           0 :                     OUString sName = removeURLPrefix(sOrgName,aStoragePair.first.second);
     253           0 :                     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           0 :                     if ( aStreamFind != aFind->second.second.end() )
     256             :                     {
     257           0 :                         pHelper = aStreamFind->second;
     258             :                     }
     259             :                     else
     260             :                     {
     261             :                         try
     262             :                         {
     263             :                             try
     264             :                             {
     265           0 :                                 pHelper.reset(new StreamHelper(aStoragePair.first.first->openStreamElement(sName,_nMode)));
     266             :                             }
     267           0 :                             catch(const Exception&)
     268             :                             {
     269           0 :                                 OUString sStrippedName = removeOldURLPrefix(sOrgName);
     270             : 
     271           0 :                                 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           0 :                                 pHelper.reset( new StreamHelper(aStoragePair.first.first->openStreamElement( sStrippedName, _nMode ) ) );
     286           0 :                             }
     287           0 :                             aFind->second.second.insert(TStreamMap::value_type(sName,pHelper));
     288             :                         }
     289           0 :                         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           0 :                             StorageContainer::throwJavaException(e,env);
     302             :                         }
     303           0 :                     }
     304           0 :                 }
     305             :             }
     306           0 :             return pHelper;
     307             :         }
     308             : 
     309           0 :         void StorageContainer::revokeStream( JNIEnv * env,jstring name, jstring key)
     310             :         {
     311           0 :             TStorages& rMap = lcl_getStorageMap();
     312           0 :             TStorages::iterator aFind = rMap.find(jstring2ustring(env,key));
     313             :             OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
     314           0 :             if ( aFind != rMap.end() )
     315           0 :                 aFind->second.second.erase(removeURLPrefix(jstring2ustring(env,name),aFind->second.first.second));
     316           0 :         }
     317             : 
     318           0 :         TStreamMap::mapped_type StorageContainer::getRegisteredStream( JNIEnv * env,jstring name, jstring key)
     319             :         {
     320           0 :             TStreamMap::mapped_type  pRet;
     321           0 :             TStorages& rMap = lcl_getStorageMap();
     322           0 :             TStorages::iterator aFind = rMap.find(jstring2ustring(env,key));
     323             :             OSL_ENSURE(aFind != rMap.end(),"Storage could not be found in list!");
     324           0 :             if ( aFind != rMap.end() )
     325             :             {
     326           0 :                 TStreamMap::iterator aStreamFind = aFind->second.second.find(removeURLPrefix(jstring2ustring(env,name),aFind->second.first.second));
     327           0 :                 if ( aStreamFind != aFind->second.second.end() )
     328           0 :                     pRet = aStreamFind->second;
     329             :             }
     330             : 
     331           0 :             return pRet;
     332             :         }
     333             : 
     334           0 :         void StorageContainer::throwJavaException(const Exception& _aException,JNIEnv * env)
     335             :         {
     336           0 :             if (JNI_FALSE != env->ExceptionCheck())
     337           0 :                 env->ExceptionClear();
     338           0 :             OString cstr( OUStringToOString(_aException.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
     339             :             OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
     340           0 :             env->ThrowNew(env->FindClass("java/io/IOException"), cstr.getStr());
     341           0 :         }
     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