1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ProviderCache.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_scripting.hxx"
33 #include <cppuhelper/implementationentry.hxx>
34 #include <cppuhelper/factory.hxx>
36 #include <util/scriptingconstants.hxx>
37 #include <util/util.hxx>
39 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
40 #include "ProviderCache.hxx"
42 using namespace com::sun::star
;
43 using namespace com::sun::star::uno
;
44 using namespace com::sun::star::script
;
45 using namespace ::scripting_util
;
47 namespace func_provider
50 ProviderCache::ProviderCache( const Reference
< XComponentContext
>& xContext
, const Sequence
< Any
>& scriptContext
)
51 throw ( RuntimeException
) : m_Sctx( scriptContext
), m_xContext( xContext
)
53 // initialise m_hProviderDetailsCache with details of ScriptProviders
54 // will use createContentEnumeration
56 m_xMgr
= m_xContext
->getServiceManager();
57 validateXRef( m_xMgr
, "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
62 ProviderCache::ProviderCache( const Reference
< XComponentContext
>& xContext
, const Sequence
< Any
>& scriptContext
, const Sequence
< ::rtl::OUString
>& blackList
)
63 throw ( RuntimeException
) : m_sBlackList( blackList
), m_Sctx( scriptContext
), m_xContext( xContext
)
66 // initialise m_hProviderDetailsCache with details of ScriptProviders
67 // will use createContentEnumeration
69 m_xMgr
= m_xContext
->getServiceManager();
70 validateXRef( m_xMgr
, "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
74 ProviderCache::~ProviderCache()
78 Reference
< provider::XScriptProvider
>
79 ProviderCache::getProvider( const ::rtl::OUString
& providerName
)
81 ::osl::Guard
< osl::Mutex
> aGuard( m_mutex
);
82 Reference
< provider::XScriptProvider
> provider
;
83 ProviderDetails_hash::iterator h_it
= m_hProviderDetailsCache
.find( providerName
);
84 if ( h_it
!= m_hProviderDetailsCache
.end() )
86 if ( h_it
->second
.provider
.is() )
88 provider
= h_it
->second
.provider
;
92 // need to create provider and insert into hash
93 provider
= createProvider( h_it
->second
);
99 Sequence
< Reference
< provider::XScriptProvider
> >
100 ProviderCache::getAllProviders() throw ( RuntimeException
)
102 Sequence
< Reference
< provider::XScriptProvider
> > providers ( m_hProviderDetailsCache
.size() );
103 // need to create providers that haven't been created already
104 // so check what providers exist and what ones don't
106 ::osl::Guard
< osl::Mutex
> aGuard( m_mutex
);
107 ProviderDetails_hash::iterator h_itEnd
= m_hProviderDetailsCache
.end();
108 ProviderDetails_hash::iterator h_it
= m_hProviderDetailsCache
.begin();
109 // should assert if size !> 0
110 if ( m_hProviderDetailsCache
.size() )
112 sal_Int32 providerIndex
= 0;
114 for ( index
= 0; h_it
!= h_itEnd
; ++h_it
, index
++ )
116 Reference
< provider::XScriptProvider
> xScriptProvider
= h_it
->second
.provider
;
117 if ( xScriptProvider
.is() )
119 providers
[ providerIndex
++ ] = xScriptProvider
;
126 xScriptProvider
= createProvider( h_it
->second
);
127 providers
[ providerIndex
++ ] = xScriptProvider
;
129 catch ( Exception
& e
)
131 ::rtl::OUString temp
= OUSTR( "ProviderCache::getAllProviders: failed to create provider, " );
132 temp
.concat( e
.Message
);
133 //throw RuntimeException( temp.concat( e.Message ),
134 // Reference< XInterface >() );
139 if ( providerIndex
< index
)
141 providers
.realloc( providerIndex
);
147 OSL_TRACE("no available providers, something very wrong!!!");
153 ProviderCache::populateCache() throw ( RuntimeException
)
155 // wrong name in services.rdb
156 ::rtl::OUString serviceName
;
157 ::osl::Guard
< osl::Mutex
> aGuard( m_mutex
);
160 ::rtl::OUString
languageProviderName( RTL_CONSTASCII_USTRINGPARAM(
161 "com.sun.star.script.provider.LanguageScriptProvider" ) );
163 Reference
< container::XContentEnumerationAccess
> xEnumAccess
= Reference
< container::XContentEnumerationAccess
>( m_xMgr
, UNO_QUERY_THROW
);
164 Reference
< container::XEnumeration
> xEnum
= xEnumAccess
->createContentEnumeration ( languageProviderName
);
166 while ( xEnum
->hasMoreElements() )
169 Reference
< lang::XSingleComponentFactory
> factory
;
170 if ( sal_False
== ( xEnum
->nextElement() >>= factory
) )
172 throw new RuntimeException( ::rtl::OUString::createFromAscii( " error extracting XSingleComponentFactory from Content enumeration. " ), Reference
< XInterface
>() );
174 validateXRef( factory
, "ProviderCache::populateCache() invalid factory" );
175 Reference
< lang::XServiceInfo
> xServiceInfo( factory
, UNO_QUERY_THROW
);
176 validateXRef( xServiceInfo
, "ProviderCache::populateCache() failed to get XServiceInfo from factory" );
178 Sequence
< ::rtl::OUString
> serviceNames
= xServiceInfo
->getSupportedServiceNames();
180 if ( serviceNames
.getLength() > 0 )
182 ::rtl::OUString
searchString( RTL_CONSTASCII_USTRINGPARAM (
183 "com.sun.star.script.provider.ScriptProviderFor" ) );
185 for ( sal_Int32 index
= 0; index
< serviceNames
.getLength(); index
++ )
187 if ( serviceNames
[ index
].indexOf( searchString
) == 0 && !isInBlackList( serviceNames
[ index
] ) )
189 serviceName
= serviceNames
[ index
];
190 ProviderDetails details
;
191 details
.factory
= factory
;
192 m_hProviderDetailsCache
[ serviceName
] = details
;
199 catch ( Exception e
)
201 ::rtl::OUString temp
= OUSTR(
202 "ProviderCache::populateCache: couldn't obtain XSingleComponentFactory for " );
203 temp
.concat( serviceName
);
204 throw RuntimeException( temp
.concat( e
.Message
), Reference
< XInterface
>() );
208 Reference
< provider::XScriptProvider
>
209 ProviderCache::createProvider( ProviderDetails
& details
) throw ( RuntimeException
)
213 details
.provider
= Reference
< provider::XScriptProvider
>(
214 details
.factory
->createInstanceWithArgumentsAndContext( m_Sctx
, m_xContext
), UNO_QUERY_THROW
);
215 validateXRef( details
.provider
, "ProviderCache::createProvider, failed to create provider");
217 catch ( RuntimeException
& e
)
219 ::rtl::OUString temp
= ::rtl::OUString::createFromAscii("ProviderCache::createProvider() Error creating provider from factory!!!");
220 throw RuntimeException( temp
.concat( e
.Message
), Reference
< XInterface
>() );
223 return details
.provider
;