2010-10-12 15:53:47 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-07-06 17:28:37 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2006-09-16 11:28:44 +00:00
|
|
|
|
2019-07-27 23:20:29 +03:00
|
|
|
#include <comphelper/sequence.hxx>
|
2003-09-04 06:21:55 +00:00
|
|
|
#include <cppuhelper/implementationentry.hxx>
|
|
|
|
#include <cppuhelper/factory.hxx>
|
2010-11-04 10:36:15 +01:00
|
|
|
#include <tools/diagnose_ex.h>
|
2018-07-28 16:47:47 +02:00
|
|
|
#include <sal/log.hxx>
|
2003-09-04 06:21:55 +00:00
|
|
|
|
|
|
|
#include <com/sun/star/container/XContentEnumerationAccess.hpp>
|
2018-05-24 15:47:30 +02:00
|
|
|
#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
|
2003-09-04 06:21:55 +00:00
|
|
|
#include "ProviderCache.hxx"
|
|
|
|
|
|
|
|
using namespace com::sun::star;
|
|
|
|
using namespace com::sun::star::uno;
|
2004-10-22 13:07:39 +00:00
|
|
|
using namespace com::sun::star::script;
|
2003-09-04 06:21:55 +00:00
|
|
|
|
|
|
|
namespace func_provider
|
|
|
|
{
|
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext ) : m_Sctx( scriptContext ), m_xContext( xContext )
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
|
|
|
// initialise m_hProviderDetailsCache with details of ScriptProviders
|
|
|
|
// will use createContentEnumeration
|
|
|
|
|
|
|
|
m_xMgr = m_xContext->getServiceManager();
|
2010-11-04 10:36:15 +01:00
|
|
|
ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
|
2003-09-04 06:21:55 +00:00
|
|
|
populateCache();
|
|
|
|
}
|
|
|
|
|
2004-07-23 13:10:46 +00:00
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext, const Sequence< OUString >& blackList ) : m_sBlackList( blackList ), m_Sctx( scriptContext ), m_xContext( xContext )
|
2004-07-23 13:10:46 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// initialise m_hProviderDetailsCache with details of ScriptProviders
|
|
|
|
// will use createContentEnumeration
|
|
|
|
|
|
|
|
m_xMgr = m_xContext->getServiceManager();
|
2010-11-04 10:36:15 +01:00
|
|
|
ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
|
2004-07-23 13:10:46 +00:00
|
|
|
populateCache();
|
|
|
|
}
|
|
|
|
|
2003-09-04 06:21:55 +00:00
|
|
|
ProviderCache::~ProviderCache()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Reference< provider::XScriptProvider >
|
2013-04-07 12:06:47 +02:00
|
|
|
ProviderCache::getProvider( const OUString& providerName )
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
|
|
|
::osl::Guard< osl::Mutex > aGuard( m_mutex );
|
|
|
|
Reference< provider::XScriptProvider > provider;
|
|
|
|
ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.find( providerName );
|
|
|
|
if ( h_it != m_hProviderDetailsCache.end() )
|
|
|
|
{
|
|
|
|
if ( h_it->second.provider.is() )
|
|
|
|
{
|
|
|
|
provider = h_it->second.provider;
|
|
|
|
}
|
2018-10-18 14:09:50 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// need to create provider and insert into hash
|
2003-09-04 06:21:55 +00:00
|
|
|
provider = createProvider( h_it->second );
|
2018-10-18 14:09:50 +02:00
|
|
|
}
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
|
|
|
return provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sequence < Reference< provider::XScriptProvider > >
|
2017-01-26 12:28:58 +01:00
|
|
|
ProviderCache::getAllProviders()
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
|
|
|
// need to create providers that haven't been created already
|
|
|
|
// so check what providers exist and what ones don't
|
|
|
|
|
|
|
|
::osl::Guard< osl::Mutex > aGuard( m_mutex );
|
2016-03-10 09:22:48 +02:00
|
|
|
Sequence < Reference< provider::XScriptProvider > > providers ( m_hProviderDetailsCache.size() );
|
2003-09-04 06:21:55 +00:00
|
|
|
// should assert if size !> 0
|
2015-01-04 19:09:27 +01:00
|
|
|
if ( !m_hProviderDetailsCache.empty() )
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
2004-01-05 13:17:54 +00:00
|
|
|
sal_Int32 providerIndex = 0;
|
2018-10-06 12:27:44 +02:00
|
|
|
for (auto& rDetail : m_hProviderDetailsCache)
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
2018-10-06 12:27:44 +02:00
|
|
|
Reference<provider::XScriptProvider> xScriptProvider = rDetail.second.provider;
|
2003-09-04 06:21:55 +00:00
|
|
|
if ( xScriptProvider.is() )
|
|
|
|
{
|
2004-01-05 13:17:54 +00:00
|
|
|
providers[ providerIndex++ ] = xScriptProvider;
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// create provider
|
|
|
|
try
|
|
|
|
{
|
2018-10-06 12:27:44 +02:00
|
|
|
xScriptProvider = createProvider(rDetail.second);
|
2004-01-05 13:17:54 +00:00
|
|
|
providers[ providerIndex++ ] = xScriptProvider;
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
2011-12-25 15:08:11 +09:00
|
|
|
catch ( const Exception& )
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
2018-04-02 08:58:19 +02:00
|
|
|
DBG_UNHANDLED_EXCEPTION("scripting");
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 12:27:44 +02:00
|
|
|
if (providerIndex < providers.getLength())
|
2004-01-05 13:17:54 +00:00
|
|
|
{
|
|
|
|
providers.realloc( providerIndex );
|
|
|
|
}
|
|
|
|
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-12 13:33:14 +02:00
|
|
|
SAL_WARN("scripting", "no available providers, something very wrong!!!");
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
|
|
|
return providers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-26 12:28:58 +01:00
|
|
|
ProviderCache::populateCache()
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
2003-09-15 13:32:37 +00:00
|
|
|
// wrong name in services.rdb
|
2013-04-07 12:06:47 +02:00
|
|
|
OUString serviceName;
|
2003-09-04 06:21:55 +00:00
|
|
|
::osl::Guard< osl::Mutex > aGuard( m_mutex );
|
|
|
|
try
|
|
|
|
{
|
2017-07-30 12:16:31 +02:00
|
|
|
OUString const languageProviderName( "com.sun.star.script.provider.LanguageScriptProvider" );
|
2005-01-27 14:32:28 +00:00
|
|
|
|
2015-11-04 15:36:18 +02:00
|
|
|
Reference< container::XContentEnumerationAccess > xEnumAccess( m_xMgr, UNO_QUERY_THROW );
|
2003-09-15 13:32:37 +00:00
|
|
|
Reference< container::XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName );
|
2003-09-04 06:21:55 +00:00
|
|
|
|
2003-09-15 13:32:37 +00:00
|
|
|
while ( xEnum->hasMoreElements() )
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
2003-09-15 13:32:37 +00:00
|
|
|
|
2010-11-04 10:36:15 +01:00
|
|
|
Reference< lang::XSingleComponentFactory > factory( xEnum->nextElement(), UNO_QUERY_THROW );
|
2003-09-15 13:32:37 +00:00
|
|
|
Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW );
|
2003-09-04 06:21:55 +00:00
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
|
2003-09-04 06:21:55 +00:00
|
|
|
|
2019-05-03 19:51:47 +03:00
|
|
|
if ( serviceNames.hasElements() )
|
2003-09-15 13:32:37 +00:00
|
|
|
{
|
2019-07-27 23:20:29 +03:00
|
|
|
auto pName = std::find_if(serviceNames.begin(), serviceNames.end(),
|
|
|
|
[this](const OUString& rName) {
|
|
|
|
return rName.startsWith("com.sun.star.script.provider.ScriptProviderFor")
|
|
|
|
&& !isInBlackList(rName);
|
|
|
|
});
|
|
|
|
if (pName != serviceNames.end())
|
2003-09-15 13:32:37 +00:00
|
|
|
{
|
2019-07-27 23:20:29 +03:00
|
|
|
serviceName = *pName;
|
|
|
|
ProviderDetails details;
|
|
|
|
details.factory = factory;
|
|
|
|
m_hProviderDetailsCache[ serviceName ] = details;
|
2003-09-15 13:32:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
2011-12-25 15:08:11 +09:00
|
|
|
catch ( const Exception &e )
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
2018-05-24 15:47:30 +02:00
|
|
|
css::uno::Any anyEx = cppu::getCaughtException();
|
|
|
|
throw css::lang::WrappedTargetRuntimeException(
|
|
|
|
"ProviderCache::populateCache: couldn't obtain XSingleComponentFactory for " + serviceName
|
|
|
|
+ " " + e.Message,
|
|
|
|
nullptr, anyEx );
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Reference< provider::XScriptProvider >
|
2017-01-26 12:28:58 +01:00
|
|
|
ProviderCache::createProvider( ProviderDetails& details )
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2010-11-04 10:36:15 +01:00
|
|
|
details.provider.set(
|
2008-03-06 15:30:55 +00:00
|
|
|
details.factory->createInstanceWithArgumentsAndContext( m_Sctx, m_xContext ), UNO_QUERY_THROW );
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
2012-10-30 09:55:11 +00:00
|
|
|
catch ( const Exception& e )
|
2003-09-04 06:21:55 +00:00
|
|
|
{
|
2018-05-24 15:47:30 +02:00
|
|
|
css::uno::Any anyEx = cppu::getCaughtException();
|
|
|
|
throw css::lang::WrappedTargetRuntimeException(
|
|
|
|
"ProviderCache::createProvider() Error creating provider from factory. " + e.Message,
|
|
|
|
nullptr, anyEx );
|
2003-09-04 06:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return details.provider;
|
|
|
|
}
|
2019-07-27 23:20:29 +03:00
|
|
|
|
|
|
|
bool
|
|
|
|
ProviderCache::isInBlackList( const OUString& serviceName )
|
|
|
|
{
|
|
|
|
return comphelper::findValue(m_sBlackList, serviceName) != -1;
|
|
|
|
}
|
2003-09-04 06:21:55 +00:00
|
|
|
} //end namespace
|
2010-10-12 15:53:47 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|