1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "precompiled_sd.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include "ConfigurationControllerResourceManager.hxx" 31*cdf0e10cSrcweir #include "ConfigurationControllerBroadcaster.hxx" 32*cdf0e10cSrcweir #include "ResourceFactoryManager.hxx" 33*cdf0e10cSrcweir #include "framework/FrameworkHelper.hxx" 34*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp> 35*cdf0e10cSrcweir #include <tools/diagnose_ex.h> 36*cdf0e10cSrcweir #include <algorithm> 37*cdf0e10cSrcweir #include <boost/bind.hpp> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir using namespace ::com::sun::star; 40*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 41*cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework; 42*cdf0e10cSrcweir using ::rtl::OUString; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir #undef VERBOSE 45*cdf0e10cSrcweir //#define VERBOSE 1 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir namespace sd { namespace framework { 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir //===== ConfigurationControllerResourceManager ================================ 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir ConfigurationControllerResourceManager::ConfigurationControllerResourceManager ( 52*cdf0e10cSrcweir const ::boost::shared_ptr<ResourceFactoryManager>& rpResourceFactoryContainer, 53*cdf0e10cSrcweir const ::boost::shared_ptr<ConfigurationControllerBroadcaster>& rpBroadcaster) 54*cdf0e10cSrcweir : maResourceMap(ResourceComparator()), 55*cdf0e10cSrcweir mpResourceFactoryContainer(rpResourceFactoryContainer), 56*cdf0e10cSrcweir mpBroadcaster(rpBroadcaster) 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir } 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir ConfigurationControllerResourceManager::~ConfigurationControllerResourceManager (void) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir ConfigurationControllerResourceManager::ResourceDescriptor 71*cdf0e10cSrcweir ConfigurationControllerResourceManager::GetResource ( 72*cdf0e10cSrcweir const Reference<XResourceId>& rxResourceId) 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir ::osl::MutexGuard aGuard (maMutex); 75*cdf0e10cSrcweir ResourceMap::const_iterator iResource (maResourceMap.find(rxResourceId)); 76*cdf0e10cSrcweir if (iResource != maResourceMap.end()) 77*cdf0e10cSrcweir return iResource->second; 78*cdf0e10cSrcweir else 79*cdf0e10cSrcweir return ResourceDescriptor(); 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir void ConfigurationControllerResourceManager::ActivateResources ( 86*cdf0e10cSrcweir const ::std::vector<Reference<XResourceId> >& rResources, 87*cdf0e10cSrcweir const Reference<XConfiguration>& rxConfiguration) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir ::osl::MutexGuard aGuard (maMutex); 90*cdf0e10cSrcweir // Iterate in normal order over the resources that are to be 91*cdf0e10cSrcweir // activated so that resources on which others depend are activated 92*cdf0e10cSrcweir // beforet the depending resources are activated. 93*cdf0e10cSrcweir ::std::for_each( 94*cdf0e10cSrcweir rResources.begin(), 95*cdf0e10cSrcweir rResources.end(), 96*cdf0e10cSrcweir ::boost::bind(&ConfigurationControllerResourceManager::ActivateResource, 97*cdf0e10cSrcweir this, _1, rxConfiguration)); 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir void ConfigurationControllerResourceManager::DeactivateResources ( 104*cdf0e10cSrcweir const ::std::vector<Reference<XResourceId> >& rResources, 105*cdf0e10cSrcweir const Reference<XConfiguration>& rxConfiguration) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir ::osl::MutexGuard aGuard (maMutex); 108*cdf0e10cSrcweir // Iterate in reverese order over the resources that are to be 109*cdf0e10cSrcweir // deactivated so that resources on which others depend are deactivated 110*cdf0e10cSrcweir // only when the depending resources have already been deactivated. 111*cdf0e10cSrcweir ::std::for_each( 112*cdf0e10cSrcweir rResources.rbegin(), 113*cdf0e10cSrcweir rResources.rend(), 114*cdf0e10cSrcweir ::boost::bind(&ConfigurationControllerResourceManager::DeactivateResource, 115*cdf0e10cSrcweir this, _1, rxConfiguration)); 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir /* In this method we do following steps. 122*cdf0e10cSrcweir 1. Get the factory with which the resource will be created. 123*cdf0e10cSrcweir 2. Create the resource. 124*cdf0e10cSrcweir 3. Add the resource to the URL->Object map of the configuration 125*cdf0e10cSrcweir controller. 126*cdf0e10cSrcweir 4. Add the resource id to the current configuration. 127*cdf0e10cSrcweir 5. Notify listeners. 128*cdf0e10cSrcweir */ 129*cdf0e10cSrcweir void ConfigurationControllerResourceManager::ActivateResource ( 130*cdf0e10cSrcweir const Reference<XResourceId>& rxResourceId, 131*cdf0e10cSrcweir const Reference<XConfiguration>& rxConfiguration) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir if ( ! rxResourceId.is()) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir OSL_ASSERT(rxResourceId.is()); 136*cdf0e10cSrcweir return; 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>=1 140*cdf0e10cSrcweir OSL_TRACE("activating resource %s\n", OUStringToOString( 141*cdf0e10cSrcweir FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr()); 142*cdf0e10cSrcweir #endif 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir // 1. Get the factory. 145*cdf0e10cSrcweir const OUString sResourceURL (rxResourceId->getResourceURL()); 146*cdf0e10cSrcweir Reference<XResourceFactory> xFactory (mpResourceFactoryContainer->GetFactory(sResourceURL)); 147*cdf0e10cSrcweir if ( ! xFactory.is()) 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>=1 150*cdf0e10cSrcweir OSL_TRACE(" no factory found fo %s\n", 151*cdf0e10cSrcweir OUStringToOString(sResourceURL, RTL_TEXTENCODING_UTF8).getStr()); 152*cdf0e10cSrcweir #endif 153*cdf0e10cSrcweir return; 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir try 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir // 2. Create the resource. 159*cdf0e10cSrcweir Reference<XResource> xResource; 160*cdf0e10cSrcweir try 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir xResource = xFactory->createResource(rxResourceId); 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir catch (lang::DisposedException&) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir // The factory is disposed and can be removed from the list 167*cdf0e10cSrcweir // of registered factories. 168*cdf0e10cSrcweir mpResourceFactoryContainer->RemoveFactoryForReference(xFactory); 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir catch(Exception&) 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir if (xResource.is()) 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>=1 178*cdf0e10cSrcweir OSL_TRACE(" successfully created\n"); 179*cdf0e10cSrcweir #endif 180*cdf0e10cSrcweir // 3. Add resource to URL->Object map. 181*cdf0e10cSrcweir AddResource(xResource, xFactory); 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir // 4. Add resource id to current configuration. 184*cdf0e10cSrcweir rxConfiguration->addResource(rxResourceId); 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir // 5. Notify the new resource to listeners of the ConfigurationController. 187*cdf0e10cSrcweir mpBroadcaster->NotifyListeners( 188*cdf0e10cSrcweir FrameworkHelper::msResourceActivationEvent, 189*cdf0e10cSrcweir rxResourceId, 190*cdf0e10cSrcweir xResource); 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir else 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>=1 195*cdf0e10cSrcweir OSL_TRACE(" resource creation failed\n"); 196*cdf0e10cSrcweir #endif 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir catch (RuntimeException&) 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir /* In this method we do following steps. 209*cdf0e10cSrcweir 1. Remove the resource from the URL->Object map of the configuration 210*cdf0e10cSrcweir controller. 211*cdf0e10cSrcweir 2. Notify listeners. 212*cdf0e10cSrcweir 3. Remove the resource id from the current configuration. 213*cdf0e10cSrcweir 4. Release the resource. 214*cdf0e10cSrcweir */ 215*cdf0e10cSrcweir void ConfigurationControllerResourceManager::DeactivateResource ( 216*cdf0e10cSrcweir const Reference<XResourceId>& rxResourceId, 217*cdf0e10cSrcweir const Reference<XConfiguration>& rxConfiguration) 218*cdf0e10cSrcweir { 219*cdf0e10cSrcweir if ( ! rxResourceId.is()) 220*cdf0e10cSrcweir return; 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir bool bSuccess (false); 223*cdf0e10cSrcweir try 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir // 1. Remove resource from URL->Object map. 226*cdf0e10cSrcweir ResourceDescriptor aDescriptor (RemoveResource(rxResourceId)); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir if (aDescriptor.mxResource.is() && aDescriptor.mxResourceFactory.is()) 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir // 2. Notifiy listeners that the resource is being deactivated. 231*cdf0e10cSrcweir mpBroadcaster->NotifyListeners( 232*cdf0e10cSrcweir FrameworkHelper::msResourceDeactivationEvent, 233*cdf0e10cSrcweir rxResourceId, 234*cdf0e10cSrcweir aDescriptor.mxResource); 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir // 3. Remove resource id from current configuration. 237*cdf0e10cSrcweir rxConfiguration->removeResource(rxResourceId); 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir // 4. Release the resource. 240*cdf0e10cSrcweir try 241*cdf0e10cSrcweir { 242*cdf0e10cSrcweir aDescriptor.mxResourceFactory->releaseResource(aDescriptor.mxResource); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir catch (lang::DisposedException& rException) 245*cdf0e10cSrcweir { 246*cdf0e10cSrcweir if ( ! rException.Context.is() 247*cdf0e10cSrcweir || rException.Context == aDescriptor.mxResourceFactory) 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir // The factory is disposed and can be removed from the 250*cdf0e10cSrcweir // list of registered factories. 251*cdf0e10cSrcweir mpResourceFactoryContainer->RemoveFactoryForReference( 252*cdf0e10cSrcweir aDescriptor.mxResourceFactory); 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir } 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir bSuccess = true; 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir catch (RuntimeException&) 260*cdf0e10cSrcweir { 261*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 262*cdf0e10cSrcweir } 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>=1 265*cdf0e10cSrcweir if (bSuccess) 266*cdf0e10cSrcweir OSL_TRACE("successfully deactivated %s\n", OUStringToOString( 267*cdf0e10cSrcweir FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr()); 268*cdf0e10cSrcweir else 269*cdf0e10cSrcweir OSL_TRACE("activating resource %s failed\n", OUStringToOString( 270*cdf0e10cSrcweir FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr()); 271*cdf0e10cSrcweir #endif 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir void ConfigurationControllerResourceManager::AddResource ( 278*cdf0e10cSrcweir const Reference<XResource>& rxResource, 279*cdf0e10cSrcweir const Reference<XResourceFactory>& rxFactory) 280*cdf0e10cSrcweir { 281*cdf0e10cSrcweir if ( ! rxResource.is()) 282*cdf0e10cSrcweir { 283*cdf0e10cSrcweir OSL_ASSERT(rxResource.is()); 284*cdf0e10cSrcweir return; 285*cdf0e10cSrcweir } 286*cdf0e10cSrcweir 287*cdf0e10cSrcweir // Add the resource to the resource container. 288*cdf0e10cSrcweir ResourceDescriptor aDescriptor; 289*cdf0e10cSrcweir aDescriptor.mxResource = rxResource; 290*cdf0e10cSrcweir aDescriptor.mxResourceFactory = rxFactory; 291*cdf0e10cSrcweir maResourceMap[rxResource->getResourceId()] = aDescriptor; 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>=2 294*cdf0e10cSrcweir OSL_TRACE("ConfigurationControllerResourceManager::AddResource(): added %s -> %x\n", 295*cdf0e10cSrcweir OUStringToOString( 296*cdf0e10cSrcweir FrameworkHelper::ResourceIdToString(rxResource->getResourceId()), 297*cdf0e10cSrcweir RTL_TEXTENCODING_UTF8).getStr(), 298*cdf0e10cSrcweir rxResource.get()); 299*cdf0e10cSrcweir #endif 300*cdf0e10cSrcweir } 301*cdf0e10cSrcweir 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir 305*cdf0e10cSrcweir ConfigurationControllerResourceManager::ResourceDescriptor 306*cdf0e10cSrcweir ConfigurationControllerResourceManager::RemoveResource ( 307*cdf0e10cSrcweir const Reference<XResourceId>& rxResourceId) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir ResourceDescriptor aDescriptor; 310*cdf0e10cSrcweir 311*cdf0e10cSrcweir ResourceMap::iterator iResource (maResourceMap.find(rxResourceId)); 312*cdf0e10cSrcweir if (iResource != maResourceMap.end()) 313*cdf0e10cSrcweir { 314*cdf0e10cSrcweir #if defined VERBOSE && VERBOSE>=2 315*cdf0e10cSrcweir OSL_TRACE("ConfigurationControllerResourceManager::RemoveResource(): removing %s -> %x\n", 316*cdf0e10cSrcweir OUStringToOString( 317*cdf0e10cSrcweir FrameworkHelper::ResourceIdToString(rxResourceId), 318*cdf0e10cSrcweir RTL_TEXTENCODING_UTF8).getStr(), 319*cdf0e10cSrcweir *iResource); 320*cdf0e10cSrcweir #endif 321*cdf0e10cSrcweir 322*cdf0e10cSrcweir aDescriptor = iResource->second; 323*cdf0e10cSrcweir maResourceMap.erase(rxResourceId); 324*cdf0e10cSrcweir } 325*cdf0e10cSrcweir 326*cdf0e10cSrcweir return aDescriptor; 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir 329*cdf0e10cSrcweir 330*cdf0e10cSrcweir 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir //===== ConfigurationControllerResourceManager::ResourceComparator ============ 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir bool ConfigurationControllerResourceManager::ResourceComparator::operator() ( 335*cdf0e10cSrcweir const Reference<XResourceId>& rxId1, 336*cdf0e10cSrcweir const Reference<XResourceId>& rxId2) const 337*cdf0e10cSrcweir { 338*cdf0e10cSrcweir if (rxId1.is() && rxId2.is()) 339*cdf0e10cSrcweir return rxId1->compareTo(rxId2)<0; 340*cdf0e10cSrcweir else if (rxId1.is()) 341*cdf0e10cSrcweir return true; 342*cdf0e10cSrcweir else if (rxId2.is()) 343*cdf0e10cSrcweir return false; 344*cdf0e10cSrcweir else 345*cdf0e10cSrcweir return false; 346*cdf0e10cSrcweir } 347*cdf0e10cSrcweir 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir 350*cdf0e10cSrcweir 351*cdf0e10cSrcweir } } // end of namespace sd::framework 352*cdf0e10cSrcweir 353