amichair commented on code in PR #95:
URL: https://github.com/apache/aries-rsa/pull/95#discussion_r3171144143


##########
rsa/src/main/java/org/apache/aries/rsa/core/event/EventAdminSender.java:
##########
@@ -18,85 +18,148 @@
  */
 package org.apache.aries.rsa.core.event;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.security.cert.X509Certificate;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventAdmin;
 import org.osgi.service.remoteserviceadmin.EndpointDescription;
+import org.osgi.service.remoteserviceadmin.ExportReference;
 import org.osgi.service.remoteserviceadmin.ImportReference;
 import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class EventAdminSender {
-    private HashMap<Integer, String> typeToTopic;
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(EventAdminSender.class);
+
     private BundleContext context;
 
     public EventAdminSender(BundleContext context) {
         this.context = context;
-        typeToTopic = new HashMap<>();
-        typeToTopic.put(RemoteServiceAdminEvent.EXPORT_ERROR, "EXPORT_ERROR");
-        typeToTopic.put(RemoteServiceAdminEvent.EXPORT_REGISTRATION, 
"EXPORT_REGISTRATION");
-        typeToTopic.put(RemoteServiceAdminEvent.EXPORT_UNREGISTRATION, 
"EXPORT_UNREGISTRATION");
-        typeToTopic.put(RemoteServiceAdminEvent.EXPORT_UPDATE, 
"EXPORT_UPDATE");
-        typeToTopic.put(RemoteServiceAdminEvent.EXPORT_WARNING, 
"EXPORT_WARNING");
-        typeToTopic.put(RemoteServiceAdminEvent.IMPORT_ERROR, "IMPORT_ERROR");
-        typeToTopic.put(RemoteServiceAdminEvent.IMPORT_REGISTRATION, 
"IMPORT_REGISTRATION");
-        typeToTopic.put(RemoteServiceAdminEvent.IMPORT_UNREGISTRATION, 
"IMPORT_UNREGISTRATION");
-        typeToTopic.put(RemoteServiceAdminEvent.IMPORT_UPDATE, 
"IMPORT_UPDATE");
-        typeToTopic.put(RemoteServiceAdminEvent.IMPORT_WARNING, 
"IMPORT_WARNING");
     }
 
-    public void send(RemoteServiceAdminEvent rsaEvent) {
-        final Event event = toEvent(rsaEvent);
-        ServiceReference<EventAdmin> sref = 
this.context.getServiceReference(EventAdmin.class);
+    private void notifyEventAdmins(String type, Event event) {
+        // according to the EventAdmin specs, we publish to the service with 
the
+        // highest ranking (which is the one returned by getServiceReference)
+        ServiceReference<EventAdmin> sref = 
context.getServiceReference(EventAdmin.class);
         if (sref != null) {
-            final EventAdmin eventAdmin = this.context.getService(sref);
-            AccessController.doPrivileged(new PrivilegedAction<Void>() {
-                public Void run() {
-                    eventAdmin.postEvent(event);
-                    return null;
+            LOG.debug("Publishing event {} to EventAdmin", type);
+            final EventAdmin eventAdmin = context.getService(sref);
+            if (eventAdmin != null) {
+                try {
+                    AccessController.doPrivileged((PrivilegedAction<Void>) () 
-> {
+                        eventAdmin.postEvent(event);
+                        return null;
+                    });
+                } finally {
+                    context.ungetService(sref);
                 }
-            });
+            }
             this.context.ungetService(sref);
         }
     }
 
-    private Event toEvent(RemoteServiceAdminEvent rsaEvent) {
-        String topic = getTopic(rsaEvent);
+    public void send(RemoteServiceAdminEvent rsae) {
+        String type = getConstName(RemoteServiceAdminEvent.class, null, 
rsae.getType(), "UNKNOWN_EVENT");
+        String topic = "org/osgi/service/remoteserviceadmin/" + type;
+        Map<String, Object> props = createProps(rsae);
+        Event event = new Event(topic, props);
+        notifyEventAdmins(type, event);
+    }
+
+    private static <K, V> void putIfNotNull(Map<K, V> map, K key, V val) {
+        if (val != null) {
+            map.put(key, val);
+        }
+    }
+
+    /**
+     * Returns the name of the first constant field (public static final) in 
the given class

Review Comment:
   ok now I went a different route with a switch statement, tiny change but a 
bit more contained and efficient.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to