Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0842e6c000cc3b5cd2e6e5e7c7fbe4e3ef833627 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.eclipse.equinox.internal.log.stream;

import java.io.Closeable;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;

import org.osgi.service.log.LogEntry;
import org.osgi.service.log.LogReaderService;
import org.osgi.util.pushstream.PushEvent;
import org.osgi.util.pushstream.PushEventConsumer;
import org.osgi.util.pushstream.PushEventSource;
import org.osgi.util.tracker.ServiceTracker;

public class LogEntrySource implements PushEventSource<LogEntry> {
	private final Executor executor;
	private final ServiceTracker<LogReaderService, AtomicReference<LogReaderService>> withHistory;
	private final Set<PushEventConsumer<? super LogEntry>> consumers = new CopyOnWriteArraySet<>();
	public LogEntrySource(Executor executor, ServiceTracker<LogReaderService, AtomicReference<LogReaderService>> withHistory) {
		this.executor = executor;
		this.withHistory = withHistory;
	}


	@Override
	public Closeable open(PushEventConsumer<? super LogEntry> aec) throws Exception {
		if (!consumers.add(aec)){
			throw new IllegalStateException("Cannot add the same consumer multiple times");
		}
		return () -> {
			if (consumers.remove(aec)) {
				try {
					aec.accept(PushEvent.close());
				} catch (Exception e) {
					// ignore here for log stream
				}
			}
		};
	}

	public void logged(LogEntry entry) {
		for (PushEventConsumer<? super LogEntry> consumer : consumers) {
			try {
				long status = consumer.accept(PushEvent.data(entry));
				if (status < 0) {
					consumer.accept(PushEvent.close());
				}
			} catch (Exception e) {
				// we ignore exceptions here for log stream
			}
		}
	}

}

Back to the top