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 212 : ConfigurationControllerResourceManager::ConfigurationControllerResourceManager (
38 : const ::boost::shared_ptr<ResourceFactoryManager>& rpResourceFactoryContainer,
39 : const ::boost::shared_ptr<ConfigurationControllerBroadcaster>& rpBroadcaster)
40 : : maResourceMap(ResourceComparator()),
41 : mpResourceFactoryContainer(rpResourceFactoryContainer),
42 212 : mpBroadcaster(rpBroadcaster)
43 : {
44 212 : }
45 :
46 212 : ConfigurationControllerResourceManager::~ConfigurationControllerResourceManager (void)
47 : {
48 212 : }
49 :
50 : ConfigurationControllerResourceManager::ResourceDescriptor
51 15666 : ConfigurationControllerResourceManager::GetResource (
52 : const Reference<XResourceId>& rxResourceId)
53 : {
54 15666 : ::osl::MutexGuard aGuard (maMutex);
55 15666 : ResourceMap::const_iterator iResource (maResourceMap.find(rxResourceId));
56 15666 : if (iResource != maResourceMap.end())
57 12390 : return iResource->second;
58 : else
59 3276 : return ResourceDescriptor();
60 : }
61 :
62 584 : void ConfigurationControllerResourceManager::ActivateResources (
63 : const ::std::vector<Reference<XResourceId> >& rResources,
64 : const Reference<XConfiguration>& rxConfiguration)
65 : {
66 584 : ::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 584 : this, _1, rxConfiguration));
75 584 : }
76 :
77 796 : void ConfigurationControllerResourceManager::DeactivateResources (
78 : const ::std::vector<Reference<XResourceId> >& rResources,
79 : const Reference<XConfiguration>& rxConfiguration)
80 : {
81 796 : ::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 796 : this, _1, rxConfiguration));
90 796 : }
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 1372 : void ConfigurationControllerResourceManager::ActivateResource (
101 : const Reference<XResourceId>& rxResourceId,
102 : const Reference<XConfiguration>& rxConfiguration)
103 : {
104 1372 : if ( ! rxResourceId.is())
105 : {
106 : OSL_ASSERT(rxResourceId.is());
107 220 : 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 1372 : const OUString sResourceURL (rxResourceId->getResourceURL());
115 2524 : Reference<XResourceFactory> xFactory (mpResourceFactoryContainer->GetFactory(sResourceURL));
116 1372 : if ( ! xFactory.is())
117 : {
118 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": no factory found for " <<
119 : OUStringToOString(sResourceURL, RTL_TEXTENCODING_UTF8).getStr());
120 220 : return;
121 : }
122 :
123 : try
124 : {
125 : // 2. Create the resource.
126 1152 : Reference<XResource> xResource;
127 : try
128 : {
129 1152 : 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 1152 : if (xResource.is())
143 : {
144 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": successfully created");
145 : // 3. Add resource to URL->Object map.
146 940 : AddResource(xResource, xFactory);
147 :
148 : // 4. Add resource id to current configuration.
149 940 : rxConfiguration->addResource(rxResourceId);
150 :
151 : // 5. Notify the new resource to listeners of the ConfigurationController.
152 : mpBroadcaster->NotifyListeners(
153 : FrameworkHelper::msResourceActivationEvent,
154 : rxResourceId,
155 940 : xResource);
156 : }
157 : else
158 : {
159 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": resource creation failed");
160 1152 : }
161 : }
162 0 : catch (RuntimeException&)
163 : {
164 : DBG_UNHANDLED_EXCEPTION();
165 1152 : }
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 940 : void ConfigurationControllerResourceManager::DeactivateResource (
177 : const Reference<XResourceId>& rxResourceId,
178 : const Reference<XConfiguration>& rxConfiguration)
179 : {
180 940 : if ( ! rxResourceId.is())
181 940 : 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 940 : ResourceDescriptor aDescriptor (RemoveResource(rxResourceId));
190 :
191 940 : 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 940 : aDescriptor.mxResource);
198 :
199 : // 3. Remove resource id from current configuration.
200 940 : rxConfiguration->removeResource(rxResourceId);
201 :
202 : // 4. Release the resource.
203 : try
204 : {
205 940 : 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 940 : }
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 940 : 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 940 : void ConfigurationControllerResourceManager::AddResource (
247 : const Reference<XResource>& rxResource,
248 : const Reference<XResourceFactory>& rxFactory)
249 : {
250 940 : if ( ! rxResource.is())
251 : {
252 : OSL_ASSERT(rxResource.is());
253 940 : return;
254 : }
255 :
256 : // Add the resource to the resource container.
257 940 : ResourceDescriptor aDescriptor;
258 940 : aDescriptor.mxResource = rxResource;
259 940 : aDescriptor.mxResourceFactory = rxFactory;
260 940 : 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 940 : ConfigurationControllerResourceManager::RemoveResource (
272 : const Reference<XResourceId>& rxResourceId)
273 : {
274 940 : ResourceDescriptor aDescriptor;
275 :
276 940 : ResourceMap::iterator iResource (maResourceMap.find(rxResourceId));
277 940 : 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 940 : aDescriptor = iResource->second;
288 940 : maResourceMap.erase(rxResourceId);
289 : }
290 :
291 940 : return aDescriptor;
292 : }
293 :
294 : //===== ConfigurationControllerResourceManager::ResourceComparator ============
295 :
296 48804 : bool ConfigurationControllerResourceManager::ResourceComparator::operator() (
297 : const Reference<XResourceId>& rxId1,
298 : const Reference<XResourceId>& rxId2) const
299 : {
300 48804 : if (rxId1.is() && rxId2.is())
301 48804 : return rxId1->compareTo(rxId2)<0;
302 0 : else if (rxId1.is())
303 0 : return true;
304 : else
305 0 : return false;
306 : }
307 :
308 114 : } } // end of namespace sd::framework
309 :
310 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|