ctkServiceReferencePrivate.cpp

Go to the documentation of this file.
00001 /*=============================================================================
00002 
00003   Library: CTK
00004 
00005   Copyright (c) 2010 German Cancer Research Center,
00006     Division of Medical and Biological Informatics
00007 
00008   Licensed under the Apache License, Version 2.0 (the "License");
00009   you may not use this file except in compliance with the License.
00010   You may obtain a copy of the License at
00011 
00012     http://www.apache.org/licenses/LICENSE-2.0
00013 
00014   Unless required by applicable law or agreed to in writing, software
00015   distributed under the License is distributed on an "AS IS" BASIS,
00016   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017   See the License for the specific language governing permissions and
00018   limitations under the License.
00019 
00020 =============================================================================*/
00021 
00022 #include "ctkServiceReferencePrivate.h"
00023 
00024 #include <QObject>
00025 #include <QMutexLocker>
00026 
00027 #include "ctkPluginConstants.h"
00028 #include "ctkServiceFactory.h"
00029 #include "ctkServiceException.h"
00030 #include "ctkPluginPrivate_p.h"
00031 #include "ctkServiceRegistrationPrivate.h"
00032 #include "ctkPluginFrameworkContext_p.h"
00033 
00034 
00035   ctkServiceReferencePrivate::ctkServiceReferencePrivate(ctkServiceRegistrationPrivate* reg)
00036     : registration(reg)
00037   {
00038   }
00039 
00040   QObject* ctkServiceReferencePrivate::getService(ctkPlugin* plugin)
00041   {
00042     QObject* s = 0;
00043     {
00044       QMutexLocker lock(&registration->propsLock);
00045       if (registration->available)
00046       {
00047         int count = registration->dependents.value(plugin);
00048         if (count == 0)
00049         {
00050           QStringList classes =
00051               registration->properties.value(PluginConstants::OBJECTCLASS).toStringList();
00052           registration->dependents[plugin] = 1;
00053           if (ctkServiceFactory* serviceFactory = qobject_cast<ctkServiceFactory*>(registration->getService()))
00054           {
00055             try
00056             {
00057               s = serviceFactory->getService(plugin, registration->q_func());
00058             }
00059             catch (const std::exception& pe)
00060             {
00061               ctkServiceException se("ctkServiceFactory throw an exception",
00062                                   ctkServiceException::FACTORY_EXCEPTION, pe);
00063               plugin->d_func()->fwCtx->listeners.frameworkError
00064                 (registration->plugin->q_func(), se);
00065               return 0;
00066             }
00067             if (s == 0) {
00068               ctkServiceException se("ctkServiceFactory produced null",
00069                                   ctkServiceException::FACTORY_ERROR);
00070               plugin->d_func()->fwCtx->listeners.frameworkError
00071                 (registration->plugin->q_func(), se);
00072               return 0;
00073             }
00074             for (QStringListIterator i(classes); i.hasNext(); )
00075             {
00076               QString cls = i.next();
00077               if (!registration->plugin->fwCtx->services.checkServiceClass(s, cls))
00078               {
00079                 ctkServiceException se(QString("ctkServiceFactory produced an object ") +
00080                                      "that did not implement: " + cls,
00081                                      ctkServiceException::FACTORY_ERROR);
00082                 plugin->d_func()->fwCtx->listeners.frameworkError
00083                   (registration->plugin->q_func(), se);
00084                 return 0;
00085               }
00086             }
00087             registration->serviceInstances.insert(plugin, s);
00088           }
00089           else
00090           {
00091             s = registration->getService();
00092           }
00093         }
00094         else
00095         {
00096           registration->dependents.insert(plugin, count + 1);
00097           if (qobject_cast<ctkServiceFactory*>(registration->getService()))
00098           {
00099             s = registration->serviceInstances.value(plugin);
00100           }
00101           else
00102           {
00103             s = registration->getService();
00104           }
00105         }
00106       }
00107     }
00108     return s;
00109   }
00110 
00121   bool ctkServiceReferencePrivate::ungetService(ctkPlugin* plugin, bool checkRefCounter)
00122   {
00123     QMutexLocker lock(&registration->propsLock);
00124     bool hadReferences = false;
00125     if (registration->reference != 0)
00126     {
00127       bool removeService = false;
00128 
00129       int count= registration->dependents.value(plugin);
00130       if (count > 0)
00131       {
00132         hadReferences = true;
00133       }
00134 
00135       if(checkRefCounter)
00136       {
00137         if (count > 1)
00138         {
00139             registration->dependents[plugin] = count - 1;
00140         }
00141         else if(count == 1)
00142         {
00143           removeService = true;
00144         }
00145       }
00146       else
00147       {
00148         removeService = true;
00149       }
00150 
00151       if (removeService)
00152       {
00153         QObject* sfi = registration->serviceInstances[plugin];
00154         registration->serviceInstances.remove(plugin);
00155         if (sfi != 0)
00156         {
00157           try
00158           {
00159             qobject_cast<ctkServiceFactory*>(registration->getService())->ungetService(plugin,
00160                 registration->q_func(), sfi);
00161           }
00162           catch (const std::exception& e)
00163           {
00164             plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), e);
00165           }
00166         }
00167         registration->dependents.remove(plugin);
00168       }
00169     }
00170 
00171     return hadReferences;
00172   }
00173 
00174 
00175 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1