package org.eclipse.osgi.internal.log;
import java.util.*;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
public class EventAdminAdapter implements ServiceTrackerCustomizer<Object, Object> {
public static final String EVENT_TOPIC = "event.topics";
private static final String[] LOG_TOPICS_ARRAY = {"*", "org/*", "org/osgi/*", "org/osgi/service/*", "org/osgi/service/log/*", "org/osgi/service/log/LogEntry/*", "org/osgi/service/log/LogEntry/LOG_ERROR", "org/osgi/service/log/LogEntry/LOG_WARNING", "org/osgi/service/log/LogEntry/LOG_INFO", "org/osgi/service/log/LogEntry/LOG_DEBUG", "org/osgi/service/log/LogEntry/LOG_OTHER"};
private static final Object LOG_TOPIC_TOKEN = new Object();
private static Collection<String> logTopics = new HashSet<>(Arrays.asList(LOG_TOPICS_ARRAY));
private static Collection<String> eventAdminObjectClass = Arrays.asList("org.osgi.service.event.EventAdmin");
private static Collection<String> eventHandlerObjectClass = Arrays.asList("org.osgi.service.event.EventHandler");
private ServiceTracker<Object, Object> eventAdminTracker;
private ServiceTracker<Object, Object> eventHandlerTracker;
private BundleContext context;
private ServiceReference<Object> eventAdmin;
private int logEventHandlers;
private ExtendedLogReaderServiceFactory logReaderServiceFactory;
private EventAdminLogListener logListener;
public EventAdminAdapter(BundleContext context, ExtendedLogReaderServiceFactory logReaderServiceFactory) {
this.context = context;
this.logReaderServiceFactory = logReaderServiceFactory;
eventAdminTracker = new ServiceTracker<>(context, "org.osgi.service.event.EventAdmin", this);
eventHandlerTracker = new ServiceTracker<>(context, "org.osgi.service.event.EventHandler", this);
}
public void start() {
eventAdminTracker.open();
eventHandlerTracker.open();
}
public void stop() {
eventAdminTracker.close();
eventHandlerTracker.close();
}
@Override
public Object addingService(ServiceReference<Object> reference) {
Object toTrack = null;
Object objectClass = reference.getProperty(Constants.OBJECTCLASS);
Object topics = reference.getProperty(EVENT_TOPIC);
if (checkServiceProp(objectClass, eventAdminObjectClass) && eventAdmin == null) {
toTrack = reference;
eventAdmin = reference;
} else if (checkServiceProp(objectClass, eventHandlerObjectClass) && checkServiceProp(topics, logTopics)) {
logEventHandlers++;
toTrack = LOG_TOPIC_TOKEN;
}
if (eventAdmin != null && logEventHandlers > 0 && logListener == null) {
try {
logListener = new EventAdminLogListener(context.getService(eventAdmin));
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
}
logReaderServiceFactory.addLogListener(logListener, ExtendedLogReaderServiceFactory.NULL_LOGGER_FILTER);
}
return toTrack;
}
@Override
public void modifiedService(ServiceReference<Object> reference, Object tracked) {
removedService(reference, tracked);
addingService(reference);
}
@Override
public void removedService(ServiceReference<Object> reference, Object tracked) {
if (tracked == eventAdmin) {
eventAdmin = null;
context.ungetService(reference);
} else if (LOG_TOPIC_TOKEN == tracked) {
logEventHandlers--;
}
if (logListener != null && (eventAdmin == null || logEventHandlers == 0)) {
logReaderServiceFactory.removeLogListener(logListener);
logListener = null;
}
}
private static boolean checkServiceProp(Object property, Collection<String> check) {
if (property instanceof String)
return check.contains(property);
if (property instanceof String[]) {
String[] topics = (String[]) property;
for (String topic : topics) {
if (check.contains(topic)) {
return true;
}
}
}
if (property instanceof Collection) {
for (Object prop : (Collection<?>) property)
if (check.contains(prop))
return true;
}
return false;
}
}