Branch data 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 : 13 : ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext )
38 [ + - ][ + - ]: 13 : throw ( RuntimeException ) : m_Sctx( scriptContext ), m_xContext( xContext )
[ + - ]
39 : : {
40 : : // initialise m_hProviderDetailsCache with details of ScriptProviders
41 : : // will use createContentEnumeration
42 : :
43 [ + - ][ + - ]: 13 : m_xMgr = m_xContext->getServiceManager();
[ + - ]
44 [ # # ][ # # ]: 13 : ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
[ # # ][ - + ]
45 [ + - ]: 13 : populateCache();
46 : 13 : }
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 [ + - ][ + - ]: 13 : ProviderCache::~ProviderCache()
[ + - ]
62 : : {
63 : 13 : }
64 : :
65 : : Reference< provider::XScriptProvider >
66 : 19 : ProviderCache::getProvider( const ::rtl::OUString& providerName )
67 : : {
68 [ + - ]: 19 : ::osl::Guard< osl::Mutex > aGuard( m_mutex );
69 : 19 : Reference< provider::XScriptProvider > provider;
70 [ + - ]: 19 : ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.find( providerName );
71 [ + - ][ + - ]: 19 : if ( h_it != m_hProviderDetailsCache.end() )
72 : : {
73 [ + - ][ + + ]: 19 : if ( h_it->second.provider.is() )
74 : : {
75 [ + - ][ + - ]: 6 : provider = h_it->second.provider;
76 : : }
77 : : else
78 : : {
79 : : // need to create provider and insert into hash
80 [ + - ][ + - ]: 13 : provider = createProvider( h_it->second );
[ + - ]
81 : : }
82 : : }
83 [ + - ]: 19 : 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 : 13 : ProviderCache::populateCache() throw ( RuntimeException )
138 : : {
139 : : // wrong name in services.rdb
140 : 13 : ::rtl::OUString serviceName;
141 [ + - ]: 13 : ::osl::Guard< osl::Mutex > aGuard( m_mutex );
142 : : try
143 : : {
144 : 13 : ::rtl::OUString languageProviderName( "com.sun.star.script.provider.LanguageScriptProvider" );
145 : :
146 [ + - ]: 13 : Reference< container::XContentEnumerationAccess > xEnumAccess = Reference< container::XContentEnumerationAccess >( m_xMgr, UNO_QUERY_THROW );
147 [ + - ][ + - ]: 13 : Reference< container::XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName );
148 : :
149 [ + - ][ + - ]: 34 : while ( xEnum->hasMoreElements() )
[ + + ]
150 : : {
151 : :
152 [ + - ][ + - ]: 21 : Reference< lang::XSingleComponentFactory > factory( xEnum->nextElement(), UNO_QUERY_THROW );
[ + - ]
153 [ + - ]: 21 : Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW );
154 : :
155 [ + - ][ + - ]: 21 : Sequence< ::rtl::OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
156 : :
157 [ + - ]: 21 : if ( serviceNames.getLength() > 0 )
158 : : {
159 : 21 : ::rtl::OUString searchString( "com.sun.star.script.provider.ScriptProviderFor" );
160 : :
161 [ + - ]: 72 : for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ )
162 : : {
163 [ + - ][ + + ]: 72 : if ( serviceNames[ index ].indexOf( searchString ) == 0 && !isInBlackList( serviceNames[ index ] ) )
[ + - ][ + - ]
[ + - ][ + + ]
164 : : {
165 [ + - ]: 21 : serviceName = serviceNames[ index ];
166 [ + - ]: 21 : ProviderDetails details;
167 [ + - ]: 21 : details.factory = factory;
168 [ + - ][ + - ]: 21 : m_hProviderDetailsCache[ serviceName ] = details;
169 [ + - ]: 21 : break;
170 : : }
171 : 21 : }
172 : : }
173 [ + - ]: 34 : }
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.concat( serviceName );
180 [ # # ]: 0 : throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
181 [ + - ]: 13 : }
182 : 13 : }
183 : :
184 : : Reference< provider::XScriptProvider >
185 : 13 : ProviderCache::createProvider( ProviderDetails& details ) throw ( RuntimeException )
186 : : {
187 : : try
188 : : {
189 : : details.provider.set(
190 [ + - ][ + - ]: 13 : details.factory->createInstanceWithArgumentsAndContext( m_Sctx, m_xContext ), UNO_QUERY_THROW );
[ + - ]
191 : : }
192 [ # # ]: 0 : catch ( const RuntimeException& 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 : 13 : return details.provider;
199 : : }
200 : : } //end namespace
201 : :
202 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|