1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_scripting.hxx" 30*cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx> 31*cdf0e10cSrcweir #include <cppuhelper/factory.hxx> 32*cdf0e10cSrcweir #include <tools/diagnose_ex.h> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #include <util/scriptingconstants.hxx> 35*cdf0e10cSrcweir #include <util/util.hxx> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #include <com/sun/star/container/XContentEnumerationAccess.hpp> 38*cdf0e10cSrcweir #include "ProviderCache.hxx" 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir using namespace com::sun::star; 41*cdf0e10cSrcweir using namespace com::sun::star::uno; 42*cdf0e10cSrcweir using namespace com::sun::star::script; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir namespace func_provider 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext ) 48*cdf0e10cSrcweir throw ( RuntimeException ) : m_Sctx( scriptContext ), m_xContext( xContext ) 49*cdf0e10cSrcweir { 50*cdf0e10cSrcweir // initialise m_hProviderDetailsCache with details of ScriptProviders 51*cdf0e10cSrcweir // will use createContentEnumeration 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir m_xMgr = m_xContext->getServiceManager(); 54*cdf0e10cSrcweir ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" ); 55*cdf0e10cSrcweir populateCache(); 56*cdf0e10cSrcweir } 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext, const Sequence< ::rtl::OUString >& blackList ) 60*cdf0e10cSrcweir throw ( RuntimeException ) : m_sBlackList( blackList ), m_Sctx( scriptContext ), m_xContext( xContext ) 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir // initialise m_hProviderDetailsCache with details of ScriptProviders 64*cdf0e10cSrcweir // will use createContentEnumeration 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir m_xMgr = m_xContext->getServiceManager(); 67*cdf0e10cSrcweir ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" ); 68*cdf0e10cSrcweir populateCache(); 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir ProviderCache::~ProviderCache() 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir Reference< provider::XScriptProvider > 76*cdf0e10cSrcweir ProviderCache::getProvider( const ::rtl::OUString& providerName ) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir ::osl::Guard< osl::Mutex > aGuard( m_mutex ); 79*cdf0e10cSrcweir Reference< provider::XScriptProvider > provider; 80*cdf0e10cSrcweir ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.find( providerName ); 81*cdf0e10cSrcweir if ( h_it != m_hProviderDetailsCache.end() ) 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir if ( h_it->second.provider.is() ) 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir provider = h_it->second.provider; 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir else 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir // need to create provider and insert into hash 90*cdf0e10cSrcweir provider = createProvider( h_it->second ); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir return provider; 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir Sequence < Reference< provider::XScriptProvider > > 97*cdf0e10cSrcweir ProviderCache::getAllProviders() throw ( RuntimeException ) 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir Sequence < Reference< provider::XScriptProvider > > providers ( m_hProviderDetailsCache.size() ); 100*cdf0e10cSrcweir // need to create providers that haven't been created already 101*cdf0e10cSrcweir // so check what providers exist and what ones don't 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir ::osl::Guard< osl::Mutex > aGuard( m_mutex ); 104*cdf0e10cSrcweir ProviderDetails_hash::iterator h_itEnd = m_hProviderDetailsCache.end(); 105*cdf0e10cSrcweir ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.begin(); 106*cdf0e10cSrcweir // should assert if size !> 0 107*cdf0e10cSrcweir if ( m_hProviderDetailsCache.size() ) 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir sal_Int32 providerIndex = 0; 110*cdf0e10cSrcweir sal_Int32 index = 0; 111*cdf0e10cSrcweir for ( index = 0; h_it != h_itEnd; ++h_it, index++ ) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir Reference< provider::XScriptProvider > xScriptProvider = h_it->second.provider; 114*cdf0e10cSrcweir if ( xScriptProvider.is() ) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir providers[ providerIndex++ ] = xScriptProvider; 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir else 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir // create provider 121*cdf0e10cSrcweir try 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir xScriptProvider = createProvider( h_it->second ); 124*cdf0e10cSrcweir providers[ providerIndex++ ] = xScriptProvider; 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir catch ( Exception& e ) 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir ::rtl::OUString temp = OUSTR( "ProviderCache::getAllProviders: failed to create provider, " ); 129*cdf0e10cSrcweir temp.concat( e.Message ); 130*cdf0e10cSrcweir //throw RuntimeException( temp.concat( e.Message ), 131*cdf0e10cSrcweir // Reference< XInterface >() ); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir if ( providerIndex < index ) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir providers.realloc( providerIndex ); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir else 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir OSL_TRACE("no available providers, something very wrong!!!"); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir return providers; 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir void 150*cdf0e10cSrcweir ProviderCache::populateCache() throw ( RuntimeException ) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir // wrong name in services.rdb 153*cdf0e10cSrcweir ::rtl::OUString serviceName; 154*cdf0e10cSrcweir ::osl::Guard< osl::Mutex > aGuard( m_mutex ); 155*cdf0e10cSrcweir try 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir ::rtl::OUString languageProviderName( RTL_CONSTASCII_USTRINGPARAM( 158*cdf0e10cSrcweir "com.sun.star.script.provider.LanguageScriptProvider" ) ); 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir Reference< container::XContentEnumerationAccess > xEnumAccess = Reference< container::XContentEnumerationAccess >( m_xMgr, UNO_QUERY_THROW ); 161*cdf0e10cSrcweir Reference< container::XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName ); 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir while ( xEnum->hasMoreElements() ) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir Reference< lang::XSingleComponentFactory > factory( xEnum->nextElement(), UNO_QUERY_THROW ); 167*cdf0e10cSrcweir Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW ); 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir Sequence< ::rtl::OUString > serviceNames = xServiceInfo->getSupportedServiceNames(); 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir if ( serviceNames.getLength() > 0 ) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir ::rtl::OUString searchString( RTL_CONSTASCII_USTRINGPARAM ( 174*cdf0e10cSrcweir "com.sun.star.script.provider.ScriptProviderFor" ) ); 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ ) 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir if ( serviceNames[ index ].indexOf( searchString ) == 0 && !isInBlackList( serviceNames[ index ] ) ) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir serviceName = serviceNames[ index ]; 181*cdf0e10cSrcweir ProviderDetails details; 182*cdf0e10cSrcweir details.factory = factory; 183*cdf0e10cSrcweir m_hProviderDetailsCache[ serviceName ] = details; 184*cdf0e10cSrcweir break; 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir catch ( Exception e ) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir ::rtl::OUString temp = OUSTR( 193*cdf0e10cSrcweir "ProviderCache::populateCache: couldn't obtain XSingleComponentFactory for " ); 194*cdf0e10cSrcweir temp.concat( serviceName ); 195*cdf0e10cSrcweir throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() ); 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir Reference< provider::XScriptProvider > 200*cdf0e10cSrcweir ProviderCache::createProvider( ProviderDetails& details ) throw ( RuntimeException ) 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir try 203*cdf0e10cSrcweir { 204*cdf0e10cSrcweir details.provider.set( 205*cdf0e10cSrcweir details.factory->createInstanceWithArgumentsAndContext( m_Sctx, m_xContext ), UNO_QUERY_THROW ); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir catch ( RuntimeException& e ) 208*cdf0e10cSrcweir { 209*cdf0e10cSrcweir ::rtl::OUString temp = ::rtl::OUString::createFromAscii("ProviderCache::createProvider() Error creating provider from factory!!!"); 210*cdf0e10cSrcweir throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() ); 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir return details.provider; 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir } //end namespace 216