[ 
https://issues.apache.org/jira/browse/FELIX-4439?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13914397#comment-13914397
 ] 

Marc de Jonge commented on FELIX-4439:
--------------------------------------

It seems that I've got it working rather quickly (about an hour of work?). It 
still needs more testing, but the basic case that I'm working on seems to work 
fine.

{code:title=FELIX-4439.patch|borderStyle=solid}
>From d6c08ad1011d527cca637c4fe30f0b554cd42911 Mon Sep 17 00:00:00 2001
From: Marc de Jonge <[email protected]>
Date: Thu, 27 Feb 2014 12:06:08 +0100
Subject: [PATCH] FELIX-4439 Added functionality to send Events from the
 ConfigurationManager through the EventAdmin

---
 .../apache/felix/cm/impl/ConfigurationManager.java | 42 ++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java 
b/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
index 12dd0de..14ff988 100644
--- a/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
+++ b/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
@@ -56,6 +56,8 @@ import org.osgi.service.cm.ConfigurationListener;
 import org.osgi.service.cm.ConfigurationPermission;
 import org.osgi.service.cm.ConfigurationPlugin;
 import org.osgi.service.cm.SynchronousConfigurationListener;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
 import org.osgi.service.log.LogService;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -142,6 +144,9 @@ public class ConfigurationManager implements 
BundleActivator, BundleListener
     // the synchronous ConfigurationEvent listeners
     private ServiceTracker syncConfigurationListenerTracker;
 
+    // the EventAdmin to send events to
+    private ServiceTracker eventAdminTracker;
+
     // service tracker for managed services
     private ManagedServiceTracker managedServiceTracker;
 
@@ -232,6 +237,10 @@ public class ConfigurationManager implements 
BundleActivator, BundleListener
             SynchronousConfigurationListener.class.getName(), null );
         syncConfigurationListenerTracker.open();
 
+        // eventadmin support
+        eventAdminTracker = new ServiceTracker( bundleContext, 
EventAdmin.class.getName(), null );
+        eventAdminTracker.open();
+
         // initialize the asynchonous updater thread
         ThreadGroup tg = new ThreadGroup( "Configuration Admin Service" );
         tg.setDaemon( true );
@@ -785,8 +794,41 @@ public class ConfigurationManager implements 
BundleActivator, BundleListener
             log( LogService.LOG_DEBUG, "No ConfigurationListeners to send {0} 
event to.", new Object[]
                 { asyncSender.getTypeName() } );
         }
+
+        // call the eventadmin when available
+        EventAdmin eventAdmin = (EventAdmin) eventAdminTracker.getService();
+        if ( eventAdmin != null )
+        {
+            fireConfigurationEvent(eventAdmin, type, pid, factoryPid);
+        }
     }
 
+    void fireConfigurationEvent(EventAdmin eventAdmin, int type, String pid, 
String factoryPid)
+    {
+        String topic = "org/osgi/service/cm/ConfigurationEvent";
+        switch(type)
+        {
+            case ConfigurationEvent.CM_DELETED:
+                topic += "/CMDELETED";
+                break;
+            case ConfigurationEvent.CM_LOCATION_CHANGED:
+                topic += "/CM_LOCATION_CHANGED";
+                break;
+            case ConfigurationEvent.CM_UPDATED:
+                topic += "/CM_UPDATED";
+                break;
+        }
+
+        Dictionary details = new Hashtable();
+        if(factoryPid != null) details.put("cm.factoryPid", factoryPid);
+        details.put("cm.pid", pid);
+        ServiceReference reference = 
configurationAdminRegistration.getReference();
+        details.put("service", reference);
+        details.put("service.id", reference.getProperty(Constants.SERVICE_ID));
+        details.put("service.objectClass", 
reference.getProperty(Constants.OBJECTCLASS));
+        details.put("service.pid", 
reference.getProperty(Constants.SERVICE_PID));
+        eventAdmin.sendEvent(new Event(topic, details));
+    }
 
     // ---------- BundleListener 
-----------------------------------------------
 
-- 
1.7.11.msysgit.1
{code}

> ConfigurationAdmin should send events through the EventAdmin
> ------------------------------------------------------------
>
>                 Key: FELIX-4439
>                 URL: https://issues.apache.org/jira/browse/FELIX-4439
>             Project: Felix
>          Issue Type: New Feature
>          Components: Configuration Admin
>    Affects Versions: configadmin-1.6.0, configadmin-1.8.0
>            Reporter: Marc de Jonge
>              Labels: feature
>   Original Estimate: 96h
>  Remaining Estimate: 96h
>
> According to the OSGi compendium specifications 4.3.0, section 104.8.1 (page 
> 88), the ConfigurationAdmin should send events through the EventAdmin when it 
> is available.
> Copy of the text:
> Configuration events must be delivered asynchronously by the Configuration 
> Admin implementation, if present. The topic of a configuration event must be:
> org/osgi/service/cm/ConfigurationEvent/<event type>
> The <event type> can be any of the following:
> - CM_DELETED
> - CM_UPDATED
> - CM_LOCATION_CHANGED
> The properties of a configuration event are:
> • cm.factoryPid – (String) The factory PID of the associated Configuration 
> object, if the target is a
> Managed Service Factory. Otherwise not set.
> • cm.pid – (String) The PID of the associated Configuration object.
> • service – (ServiceReference) The Service Reference of the Configuration 
> Admin service.
> • service.id – (Long) The Configuration Admin service's ID.
> • service.objectClass – (String[]) The Configuration Admin service's object 
> class (which must
> include org.osgi.service.cm.ConfigurationAdmin)
> • service.pid – (String) The Configuration Admin service's persistent 
> identity, if set.
> I'd like to use the EventAdmin for this information, because it is guaranteed 
> to have no side effects and to be informative only.
> Now of course I could use the ConfigurationListener for this, but it seems 
> that this has some unwanted side effects. Unbounded configurations now seem 
> to bind to my listenening bundle, while I only want to present some 
> configuration status on a Web UI. There is another bundle to which the 
> configuration should bind, but I use DS to do so. Maybe this is another 
> issue...



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Reply via email to