LibreOffice Module scripting (master) 1
ProviderCache.cxx
Go to the documentation of this file.
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
22#include <sal/log.hxx>
23
24#include <com/sun/star/container/XContentEnumerationAccess.hpp>
25#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
26#include <com/sun/star/lang/XServiceInfo.hpp>
27#include "ProviderCache.hxx"
28
29using namespace com::sun::star;
30using namespace com::sun::star::uno;
31using namespace com::sun::star::script;
32
33namespace func_provider
34{
35
36ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext ) : m_Sctx( scriptContext ), m_xContext( xContext )
37{
38 // initialise m_hProviderDetailsCache with details of ScriptProviders
39 // will use createContentEnumeration
40
41 m_xMgr = m_xContext->getServiceManager();
42 ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
43 populateCache();
44}
45
46
47ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext, const Sequence< OUString >& denyList ) : m_sDenyList( denyList ), m_Sctx( scriptContext ), m_xContext( xContext )
48
49{
50 // initialise m_hProviderDetailsCache with details of ScriptProviders
51 // will use createContentEnumeration
52
53 m_xMgr = m_xContext->getServiceManager();
54 ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
55 populateCache();
56}
57
58ProviderCache::~ProviderCache()
59{
60}
61
63ProviderCache::getProvider( const OUString& providerName )
64{
65 std::scoped_lock aGuard( m_mutex );
67 ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.find( providerName );
68 if ( h_it != m_hProviderDetailsCache.end() )
69 {
70 if ( h_it->second.provider.is() )
71 {
72 provider = h_it->second.provider;
73 }
74 else
75 {
76 // need to create provider and insert into hash
77 provider = createProvider( h_it->second );
78 }
79 }
80 return provider;
81}
82
84ProviderCache::getAllProviders()
85{
86 // need to create providers that haven't been created already
87 // so check what providers exist and what ones don't
88
89 std::scoped_lock aGuard( m_mutex );
90 Sequence < Reference< provider::XScriptProvider > > providers ( m_hProviderDetailsCache.size() );
91 // should assert if size !> 0
92 if ( !m_hProviderDetailsCache.empty() )
93 {
94 auto pproviders = providers.getArray();
95 sal_Int32 providerIndex = 0;
96 for (auto& rDetail : m_hProviderDetailsCache)
97 {
98 Reference<provider::XScriptProvider> xScriptProvider = rDetail.second.provider;
99 if ( xScriptProvider.is() )
100 {
101 pproviders[ providerIndex++ ] = xScriptProvider;
102 }
103 else
104 {
105 // create provider
106 try
107 {
108 xScriptProvider = createProvider(rDetail.second);
109 pproviders[ providerIndex++ ] = xScriptProvider;
110 }
111 catch ( const Exception& )
112 {
113 DBG_UNHANDLED_EXCEPTION("scripting");
114 }
115 }
116 }
117
118 if (providerIndex < providers.getLength())
119 {
120 providers.realloc( providerIndex );
121 }
122
123 }
124 else
125 {
126 SAL_WARN("scripting", "no available providers, something very wrong!!!");
127 }
128 return providers;
129}
130
131void
132ProviderCache::populateCache()
133{
134 // wrong name in services.rdb
135 OUString serviceName;
136 std::scoped_lock aGuard( m_mutex );
137 try
138 {
139 Reference< container::XContentEnumerationAccess > xEnumAccess( m_xMgr, UNO_QUERY_THROW );
140 Reference< container::XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( "com.sun.star.script.provider.LanguageScriptProvider" );
141
142 while ( xEnum->hasMoreElements() )
143 {
144
145 Reference< lang::XSingleComponentFactory > factory( xEnum->nextElement(), UNO_QUERY_THROW );
146 Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW );
147
148 const Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
149
150 if ( serviceNames.hasElements() )
151 {
152 auto pName = std::find_if(serviceNames.begin(), serviceNames.end(),
153 [this](const OUString& rName) {
154 return rName.startsWith("com.sun.star.script.provider.ScriptProviderFor")
155 && !isInDenyList(rName);
156 });
157 if (pName != serviceNames.end())
158 {
159 serviceName = *pName;
160 ProviderDetails details;
161 details.factory = factory;
162 m_hProviderDetailsCache[ serviceName ] = details;
163 }
164 }
165 }
166 }
167 catch ( const Exception &e )
168 {
169 css::uno::Any anyEx = cppu::getCaughtException();
170 throw css::lang::WrappedTargetRuntimeException(
171 "ProviderCache::populateCache: couldn't obtain XSingleComponentFactory for " + serviceName
172 + " " + e.Message,
173 nullptr, anyEx );
174 }
175}
176
178ProviderCache::createProvider( ProviderDetails& details )
179{
180 try
181 {
182 details.provider.set(
183 details.factory->createInstanceWithArgumentsAndContext( m_Sctx, m_xContext ), UNO_QUERY_THROW );
184 }
185 catch ( const Exception& e )
186 {
187 css::uno::Any anyEx = cppu::getCaughtException();
188 throw css::lang::WrappedTargetRuntimeException(
189 "ProviderCache::createProvider() Error creating provider from factory. " + e.Message,
190 nullptr, anyEx );
191 }
192
193 return details.provider;
194}
195
196bool
197ProviderCache::isInDenyList( const OUString& serviceName ) const
198{
199 return comphelper::findValue(m_sDenyList, serviceName) != -1;
200}
201} //end namespace
202
203/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
const char * pName
ProviderCache(const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Sequence< css::uno::Any > &scriptContext)
#define ENSURE_OR_THROW(c, m)
#define DBG_UNHANDLED_EXCEPTION(...)
Mutex m_mutex
#define SAL_WARN(area, stream)
@ Exception
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
Any SAL_CALL getCaughtException()
css::uno::Reference< css::lang::XSingleComponentFactory > factory
css::uno::Reference< css::script::provider::XScriptProvider > provider