Update ooo320-m1
[ooovba.git] / configmgr / source / backend / updatesvc.cxx
blobed042dc37d59d2f32d92d9d1df0cce4e49717842
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: updatesvc.cxx,v $
10 * $Revision: 1.12 $
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 "updatesvc.hxx"
36 #ifndef CONFIGMGR_API_FACTORY_HXX_
37 #include "confapifactory.hxx"
38 #endif
39 #include "emptylayer.hxx"
40 #include <com/sun/star/configuration/backend/XUpdatableLayer.hpp>
41 #include <com/sun/star/configuration/backend/XLayerHandler.hpp>
42 #include <com/sun/star/beans/NamedValue.hpp>
43 #include <com/sun/star/lang/IllegalArgumentException.hpp>
45 // -----------------------------------------------------------------------------
47 namespace configmgr
49 // -----------------------------------------------------------------------------
50 namespace backend
52 // -----------------------------------------------------------------------------
53 namespace uno = ::com::sun::star::uno;
54 namespace lang = ::com::sun::star::lang;
55 namespace beans = ::com::sun::star::beans;
56 namespace backenduno = ::com::sun::star::configuration::backend;
57 // -----------------------------------------------------------------------------
59 sal_Char const * const aUpdateMergerServices[] =
61 "com.sun.star.configuration.backend.LayerUpdateMerger",
64 const ServiceImplementationInfo aUpdateMergerSI =
66 "com.sun.star.comp.configuration.backend.LayerUpdateMerger",
67 aUpdateMergerServices,
70 // -----------------------------------------------------------------------------
72 const ServiceRegistrationInfo* getUpdateMergerServiceInfo()
73 { return getRegistrationInfo(& aUpdateMergerSI); }
74 // -----------------------------------------------------------------------------
76 inline
77 ServiceInfoHelper UpdateService::getServiceInfo()
79 return & aUpdateMergerSI;
81 // -----------------------------------------------------------------------------
84 UpdateService::UpdateService(uno::Reference< uno::XComponentContext > const & _xContext)
85 : m_xServiceFactory(_xContext->getServiceManager(),uno::UNO_QUERY)
86 , m_aSourceMode(merge)
88 if (!m_xServiceFactory.is())
90 rtl::OUString sMessage( RTL_CONSTASCII_USTRINGPARAM("Configuration Update Merger: Context has no service manager (or missing interface)"));
91 throw uno::RuntimeException(sMessage,NULL);
94 // -----------------------------------------------------------------------------
96 // XInitialization
98 void SAL_CALL
99 UpdateService::initialize( const uno::Sequence< uno::Any >& aArguments )
100 throw (uno::Exception, uno::RuntimeException)
102 sal_Int16 const nCount = static_cast<sal_Int16>(aArguments.getLength());
104 if (sal_Int32(nCount) != aArguments.getLength())
106 rtl::OUString sMessage( RTL_CONSTASCII_USTRINGPARAM("Too many arguments to initialize a Configuration Update Merger"));
107 throw lang::IllegalArgumentException(sMessage,*this,0);
110 for (sal_Int16 i = 0; i < nCount; ++i)
112 uno::Reference< backenduno::XUpdatableLayer > xUpdLayer;
113 if (aArguments[i] >>= xUpdLayer)
115 m_xSourceLayer = xUpdLayer.get();
116 m_xLayerWriter.clear();
118 OSL_ASSERT( uno::Reference< backenduno::XUpdatableLayer >::query(m_xSourceLayer).is() || !xUpdLayer.is() );
120 continue;
123 if (aArguments[i] >>= m_xSourceLayer)
124 continue;
126 if (aArguments[i] >>= m_xLayerWriter)
127 continue;
129 beans::NamedValue aExtraArg;
130 if (aArguments[i] >>= aExtraArg)
132 OSL_VERIFY( setImplementationProperty(aExtraArg.Name, aExtraArg.Value) );
134 continue;
137 rtl::OUString sMessage( RTL_CONSTASCII_USTRINGPARAM("Cannot use argument to initialize a Configuration Update Merger"
138 "- XLayer, XLayerHandler or XUpdatableLayer expected"));
139 throw lang::IllegalArgumentException(sMessage,*this,i);
143 // -----------------------------------------------------------------------------
145 sal_Bool UpdateService::setImplementationProperty(rtl::OUString const & aName, uno::Any const & aValue)
147 if (aName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Overwrite")))
149 sal_Bool bOverwrite = sal_False;
150 if (aValue >>= bOverwrite)
152 if (!bOverwrite)
153 m_aSourceMode = protect;
155 else if (protect == m_aSourceMode)
156 m_aSourceMode = merge;
158 return true;
162 else if (aName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Truncate")))
164 sal_Bool bTruncate = sal_False;
165 if (aValue >>= bTruncate)
167 if (!bTruncate)
168 m_aSourceMode = merge;
170 else if (merge == m_aSourceMode)
171 m_aSourceMode = truncate;
173 return true;
177 return false;
179 // -----------------------------------------------------------------------------
181 bool UpdateService::validateSourceLayerAndCheckNotEmpty() SAL_THROW( (lang::IllegalAccessException) )
183 switch (m_aSourceMode)
185 case merge: // TODO: check for readonly layer
186 return true;
188 case protect: if (!checkEmptyLayer(m_xSourceLayer))
189 raiseIllegalAccessException("UpdateService: Layer already exists");
190 // else fall through
192 case truncate: return false;
194 default: OSL_ASSERT(!"not reached");
195 return true;
198 // -----------------------------------------------------------------------------
200 uno::Reference< backenduno::XLayer > UpdateService::getSourceLayer() SAL_THROW( (lang::IllegalAccessException) )
202 if ( validateSourceLayerAndCheckNotEmpty() )
203 return m_xSourceLayer;
204 else
205 return createEmptyLayer();
207 // -----------------------------------------------------------------------------
209 void UpdateService::raiseIllegalAccessException(sal_Char const * pMsg)
210 SAL_THROW( (lang::IllegalAccessException) )
212 rtl::OUString sMsg = rtl::OUString::createFromAscii(pMsg);
213 throw lang::IllegalAccessException(sMsg,*this);
215 // -----------------------------------------------------------------------------
217 void UpdateService::writeUpdatedLayer(uno::Reference< backenduno::XLayer > const & _xLayer)
219 OSL_ENSURE( _xLayer.is(), "UpdateService: Trying to write NULL XLayer");
221 if (!_xLayer.is())
223 rtl::OUString sMessage( RTL_CONSTASCII_USTRINGPARAM("Update Merger - Internal error: trying to write a NULL Layer"));
224 throw uno::RuntimeException(sMessage,*this);
227 // use our layer writer, if we have one
228 if ( m_xLayerWriter.is() )
230 _xLayer->readData( m_xLayerWriter );
231 return;
234 // look for an updatable layer otherwise
235 uno::Reference< backenduno::XUpdatableLayer > xUpdLayer(m_xSourceLayer, uno::UNO_QUERY);
236 if (xUpdLayer.is())
238 xUpdLayer->replaceWith( _xLayer );
239 return;
242 rtl::OUString sMessage( RTL_CONSTASCII_USTRINGPARAM("Update Merger: Cannot write merge results - no recipient available."));
243 throw uno::RuntimeException(sMessage,*this);
245 // -----------------------------------------------------------------------------
247 // XServiceInfo
249 ::rtl::OUString SAL_CALL
250 UpdateService::getImplementationName( )
251 throw (uno::RuntimeException)
253 return getServiceInfo().getImplementationName( );
255 // -----------------------------------------------------------------------------
258 sal_Bool SAL_CALL
259 UpdateService::supportsService( const ::rtl::OUString& ServiceName )
260 throw (uno::RuntimeException)
262 return getServiceInfo().supportsService( ServiceName );
264 // -----------------------------------------------------------------------------
267 uno::Sequence< ::rtl::OUString > SAL_CALL
268 UpdateService::getSupportedServiceNames( )
269 throw (uno::RuntimeException)
271 return getServiceInfo().getSupportedServiceNames( );
273 // -----------------------------------------------------------------------------
275 // -----------------------------------------------------------------------------
276 // -----------------------------------------------------------------------------
277 } // namespace
279 // -----------------------------------------------------------------------------
280 } // namespace