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