diff --git a/BlueBerry/Bundles/org.blueberry.ui.qt/src/internal/berryServiceLocator.cpp b/BlueBerry/Bundles/org.blueberry.ui.qt/src/internal/berryServiceLocator.cpp index edfd443550..c8c0d74327 100755 --- a/BlueBerry/Bundles/org.blueberry.ui.qt/src/internal/berryServiceLocator.cpp +++ b/BlueBerry/Bundles/org.blueberry.ui.qt/src/internal/berryServiceLocator.cpp @@ -1,236 +1,239 @@ /*=================================================================== BlueBerry Platform Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "berryServiceLocator.h" #include "berryWorkbenchServiceRegistry.h" #include "services/berryIServiceFactory.h" #include "services/berryINestable.h" namespace berry { ServiceLocator::ParentLocator::ParentLocator( IServiceLocator* parent, const QString& serviceInterface) : locator(parent), key(serviceInterface) { } Object* ServiceLocator::ParentLocator::GetService(const QString& api) { if (key == api) { try { return locator->GetService(key); } catch (const BadWeakPointerException& /*e*/) { } } return NULL; } bool ServiceLocator::ParentLocator::HasService(const QString& api) const { if (key == api) { return true; } return false; } ServiceLocator::ServiceLocator() : activated(false) , factory(NULL) , parent(NULL) , disposed(false) { } ServiceLocator::ServiceLocator(IServiceLocator* _parent, const IServiceFactory* _factory, IDisposable::WeakPtr _owner) : activated(false), factory(_factory), parent(_parent), disposed(false), owner(_owner) { } void ServiceLocator::Activate() { activated = true; for (KeyToServiceMapType::iterator serviceItr = services.begin(); serviceItr != services.end(); ++serviceItr) { Object* service = serviceItr.value(); if (INestable* nestableService = dynamic_cast(service)) { nestableService->Activate(); } } } void ServiceLocator::Deactivate() { activated = false; for (KeyToServiceMapType::iterator serviceItr = services.begin(); serviceItr != services.end(); ++serviceItr) { Object* service = serviceItr.value(); if (INestable* nestableService = dynamic_cast(service)) { nestableService->Deactivate(); } } } void ServiceLocator::Dispose() { for (KeyToServiceMapType::iterator serviceItr = services.begin(); serviceItr != services.end(); ++serviceItr) { Object* object = serviceItr.value(); if (IDisposable* service = dynamic_cast(object)) { service->Dispose(); } } services.clear(); parent = 0; disposed = true; } Object* ServiceLocator::GetService(const QString& key) { if (disposed) { return NULL; } KeyToServiceMapType::const_iterator iter = services.find(key); Object* service = NULL; if (iter != services.end()) { service = iter.value(); } else { // if we don't have a service in our cache then: // 1. check our local factory // 2. go to the registry // or 3. use the parent service IServiceLocator::Pointer factoryParent = WorkbenchServiceRegistry::GLOBAL_PARENT; if (parent) { factoryParent = new ParentLocator(parent, key); } if (factory) { service = factory->Create(key, factoryParent.GetPointer(), this); } if (!service) { Object::Pointer factoryService = WorkbenchServiceRegistry::GetRegistry()->GetService(key, factoryParent.GetPointer(), this); - managedFactoryServices.push_back(factoryService); + if (factoryService) + { + managedFactoryServices.push_back(factoryService); + } service = factoryService.GetPointer(); } if (!service) { service = factoryParent->GetService(key); } else { this->RegisterService(key, service); } } return service; } bool ServiceLocator::HasService(const QString& key) const { if (disposed) { return false; } if (services.find(key) != services.end()) { return true; } return false; } void ServiceLocator::RegisterService(const QString& api, Object* service) const { if (api.isEmpty()) { throw ctkInvalidArgumentException("The service key cannot be empty"); } // if (!api.isInstance(service)) // { // throw new IllegalArgumentException("The service does not implement the given interface"); //$NON-NLS-1$ // } if (services.find(api) != services.end()) { Object* currentService = services[api]; services.remove(api); if (IDisposable* disposable = dynamic_cast(currentService)) { disposable->Dispose(); } } if (service) { services.insert(api, service); if (INestable* nestable = dynamic_cast(service)) { if (activated) { nestable->Activate(); } } } } bool ServiceLocator::IsDisposed() const { return disposed; } void ServiceLocator::UnregisterServices(const QList& /*serviceNames*/) { IDisposable::Pointer d(owner); if (d) { d->Dispose(); } } }