Selaa lähdekoodia

Added EventAdmin adapter to ConfigAdmin implementation.

Sascha Zelzer 14 vuotta sitten
vanhempi
commit
87378578bf

+ 3 - 0
Plugins/org.commontk.configadmin/CMakeLists.txt

@@ -17,6 +17,8 @@ SET(PLUGIN_SRCS
   ctkConfigurationAdminFactory_p.h
   ctkConfigurationAdminImpl.cpp
   ctkConfigurationAdminImpl_p.h
+  ctkConfigurationEventAdapter_p.h
+  ctkConfigurationEventAdapter.cpp
   ctkConfigurationImpl.cpp
   ctkConfigurationImpl_p.h
   ctkConfigurationStore.cpp
@@ -33,6 +35,7 @@ SET(PLUGIN_MOC_SRCS
   ctkConfigurationAdminActivator_p.h
   ctkConfigurationAdminFactory_p.h
   ctkConfigurationAdminImpl_p.h
+  ctkConfigurationEventAdapter_p.h
 )
 
 # Qt Designer files which should be processed by Qts uic

+ 7 - 10
Plugins/org.commontk.configadmin/ctkConfigurationAdminActivator.cpp

@@ -22,6 +22,7 @@
 
 #include "ctkConfigurationAdminActivator_p.h"
 #include "ctkConfigurationAdminFactory_p.h"
+#include "ctkConfigurationEventAdapter_p.h"
 
 #include <ctkServiceTracker.h>
 #include <service/cm/ctkConfigurationAdmin.h>
@@ -31,7 +32,7 @@
 
 
 ctkConfigurationAdminActivator::ctkConfigurationAdminActivator()
-  : logTracker(0), factory(0)
+  : logTracker(0), factory(0), eventAdapter(0)
 {
 }
 
@@ -46,10 +47,8 @@ void ctkConfigurationAdminActivator::start(ctkPluginContext* context)
   logFileFallback.open(stdout, QIODevice::WriteOnly);
   logTracker = new ctkCMLogTracker(context, &logFileFallback);
   logTracker->open();
-//  if (checkEventAdmin()) {
-//     eventAdapter = new ConfigurationEventAdapter(context);
-//     eventAdapter.start();
-//    }
+  eventAdapter = new ctkConfigurationEventAdapter(context);
+  eventAdapter->start();
   factory = new ctkConfigurationAdminFactory(context, logTracker);
   factory->start();
   context->connectPluginListener(factory, SLOT(pluginChanged(const ctkPluginEvent&)));
@@ -69,11 +68,9 @@ void ctkConfigurationAdminActivator::stop(ctkPluginContext* context)
   delete factory;
   factory = 0;
 
-//  if (eventAdapter != null)
-//  {
-//    eventAdapter.stop();
-//    eventAdapter = null;
-//  }
+  eventAdapter->stop();
+  delete eventAdapter;
+  eventAdapter = 0;
 
   logTracker->close();
   delete logTracker;

+ 2 - 0
Plugins/org.commontk.configadmin/ctkConfigurationAdminActivator_p.h

@@ -28,6 +28,7 @@
 #include "ctkCMLogTracker_p.h"
 
 class ctkConfigurationAdminFactory;
+class ctkConfigurationEventAdapter;
 
 /**
  * The ctkConfigurationAdminActivator starts the ctkConfigurationAdminFactory but also handles passing in the Service
@@ -56,6 +57,7 @@ private:
   ctkCMLogTracker* logTracker;
   ctkServiceRegistration registration;
   ctkConfigurationAdminFactory* factory;
+  ctkConfigurationEventAdapter* eventAdapter;
 
 }; // ctkConfigurationAdminActivator
 

+ 109 - 0
Plugins/org.commontk.configadmin/ctkConfigurationEventAdapter.cpp

@@ -0,0 +1,109 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#include "ctkConfigurationEventAdapter_p.h"
+
+#include <service/event/ctkEvent.h>
+
+const QString ctkConfigurationEventAdapter::TOPIC = "org/commontk/service/cm/ConfigurationEvent";
+const QChar ctkConfigurationEventAdapter::TOPIC_SEPARATOR = '/';
+
+const QString ctkConfigurationEventAdapter::CM_UPDATED = "CM_UPDATED";
+const QString ctkConfigurationEventAdapter::CM_DELETED = "CM_DELETED";
+
+const QString ctkConfigurationEventAdapter::CM_FACTORY_PID = "cm.factoryPid";
+const QString ctkConfigurationEventAdapter::CM_PID = "cm.pid";
+const QString ctkConfigurationEventAdapter::SERVICE = "service";
+const QString ctkConfigurationEventAdapter::SERVICE_ID = "service.id";
+const QString ctkConfigurationEventAdapter::SERVICE_OBJECTCLASS = "service.objectClass";
+const QString ctkConfigurationEventAdapter::SERVICE_PID = "service.pid";
+
+
+ctkConfigurationEventAdapter::ctkConfigurationEventAdapter(ctkPluginContext* context)
+  : context(context), eventAdminTracker(context)
+{
+
+}
+
+void ctkConfigurationEventAdapter::start()
+{
+  eventAdminTracker.open();
+  configListenerRegistration = context->registerService<ctkConfigurationListener>(this);
+}
+
+void ctkConfigurationEventAdapter::stop()
+{
+  configListenerRegistration.unregister();
+  configListenerRegistration = 0;
+  eventAdminTracker.close();
+}
+
+void ctkConfigurationEventAdapter::configurationEvent(const ctkConfigurationEvent& event)
+{
+  ctkEventAdmin* eventAdmin = eventAdminTracker.getService();
+  if (eventAdmin == 0)
+  {
+    return;
+  }
+  QString typeName;
+  switch (event.getType())
+  {
+  case ctkConfigurationEvent::CM_UPDATED :
+    typeName = CM_UPDATED;
+    break;
+  case ctkConfigurationEvent::CM_DELETED :
+    typeName = CM_DELETED;
+    break;
+  default : // do nothing
+    return;
+  }
+  QString topic = TOPIC + TOPIC_SEPARATOR + typeName;
+  ctkServiceReference ref = event.getReference();
+  if (!ref)
+  {
+    throw ctkRuntimeException("ctkConfigurationEvent::getServiceReference() is null");
+  }
+  ctkDictionary properties;
+  properties.insert(CM_PID, event.getPid());
+  if (event.getFactoryPid().isNull())
+  {
+    properties.insert(CM_FACTORY_PID, event.getFactoryPid());
+  }
+  putServiceReferenceProperties(properties, ref);
+  ctkEvent convertedEvent(topic, properties);
+  eventAdmin->postEvent(convertedEvent);
+}
+
+void ctkConfigurationEventAdapter::putServiceReferenceProperties(ctkDictionary& properties, const ctkServiceReference& ref)
+{
+  properties.insert(SERVICE, QVariant::fromValue(ref));
+  properties.insert(SERVICE_ID, ref.getProperty(ctkPluginConstants::SERVICE_ID));
+  QVariant o = ref.getProperty(ctkPluginConstants::SERVICE_PID);
+  if (o.canConvert<QString>())
+  {
+    properties.insert(SERVICE_PID, o);
+  }
+  QVariant o2 = ref.getProperty(ctkPluginConstants::OBJECTCLASS);
+  if (o.canConvert<QStringList>())
+  {
+    properties.insert(SERVICE_OBJECTCLASS, o2);
+  }
+}

+ 71 - 0
Plugins/org.commontk.configadmin/ctkConfigurationEventAdapter_p.h

@@ -0,0 +1,71 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#ifndef CTKCONFIGURATIONEVENTADAPTER_P_H
+#define CTKCONFIGURATIONEVENTADAPTER_P_H
+
+#include <QObject>
+
+#include <ctkServiceTracker.h>
+#include <service/cm/ctkConfigurationListener.h>
+#include <service/event/ctkEventAdmin.h>
+
+
+class ctkConfigurationEventAdapter : public QObject, public ctkConfigurationListener
+{
+  Q_OBJECT
+  Q_INTERFACES(ctkConfigurationListener)
+
+public:
+
+  // constants for Event topic substring
+  static const QString TOPIC; // = "org/commontk/service/cm/ConfigurationEvent"
+  static const QChar TOPIC_SEPARATOR; // = '/'
+  // constants for Event types
+  static const QString CM_UPDATED; // = "CM_UPDATED"
+  static const QString CM_DELETED; // = "CM_DELETED"
+  // constants for Event properties
+  static const QString CM_FACTORY_PID; // = "cm.factoryPid"
+  static const QString CM_PID; // = "cm.pid"
+  static const QString SERVICE; // = "service"
+  static const QString SERVICE_ID; // = "service.id"
+  static const QString SERVICE_OBJECTCLASS; // = "service.objectClass"
+  static const QString SERVICE_PID; // = "service.pid"
+
+private:
+
+  ctkPluginContext* const context;
+  ctkServiceRegistration configListenerRegistration;
+  ctkServiceTracker<ctkEventAdmin*> eventAdminTracker;
+
+public:
+
+  ctkConfigurationEventAdapter(ctkPluginContext* context);
+
+  void start();
+  void stop();
+
+   void configurationEvent(const ctkConfigurationEvent& event);
+
+  void putServiceReferenceProperties(ctkDictionary& properties, const ctkServiceReference& ref);
+};
+
+#endif // CTKCONFIGURATIONEVENTADAPTER_P_H