1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _FRAMEWORK_SCRIPT_PROVIDER_PROVIDERCACHE_HXX_
25 #define _FRAMEWORK_SCRIPT_PROVIDER_PROVIDERCACHE_HXX_
26 
27 #include <hash_map>
28 #include <osl/mutex.hxx>
29 #include <rtl/ustring.hxx>
30 #include <cppuhelper/implbase1.hxx>
31 #include <com/sun/star/uno/RuntimeException.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
34 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
35 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
36 
37 #include <com/sun/star/frame/XModel.hpp>
38 #include <com/sun/star/script/provider/XScriptProvider.hpp>
39 
40 #include "ScriptingContext.hxx"
41 
42 namespace func_provider
43 {
44 // for simplification
45 #define css ::com::sun::star
46 
47 //Typedefs
48 //=============================================================================
49 
50 struct ProviderDetails
51 {
52     //css::uno::Reference< css::lang::XSingleServiceFactory > factory;
53     css::uno::Reference< css::lang::XSingleComponentFactory > factory;
54     css::uno::Reference< css::script::provider::XScriptProvider > provider;
55 };
56 typedef ::std::hash_map < ::rtl::OUString, ProviderDetails , ::rtl::OUStringHash,
57             ::std::equal_to< ::rtl::OUString > > ProviderDetails_hash;
58 
59 
60 class ProviderCache
61 {
62 
63 public:
64      ProviderCache( const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Sequence< css::uno::Any >& scriptContext )
65         throw ( css::uno::RuntimeException );
66      ProviderCache( const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Sequence< css::uno::Any >& scriptContext,
67         const css::uno::Sequence< ::rtl::OUString >& blackList )
68         throw ( css::uno::RuntimeException );
69     ~ProviderCache();
70      css::uno::Reference< css::script::provider::XScriptProvider >
71          getProvider( const ::rtl::OUString& providerName );
72      css::uno::Sequence < css::uno::Reference< css::script::provider::XScriptProvider > >
73          getAllProviders() throw ( css::uno::RuntimeException );
74 private:
75     void populateCache()
76         throw ( css::uno::RuntimeException );
77 
78    css::uno::Reference< css::script::provider::XScriptProvider >
79         createProvider( ProviderDetails& details ) throw ( css::uno::RuntimeException );
isInBlackList(const::rtl::OUString & serviceName)80     bool isInBlackList( const ::rtl::OUString& serviceName )
81     {
82         if ( m_sBlackList.getLength() > 0 )
83         {
84             for ( sal_Int32 index = 0; index < m_sBlackList.getLength(); index++ )
85             {
86                 if ( m_sBlackList[ index ].equals( serviceName ) )
87                 {
88                     return true;
89                 }
90             }
91         }
92         return false;
93     }
94     css::uno::Sequence< ::rtl::OUString >  m_sBlackList;
95     ProviderDetails_hash  m_hProviderDetailsCache;
96     osl::Mutex m_mutex;
97     css::uno::Sequence< css::uno::Any >  m_Sctx;
98     css::uno::Reference< css::uno::XComponentContext > m_xContext;
99     css::uno::Reference< css::lang::XMultiComponentFactory > m_xMgr;
100 
101 
102 };
103 } // func_provider
104 #endif
105