Update ooo320-m1
[ooovba.git] / configmgr / source / registry / configregistry.cxx
blob636d1fd9db126980dcf4cc165d3d883185c28e48
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: configregistry.cxx,v $
10 * $Revision: 1.13 $
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"
35 #include "configregistry.hxx"
36 #include "cfgregistrykey.hxx"
37 #include "confapifactory.hxx"
38 #include "datalock.hxx"
39 #include "utility.hxx"
40 #include <comphelper/sequence.hxx>
41 #include <cppuhelper/typeprovider.hxx>
42 #include <osl/diagnose.h>
43 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
44 #include <com/sun/star/lang/DisposedException.hpp>
45 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
46 #include <com/sun/star/beans/PropertyValue.hpp>
47 #include <com/sun/star/container/XNameAccess.hpp>
48 #include <com/sun/star/uno/XComponentContext.hpp>
50 #define THISREF() static_cast< ::cppu::OWeakObject* >(this)
51 #define UNISTRING(c) makeUniString(c)
53 //..........................................................................
54 namespace configmgr
56 //..........................................................................
58 namespace beans = ::com::sun::star::beans;
60 //==========================================================================
61 //= OConfigurationRegistry
62 //==========================================================================
63 inline
64 static
65 rtl::OUString makeUniString(char const* c)
67 return rtl::OUString::createFromAscii(c);
71 // #99130# Don't export SimpleRegistry service
72 static sal_Char const * const aExportedConfigRegistryServices[] =
74 "com.sun.star.configuration.ConfigurationRegistry",
75 NULL
77 static sal_Char const * const aAdditionalConfigRegistryServices[] =
79 "com.sun.star.registry.SimpleRegistry",
80 NULL
83 sal_Char const * const aConfigRegistryImplementationName = "com.sun.star.comp.configuration.OConfigurationRegistry";
85 const ServiceImplementationInfo OConfigurationRegistry::s_aServiceInfo =
87 aConfigRegistryImplementationName,
88 aExportedConfigRegistryServices,
89 aAdditionalConfigRegistryServices
92 com::sun::star::uno::Reference< com::sun::star::uno::XInterface > SAL_CALL instantiateConfigRegistry(uno::Reference< uno::XComponentContext > const& xContext )
94 OSL_ASSERT( xContext.is() );
95 com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), com::sun::star::uno::UNO_QUERY );
96 ::cppu::OWeakObject * pNewInstance = new OConfigurationRegistry(xServiceManager);
97 return pNewInstance;
100 const ServiceRegistrationInfo* getConfigurationRegistryServiceInfo()
102 return getRegistrationInfo(& OConfigurationRegistry::s_aServiceInfo);
105 //--------------------------------------------------------------------------
106 OConfigurationRegistry::OConfigurationRegistry(const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >& _rORB) throw(com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException)
107 :ServiceComponentImpl(&s_aServiceInfo)
108 ,m_xORB(_rORB)
110 // create the configuration provider used for accessing the configuration
111 OSL_ENSURE(m_xORB.is(), "OConfigurationRegistry::OConfigurationRegistry : invalid service factory !");
112 if (m_xORB.is())
114 m_xConfigurationProvider =
115 m_xConfigurationProvider.query(
116 m_xORB->createInstance(UNISTRING("com.sun.star.configuration.ConfigurationProvider"))
120 if (!m_xConfigurationProvider.is())
122 // it's heavily needed ...
123 throw com::sun::star::lang::ServiceNotRegisteredException(UNISTRING("Failed to instantiate the mandatory service com.sun.star.configuration.ConfigurationProvider."),
124 THISREF());
128 //--------------------------------------------------------------------------
129 com::sun::star::uno::Any SAL_CALL OConfigurationRegistry::queryInterface( const com::sun::star::uno::Type& _rType ) throw(com::sun::star::uno::RuntimeException)
131 com::sun::star::uno::Any aReturn = ServiceComponentImpl::queryInterface(_rType);
132 if (!aReturn.hasValue())
133 aReturn = cppu::ImplHelper2< com::sun::star::registry::XSimpleRegistry, com::sun::star::util::XFlushable >::queryInterface(_rType);
134 return aReturn;
137 //--------------------------------------------------------------------------
138 com::sun::star::uno::Sequence< com::sun::star::uno::Type > SAL_CALL OConfigurationRegistry::getTypes( ) throw(com::sun::star::uno::RuntimeException)
140 return ::comphelper::concatSequences(
141 ServiceComponentImpl::getTypes(),
142 cppu::ImplHelper2< com::sun::star::registry::XSimpleRegistry, com::sun::star::util::XFlushable >::getTypes());
145 //--------------------------------------------------------------------------
146 com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL OConfigurationRegistry::getImplementationId( ) throw(com::sun::star::uno::RuntimeException)
148 static cppu::OImplementationId aId;
149 return aId.getImplementationId();
152 //--------------------------------------------------------------------------
153 ::rtl::OUString OConfigurationRegistry::getNodePathFromURL(const ::rtl::OUString& _rURL)
155 // TODO
156 return _rURL;
159 //--------------------------------------------------------------------------
160 ::rtl::OUString SAL_CALL OConfigurationRegistry::getURL() throw(com::sun::star::uno::RuntimeException)
162 UnoApiLock aLock;
163 return m_sLocation;
166 //--------------------------------------------------------------------------
168 // Not guarded !
169 void OConfigurationRegistry::implCheckOpen() throw(com::sun::star::registry::InvalidRegistryException, com::sun::star::uno::RuntimeException)
171 if (!implIsOpen())
172 throw com::sun::star::registry::InvalidRegistryException(UNISTRING("The registry is not bound to a configuration node."), THISREF());
175 //--------------------------------------------------------------------------
176 void SAL_CALL OConfigurationRegistry::open( const ::rtl::OUString& _rURL, sal_Bool _bReadOnly, sal_Bool /*_bCreate*/ ) throw(com::sun::star::registry::InvalidRegistryException, com::sun::star::uno::RuntimeException)
178 UnoApiLock aLock;
180 if (implIsOpen())
181 close();
183 ::rtl::OUString sNodePath = getNodePathFromURL(_rURL);
185 if (!m_xConfigurationProvider.is())
186 throw com::sun::star::lang::DisposedException(UNISTRING("invalid object. configuration provider is already disposed."), THISREF());
188 com::sun::star::uno::Reference< com::sun::star::uno::XInterface > xNodeAccess;
191 char const * const sAccessType = _bReadOnly ?
192 "com.sun.star.configuration.ConfigurationAccess" :
193 "com.sun.star.configuration.ConfigurationUpdateAccess";
195 // prepare parameters for creating the config access : the node path
196 beans::PropertyValue aArgValue;
197 aArgValue.Handle = -1;
199 // currently theres is one parameter: the node path
200 com::sun::star::uno::Sequence< com::sun::star::uno::Any > aArguments(1);
202 aArgValue.Name = UNISTRING("nodepath");
203 aArgValue.Value <<= sNodePath;
205 aArguments[0] <<= aArgValue;
208 xNodeAccess = m_xConfigurationProvider->createInstanceWithArguments(UNISTRING(sAccessType), aArguments);
210 catch (com::sun::star::uno::RuntimeException&)
211 { // allowed to leave this method
212 throw;
214 catch (com::sun::star::uno::Exception& e)
215 { // not allowed to leave this method
216 ::rtl::OUString sMessage = UNISTRING("The configuration provider does not supply a registry access for the requested Node.");
217 sMessage += UNISTRING(" original error message of the provider : ");
218 sMessage += e.Message;
219 throw com::sun::star::registry::InvalidRegistryException(sMessage, THISREF());
222 com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > xReadRoot(xNodeAccess, com::sun::star::uno::UNO_QUERY);
223 if (!_bReadOnly)
224 m_xUpdateRoot = m_xUpdateRoot.query(xReadRoot);
226 if (!xReadRoot.is() || (!_bReadOnly && !m_xUpdateRoot.is()))
227 throw com::sun::star::registry::InvalidRegistryException(UNISTRING("The object supplied the by configuration provider is invalid."), THISREF());
229 m_xRootKey = new OConfigurationRegistryKey(xReadRoot, !_bReadOnly, OConfigurationRegistryKey::SubtreeRoot());
230 m_xSubtreeRoot = xNodeAccess;
233 //--------------------------------------------------------------------------
234 sal_Bool SAL_CALL OConfigurationRegistry::isValid( ) throw(com::sun::star::uno::RuntimeException)
236 UnoApiLock aLock;
237 return implIsOpen();
240 //--------------------------------------------------------------------------
241 sal_Bool OConfigurationRegistry::implIsOpen( ) throw(com::sun::star::uno::RuntimeException)
243 return m_xRootKey.is();
246 //--------------------------------------------------------------------------
247 void SAL_CALL OConfigurationRegistry::close( ) throw(com::sun::star::registry::InvalidRegistryException, com::sun::star::uno::RuntimeException)
249 UnoApiLock aLock;
251 com::sun::star::uno::Reference< com::sun::star::registry::XRegistryKey > xRootKey(m_xRootKey);
252 m_xRootKey = NULL;
254 com::sun::star::uno::Reference< XComponent > xRootComponent(m_xSubtreeRoot, com::sun::star::uno::UNO_QUERY);
255 m_xSubtreeRoot = NULL;
256 m_xUpdateRoot = NULL;
258 m_sLocation = ::rtl::OUString();
260 if (xRootKey.is())
261 xRootKey->closeKey();
263 if (xRootComponent.is())
264 xRootComponent->dispose();
267 //--------------------------------------------------------------------------
268 void SAL_CALL OConfigurationRegistry::disposing()
270 close();
273 UnoApiLock aLock;
275 m_xConfigurationProvider.clear();
276 m_xORB.clear();
279 ServiceComponentImpl::disposing();
282 //--------------------------------------------------------------------------
283 void SAL_CALL OConfigurationRegistry::destroy( ) throw(com::sun::star::registry::InvalidRegistryException, com::sun::star::uno::RuntimeException)
285 UnoApiLock aLock;
286 implCheckOpen();
288 throw com::sun::star::registry::InvalidRegistryException(UNISTRING("This registry is a wrapper for a configuration access. It can not be destroyed."), THISREF());
291 //--------------------------------------------------------------------------
292 com::sun::star::uno::Reference< com::sun::star::registry::XRegistryKey > SAL_CALL OConfigurationRegistry::getRootKey( ) throw(com::sun::star::registry::InvalidRegistryException, com::sun::star::uno::RuntimeException)
294 UnoApiLock aLock;
295 implCheckOpen();
297 return m_xRootKey;
300 //--------------------------------------------------------------------------
301 sal_Bool SAL_CALL OConfigurationRegistry::isReadOnly( ) throw(com::sun::star::registry::InvalidRegistryException, com::sun::star::uno::RuntimeException)
303 UnoApiLock aLock;
304 implCheckOpen();
306 return !m_xUpdateRoot.is();
307 // if we don't have the update root, we're readonly
310 //--------------------------------------------------------------------------
311 void SAL_CALL OConfigurationRegistry::mergeKey( const ::rtl::OUString& /*aKeyName*/, const ::rtl::OUString& /*aUrl*/ ) throw(com::sun::star::registry::InvalidRegistryException, com::sun::star::registry::MergeConflictException, com::sun::star::uno::RuntimeException)
313 UnoApiLock aLock;
314 implCheckOpen();
316 // not supported. but we can't throw an NoSupportException here ...
317 throw com::sun::star::registry::InvalidRegistryException(UNISTRING("You can't merge into this registry. It's just a wrapper for a configuration node, which has a fixed structure which can not be modified"), THISREF());
320 //--------------------------------------------------------------------------
321 void SAL_CALL OConfigurationRegistry::flush( ) throw(com::sun::star::uno::RuntimeException)
324 UnoApiLock aLock;
325 if (m_xUpdateRoot.is())
329 m_xUpdateRoot->commitChanges();
331 catch (com::sun::star::lang::WrappedTargetException& e)
332 { // not allowed to leave this method
334 ::rtl::OUString sMessage;
335 sMessage = UNISTRING("The changes made could not be committed. Orginal exception message : ");
336 sMessage += e.Message;
338 // TODO : the specification of XFlushable has to be changed !!!!!
339 OSL_ENSURE(sal_False, "OConfigurationRegistry::flush : caught an exception, could not flush the data !");
340 // return;
342 throw com::sun::star::lang::WrappedTargetRuntimeException(sMessage, THISREF(), e.TargetException);
347 com::sun::star::uno::Reference< com::sun::star::util::XFlushListener > const * const pSelector = 0;
348 if (cppu::OInterfaceContainerHelper* pContainer = this->rBHelper.getContainer(::getCppuType(pSelector)) )
350 ::cppu::OInterfaceIteratorHelper aIter( *pContainer );
352 com::sun::star::lang::EventObject aFlushed(THISREF());
353 while (aIter.hasMoreElements())
356 static_cast< com::sun::star::util::XFlushListener* >(aIter.next())->flushed(aFlushed);
358 catch (uno::Exception & )
363 //--------------------------------------------------------------------------
364 void SAL_CALL OConfigurationRegistry::addFlushListener( const com::sun::star::uno::Reference< com::sun::star::util::XFlushListener >& _rxListener ) throw(com::sun::star::uno::RuntimeException)
366 this->rBHelper.addListener(::getCppuType(&_rxListener),_rxListener);
369 //--------------------------------------------------------------------------
370 void SAL_CALL OConfigurationRegistry::removeFlushListener( const com::sun::star::uno::Reference< com::sun::star::util::XFlushListener >& _rxListener ) throw(com::sun::star::uno::RuntimeException)
372 this->rBHelper.removeListener(::getCppuType(&_rxListener),_rxListener);
375 //..........................................................................
376 } // namespace configmgr
377 //..........................................................................