LibreOffice Module sd (master) 1
ConfigurationControllerResourceManager.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
24#include <com/sun/star/lang/DisposedException.hpp>
25#include <com/sun/star/drawing/framework/XConfiguration.hpp>
26#include <com/sun/star/drawing/framework/XResourceFactory.hpp>
28#include <sal/log.hxx>
29#include <algorithm>
30#include <utility>
31
32using namespace ::com::sun::star;
33using namespace ::com::sun::star::uno;
35
36namespace sd::framework {
37
38//===== ConfigurationControllerResourceManager ================================
39
41 std::shared_ptr<ResourceFactoryManager> pResourceFactoryContainer,
42 std::shared_ptr<ConfigurationControllerBroadcaster> pBroadcaster)
43 : maResourceMap(ResourceComparator()),
44 mpResourceFactoryContainer(std::move(pResourceFactoryContainer)),
45 mpBroadcaster(std::move(pBroadcaster))
46{
47}
48
50{
51}
52
55 const Reference<XResourceId>& rxResourceId)
56{
57 ::osl::MutexGuard aGuard (maMutex);
58 ResourceMap::const_iterator iResource (maResourceMap.find(rxResourceId));
59 if (iResource != maResourceMap.end())
60 return iResource->second;
61 else
62 return ResourceDescriptor();
63}
64
66 const ::std::vector<Reference<XResourceId> >& rResources,
67 const Reference<XConfiguration>& rxConfiguration)
68{
69 ::osl::MutexGuard aGuard (maMutex);
70 // Iterate in normal order over the resources that are to be
71 // activated so that resources on which others depend are activated
72 // before the depending resources are activated.
73 for (const Reference<XResourceId>& xResource : rResources)
74 ActivateResource(xResource, rxConfiguration);
75}
76
78 const ::std::vector<Reference<XResourceId> >& rResources,
79 const Reference<XConfiguration>& rxConfiguration)
80{
81 ::osl::MutexGuard aGuard (maMutex);
82 // Iterate in reverse order over the resources that are to be
83 // deactivated so that resources on which others depend are deactivated
84 // only when the depending resources have already been deactivated.
85 ::std::for_each(
86 rResources.rbegin(),
87 rResources.rend(),
88 [&] (Reference<XResourceId> const& xResource) {
89 return DeactivateResource(xResource, rxConfiguration);
90 } );
91}
92
93/* In this method we do following steps.
94 1. Get the factory with which the resource will be created.
95 2. Create the resource.
96 3. Add the resource to the URL->Object map of the configuration
97 controller.
98 4. Add the resource id to the current configuration.
99 5. Notify listeners.
100*/
102 const Reference<XResourceId>& rxResourceId,
103 const Reference<XConfiguration>& rxConfiguration)
104{
105 if ( ! rxResourceId.is())
106 {
107 OSL_ASSERT(rxResourceId.is());
108 return;
109 }
110
111 SAL_INFO("sd.fwk", __func__ << ": activating resource " <<
113
114 // 1. Get the factory.
115 const OUString sResourceURL (rxResourceId->getResourceURL());
116 Reference<XResourceFactory> xFactory (mpResourceFactoryContainer->GetFactory(sResourceURL));
117 if ( ! xFactory.is())
118 {
119 SAL_INFO("sd.fwk", __func__ << ": no factory found for " << sResourceURL);
120 return;
121 }
122
123 try
124 {
125 // 2. Create the resource.
126 Reference<XResource> xResource;
127 try
128 {
129 xResource = xFactory->createResource(rxResourceId);
130 }
131 catch (lang::DisposedException&)
132 {
133 // The factory is disposed and can be removed from the list
134 // of registered factories.
135 mpResourceFactoryContainer->RemoveFactoryForReference(xFactory);
136 }
137 catch (Exception&) {}
138
139 if (xResource.is())
140 {
141 SAL_INFO("sd.fwk", __func__ << ": successfully created");
142 // 3. Add resource to URL->Object map.
143 AddResource(xResource, xFactory);
144
145 // 4. Add resource id to current configuration.
146 rxConfiguration->addResource(rxResourceId);
147
148 // 5. Notify the new resource to listeners of the ConfigurationController.
149 mpBroadcaster->NotifyListeners(
151 rxResourceId,
152 xResource);
153 }
154 else
155 {
156 SAL_INFO("sd.fwk", __func__ << ": resource creation failed");
157 }
158 }
159 catch (RuntimeException&)
160 {
162 }
163}
164
165/* In this method we do following steps.
166 1. Remove the resource from the URL->Object map of the configuration
167 controller.
168 2. Notify listeners that deactivation has started.
169 3. Remove the resource id from the current configuration.
170 4. Release the resource.
171 5. Notify listeners about that deactivation is completed.
172*/
174 const Reference<XResourceId>& rxResourceId,
175 const Reference<XConfiguration>& rxConfiguration)
176{
177 if ( ! rxResourceId.is())
178 return;
179
180#if OSL_DEBUG_LEVEL >= 1
181 bool bSuccess (false);
182#endif
183 try
184 {
185 // 1. Remove resource from URL->Object map.
186 ResourceDescriptor aDescriptor (RemoveResource(rxResourceId));
187
188 if (aDescriptor.mxResource.is() && aDescriptor.mxResourceFactory.is())
189 {
190 // 2. Notify listeners that the resource is being deactivated.
191 mpBroadcaster->NotifyListeners(
193 rxResourceId,
194 aDescriptor.mxResource);
195
196 // 3. Remove resource id from current configuration.
197 rxConfiguration->removeResource(rxResourceId);
198
199 // 4. Release the resource.
200 try
201 {
202 aDescriptor.mxResourceFactory->releaseResource(aDescriptor.mxResource);
203 }
204 catch (const lang::DisposedException& rException)
205 {
206 if ( ! rException.Context.is()
207 || rException.Context == aDescriptor.mxResourceFactory)
208 {
209 // The factory is disposed and can be removed from the
210 // list of registered factories.
211 mpResourceFactoryContainer->RemoveFactoryForReference(
212 aDescriptor.mxResourceFactory);
213 }
214 }
215
216#if OSL_DEBUG_LEVEL >= 1
217 bSuccess = true;
218#endif
219 }
220 }
221 catch (RuntimeException&)
222 {
224 }
225
226 // 5. Notify listeners that the resource is being deactivated.
227 mpBroadcaster->NotifyListeners(
229 rxResourceId,
230 nullptr);
231
232#if OSL_DEBUG_LEVEL >= 1
233 if (bSuccess)
234 SAL_INFO("sd.fwk", __func__ << ": successfully deactivated " <<
236 else
237 SAL_INFO("sd.fwk", __func__ << ": activating resource " <<
239 << " failed");
240#endif
241}
242
244 const Reference<XResource>& rxResource,
245 const Reference<XResourceFactory>& rxFactory)
246{
247 if ( ! rxResource.is())
248 {
249 OSL_ASSERT(rxResource.is());
250 return;
251 }
252
253 // Add the resource to the resource container.
254 ResourceDescriptor aDescriptor;
255 aDescriptor.mxResource = rxResource;
256 aDescriptor.mxResourceFactory = rxFactory;
257 maResourceMap[rxResource->getResourceId()] = aDescriptor;
258
259#if OSL_DEBUG_LEVEL >= 2
260 SAL_INFO("sd.fwk", __func__ << ": ConfigurationControllerResourceManager::AddResource(): added " <<
261 FrameworkHelper::ResourceIdToString(rxResource->getResourceId()) <<
262 " -> " << rxResource.get());
263#endif
264}
265
268 const Reference<XResourceId>& rxResourceId)
269{
270 ResourceDescriptor aDescriptor;
271
272 ResourceMap::iterator iResource (maResourceMap.find(rxResourceId));
273 if (iResource != maResourceMap.end())
274 {
275#if OSL_DEBUG_LEVEL >= 2
276 SAL_INFO("sd.fwk", __func__ << ": ConfigurationControllerResourceManager::RemoveResource(): removing " <<
278 " -> " << &iResource);
279#endif
280
281 aDescriptor = iResource->second;
282 maResourceMap.erase(rxResourceId);
283 }
284
285 return aDescriptor;
286}
287
288//===== ConfigurationControllerResourceManager::ResourceComparator ============
289
291 const Reference<XResourceId>& rxId1,
292 const Reference<XResourceId>& rxId2) const
293{
294 if (rxId1.is() && rxId2.is())
295 return rxId1->compareTo(rxId2)<0;
296 else if (rxId1.is())
297 return true;
298 else
299 return false;
300}
301
302} // end of namespace sd::framework
303
304/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator()(const css::uno::Reference< css::drawing::framework::XResourceId > &rxId1, const css::uno::Reference< css::drawing::framework::XResourceId > &rxId2) const
For every active resource both the resource itself as well as its creating factory are remembered,...
css::uno::Reference< css::drawing::framework::XResourceFactory > mxResourceFactory
std::shared_ptr< ConfigurationControllerBroadcaster > mpBroadcaster
This broadcaster is used to notify the activation and deactivation of resources.
void ActivateResources(const ::std::vector< css::uno::Reference< css::drawing::framework::XResourceId > > &rResources, const css::uno::Reference< css::drawing::framework::XConfiguration > &rxConfiguration)
Activate all the resources that are specified by resource ids in rResources.
void DeactivateResources(const ::std::vector< css::uno::Reference< css::drawing::framework::XResourceId > > &rResources, const css::uno::Reference< css::drawing::framework::XConfiguration > &rxConfiguration)
Deactivate all the resources that are specified by resource ids in rResources.
void DeactivateResource(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId, const css::uno::Reference< css::drawing::framework::XConfiguration > &rxConfiguration)
ResourceDescriptor GetResource(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId)
Return the descriptor for the specified resource.
void ActivateResource(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId, const css::uno::Reference< css::drawing::framework::XConfiguration > &rxConfiguration)
void AddResource(const css::uno::Reference< css::drawing::framework::XResource > &rxResource, const css::uno::Reference< css::drawing::framework::XResourceFactory > &rxFactory)
ConfigurationControllerResourceManager(std::shared_ptr< ResourceFactoryManager > pResourceFactoryContainer, std::shared_ptr< ConfigurationControllerBroadcaster > pBroadcaster)
A new ResourceManager object is created with the resource factory container for creating resources an...
ResourceDescriptor RemoveResource(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId)
static constexpr OUStringLiteral msResourceDeactivationEvent
static constexpr OUStringLiteral msResourceDeactivationEndEvent
static OUString ResourceIdToString(const css::uno::Reference< css::drawing::framework::XResourceId > &rxResourceId)
Return a string representation of the given XResourceId object.
static constexpr OUStringLiteral msResourceActivationEvent
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XSingleServiceFactory > xFactory
#define SAL_INFO(area, stream)
@ Exception