1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <cppuhelper/implementationentry.hxx>
21 #include <cppuhelper/factory.hxx>
22 #include <tools/diagnose_ex.h>
24 #include <util/scriptingconstants.hxx>
26 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
27 #include "ProviderCache.hxx"
29 using namespace com::sun::star
;
30 using namespace com::sun::star::uno
;
31 using namespace com::sun::star::script
;
33 namespace func_provider
36 ProviderCache::ProviderCache( const Reference
< XComponentContext
>& xContext
, const Sequence
< Any
>& scriptContext
)
37 throw ( RuntimeException
) : m_Sctx( scriptContext
), m_xContext( xContext
)
39 // initialise m_hProviderDetailsCache with details of ScriptProviders
40 // will use createContentEnumeration
42 m_xMgr
= m_xContext
->getServiceManager();
43 ENSURE_OR_THROW( m_xMgr
.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
48 ProviderCache::ProviderCache( const Reference
< XComponentContext
>& xContext
, const Sequence
< Any
>& scriptContext
, const Sequence
< OUString
>& blackList
)
49 throw ( RuntimeException
) : m_sBlackList( blackList
), m_Sctx( scriptContext
), m_xContext( xContext
)
52 // initialise m_hProviderDetailsCache with details of ScriptProviders
53 // will use createContentEnumeration
55 m_xMgr
= m_xContext
->getServiceManager();
56 ENSURE_OR_THROW( m_xMgr
.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
60 ProviderCache::~ProviderCache()
64 Reference
< provider::XScriptProvider
>
65 ProviderCache::getProvider( const OUString
& providerName
)
67 ::osl::Guard
< osl::Mutex
> aGuard( m_mutex
);
68 Reference
< provider::XScriptProvider
> provider
;
69 ProviderDetails_hash::iterator h_it
= m_hProviderDetailsCache
.find( providerName
);
70 if ( h_it
!= m_hProviderDetailsCache
.end() )
72 if ( h_it
->second
.provider
.is() )
74 provider
= h_it
->second
.provider
;
78 // need to create provider and insert into hash
79 provider
= createProvider( h_it
->second
);
85 Sequence
< Reference
< provider::XScriptProvider
> >
86 ProviderCache::getAllProviders() throw ( RuntimeException
)
88 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
92 ::osl::Guard
< osl::Mutex
> aGuard( m_mutex
);
93 ProviderDetails_hash::iterator h_itEnd
= m_hProviderDetailsCache
.end();
94 ProviderDetails_hash::iterator h_it
= m_hProviderDetailsCache
.begin();
95 // should assert if size !> 0
96 if ( !m_hProviderDetailsCache
.empty() )
98 sal_Int32 providerIndex
= 0;
100 for ( index
= 0; h_it
!= h_itEnd
; ++h_it
, index
++ )
102 Reference
< provider::XScriptProvider
> xScriptProvider
= h_it
->second
.provider
;
103 if ( xScriptProvider
.is() )
105 providers
[ providerIndex
++ ] = xScriptProvider
;
112 xScriptProvider
= createProvider( h_it
->second
);
113 providers
[ providerIndex
++ ] = xScriptProvider
;
115 catch ( const Exception
& )
117 DBG_UNHANDLED_EXCEPTION();
122 if ( providerIndex
< index
)
124 providers
.realloc( providerIndex
);
130 OSL_TRACE("no available providers, something very wrong!!!");
136 ProviderCache::populateCache() throw ( RuntimeException
)
138 // wrong name in services.rdb
139 OUString serviceName
;
140 ::osl::Guard
< osl::Mutex
> aGuard( m_mutex
);
143 OUString
languageProviderName( "com.sun.star.script.provider.LanguageScriptProvider" );
145 Reference
< container::XContentEnumerationAccess
> xEnumAccess
= Reference
< container::XContentEnumerationAccess
>( m_xMgr
, UNO_QUERY_THROW
);
146 Reference
< container::XEnumeration
> xEnum
= xEnumAccess
->createContentEnumeration ( languageProviderName
);
148 while ( xEnum
->hasMoreElements() )
151 Reference
< lang::XSingleComponentFactory
> factory( xEnum
->nextElement(), UNO_QUERY_THROW
);
152 Reference
< lang::XServiceInfo
> xServiceInfo( factory
, UNO_QUERY_THROW
);
154 Sequence
< OUString
> serviceNames
= xServiceInfo
->getSupportedServiceNames();
156 if ( serviceNames
.getLength() > 0 )
158 OUString
searchString( "com.sun.star.script.provider.ScriptProviderFor" );
160 for ( sal_Int32 index
= 0; index
< serviceNames
.getLength(); index
++ )
162 if ( serviceNames
[ index
].startsWith( searchString
) && !isInBlackList( serviceNames
[ index
] ) )
164 serviceName
= serviceNames
[ index
];
165 ProviderDetails details
;
166 details
.factory
= factory
;
167 m_hProviderDetailsCache
[ serviceName
] = details
;
174 catch ( const Exception
&e
)
177 "ProviderCache::populateCache: couldn't obtain XSingleComponentFactory for "
179 throw RuntimeException( temp
.concat( e
.Message
) );
183 Reference
< provider::XScriptProvider
>
184 ProviderCache::createProvider( ProviderDetails
& details
) throw ( RuntimeException
)
188 details
.provider
.set(
189 details
.factory
->createInstanceWithArgumentsAndContext( m_Sctx
, m_xContext
), UNO_QUERY_THROW
);
191 catch ( const Exception
& e
)
193 OUString
temp("ProviderCache::createProvider() Error creating provider from factory!!!\n");
194 throw RuntimeException( temp
.concat( e
.Message
) );
197 return details
.provider
;
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */