Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef2ff332dbf2c4c5295466f1bb9bed40bbe80b7b (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2017 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.log.stream;

import java.io.Closeable;
import java.util.Enumeration;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;

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.pushstream.PushStream;
import org.osgi.util.tracker.ServiceTracker;

public class LogEntrySource implements PushEventSource<LogEntry> {
	private final Set<PushEventConsumer<? super LogEntry>> consumers = new CopyOnWriteArraySet<>();
	private final ServiceTracker<LogReaderService, AtomicReference<LogReaderService>> withHistory;
	private volatile PushStream<LogEntry> logStream;
	private final ReentrantLock historyLock = new ReentrantLock();
	

	
	public LogEntrySource(ServiceTracker<LogReaderService, AtomicReference<LogReaderService>> withHistory) {
		this.withHistory = withHistory;
		
	}
	
	public PushStream<LogEntry> getLogStream() {
		return logStream;
	}
	
	public void setLogStream(PushStream<LogEntry> logStream) {
		this.logStream = logStream;
	}

	
	/* Open method isused to connect to the source and begin receiving a stream of events.
	 * It returns an AutoCloseable which can be used to close the event stream. 
	 * If the close method is called on this object then the stream is terminated by sending a close event.
	 * (non-Javadoc)
	 * @see org.osgi.util.pushstream.PushEventSource#open(org.osgi.util.pushstream.PushEventConsumer)
	 */
 
	@Override
	public Closeable open(PushEventConsumer<? super LogEntry> aec) throws Exception {
		
		LinkedBlockingDeque<LogEntry> historyList = new LinkedBlockingDeque<LogEntry>();
		
		if (!consumers.add(aec)){
			throw new IllegalStateException("Cannot add the same consumer multiple times");
		}
	

		/*when history is not equal to null then we acquire a lock to provide the full history 
		 * to the consumer first before any other new entries
		 */
		if(withHistory!=null){
			historyLock.lock();
			try{
				AtomicReference<LogReaderService> readerRef = withHistory.getService();
				LogReaderService reader = readerRef.get();
				if (reader != null){
					// Enumeration has the most recent entry first
					Enumeration<LogEntry> e= reader.getLog();
					if(e!=null){
						while(e.hasMoreElements()){
							historyList.add(e.nextElement());
						}
					}
					//Logging the history in the order of their appearance
					if(historyList!=null){
						while(!historyList.isEmpty()){
							LogEntry logEntry = historyList.removeLast();
							logged(logEntry);
						}
					}
				}
			}
			finally{
				historyLock.unlock();
			}
		}
		
		Closeable result = () -> {
			if (consumers.remove(aec)) {
				try {
					aec.accept(PushEvent.close());
				} catch (Exception e) {
					// ignore here for log stream
				}
			}
		};
		
		return result;
	}
	
	
	
	public void logged(LogEntry entry) {
		if (withHistory != null) {
			historyLock.lock();
		}
		
		/*consumer accepts the incoming log entries and returns a back pressure. 
		 * A return of zero indicates that event delivery may continue immediately. 
		 * A positive return value indicates that the source should delay sending any further events for the requested number of milliseconds. 
		 * A return value of -1 indicates that no further events should be sent and that the stream can be closed.
		 * @see org.osgi.util.pushstream.PushEventConsumer<T>
		 */
		try{
			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
				}
			}
		}
		finally{
			if(withHistory != null){
				historyLock.unlock();
			}
			
		}
	}
}

Back to the top