Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8cf9a5cfa9080cc75ab527d5d6f3cb0405a98326 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*******************************************************************************
 * 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.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.log.LogEntry;
import org.osgi.service.log.LogListener;
import org.osgi.service.log.LogReaderService;
import org.osgi.service.log.stream.LogStreamProvider;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/* LogStreamManager is used to start and stop the bundle and keeps the track of the logs using the 
 * ServiceTrackerCustomizer<LogReaderService, AtomicReference<LogReaderService>> which listens to 
 * the incoming logs using the LogListener. It is also responsible to provide service tracker 
 * and each log entry to the LogStreamProviderFactory.
 * 
 */
public class LogStreamManager implements BundleActivator, ServiceTrackerCustomizer<LogReaderService, AtomicReference<LogReaderService>>, LogListener {
	private ServiceRegistration<LogStreamProvider> logStreamServiceRegistration;
	private LogStreamProviderFactory logStreamProviderFactory;
	private ServiceTracker<LogReaderService, AtomicReference<LogReaderService>> logReaderService;
	BundleContext context;
	ReentrantLock eventProducerLock = new ReentrantLock();

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	@Override
	public void start(BundleContext bc) throws Exception {

		this.context = bc;
		logReaderService = new ServiceTracker<>(context, LogReaderService.class, this);
		logReaderService.open();

		logStreamProviderFactory = new LogStreamProviderFactory(logReaderService);
		logStreamServiceRegistration = context.registerService(LogStreamProvider.class, logStreamProviderFactory, null);

	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	@Override
	public void stop(BundleContext bundleContext) throws Exception {
		logReaderService.close();
		logStreamServiceRegistration.unregister();
		logStreamServiceRegistration = null;
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(org.osgi.framework.ServiceReference)
	 */

	@Override
	public AtomicReference<LogReaderService> addingService(ServiceReference<LogReaderService> reference) {
		AtomicReference<LogReaderService> tracked = new AtomicReference<>();
		modifiedService(reference, tracked);
		return tracked;
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object)
	 */
	@Override
	public void modifiedService(ServiceReference<LogReaderService> modifiedServiceRef, AtomicReference<LogReaderService> modifiedTracked) {
		eventProducerLock.lock();
		try {
			// Check if the currently used reader service is lower ranked that the modified serviceRef
			ServiceReference<LogReaderService> currentServiceRef = logReaderService.getServiceReference();
			if (currentServiceRef == null || modifiedServiceRef.compareTo(currentServiceRef) > 0) {
				// The modified service reference is higher ranked than the currently used one;
				// Use the modified service reference instead.
				LogReaderService readerService = context.getService(modifiedServiceRef);
				if (readerService != null) {
					if (modifiedTracked.get() == null) {
						// update our tracked object for the reference with the real service
						modifiedTracked.set(readerService);
					}
					// remove our listener from the currently used service
					if (currentServiceRef != null) {
						AtomicReference<LogReaderService> currentTracked = logReaderService.getService(currentServiceRef);
						if (currentTracked != null) {
							LogReaderService currentLogReader = currentTracked.get();
							if (currentLogReader != null) {
								// we were really using this service;
								// remove our listener and unget the service
								currentLogReader.removeLogListener(this);
								context.ungetService(currentServiceRef);
								// finally null out our tracked reference
								currentTracked.set(null);
							}
						}
					}

					readerService.addLogListener(this);
				}
			}
		} finally {
			eventProducerLock.unlock();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference, java.lang.Object)
	 */
	@Override
	public void removedService(ServiceReference<LogReaderService> removedRef, AtomicReference<LogReaderService> removedTracked) {
		eventProducerLock.lock();
		try {
			LogReaderService removedLogReader = removedTracked.get();
			if (removedLogReader != null) {
				// remove the listener
				removedLogReader.removeLogListener(this);
				context.ungetService(removedRef);
				removedTracked.set(null);
			}
			ServiceReference<LogReaderService> currentRef = logReaderService.getServiceReference();
			if (currentRef != null) {
				AtomicReference<LogReaderService> currentTracked = logReaderService.getService(currentRef);
				if (currentTracked != null) {
					LogReaderService currentLogReader = currentTracked.get();
					if (currentLogReader == null) {
						currentLogReader = context.getService(currentRef);
						currentTracked.set(currentLogReader);
					}
					if (currentLogReader != null) {
						currentLogReader.addLogListener(this);
					}
				}
			}
		} finally {
			eventProducerLock.unlock();
		}
	}

	/* It is used to post each log entry to the LogStreamProviderFactory
	 * (non-Javadoc)
	 * @see org.osgi.service.log.LogListener#logged(org.osgi.service.log.LogEntry)
	 */

	@Override
	public void logged(LogEntry entry) {

		logStreamProviderFactory.postLogEntry(entry);
	}

}

Back to the top