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 //_______________________________________________
60 static const ::rtl::OUString CFGPATH_FACTORIES
= ::rtl::OUString::createFromAscii("/org.openoffice.Setup/Office/Factories");
61 static const ::rtl::OUString MODULEPROP_IDENTIFIER
= ::rtl::OUString::createFromAscii("ooSetupFactoryModuleIdentifier" );
63 /*-----------------------------------------------
65 -----------------------------------------------*/
66 DEFINE_XINTERFACE_7(ModuleManager
,
68 DIRECT_INTERFACE(css::lang::XTypeProvider
),
69 DIRECT_INTERFACE(css::lang::XServiceInfo
),
70 DIRECT_INTERFACE(css::container::XNameReplace
),
71 DIRECT_INTERFACE(css::container::XNameAccess
),
72 DIRECT_INTERFACE(css::container::XElementAccess
),
73 DIRECT_INTERFACE(css::container::XContainerQuery
),
74 DIRECT_INTERFACE(css::frame::XModuleManager
))
76 /*-----------------------------------------------
78 -----------------------------------------------*/
79 DEFINE_XTYPEPROVIDER_7(ModuleManager
,
80 css::lang::XTypeProvider
,
81 css::lang::XServiceInfo
,
82 css::container::XNameReplace
,
83 css::container::XNameAccess
,
84 css::container::XElementAccess
,
85 css::container::XContainerQuery
,
86 css::frame::XModuleManager
)
88 /*-----------------------------------------------
90 -----------------------------------------------*/
91 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(ModuleManager
,
93 SERVICENAME_MODULEMANAGER
,
94 IMPLEMENTATIONNAME_MODULEMANAGER
)
96 /*-----------------------------------------------
98 -----------------------------------------------*/
103 I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
104 to create a new instance of this class by our own supported service factory.
105 see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
110 /*-----------------------------------------------
112 -----------------------------------------------*/
113 ModuleManager::ModuleManager(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
)
119 /*-----------------------------------------------
121 -----------------------------------------------*/
122 ModuleManager::~ModuleManager()
128 /*-----------------------------------------------
130 -----------------------------------------------*/
131 ::rtl::OUString SAL_CALL
ModuleManager::identify(const css::uno::Reference
< css::uno::XInterface
>& xModule
)
132 throw(css::lang::IllegalArgumentException
,
133 css::frame::UnknownModuleException
,
134 css::uno::RuntimeException
)
137 css::uno::Reference
< css::frame::XFrame
> xFrame (xModule
, css::uno::UNO_QUERY
);
138 css::uno::Reference
< css::awt::XWindow
> xWindow (xModule
, css::uno::UNO_QUERY
);
139 css::uno::Reference
< css::frame::XController
> xController(xModule
, css::uno::UNO_QUERY
);
140 css::uno::Reference
< css::frame::XModel
> xModel (xModule
, css::uno::UNO_QUERY
);
145 (!xController
.is()) &&
149 throw css::lang::IllegalArgumentException(
150 ::rtl::OUString::createFromAscii("Given module is not a frame nor a window, controller or model."),
151 static_cast< ::cppu::OWeakObject
* >(this),
157 xController
= xFrame
->getController();
158 xWindow
= xFrame
->getComponentWindow();
160 if (xController
.is())
161 xModel
= xController
->getModel();
163 // modules are implemented by the deepest component in hierarchy ...
164 // Means: model -> controller -> window
165 // No fallbacks to higher components are allowed !
166 // Note : A frame provides access to module components only ... but it's not a module by himself.
168 ::rtl::OUString sModule
;
170 sModule
= implts_identify(xModel
);
172 if (xController
.is())
173 sModule
= implts_identify(xController
);
176 sModule
= implts_identify(xWindow
);
178 if (sModule
.getLength() < 1)
179 throw css::frame::UnknownModuleException(
180 ::rtl::OUString::createFromAscii("Cant find suitable module for the given component."),
181 static_cast< ::cppu::OWeakObject
* >(this));
186 /*-----------------------------------------------
188 -----------------------------------------------*/
189 void SAL_CALL
ModuleManager::replaceByName(const ::rtl::OUString
& sName
,
190 const css::uno::Any
& aValue
)
191 throw (css::lang::IllegalArgumentException
,
192 css::container::NoSuchElementException
,
193 css::lang::WrappedTargetException
,
194 css::uno::RuntimeException
)
196 ::comphelper::SequenceAsHashMap
lProps(aValue
);
197 if (lProps
.size() < 1)
199 throw css::lang::IllegalArgumentException(
200 ::rtl::OUString::createFromAscii("No properties given to replace part of module."),
201 static_cast< css::container::XNameAccess
* >(this),
205 // SAFE -> ----------------------------------
206 ReadGuard
aReadLock(m_aLock
);
207 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
209 // <- SAFE ----------------------------------
211 // get access to the element
212 // Note: Dont use impl_getConfig() method here. Because it creates a readonly access only, further
213 // it cache it as a member of this module manager instance. If we change some props there ... but dont
214 // flush changes (because an error occured) we will read them later. If we use a different config access
215 // we can close it without a flush ... and our read data wont be affected .-)
216 css::uno::Reference
< css::uno::XInterface
> xCfg
= ::comphelper::ConfigurationHelper::openConfig(
219 ::comphelper::ConfigurationHelper::E_STANDARD
);
220 css::uno::Reference
< css::container::XNameAccess
> xModules (xCfg
, css::uno::UNO_QUERY_THROW
);
221 css::uno::Reference
< css::container::XNameReplace
> xModule
;
223 xModules
->getByName(sName
) >>= xModule
;
226 throw css::uno::RuntimeException(
227 ::rtl::OUString::createFromAscii("Was not able to get write access to the requested module entry inside configuration."),
228 static_cast< css::container::XNameAccess
* >(this));
231 ::comphelper::SequenceAsHashMap::const_iterator pProp
;
232 for ( pProp
= lProps
.begin();
233 pProp
!= lProps
.end() ;
236 const ::rtl::OUString
& sPropName
= pProp
->first
;
237 const css::uno::Any
& aPropValue
= pProp
->second
;
239 // let "NoSuchElementException" out ! We support the same API ...
240 // and without a flush() at the end all changed data before will be ignored !
241 xModule
->replaceByName(sPropName
, aPropValue
);
244 ::comphelper::ConfigurationHelper::flush(xCfg
);
247 /*-----------------------------------------------
249 -----------------------------------------------*/
250 css::uno::Any SAL_CALL
ModuleManager::getByName(const ::rtl::OUString
& sName
)
251 throw(css::container::NoSuchElementException
,
252 css::lang::WrappedTargetException
,
253 css::uno::RuntimeException
)
255 // get access to the element
256 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
257 css::uno::Any aElement
= xCFG
->getByName(sName
);
259 css::uno::Reference
< css::container::XNameAccess
> xModule
;
260 aElement
>>= xModule
;
263 throw css::uno::RuntimeException(
264 ::rtl::OUString::createFromAscii("Was not able to get write access to the requested module entry inside configuration."),
265 static_cast< css::container::XNameAccess
* >(this));
268 // convert it to seq< PropertyValue >
269 const css::uno::Sequence
< ::rtl::OUString
> lPropNames
= xModule
->getElementNames();
270 ::comphelper::SequenceAsHashMap lProps
;
271 sal_Int32 c
= lPropNames
.getLength();
274 lProps
[MODULEPROP_IDENTIFIER
] <<= sName
;
277 const ::rtl::OUString
& sPropName
= lPropNames
[i
];
278 lProps
[sPropName
] = xModule
->getByName(sPropName
);
281 return css::uno::makeAny(lProps
.getAsConstPropertyValueList());
284 /*-----------------------------------------------
286 -----------------------------------------------*/
287 css::uno::Sequence
< ::rtl::OUString
> SAL_CALL
ModuleManager::getElementNames()
288 throw(css::uno::RuntimeException
)
290 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
291 return xCFG
->getElementNames();
294 /*-----------------------------------------------
296 -----------------------------------------------*/
297 sal_Bool SAL_CALL
ModuleManager::hasByName(const ::rtl::OUString
& sName
)
298 throw(css::uno::RuntimeException
)
300 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
301 return xCFG
->hasByName(sName
);
304 /*-----------------------------------------------
306 -----------------------------------------------*/
307 css::uno::Type SAL_CALL
ModuleManager::getElementType()
308 throw(css::uno::RuntimeException
)
310 return ::getCppuType((const css::uno::Sequence
< css::beans::PropertyValue
>*)0);
313 /*-----------------------------------------------
315 -----------------------------------------------*/
316 sal_Bool SAL_CALL
ModuleManager::hasElements()
317 throw(css::uno::RuntimeException
)
319 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
320 return xCFG
->hasElements();
323 /*-----------------------------------------------
325 -----------------------------------------------*/
326 css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
ModuleManager::createSubSetEnumerationByQuery(const ::rtl::OUString
&)
327 throw(css::uno::RuntimeException
)
329 return css::uno::Reference
< css::container::XEnumeration
>();
332 /*-----------------------------------------------
334 -----------------------------------------------*/
335 css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
ModuleManager::createSubSetEnumerationByProperties(const css::uno::Sequence
< css::beans::NamedValue
>& lProperties
)
336 throw(css::uno::RuntimeException
)
338 ::comphelper::SequenceAsHashMap
lSearchProps (lProperties
);
339 css::uno::Sequence
< ::rtl::OUString
> lModules
= getElementNames();
340 sal_Int32 c
= lModules
.getLength();
342 ::comphelper::SequenceAsVector
< css::uno::Any
> lResult
;
348 const ::rtl::OUString
& sModule
= lModules
[i
];
349 ::comphelper::SequenceAsHashMap lModuleProps
= getByName(sModule
);
351 if (lModuleProps
.match(lSearchProps
))
352 lResult
.push_back(css::uno::makeAny(lModuleProps
.getAsConstPropertyValueList()));
354 catch(const css::uno::Exception
&)
358 ::comphelper::OAnyEnumeration
* pEnum
= new ::comphelper::OAnyEnumeration(lResult
.getAsConstList());
359 css::uno::Reference
< css::container::XEnumeration
> xEnum(static_cast< css::container::XEnumeration
* >(pEnum
), css::uno::UNO_QUERY_THROW
);
363 /*-----------------------------------------------
365 -----------------------------------------------*/
366 css::uno::Reference
< css::container::XNameAccess
> ModuleManager::implts_getConfig()
367 throw(css::uno::RuntimeException
)
369 // SAFE -> ----------------------------------
370 ReadGuard
aReadLock(m_aLock
);
373 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
375 // <- SAFE ----------------------------------
377 css::uno::Reference
< css::uno::XInterface
> xCfg
;
380 xCfg
= ::comphelper::ConfigurationHelper::openConfig(
383 ::comphelper::ConfigurationHelper::E_READONLY
);
385 catch(const css::uno::RuntimeException
& exRun
)
387 catch(const css::uno::Exception
&)
390 // SAFE -> ----------------------------------
391 WriteGuard
aWriteLock(m_aLock
);
392 m_xCFG
= css::uno::Reference
< css::container::XNameAccess
>(xCfg
, css::uno::UNO_QUERY_THROW
);
394 // <- SAFE ----------------------------------
397 /*-----------------------------------------------
399 -----------------------------------------------*/
400 ::rtl::OUString
ModuleManager::implts_identify(const css::uno::Reference
< css::uno::XInterface
>& xComponent
)
402 // Search for an optional (!) interface XModule first.
403 // Its used to overrule an existing service name. Used e.g. by our database form designer
404 // which uses a writer module internaly.
405 css::uno::Reference
< css::frame::XModule
> xModule(xComponent
, css::uno::UNO_QUERY
);
407 return xModule
->getIdentifier();
409 // detect modules in a generic way ...
410 // comparing service names with configured entries ...
411 css::uno::Reference
< css::lang::XServiceInfo
> xInfo(xComponent
, css::uno::UNO_QUERY
);
413 return ::rtl::OUString();
415 const css::uno::Sequence
< ::rtl::OUString
> lKnownModules
= getElementNames();
416 const ::rtl::OUString
* pKnownModules
= lKnownModules
.getConstArray();
417 sal_Int32 c
= lKnownModules
.getLength();
422 if (xInfo
->supportsService(pKnownModules
[i
]))
423 return pKnownModules
[i
];
426 return ::rtl::OUString();
429 } // namespace framework