alien11689 commented on code in PR #95:
URL: https://github.com/apache/aries-rsa/pull/95#discussion_r3170713264
##########
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:
Oh, why people are not using enums... it's like java 1.4...
I would prefer not to rely on reflection here as an abstraction
the usage is limites only to a single class and assume the field names
starting with `IMPORT_` and `EXPORT_` - let's link in the javadoc to this class
and make it explicit only for it
https://docs.osgi.org/javadoc/osgi.cmpn/8.1.0/org/osgi/service/remoteserviceadmin/RemoteServiceAdminEvent.html
I don't think the pattern should be repeated (but I may be wrong...)
other thing - can we use or get rid off prefix parameter? it's not used
right now
--
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]