update dev300-m58
[ooovba.git] / configmgr / source / misc / providerwrapper.cxx
blob5fdf876745d9577dd8b481a7f49d7189cebcc0c6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: providerwrapper.cxx,v $
10 * $Revision: 1.7 $
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_configmgr.hxx"
34 #include "providerwrapper.hxx"
35 #include "bootstrap.hxx"
36 #include "bootstrapcontext.hxx"
39 #include <com/sun/star/lang/NullPointerException.hpp>
40 #include <com/sun/star/lang/DisposedException.hpp>
42 #include <algorithm>
45 namespace configmgr
47 //==========================================================================
48 namespace uno = com::sun::star::uno;
49 namespace lang = com::sun::star::lang;
50 //==========================================================================
51 //= ProviderWrapper
52 //==========================================================================
54 uno::Reference< uno::XInterface > ProviderWrapper::create( uno::Reference< uno::XInterface > xDelegate, uno::Sequence< com::sun::star::beans::NamedValue > const & aPresets)
56 uno::Reference< lang::XMultiServiceFactory > xProvDelegate(xDelegate, uno::UNO_QUERY);
57 if (!xProvDelegate.is())
59 rtl::OUString sMsg(RTL_CONSTASCII_USTRINGPARAM("ProviderWrapper: Cannot wrap a NULL provider"));
60 throw lang::NullPointerException(sMsg,NULL);
62 //Strip prefixes
63 uno::Sequence< com::sun::star::beans::NamedValue > aStrippedPresets = aPresets;
65 for (sal_Int32 i = 0; i < aPresets.getLength(); ++i)
67 if(aPresets[i].Name.matchAsciiL(RTL_CONSTASCII_STRINGPARAM(CONTEXT_ITEM_PREFIX_ )))
69 aStrippedPresets[i].Name = aPresets[i].Name.copy(RTL_CONSTASCII_LENGTH(CONTEXT_ITEM_PREFIX_ ));
73 uno::Reference< lang::XMultiServiceFactory > xResult( new ProviderWrapper(xProvDelegate,aStrippedPresets) );
75 DisposingForwarder::forward( uno::Reference< lang::XComponent >::query(xProvDelegate),uno::Reference< lang::XComponent >::query(xResult) );
76 return uno::Reference< uno::XInterface >( xResult, uno::UNO_QUERY );
80 ProviderWrapper::ProviderWrapper(uno::Reference< lang::XMultiServiceFactory > const & xDelegate, uno::Sequence< com::sun::star::beans::NamedValue > const & aPresets)
81 : cppu::WeakComponentImplHelper2< lang::XMultiServiceFactory, lang::XServiceInfo >( PWMutexHolder::mutex )
82 , m_xDelegate(xDelegate)
83 , m_aDefaults(aPresets.getLength())
85 OSL_ASSERT(m_xDelegate.is());
87 for (sal_Int32 i = 0; i<aPresets.getLength(); ++i)
89 m_aDefaults[i] <<= aPresets[i];
93 ProviderWrapper::~ProviderWrapper() {}
95 void SAL_CALL ProviderWrapper::disposing()
97 osl::MutexGuard lock(mutex);
98 m_xDelegate.clear();
101 uno::Reference< lang::XMultiServiceFactory > ProviderWrapper::getDelegate()
103 osl::MutexGuard lock(mutex);
104 if (!m_xDelegate.is())
106 rtl::OUString sMsg(RTL_CONSTASCII_USTRINGPARAM("ProviderWrapper: Delegate Provider has been disposed"));
107 throw lang::DisposedException(sMsg,*this);
109 return m_xDelegate;
113 uno::Reference<lang::XServiceInfo> ProviderWrapper::getDelegateInfo()
115 uno::Reference<lang::XServiceInfo> xDelegate( this->getDelegate(), uno::UNO_QUERY );
116 if (!xDelegate.is())
118 rtl::OUString sMsg(RTL_CONSTASCII_USTRINGPARAM("ProviderWrapper: Delegate Provider has no service info"));
119 throw uno::RuntimeException(sMsg,*this);
121 return xDelegate;
124 /// XMultiServiceFactory
125 static inline uno::Any const * begin(uno::Sequence< uno::Any > const & aArgs)
126 { return aArgs.getConstArray(); }
127 static inline uno::Any const * end(uno::Sequence< uno::Any > const & aArgs)
128 { return aArgs.getConstArray() + aArgs.getLength(); }
129 static inline uno::Any * begin(uno::Sequence< uno::Any > & aArgs)
130 { return aArgs.getArray(); }
131 static inline uno::Any * end(uno::Sequence< uno::Any > & aArgs)
132 { return aArgs.getArray() + aArgs.getLength(); }
134 uno::Sequence< uno::Any > ProviderWrapper::patchArguments(uno::Sequence< uno::Any > const & aArgs) const
136 // rely on evaluation order front to back
137 if (m_aDefaults.getLength() == 0) return aArgs;
139 uno::Sequence< uno::Any > aResult(m_aDefaults.getLength() + aArgs.getLength());
141 uno::Any * pNext = std::copy(begin(m_aDefaults),end(m_aDefaults),begin(aResult));
142 pNext = std::copy(begin(aArgs),end(aArgs),pNext);
144 OSL_ASSERT(end(aResult) == pNext);
146 return aResult;
149 uno::Reference< uno::XInterface > SAL_CALL
150 ProviderWrapper::createInstance( const rtl::OUString& aServiceSpecifier )
151 throw(uno::Exception, uno::RuntimeException)
153 return getDelegate()->createInstanceWithArguments(aServiceSpecifier,m_aDefaults);
156 uno::Reference< uno::XInterface > SAL_CALL
157 ProviderWrapper::createInstanceWithArguments( const ::rtl::OUString& ServiceSpecifier, const uno::Sequence< uno::Any >& rArguments )
158 throw(uno::Exception, uno::RuntimeException)
160 return getDelegate()->createInstanceWithArguments(ServiceSpecifier,patchArguments(rArguments));
163 uno::Sequence< rtl::OUString > SAL_CALL
164 ProviderWrapper::getAvailableServiceNames( )
165 throw(uno::RuntimeException)
167 return getDelegate()->getAvailableServiceNames( );
170 /// XServiceInfo
171 rtl::OUString SAL_CALL
172 ProviderWrapper::getImplementationName( )
173 throw(uno::RuntimeException)
175 return rtl::OUString::createFromAscii("com.sun.star.comp.configuration.ConfigurationProviderWrapper");
178 sal_Bool SAL_CALL
179 ProviderWrapper::supportsService( const ::rtl::OUString& ServiceName )
180 throw(uno::RuntimeException)
182 return getDelegateInfo()->supportsService( ServiceName );
185 uno::Sequence< rtl::OUString > SAL_CALL
186 ProviderWrapper::getSupportedServiceNames( )
187 throw(uno::RuntimeException)
189 return getDelegateInfo()->getSupportedServiceNames( );
193 } // namespace configmgr