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

Generated by: LCOV version 1.10