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: importsvc.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_configmgr.hxx"
34 #include "importsvc.hxx"
35 #include "importmergehandler.hxx"
36 #include "backendfactory.hxx"
38 #ifndef CONFIGMGR_API_FACTORY_HXX_
39 #include "confapifactory.hxx"
41 #include <com/sun/star/beans/NamedValue.hpp>
42 // -----------------------------------------------------------------------------
46 // -----------------------------------------------------------------------------
49 // -----------------------------------------------------------------------------
50 namespace uno
= ::com::sun::star::uno
;
51 namespace lang
= ::com::sun::star::lang
;
52 namespace beans
= ::com::sun::star::beans
;
53 namespace backenduno
= ::com::sun::star::configuration::backend
;
54 // -----------------------------------------------------------------------------
55 // -----------------------------------------------------------------------------
57 sal_Char
const * const aMergeImporterServices
[] =
59 "com.sun.star.configuration.backend.MergeImporter",
61 "com.sun.star.configuration.backend.Importer",
64 const ServiceImplementationInfo aMergeImporterSI
=
66 "com.sun.star.comp.configuration.backend.MergeImporter",
67 aMergeImporterServices
, aMergeImporterServices
+ 2
69 // -----------------------------------------------------------------------------
71 const ServiceRegistrationInfo
* getMergeImportServiceInfo()
72 { return getRegistrationInfo(& aMergeImporterSI
); }
73 // -----------------------------------------------------------------------------
75 MergeImportService::MergeImportService(uno::Reference
< uno::XComponentContext
> const & _xContext
)
76 : ImportService(_xContext
, &aMergeImporterSI
)
79 // -----------------------------------------------------------------------------
81 uno::Reference
< uno::XInterface
> SAL_CALL instantiateMergeImporter
82 ( uno::Reference
< uno::XComponentContext
> const& xContext
)
84 return * new MergeImportService( xContext
);
86 // -----------------------------------------------------------------------------
88 uno::Reference
< backenduno::XLayerHandler
> MergeImportService::createImportHandler(uno::Reference
< backenduno::XBackend
> const & xBackend
, rtl::OUString
const & aEntity
)
92 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("configmgr::backend::ImportService: Trying to import without a backend. No default backend could be created") );
93 throw lang::NullPointerException(sMessage
,*this);
96 uno::Reference
< backenduno::XLayerHandler
> aHandler( new ImportMergeHandler(xBackend
, ImportMergeHandler::merge
, aEntity
, m_bSendNotification
) );
100 // -----------------------------------------------------------------------------
101 // -----------------------------------------------------------------------------
103 sal_Char
const * const aCopyImporterServices
[] =
105 "com.sun.star.configuration.backend.CopyImporter",
107 "com.sun.star.configuration.backend.Importer",
110 const ServiceImplementationInfo aCopyImporterSI
=
112 "com.sun.star.comp.configuration.backend.CopyImporter",
113 aCopyImporterServices
, aCopyImporterServices
+ 2
115 // -----------------------------------------------------------------------------
117 const ServiceRegistrationInfo
* getCopyImportServiceInfo()
118 { return getRegistrationInfo(& aCopyImporterSI
); }
119 // -----------------------------------------------------------------------------
121 CopyImportService::CopyImportService(uno::Reference
< uno::XComponentContext
> const & _xContext
)
122 : ImportService(_xContext
, &aCopyImporterSI
)
125 // -----------------------------------------------------------------------------
127 uno::Reference
< uno::XInterface
> SAL_CALL instantiateCopyImporter
128 ( uno::Reference
< uno::XComponentContext
> const& xContext
)
130 return * new CopyImportService( xContext
);
132 // -----------------------------------------------------------------------------
134 uno::Reference
< backenduno::XLayerHandler
> CopyImportService::createImportHandler(uno::Reference
< backenduno::XBackend
> const & xBackend
, rtl::OUString
const & aEntity
)
138 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("configmgr::backend::ImportService: Trying to import without a backend. No default backend could be created") );
139 throw lang::NullPointerException(sMessage
,*this);
142 ImportMergeHandler::Mode aMode
= m_bOverwrite
? ImportMergeHandler::copy
: ImportMergeHandler::no_overwrite
;
143 uno::Reference
< backenduno::XLayerHandler
> aHandler( new ImportMergeHandler(xBackend
,aMode
,aEntity
) );
147 // -----------------------------------------------------------------------------
149 sal_Bool
CopyImportService::setImplementationProperty(rtl::OUString
const & aName
, uno::Any
const & aValue
)
151 if (aName
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Overwrite")))
153 return (aValue
>>= m_bOverwrite
);
156 return ImportService::setImplementationProperty(aName
,aValue
);
158 // -----------------------------------------------------------------------------
159 // -----------------------------------------------------------------------------
160 // -----------------------------------------------------------------------------
162 ImportService::ImportService(uno::Reference
< uno::XComponentContext
> const & _xContext
, ServiceInfoHelper
const & aSvcInfo
)
163 : m_bSendNotification(false)
165 , m_xContext(_xContext
)
166 , m_xDestinationBackend()
167 , m_aServiceInfo(aSvcInfo
)
169 if (!m_xContext
.is())
171 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("Configuration Importer: Unexpected NULL context"));
172 throw lang::NullPointerException(sMessage
,NULL
);
175 // -----------------------------------------------------------------------------
177 ImportService::~ImportService()
179 // -----------------------------------------------------------------------------
181 uno::Reference
< backenduno::XBackend
> ImportService::createDefaultBackend() const
183 return BackendFactory::instance( m_xContext
).getUnoBackend();
185 // -----------------------------------------------------------------------------
187 sal_Bool
ImportService::setImplementationProperty(rtl::OUString
const & aName
, uno::Any
const & aValue
)
189 if (aName
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Notify")))
191 return (aValue
>>= m_bSendNotification
);
196 // -----------------------------------------------------------------------------
201 ImportService::initialize( const uno::Sequence
< uno::Any
>& aArguments
)
202 throw (uno::Exception
, uno::RuntimeException
)
204 sal_Int16
const nCount
= static_cast<sal_Int16
>(aArguments
.getLength());
206 if (sal_Int32(nCount
) != aArguments
.getLength())
208 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("Too many arguments to initialize a Configuration Importer"));
209 throw lang::IllegalArgumentException(sMessage
,*this,0);
212 for (sal_Int16 i
= 0; i
< nCount
; ++i
)
214 beans::NamedValue aExtraArg
;
215 if (aArguments
[i
] >>= aExtraArg
)
217 OSL_VERIFY( setImplementationProperty(aExtraArg
.Name
, aExtraArg
.Value
) );
222 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("Cannot use argument to initialize a Configuration Importer"
223 "- NamedValue expected"));
224 throw lang::IllegalArgumentException(sMessage
,*this,i
+1);
228 // -----------------------------------------------------------------------------
232 uno::Reference
< backenduno::XBackend
> SAL_CALL
233 ImportService::getTargetBackend( )
234 throw (uno::RuntimeException
)
236 uno::Reference
< backenduno::XBackend
> xRet
;
238 osl::MutexGuard
aGuard(m_aMutex
);
239 xRet
= m_xDestinationBackend
;
244 xRet
= createDefaultBackend();
246 osl::MutexGuard
aGuard(m_aMutex
);
247 if (!m_xDestinationBackend
.is())
248 m_xDestinationBackend
= xRet
;
252 // -----------------------------------------------------------------------------
255 ImportService::setTargetBackend( const uno::Reference
< backenduno::XBackend
>& aBackend
)
256 throw (lang::NullPointerException
, uno::RuntimeException
)
260 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("configmgr::backend::ImportService: Trying to set a NULL backend") );
261 throw lang::NullPointerException(sMessage
,*this);
264 osl::MutexGuard
aGuard(m_aMutex
);
265 m_xDestinationBackend
= aBackend
;
267 // -----------------------------------------------------------------------------
270 ImportService::importLayer( const uno::Reference
< backenduno::XLayer
>& aLayer
)
271 throw ( backenduno::MalformedDataException
,
272 lang::WrappedTargetException
, lang::IllegalArgumentException
,
273 lang::NullPointerException
, uno::RuntimeException
)
277 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("configmgr::backend::ImportService: Trying to import a NULL layer") );
278 throw lang::NullPointerException(sMessage
,*this);
281 uno::Reference
< backenduno::XLayerHandler
> aInputHandler
= createImportHandler( getTargetBackend() );
282 aLayer
->readData( aInputHandler
);
285 // -----------------------------------------------------------------------------
288 ImportService::importLayerForEntity( const uno::Reference
< backenduno::XLayer
>& aLayer
, const rtl::OUString
& aEntity
)
289 throw ( backenduno::MalformedDataException
,
290 lang::WrappedTargetException
, lang::IllegalArgumentException
,
291 lang::NullPointerException
, uno::RuntimeException
)
295 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("configmgr::backend::ImportService: Trying to import a NULL layer") );
296 throw lang::NullPointerException(sMessage
,*this);
299 uno::Reference
< backenduno::XLayerHandler
> aInputHandler
= createImportHandler( getTargetBackend(), aEntity
);
300 aLayer
->readData( aInputHandler
);
302 // -----------------------------------------------------------------------------
306 rtl::OUString SAL_CALL
307 ImportService::getImplementationName( )
308 throw (uno::RuntimeException
)
310 return getServiceInfo().getImplementationName( );
312 // -----------------------------------------------------------------------------
316 ImportService::supportsService( const rtl::OUString
& ServiceName
)
317 throw (uno::RuntimeException
)
319 return getServiceInfo().supportsService( ServiceName
);
321 // -----------------------------------------------------------------------------
324 uno::Sequence
< ::rtl::OUString
> SAL_CALL
325 ImportService::getSupportedServiceNames( )
326 throw (uno::RuntimeException
)
328 return getServiceInfo().getSupportedServiceNames( );
330 // -----------------------------------------------------------------------------
332 // -----------------------------------------------------------------------------
333 // -----------------------------------------------------------------------------
336 // -----------------------------------------------------------------------------