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

Generated by: LCOV version 1.10