LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/scripting/source/provider - ProviderCache.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 43 74 58.1 %
Date: 2013-07-09 Functions: 5 7 71.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 <cppuhelper/implementationentry.hxx>
      21             : #include <cppuhelper/factory.hxx>
      22             : #include <tools/diagnose_ex.h>
      23             : 
      24             : #include <util/scriptingconstants.hxx>
      25             : 
      26             : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
      27             : #include "ProviderCache.hxx"
      28             : 
      29             : using namespace com::sun::star;
      30             : using namespace com::sun::star::uno;
      31             : using namespace com::sun::star::script;
      32             : 
      33             : namespace func_provider
      34             : {
      35             : 
      36          28 : ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext )
      37          28 :     throw ( RuntimeException ) : m_Sctx( scriptContext ), m_xContext( xContext )
      38             : {
      39             :     // initialise m_hProviderDetailsCache with details of ScriptProviders
      40             :     // will use createContentEnumeration
      41             : 
      42          28 :     m_xMgr = m_xContext->getServiceManager();
      43          28 :     ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
      44          28 :     populateCache();
      45          28 : }
      46             : 
      47             : 
      48           0 : ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext, const Sequence< OUString >& blackList )
      49           0 :     throw ( RuntimeException ) : m_sBlackList( blackList ), m_Sctx( scriptContext ), m_xContext( xContext )
      50             : 
      51             : {
      52             :     // initialise m_hProviderDetailsCache with details of ScriptProviders
      53             :     // will use createContentEnumeration
      54             : 
      55           0 :     m_xMgr = m_xContext->getServiceManager();
      56           0 :     ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
      57           0 :     populateCache();
      58           0 : }
      59             : 
      60          28 : ProviderCache::~ProviderCache()
      61             : {
      62          28 : }
      63             : 
      64             : Reference< provider::XScriptProvider >
      65          76 : ProviderCache::getProvider( const OUString& providerName )
      66             : {
      67          76 :     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
      68          76 :     Reference< provider::XScriptProvider > provider;
      69          76 :     ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.find( providerName );
      70          76 :     if ( h_it != m_hProviderDetailsCache.end() )
      71             :     {
      72          76 :         if (  h_it->second.provider.is() )
      73             :         {
      74          48 :             provider = h_it->second.provider;
      75             :         }
      76             :     else
      77             :     {
      78             :         // need to create provider and insert into hash
      79          28 :             provider = createProvider( h_it->second );
      80             :     }
      81             :     }
      82          76 :     return provider;
      83             : }
      84             : 
      85             : Sequence < Reference< provider::XScriptProvider > >
      86           0 : ProviderCache::getAllProviders() throw ( RuntimeException )
      87             : {
      88           0 :     Sequence < Reference< provider::XScriptProvider > > providers (  m_hProviderDetailsCache.size() );
      89             :     // need to create providers that haven't been created already
      90             :     // so check what providers exist and what ones don't
      91             : 
      92           0 :     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
      93           0 :     ProviderDetails_hash::iterator h_itEnd =  m_hProviderDetailsCache.end();
      94           0 :     ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.begin();
      95             :     // should assert if size !>  0
      96           0 :     if (  m_hProviderDetailsCache.size() )
      97             :     {
      98           0 :         sal_Int32 providerIndex = 0;
      99           0 :     sal_Int32 index = 0;
     100           0 :         for ( index = 0; h_it !=  h_itEnd; ++h_it, index++ )
     101             :         {
     102           0 :             Reference< provider::XScriptProvider > xScriptProvider  = h_it->second.provider;
     103           0 :             if ( xScriptProvider.is() )
     104             :             {
     105           0 :                 providers[ providerIndex++ ] = xScriptProvider;
     106             :             }
     107             :             else
     108             :             {
     109             :                 // create provider
     110             :                 try
     111             :                 {
     112           0 :                     xScriptProvider  = createProvider( h_it->second );
     113           0 :                     providers[ providerIndex++ ] = xScriptProvider;
     114             :                 }
     115           0 :                 catch ( const Exception& )
     116             :                 {
     117             :                     DBG_UNHANDLED_EXCEPTION();
     118             :                 }
     119             :             }
     120           0 :         }
     121             : 
     122           0 :         if ( providerIndex < index )
     123             :         {
     124           0 :             providers.realloc( providerIndex );
     125             :         }
     126             : 
     127             :     }
     128             :     else
     129             :     {
     130             :         OSL_TRACE("no available providers, something very wrong!!!");
     131             :     }
     132           0 :     return providers;
     133             : }
     134             : 
     135             : void
     136          28 : ProviderCache::populateCache() throw ( RuntimeException )
     137             : {
     138             :     // wrong name in services.rdb
     139          28 :     OUString serviceName;
     140          56 :     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
     141             :     try
     142             :     {
     143          28 :         OUString languageProviderName( "com.sun.star.script.provider.LanguageScriptProvider"  );
     144             : 
     145          56 :         Reference< container::XContentEnumerationAccess > xEnumAccess = Reference< container::XContentEnumerationAccess >( m_xMgr, UNO_QUERY_THROW );
     146          56 :         Reference< container::XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName );
     147             : 
     148          92 :         while ( xEnum->hasMoreElements() )
     149             :         {
     150             : 
     151          36 :             Reference< lang::XSingleComponentFactory > factory( xEnum->nextElement(), UNO_QUERY_THROW );
     152          72 :             Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW );
     153             : 
     154          72 :             Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
     155             : 
     156          36 :             if ( serviceNames.getLength() > 0 )
     157             :             {
     158          36 :                 OUString searchString( "com.sun.star.script.provider.ScriptProviderFor"  );
     159             : 
     160         138 :                 for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ )
     161             :                 {
     162         138 :                     if ( serviceNames[ index ].indexOf( searchString ) == 0 && !isInBlackList(  serviceNames[ index ] ) )
     163             :                     {
     164          36 :                         serviceName = serviceNames[ index ];
     165          36 :                         ProviderDetails details;
     166          36 :                         details.factory = factory;
     167          36 :                         m_hProviderDetailsCache[ serviceName ] = details;
     168          36 :                         break;
     169             :                     }
     170          36 :                 }
     171             :             }
     172          64 :         }
     173             :     }
     174           0 :     catch ( const Exception &e )
     175             :     {
     176             :         OUString temp =
     177             :             "ProviderCache::populateCache: couldn't obtain XSingleComponentFactory for "
     178           0 :             + serviceName;
     179           0 :         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
     180          28 :     }
     181          28 : }
     182             : 
     183             : Reference< provider::XScriptProvider >
     184          28 : ProviderCache::createProvider( ProviderDetails& details ) throw ( RuntimeException )
     185             : {
     186             :     try
     187             :     {
     188             :         details.provider.set(
     189          28 :             details.factory->createInstanceWithArgumentsAndContext( m_Sctx, m_xContext ), UNO_QUERY_THROW );
     190             :     }
     191           0 :     catch ( const Exception& e )
     192             :     {
     193           0 :         OUString temp("ProviderCache::createProvider() Error creating provider from factory!!!\n");
     194           0 :         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
     195             :     }
     196             : 
     197          28 :     return details.provider;
     198             : }
     199             : } //end namespace
     200             : 
     201             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10