Update ooo320-m1
[ooovba.git] / configmgr / source / misc / providerfactory.cxx
blob77f7432257e49f5fd316a205e9f3344484bd92d9
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: providerfactory.cxx,v $
10 * $Revision: 1.24 $
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"
33 #include <stdio.h>
35 #include "providerfactory.hxx"
37 #ifndef CONFIGMGR_API_FACTORY_HXX_
38 #include "confapifactory.hxx"
39 #endif
40 #include "bootstrap.hxx"
41 #include "providerwrapper.hxx"
42 #include <com/sun/star/lang/IllegalArgumentException.hpp>
43 #include <com/sun/star/configuration/CannotLoadConfigurationException.hpp>
44 #include <cppuhelper/exc_hlp.hxx>
45 #include <rtl/ustrbuf.hxx>
46 #include <osl/diagnose.h>
47 #include <rtl/logfile.hxx>
49 //---------------------------------------------------------------------------------------
50 namespace configmgr
52 //---------------------------------------------------------------------------------------
53 //= OProviderFactory
54 //---------------------------------------------------------------------------------------
56 //---------------------------------------------------------------------------------------
58 ProviderFactory::ProviderFactory(rtl::OUString const & aImplementationName, bool bAdmin)
59 : m_aImplementationName(aImplementationName)
60 , m_bAdmin(bAdmin)
63 //---------------------------------------------------------------------------------------
65 ProviderFactory::~ProviderFactory()
68 //---------------------------------------------------------------------------------------
70 uno::Reference< uno::XInterface > ProviderFactory::getProviderAlways(uno::Reference< uno::XComponentContext > const & xContext)
72 RTL_LOGFILE_CONTEXT_AUTHOR(aLog, "configmgr::ProviderFactory", "jb99855", "configmgr::ProviderFactory::getProviderAlways()");
73 uno::Reference< uno::XInterface > xResult = getDefaultConfigProviderSingleton(xContext);
75 // check for success
76 OSL_ENSURE(xResult.is(), "Context could not create provider, but returned NULL instead of throwing an exception");
77 if (!xResult.is())
79 static sal_Char const sCannotCreate[] = "Cannot create ConfigurationProvider. Unknown backend or factory error.";
81 throw com::sun::star::configuration::CannotLoadConfigurationException( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(sCannotCreate)), *this );
84 return xResult;
86 //---------------------------------------------------------------------------------------
87 uno::Reference< uno::XInterface > ProviderFactory::getProviderFromContext(uno::Reference< uno::XComponentContext > const & xContext)
89 OSL_ENSURE(ContextReader::testAdminService(xContext, this->m_bAdmin),
90 "Creation context admin flag does not match service being created");
92 try
94 return getProviderAlways(xContext);
96 catch(uno::Exception& e)
98 ContextReader aContext(xContext);
100 uno::Any aError = aContext.getBootstrapError();
101 if (aError.hasValue())
103 OSL_ASSERT(aError.getValueTypeClass() == uno::TypeClass_EXCEPTION);
104 cppu::throwException(aError);
107 OSL_ASSERT(aContext.isBootstrapValid());
109 static const sal_Char sErrContext[] = "Cannot open Configuration: ";
110 rtl::OUString const sContext(RTL_CONSTASCII_USTRINGPARAM(sErrContext));
111 e.Message = sContext.concat(e.Message);
112 throw;
115 //---------------------------------------------------------------------------------------
116 uno::Reference< uno::XInterface > ProviderFactory::createProviderWithArguments(uno::Reference< uno::XComponentContext > const & xContext, uno::Sequence < uno::Any > const & _aArguments)
118 RTL_LOGFILE_CONTEXT_AUTHOR(aLog, "configmgr::ProviderFactory", "jb99855", "configmgr::ProviderFactory::createProviderWithArguments()");
120 ContextReader aContext(xContext);
121 ArgumentHelper aParser(aContext.getBootstrapContext());
123 uno::Sequence < beans::NamedValue > aValues(_aArguments.getLength() + 2);
124 sal_Int32 nCount = parseArguments(aParser,aValues,_aArguments);
126 bool bNeedNewBackend = aParser.hasBackendArguments();
128 if (!aContext.testAdminService(aContext.getBaseContext(),m_bAdmin))
130 bNeedNewBackend = true;
131 OSL_ASSERT( nCount+2 <= aValues.getLength());
132 aValues[nCount++] = ArgumentHelper::makeAdminServiceOverride(m_bAdmin);
133 aValues[nCount++] = BootstrapContext::makePassthroughMarker(sal_False);
136 OSL_ASSERT(nCount <= aValues.getLength());
137 aValues.realloc(nCount);
139 if (bNeedNewBackend)
141 uno::Reference< uno::XComponentContext > xMergedContext = BootstrapContext::createWrapper(xContext,aValues);
142 uno::Reference< uno::XInterface > xResult = getProviderFromContext(xMergedContext);
144 return xResult;
146 else
148 uno::Reference< uno::XInterface > xBaseProvider = getProviderFromContext(xContext);
149 uno::Reference< uno::XInterface > xResult = ProviderWrapper::create(xBaseProvider,aValues);
151 return xResult;
154 //---------------------------------------------------------------------------------------
155 uno::Reference< uno::XInterface > ProviderFactory::createProvider(uno::Reference< uno::XComponentContext > const & xContext, bool bAdmin)
157 RTL_LOGFILE_CONTEXT_AUTHOR(aLog, "configmgr::ProviderFactory", "jb99855", "configmgr::ProviderFactory::createProvider(bAdmin)");
159 uno::Sequence < beans::NamedValue > aValues(2);
160 aValues[0] = ArgumentHelper::makeAdminServiceOverride(bAdmin);
161 aValues[1] = BootstrapContext::makePassthroughMarker(sal_False);
163 uno::Reference< uno::XComponentContext > xMergedContext = BootstrapContext::createWrapper(xContext,aValues);
164 uno::Reference< uno::XInterface > xResult = getProviderFromContext(xMergedContext);
166 return xResult;
168 //---------------------------------------------------------------------------------------
169 uno::Reference< uno::XInterface > ProviderFactory::createProvider(uno::Reference< uno::XComponentContext > const & xContext)
171 RTL_LOGFILE_CONTEXT_AUTHOR(aLog, "configmgr::ProviderFactory", "jb99855", "configmgr::ProviderFactory::createProvider()");
173 if (BootstrapContext::isPassthrough(xContext))
175 // make sure this uses a new BootstrapContext !
176 uno::Reference< uno::XComponentContext > xPatchedContext = BootstrapContext::createWrapper(xContext,uno::Sequence < beans::NamedValue >());
177 return getProviderFromContext(xPatchedContext);
179 else
180 return getProviderFromContext(xContext);
182 //---------------------------------------------------------------------------------------
184 sal_Int32 ProviderFactory::parseArguments(ArgumentHelper & aParser, uno::Sequence < beans::NamedValue > & rValues, uno::Sequence < uno::Any > const & _aArguments)
186 OSL_ASSERT(rValues.getLength() >= _aArguments.getLength());
188 sal_Int32 nCount = 0;
189 for (sal_Int32 i = 0; i < _aArguments.getLength(); ++i)
191 if (!aParser.extractArgument(rValues[nCount],_aArguments[i]))
193 rtl::OUStringBuffer sMsg;
194 sMsg.appendAscii("ProviderFactory: Unexpected Argument Type. ");
195 sMsg.appendAscii("Expected NamedValue or PropertyValue, ");
196 sMsg.appendAscii("found ").append(_aArguments[i].getValueTypeName()).appendAscii(". ");
197 throw lang::IllegalArgumentException(sMsg.makeStringAndClear(),*this,static_cast<sal_Int16>(i));
200 if (aParser.filterAndAdjustArgument(rValues[nCount]))
202 aParser.checkBackendArgument(rValues[nCount]);
203 ++nCount;
206 return nCount;
208 //---------------------------------------------------------------------------------------
210 uno::Reference< uno::XInterface >
211 SAL_CALL ProviderFactory::createInstanceWithContext( const uno::Reference< uno::XComponentContext >& xContext )
212 throw (uno::Exception, ::com::sun::star::uno::RuntimeException)
214 // default provider ?
215 if (ContextReader::testAdminService(xContext,m_bAdmin))
216 return createProvider( xContext );
218 else
219 return createProvider(xContext,m_bAdmin);
221 //---------------------------------------------------------------------------------------
223 uno::Reference< uno::XInterface > SAL_CALL
224 ProviderFactory::createInstanceWithArgumentsAndContext( const uno::Sequence< uno::Any >& aArguments, const uno::Reference< uno::XComponentContext >& xContext )
225 throw (uno::Exception, uno::RuntimeException)
227 // default request
228 return createProviderWithArguments(xContext, aArguments);
231 //---------------------------------------------------------------------------------------
232 //---------------------------------------------------------------------------------------
234 uno::Reference< lang::XSingleComponentFactory > SAL_CALL createProviderFactory(
235 rtl::OUString const & aImplementationName,
236 bool bAdmin
239 return new ProviderFactory(aImplementationName, bAdmin);
241 //---------------------------------------------------------------------------------------
242 } // namespace configmgr
243 //........................................................................