LCOV - code coverage report
Current view: top level - scripting/source/provider - ProviderCache.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 43 74 58.1 %
Date: 2015-06-13 12:38:46 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          31 : ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext )
      37          31 :     throw ( RuntimeException ) : m_Sctx( scriptContext ), m_xContext( xContext )
      38             : {
      39             :     // initialise m_hProviderDetailsCache with details of ScriptProviders
      40             :     // will use createContentEnumeration
      41             : 
      42          31 :     m_xMgr = m_xContext->getServiceManager();
      43          31 :     ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
      44          31 :     populateCache();
      45          31 : }
      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          31 : ProviderCache::~ProviderCache()
      61             : {
      62          31 : }
      63             : 
      64             : Reference< provider::XScriptProvider >
      65          80 : ProviderCache::getProvider( const OUString& providerName )
      66             : {
      67          80 :     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
      68          80 :     Reference< provider::XScriptProvider > provider;
      69          80 :     ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.find( providerName );
      70          80 :     if ( h_it != m_hProviderDetailsCache.end() )
      71             :     {
      72          80 :         if (  h_it->second.provider.is() )
      73             :         {
      74          49 :             provider = h_it->second.provider;
      75             :         }
      76             :     else
      77             :     {
      78             :         // need to create provider and insert into hash
      79          31 :             provider = createProvider( h_it->second );
      80             :     }
      81             :     }
      82          80 :     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.empty() )
      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          31 : ProviderCache::populateCache() throw ( RuntimeException )
     137             : {
     138             :     // wrong name in services.rdb
     139          31 :     OUString serviceName;
     140          62 :     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
     141             :     try
     142             :     {
     143          31 :         OUString languageProviderName( "com.sun.star.script.provider.LanguageScriptProvider"  );
     144             : 
     145          62 :         Reference< container::XContentEnumerationAccess > xEnumAccess = Reference< container::XContentEnumerationAccess >( m_xMgr, UNO_QUERY_THROW );
     146          62 :         Reference< container::XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName );
     147             : 
     148          99 :         while ( xEnum->hasMoreElements() )
     149             :         {
     150             : 
     151          37 :             Reference< lang::XSingleComponentFactory > factory( xEnum->nextElement(), UNO_QUERY_THROW );
     152          74 :             Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW );
     153             : 
     154          74 :             Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
     155             : 
     156          37 :             if ( serviceNames.getLength() > 0 )
     157             :             {
     158          37 :                 OUString searchString( "com.sun.star.script.provider.ScriptProviderFor"  );
     159             : 
     160          61 :                 for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ )
     161             :                 {
     162          61 :                     if ( serviceNames[ index ].startsWith( searchString ) && !isInBlackList(  serviceNames[ index ] ) )
     163             :                     {
     164          37 :                         serviceName = serviceNames[ index ];
     165          37 :                         ProviderDetails details;
     166          37 :                         details.factory = factory;
     167          37 :                         m_hProviderDetailsCache[ serviceName ] = details;
     168          37 :                         break;
     169             :                     }
     170          37 :                 }
     171             :             }
     172          68 :         }
     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 ) );
     180          31 :     }
     181          31 : }
     182             : 
     183             : Reference< provider::XScriptProvider >
     184          31 : ProviderCache::createProvider( ProviderDetails& details ) throw ( RuntimeException )
     185             : {
     186             :     try
     187             :     {
     188             :         details.provider.set(
     189          31 :             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 ) );
     195             :     }
     196             : 
     197          31 :     return details.provider;
     198             : }
     199             : } //end namespace
     200             : 
     201             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11