ctkServiceRegistration.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 "ctkServiceRegistration.h"
00023 #include "ctkServiceRegistrationPrivate.h"
00024 #include "ctkPluginFrameworkContext_p.h"
00025 #include "ctkPluginPrivate_p.h"
00026 #include "ctkServiceFactory.h"
00027 
00028 #include <QMutex>
00029 
00030 #include <stdexcept>
00031 
00032 
00033   ctkServiceRegistration::ctkServiceRegistration(ctkPluginPrivate* plugin, QObject* service,
00034                       const ServiceProperties& props)
00035     : d_ptr(new ctkServiceRegistrationPrivate(this, plugin, service, props))
00036   {
00037 
00038   }
00039 
00040   ctkServiceRegistration::ctkServiceRegistration(ctkServiceRegistrationPrivate& dd)
00041     : d_ptr(&dd)
00042   {
00043 
00044   }
00045 
00046   ctkServiceReference* ctkServiceRegistration::getReference()
00047   {
00048     Q_D(ctkServiceRegistration);
00049 
00050     if (!d->available) throw std::logic_error("Service is unregistered");
00051 
00052     return d->reference;
00053   }
00054 
00055   void ctkServiceRegistration::setProperties(const ServiceProperties& properties)
00056   {
00057 //    QMutexLocker lock(eventLock);
00058 //          Set before;
00059 //          // TBD, optimize the locking of services
00060 //          synchronized (bundle.fwCtx.services) {
00061 //
00062 //            synchronized (properties) {
00063 //              if (available) {
00064 //                // NYI! Optimize the MODIFIED_ENDMATCH code
00065 //                Object old_rank = properties.get(Constants.SERVICE_RANKING);
00066 //                before = bundle.fwCtx.listeners.getMatchingServiceListeners(reference);
00067 //                String[] classes = (String[])properties.get(Constants.OBJECTCLASS);
00068 //                Long sid = (Long)properties.get(Constants.SERVICE_ID);
00069 //                properties = new PropertiesDictionary(props, classes, sid);
00070 //                Object new_rank = properties.get(Constants.SERVICE_RANKING);
00071 //                if (old_rank != new_rank && new_rank instanceof Integer &&
00072 //                    !((Integer)new_rank).equals(old_rank)) {
00073 //                  bundle.fwCtx.services.updateServiceRegistrationOrder(this, classes);
00074 //                }
00075 //              } else {
00076 //                throw new IllegalStateException("Service is unregistered");
00077 //              }
00078 //            }
00079 //          }
00080 //          bundle.fwCtx.listeners
00081 //            .serviceChanged(bundle.fwCtx.listeners.getMatchingServiceListeners(reference),
00082 //                            new ServiceEvent(ServiceEvent.MODIFIED, reference),
00083 //                            before);
00084 //          bundle.fwCtx.listeners
00085 //            .serviceChanged(before,
00086 //                            new ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, reference),
00087 //                            null);
00088 
00089   }
00090 
00091   void ctkServiceRegistration::unregister()
00092   {
00093     Q_D(ctkServiceRegistration);
00094 
00095     if (d->unregistering) return; // Silently ignore redundant unregistration.
00096     {
00097       QMutexLocker lock(&d->eventLock);
00098       if (d->unregistering) return;
00099       d->unregistering = true;
00100 
00101       if (d->available)
00102       {
00103         if (d->plugin)
00104         {
00105           d->plugin->fwCtx->services.removeServiceRegistration(this);
00106         }
00107       }
00108       else
00109       {
00110         throw std::logic_error("Service is unregistered");
00111       }
00112     }
00113 
00114     if (d->plugin)
00115     {
00116       //TODO
00117 //      bundle.fwCtx.listeners
00118 //            .serviceChanged(bundle.fwCtx.listeners.getMatchingServiceListeners(reference),
00119 //                            new ServiceEvent(ServiceEvent.UNREGISTERING, reference),
00120 //                            null);
00121     }
00122 
00123     {
00124       QMutexLocker lock(&d->eventLock);
00125       {
00126         QMutexLocker lock2(&d->propsLock);
00127         d->available = false;
00128         if (d->plugin)
00129         {
00130           for (QHashIterator<ctkPlugin*, QObject*> i(d->serviceInstances); i.hasNext();)
00131           {
00132             QObject* obj = i.next().value();
00133             try
00134             {
00135               // NYI, don't call inside lock
00136               qobject_cast<ctkServiceFactory*>(d->service)->ungetService(i.key(),
00137                                                          this,
00138                                                          obj);
00139             }
00140             catch (const std::exception& ue)
00141             {
00142               ctkPluginFrameworkEvent pfwEvent(ctkPluginFrameworkEvent::ERROR, d->plugin->q_func(), ue);
00143               d->plugin->fwCtx->listeners
00144                   .emitFrameworkEvent(pfwEvent);
00145             }
00146           }
00147         }
00148         d->plugin = 0;
00149         d->dependents.clear();
00150         d->service = 0;
00151         d->serviceInstances.clear();;
00152         d->unregistering = false;
00153       }
00154     }
00155   }
00156 
00157   bool ctkServiceRegistration::operator<(const ctkServiceRegistration& o) const
00158   {
00159     Q_D(const ctkServiceRegistration);
00160     return d->reference->operator <(*(o.d_func()->reference));
00161 
00162 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1