Line data Source code
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 :
20 :
21 : #include "ConfigurationControllerResourceManager.hxx"
22 : #include "ConfigurationControllerBroadcaster.hxx"
23 : #include "ResourceFactoryManager.hxx"
24 : #include "framework/FrameworkHelper.hxx"
25 : #include <com/sun/star/lang/DisposedException.hpp>
26 : #include <tools/diagnose_ex.h>
27 : #include <algorithm>
28 : #include <boost/bind.hpp>
29 :
30 : using namespace ::com::sun::star;
31 : using namespace ::com::sun::star::uno;
32 : using namespace ::com::sun::star::drawing::framework;
33 :
34 : namespace sd { namespace framework {
35 :
36 : //===== ConfigurationControllerResourceManager ================================
37 :
38 0 : ConfigurationControllerResourceManager::ConfigurationControllerResourceManager (
39 : const ::boost::shared_ptr<ResourceFactoryManager>& rpResourceFactoryContainer,
40 : const ::boost::shared_ptr<ConfigurationControllerBroadcaster>& rpBroadcaster)
41 : : maResourceMap(ResourceComparator()),
42 : mpResourceFactoryContainer(rpResourceFactoryContainer),
43 0 : mpBroadcaster(rpBroadcaster)
44 : {
45 0 : }
46 :
47 :
48 :
49 :
50 0 : ConfigurationControllerResourceManager::~ConfigurationControllerResourceManager (void)
51 : {
52 0 : }
53 :
54 :
55 :
56 :
57 : ConfigurationControllerResourceManager::ResourceDescriptor
58 0 : ConfigurationControllerResourceManager::GetResource (
59 : const Reference<XResourceId>& rxResourceId)
60 : {
61 0 : ::osl::MutexGuard aGuard (maMutex);
62 0 : ResourceMap::const_iterator iResource (maResourceMap.find(rxResourceId));
63 0 : if (iResource != maResourceMap.end())
64 0 : return iResource->second;
65 : else
66 0 : return ResourceDescriptor();
67 : }
68 :
69 :
70 :
71 :
72 0 : void ConfigurationControllerResourceManager::ActivateResources (
73 : const ::std::vector<Reference<XResourceId> >& rResources,
74 : const Reference<XConfiguration>& rxConfiguration)
75 : {
76 0 : ::osl::MutexGuard aGuard (maMutex);
77 : // Iterate in normal order over the resources that are to be
78 : // activated so that resources on which others depend are activated
79 : // beforet the depending resources are activated.
80 : ::std::for_each(
81 : rResources.begin(),
82 : rResources.end(),
83 : ::boost::bind(&ConfigurationControllerResourceManager::ActivateResource,
84 0 : this, _1, rxConfiguration));
85 0 : }
86 :
87 :
88 :
89 :
90 0 : void ConfigurationControllerResourceManager::DeactivateResources (
91 : const ::std::vector<Reference<XResourceId> >& rResources,
92 : const Reference<XConfiguration>& rxConfiguration)
93 : {
94 0 : ::osl::MutexGuard aGuard (maMutex);
95 : // Iterate in reverese order over the resources that are to be
96 : // deactivated so that resources on which others depend are deactivated
97 : // only when the depending resources have already been deactivated.
98 : ::std::for_each(
99 : rResources.rbegin(),
100 : rResources.rend(),
101 : ::boost::bind(&ConfigurationControllerResourceManager::DeactivateResource,
102 0 : this, _1, rxConfiguration));
103 0 : }
104 :
105 :
106 :
107 :
108 : /* In this method we do following steps.
109 : 1. Get the factory with which the resource will be created.
110 : 2. Create the resource.
111 : 3. Add the resource to the URL->Object map of the configuration
112 : controller.
113 : 4. Add the resource id to the current configuration.
114 : 5. Notify listeners.
115 : */
116 0 : void ConfigurationControllerResourceManager::ActivateResource (
117 : const Reference<XResourceId>& rxResourceId,
118 : const Reference<XConfiguration>& rxConfiguration)
119 : {
120 0 : if ( ! rxResourceId.is())
121 : {
122 : OSL_ASSERT(rxResourceId.is());
123 0 : return;
124 : }
125 :
126 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": activating resource " << OUStringToOString(
127 : FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
128 :
129 : // 1. Get the factory.
130 0 : const OUString sResourceURL (rxResourceId->getResourceURL());
131 0 : Reference<XResourceFactory> xFactory (mpResourceFactoryContainer->GetFactory(sResourceURL));
132 0 : if ( ! xFactory.is())
133 : {
134 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": no factory found for " <<
135 : OUStringToOString(sResourceURL, RTL_TEXTENCODING_UTF8).getStr());
136 0 : return;
137 : }
138 :
139 : try
140 : {
141 : // 2. Create the resource.
142 0 : Reference<XResource> xResource;
143 : try
144 : {
145 0 : xResource = xFactory->createResource(rxResourceId);
146 : }
147 0 : catch (lang::DisposedException&)
148 : {
149 : // The factory is disposed and can be removed from the list
150 : // of registered factories.
151 0 : mpResourceFactoryContainer->RemoveFactoryForReference(xFactory);
152 : }
153 0 : catch (Exception& e)
154 : {
155 : (void)e;
156 : }
157 :
158 0 : if (xResource.is())
159 : {
160 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": successfully created");
161 : // 3. Add resource to URL->Object map.
162 0 : AddResource(xResource, xFactory);
163 :
164 : // 4. Add resource id to current configuration.
165 0 : rxConfiguration->addResource(rxResourceId);
166 :
167 : // 5. Notify the new resource to listeners of the ConfigurationController.
168 : mpBroadcaster->NotifyListeners(
169 : FrameworkHelper::msResourceActivationEvent,
170 : rxResourceId,
171 0 : xResource);
172 : }
173 : else
174 : {
175 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": resource creation failed");
176 0 : }
177 : }
178 0 : catch (RuntimeException&)
179 : {
180 : DBG_UNHANDLED_EXCEPTION();
181 0 : }
182 : }
183 :
184 :
185 :
186 :
187 : /* In this method we do following steps.
188 : 1. Remove the resource from the URL->Object map of the configuration
189 : controller.
190 : 2. Notify listeners that deactivation has started.
191 : 3. Remove the resource id from the current configuration.
192 : 4. Release the resource.
193 : 5. Notify listeners about that deactivation is completed.
194 : */
195 0 : void ConfigurationControllerResourceManager::DeactivateResource (
196 : const Reference<XResourceId>& rxResourceId,
197 : const Reference<XConfiguration>& rxConfiguration)
198 : {
199 0 : if ( ! rxResourceId.is())
200 0 : return;
201 :
202 : #if OSL_DEBUG_LEVEL >= 1
203 : bool bSuccess (false);
204 : #endif
205 : try
206 : {
207 : // 1. Remove resource from URL->Object map.
208 0 : ResourceDescriptor aDescriptor (RemoveResource(rxResourceId));
209 :
210 0 : if (aDescriptor.mxResource.is() && aDescriptor.mxResourceFactory.is())
211 : {
212 : // 2. Notifiy listeners that the resource is being deactivated.
213 : mpBroadcaster->NotifyListeners(
214 : FrameworkHelper::msResourceDeactivationEvent,
215 : rxResourceId,
216 0 : aDescriptor.mxResource);
217 :
218 : // 3. Remove resource id from current configuration.
219 0 : rxConfiguration->removeResource(rxResourceId);
220 :
221 : // 4. Release the resource.
222 : try
223 : {
224 0 : aDescriptor.mxResourceFactory->releaseResource(aDescriptor.mxResource);
225 : }
226 0 : catch (const lang::DisposedException& rException)
227 : {
228 0 : if ( ! rException.Context.is()
229 0 : || rException.Context == aDescriptor.mxResourceFactory)
230 : {
231 : // The factory is disposed and can be removed from the
232 : // list of registered factories.
233 : mpResourceFactoryContainer->RemoveFactoryForReference(
234 0 : aDescriptor.mxResourceFactory);
235 : }
236 : }
237 :
238 : #if OSL_DEBUG_LEVEL >= 1
239 : bSuccess = true;
240 : #endif
241 0 : }
242 : }
243 0 : catch (RuntimeException&)
244 : {
245 : DBG_UNHANDLED_EXCEPTION();
246 : }
247 :
248 : // 5. Notifiy listeners that the resource is being deactivated.
249 : mpBroadcaster->NotifyListeners(
250 : FrameworkHelper::msResourceDeactivationEndEvent,
251 : rxResourceId,
252 0 : NULL);
253 :
254 : #if OSL_DEBUG_LEVEL >= 1
255 : if (bSuccess)
256 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": successfully deactivated " << OUStringToOString(
257 : FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
258 : else
259 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": activating resource " << OUStringToOString(
260 : FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr()
261 : << " failed");
262 : #endif
263 : }
264 :
265 :
266 :
267 :
268 0 : void ConfigurationControllerResourceManager::AddResource (
269 : const Reference<XResource>& rxResource,
270 : const Reference<XResourceFactory>& rxFactory)
271 : {
272 0 : if ( ! rxResource.is())
273 : {
274 : OSL_ASSERT(rxResource.is());
275 0 : return;
276 : }
277 :
278 : // Add the resource to the resource container.
279 0 : ResourceDescriptor aDescriptor;
280 0 : aDescriptor.mxResource = rxResource;
281 0 : aDescriptor.mxResourceFactory = rxFactory;
282 0 : maResourceMap[rxResource->getResourceId()] = aDescriptor;
283 :
284 : #if OSL_DEBUG_LEVEL >= 2
285 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ConfigurationControllerResourceManager::AddResource(): added " <<
286 : OUStringToOString(
287 : FrameworkHelper::ResourceIdToString(rxResource->getResourceId()),
288 : RTL_TEXTENCODING_UTF8).getStr() << " -> " << rxResource.get());
289 : #endif
290 : }
291 :
292 :
293 :
294 :
295 : ConfigurationControllerResourceManager::ResourceDescriptor
296 0 : ConfigurationControllerResourceManager::RemoveResource (
297 : const Reference<XResourceId>& rxResourceId)
298 : {
299 0 : ResourceDescriptor aDescriptor;
300 :
301 0 : ResourceMap::iterator iResource (maResourceMap.find(rxResourceId));
302 0 : if (iResource != maResourceMap.end())
303 : {
304 : #if OSL_DEBUG_LEVEL >= 2
305 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ConfigurationControllerResourceManager::RemoveResource(): removing " <<
306 : OUStringToOString(
307 : FrameworkHelper::ResourceIdToString(rxResourceId),
308 : RTL_TEXTENCODING_UTF8).getStr() <<
309 : " -> " << &iResource);
310 : #endif
311 :
312 0 : aDescriptor = iResource->second;
313 0 : maResourceMap.erase(rxResourceId);
314 : }
315 :
316 0 : return aDescriptor;
317 : }
318 :
319 :
320 :
321 :
322 : //===== ConfigurationControllerResourceManager::ResourceComparator ============
323 :
324 0 : bool ConfigurationControllerResourceManager::ResourceComparator::operator() (
325 : const Reference<XResourceId>& rxId1,
326 : const Reference<XResourceId>& rxId2) const
327 : {
328 0 : if (rxId1.is() && rxId2.is())
329 0 : return rxId1->compareTo(rxId2)<0;
330 0 : else if (rxId1.is())
331 0 : return true;
332 : else
333 0 : return false;
334 : }
335 :
336 :
337 :
338 :
339 0 : } } // end of namespace sd::framework
340 :
341 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|