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: modulemanager.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_framework.hxx"
34 #include "services/modulemanager.hxx"
35 #include "services/frame.hxx"
37 //_______________________________________________
39 #include <threadhelp/readguard.hxx>
40 #include <threadhelp/writeguard.hxx>
43 //_______________________________________________
45 #include <com/sun/star/frame/XFrame.hpp>
46 #include <com/sun/star/frame/XController.hpp>
47 #include <com/sun/star/frame/XModel.hpp>
48 #include <com/sun/star/frame/XModule.hpp>
49 #include <comphelper/configurationhelper.hxx>
50 #include <comphelper/sequenceashashmap.hxx>
51 #include <comphelper/sequenceasvector.hxx>
52 #include <comphelper/enumhelper.hxx>
54 //_______________________________________________
56 #include <rtl/logfile.hxx>
61 static const ::rtl::OUString CFGPATH_FACTORIES
= ::rtl::OUString::createFromAscii("/org.openoffice.Setup/Office/Factories");
62 static const ::rtl::OUString MODULEPROP_IDENTIFIER
= ::rtl::OUString::createFromAscii("ooSetupFactoryModuleIdentifier" );
64 /*-----------------------------------------------
66 -----------------------------------------------*/
67 DEFINE_XINTERFACE_7(ModuleManager
,
69 DIRECT_INTERFACE(css::lang::XTypeProvider
),
70 DIRECT_INTERFACE(css::lang::XServiceInfo
),
71 DIRECT_INTERFACE(css::container::XNameReplace
),
72 DIRECT_INTERFACE(css::container::XNameAccess
),
73 DIRECT_INTERFACE(css::container::XElementAccess
),
74 DIRECT_INTERFACE(css::container::XContainerQuery
),
75 DIRECT_INTERFACE(css::frame::XModuleManager
))
77 /*-----------------------------------------------
79 -----------------------------------------------*/
80 DEFINE_XTYPEPROVIDER_7(ModuleManager
,
81 css::lang::XTypeProvider
,
82 css::lang::XServiceInfo
,
83 css::container::XNameReplace
,
84 css::container::XNameAccess
,
85 css::container::XElementAccess
,
86 css::container::XContainerQuery
,
87 css::frame::XModuleManager
)
89 /*-----------------------------------------------
91 -----------------------------------------------*/
92 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(ModuleManager
,
94 SERVICENAME_MODULEMANAGER
,
95 IMPLEMENTATIONNAME_MODULEMANAGER
)
97 /*-----------------------------------------------
99 -----------------------------------------------*/
104 I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
105 to create a new instance of this class by our own supported service factory.
106 see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
111 /*-----------------------------------------------
113 -----------------------------------------------*/
114 ModuleManager::ModuleManager(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
)
120 /*-----------------------------------------------
122 -----------------------------------------------*/
123 ModuleManager::~ModuleManager()
129 /*-----------------------------------------------
131 -----------------------------------------------*/
132 ::rtl::OUString SAL_CALL
ModuleManager::identify(const css::uno::Reference
< css::uno::XInterface
>& xModule
)
133 throw(css::lang::IllegalArgumentException
,
134 css::frame::UnknownModuleException
,
135 css::uno::RuntimeException
)
138 css::uno::Reference
< css::frame::XFrame
> xFrame (xModule
, css::uno::UNO_QUERY
);
139 css::uno::Reference
< css::awt::XWindow
> xWindow (xModule
, css::uno::UNO_QUERY
);
140 css::uno::Reference
< css::frame::XController
> xController(xModule
, css::uno::UNO_QUERY
);
141 css::uno::Reference
< css::frame::XModel
> xModel (xModule
, css::uno::UNO_QUERY
);
146 (!xController
.is()) &&
150 throw css::lang::IllegalArgumentException(
151 ::rtl::OUString::createFromAscii("Given module is not a frame nor a window, controller or model."),
152 static_cast< ::cppu::OWeakObject
* >(this),
158 xController
= xFrame
->getController();
159 xWindow
= xFrame
->getComponentWindow();
161 if (xController
.is())
162 xModel
= xController
->getModel();
164 // modules are implemented by the deepest component in hierarchy ...
165 // Means: model -> controller -> window
166 // No fallbacks to higher components are allowed !
167 // Note : A frame provides access to module components only ... but it's not a module by himself.
169 ::rtl::OUString sModule
;
171 sModule
= implts_identify(xModel
);
173 if (xController
.is())
174 sModule
= implts_identify(xController
);
177 sModule
= implts_identify(xWindow
);
179 if (sModule
.getLength() < 1)
180 throw css::frame::UnknownModuleException(
181 ::rtl::OUString::createFromAscii("Cant find suitable module for the given component."),
182 static_cast< ::cppu::OWeakObject
* >(this));
187 /*-----------------------------------------------
189 -----------------------------------------------*/
190 void SAL_CALL
ModuleManager::replaceByName(const ::rtl::OUString
& sName
,
191 const css::uno::Any
& aValue
)
192 throw (css::lang::IllegalArgumentException
,
193 css::container::NoSuchElementException
,
194 css::lang::WrappedTargetException
,
195 css::uno::RuntimeException
)
197 ::comphelper::SequenceAsHashMap
lProps(aValue
);
200 throw css::lang::IllegalArgumentException(
201 ::rtl::OUString::createFromAscii("No properties given to replace part of module."),
202 static_cast< css::container::XNameAccess
* >(this),
206 // SAFE -> ----------------------------------
207 ReadGuard
aReadLock(m_aLock
);
208 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
210 // <- SAFE ----------------------------------
212 // get access to the element
213 // Note: Dont use impl_getConfig() method here. Because it creates a readonly access only, further
214 // it cache it as a member of this module manager instance. If we change some props there ... but dont
215 // flush changes (because an error occured) we will read them later. If we use a different config access
216 // we can close it without a flush ... and our read data wont be affected .-)
217 css::uno::Reference
< css::uno::XInterface
> xCfg
= ::comphelper::ConfigurationHelper::openConfig(
220 ::comphelper::ConfigurationHelper::E_STANDARD
);
221 css::uno::Reference
< css::container::XNameAccess
> xModules (xCfg
, css::uno::UNO_QUERY_THROW
);
222 css::uno::Reference
< css::container::XNameReplace
> xModule
;
224 xModules
->getByName(sName
) >>= xModule
;
227 throw css::uno::RuntimeException(
228 ::rtl::OUString::createFromAscii("Was not able to get write access to the requested module entry inside configuration."),
229 static_cast< css::container::XNameAccess
* >(this));
232 ::comphelper::SequenceAsHashMap::const_iterator pProp
;
233 for ( pProp
= lProps
.begin();
234 pProp
!= lProps
.end() ;
237 const ::rtl::OUString
& sPropName
= pProp
->first
;
238 const css::uno::Any
& aPropValue
= pProp
->second
;
240 // let "NoSuchElementException" out ! We support the same API ...
241 // and without a flush() at the end all changed data before will be ignored !
242 xModule
->replaceByName(sPropName
, aPropValue
);
245 ::comphelper::ConfigurationHelper::flush(xCfg
);
248 /*-----------------------------------------------
250 -----------------------------------------------*/
251 css::uno::Any SAL_CALL
ModuleManager::getByName(const ::rtl::OUString
& sName
)
252 throw(css::container::NoSuchElementException
,
253 css::lang::WrappedTargetException
,
254 css::uno::RuntimeException
)
256 // get access to the element
257 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
258 css::uno::Reference
< css::container::XNameAccess
> xModule
;
259 xCFG
->getByName(sName
) >>= xModule
;
262 throw css::uno::RuntimeException(
263 ::rtl::OUString::createFromAscii("Was not able to get write access to the requested module entry inside configuration."),
264 static_cast< css::container::XNameAccess
* >(this));
267 // convert it to seq< PropertyValue >
268 const css::uno::Sequence
< ::rtl::OUString
> lPropNames
= xModule
->getElementNames();
269 ::comphelper::SequenceAsHashMap lProps
;
270 sal_Int32 c
= lPropNames
.getLength();
273 lProps
[MODULEPROP_IDENTIFIER
] <<= sName
;
276 const ::rtl::OUString
& sPropName
= lPropNames
[i
];
277 lProps
[sPropName
] = xModule
->getByName(sPropName
);
280 return css::uno::makeAny(lProps
.getAsConstPropertyValueList());
283 /*-----------------------------------------------
285 -----------------------------------------------*/
286 css::uno::Sequence
< ::rtl::OUString
> SAL_CALL
ModuleManager::getElementNames()
287 throw(css::uno::RuntimeException
)
289 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
290 return xCFG
->getElementNames();
293 /*-----------------------------------------------
295 -----------------------------------------------*/
296 sal_Bool SAL_CALL
ModuleManager::hasByName(const ::rtl::OUString
& sName
)
297 throw(css::uno::RuntimeException
)
299 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
300 return xCFG
->hasByName(sName
);
303 /*-----------------------------------------------
305 -----------------------------------------------*/
306 css::uno::Type SAL_CALL
ModuleManager::getElementType()
307 throw(css::uno::RuntimeException
)
309 return ::getCppuType((const css::uno::Sequence
< css::beans::PropertyValue
>*)0);
312 /*-----------------------------------------------
314 -----------------------------------------------*/
315 sal_Bool SAL_CALL
ModuleManager::hasElements()
316 throw(css::uno::RuntimeException
)
318 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
319 return xCFG
->hasElements();
322 /*-----------------------------------------------
324 -----------------------------------------------*/
325 css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
ModuleManager::createSubSetEnumerationByQuery(const ::rtl::OUString
&)
326 throw(css::uno::RuntimeException
)
328 return css::uno::Reference
< css::container::XEnumeration
>();
331 /*-----------------------------------------------
333 -----------------------------------------------*/
334 css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
ModuleManager::createSubSetEnumerationByProperties(const css::uno::Sequence
< css::beans::NamedValue
>& lProperties
)
335 throw(css::uno::RuntimeException
)
337 ::comphelper::SequenceAsHashMap
lSearchProps (lProperties
);
338 css::uno::Sequence
< ::rtl::OUString
> lModules
= getElementNames();
339 sal_Int32 c
= lModules
.getLength();
341 ::comphelper::SequenceAsVector
< css::uno::Any
> lResult
;
347 const ::rtl::OUString
& sModule
= lModules
[i
];
348 ::comphelper::SequenceAsHashMap lModuleProps
= getByName(sModule
);
350 if (lModuleProps
.match(lSearchProps
))
351 lResult
.push_back(css::uno::makeAny(lModuleProps
.getAsConstPropertyValueList()));
353 catch(const css::uno::Exception
&)
357 ::comphelper::OAnyEnumeration
* pEnum
= new ::comphelper::OAnyEnumeration(lResult
.getAsConstList());
358 css::uno::Reference
< css::container::XEnumeration
> xEnum(static_cast< css::container::XEnumeration
* >(pEnum
), css::uno::UNO_QUERY_THROW
);
362 /*-----------------------------------------------
364 -----------------------------------------------*/
365 css::uno::Reference
< css::container::XNameAccess
> ModuleManager::implts_getConfig()
366 throw(css::uno::RuntimeException
)
368 // SAFE -> ----------------------------------
369 ReadGuard
aReadLock(m_aLock
);
372 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
374 // <- SAFE ----------------------------------
376 css::uno::Reference
< css::uno::XInterface
> xCfg
;
379 xCfg
= ::comphelper::ConfigurationHelper::openConfig(
382 ::comphelper::ConfigurationHelper::E_READONLY
);
384 catch(const css::uno::RuntimeException
& exRun
)
386 catch(const css::uno::Exception
&)
389 // SAFE -> ----------------------------------
390 WriteGuard
aWriteLock(m_aLock
);
391 m_xCFG
= css::uno::Reference
< css::container::XNameAccess
>(xCfg
, css::uno::UNO_QUERY_THROW
);
393 // <- SAFE ----------------------------------
396 /*-----------------------------------------------
398 -----------------------------------------------*/
399 ::rtl::OUString
ModuleManager::implts_identify(const css::uno::Reference
< css::uno::XInterface
>& xComponent
)
401 // Search for an optional (!) interface XModule first.
402 // Its used to overrule an existing service name. Used e.g. by our database form designer
403 // which uses a writer module internaly.
404 css::uno::Reference
< css::frame::XModule
> xModule(xComponent
, css::uno::UNO_QUERY
);
406 return xModule
->getIdentifier();
408 // detect modules in a generic way ...
409 // comparing service names with configured entries ...
410 css::uno::Reference
< css::lang::XServiceInfo
> xInfo(xComponent
, css::uno::UNO_QUERY
);
412 return ::rtl::OUString();
414 const css::uno::Sequence
< ::rtl::OUString
> lKnownModules
= getElementNames();
415 const ::rtl::OUString
* pKnownModules
= lKnownModules
.getConstArray();
416 sal_Int32 c
= lKnownModules
.getLength();
421 if (xInfo
->supportsService(pKnownModules
[i
]))
422 return pKnownModules
[i
];
425 return ::rtl::OUString();
428 } // namespace framework