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