Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0e3a1d29fe6d0f4e8cb500877222129f34b46d9b (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/****************************************************************************
 * Copyright (c) 2005, 2010 Jan S. Rellermeyer, Systems Group,
 * Department of Computer Science, ETH Zurich 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:
 *    Jan S. Rellermeyer - initial API and implementation
 *    Markus Alexander Kuppe - enhancements and bug fixes
 *
*****************************************************************************/
package ch.ethz.iks.slp.impl;

import java.util.Dictionary;

import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;

import ch.ethz.iks.slp.impl.filter.Filter;

/**
 * Platform abstraction for the OSGi implementation.
 * 
 * @author Jan S. Rellermeyer, ETH Zurich
 */
public class OSGiPlatformAbstraction implements PlatformAbstraction,
		ServiceListener {

	/**
	 * 
	 */
	private final BundleContext context;

	/**
	 * 
	 */
	private LogService log = new NullPatternLogService();		

	/**
	 * Constructor.
	 * 
	 * @param context
	 *            the bundle context from the OSGi framework.
	 * @param log
	 *            the LogService, or null.
	 * @param debug
	 *            true if debugging is enabled.
	 * @throws InvalidSyntaxException 
	 * 				may never happen
	 */
	OSGiPlatformAbstraction(BundleContext context) throws InvalidSyntaxException {
		this.context = context;

		// initially get the LogService
		final ServiceReference sref = context
				.getServiceReference(LogService.class.getName());
		if (sref != null) {
			this.log = (LogService) context.getService(sref);
		}

		// track the LogService for life cycle events
		context.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + LogService.class.getName() + ")");

		logDebug("jSLP OSGi started.");
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#createFilter(java.lang.String)
	 */
	public Filter createFilter(String filterString) {
		try {
			final org.osgi.framework.Filter filter = context
					.createFilter(filterString);
			return new Filter() {
				public boolean match(Dictionary values) {
					return filter.match(values);
				}

				public String toString() {
					return filter.toString();
				}
			};
		} catch (InvalidSyntaxException e) {
			throw new IllegalArgumentException(e.getMessage());
		}
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#logDebug(java.lang.String)
	 */
	public void logDebug(String message) {
		if(SLPCore.CONFIG.getDebugEnabled()) {
			log.log(LogService.LOG_DEBUG, message);
		}
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#logDebug(java.lang.String,
	 *      java.lang.Throwable)
	 */
	public void logDebug(String message, Throwable exception) {
		if(SLPCore.CONFIG.getDebugEnabled()) {
			log.log(LogService.LOG_DEBUG, message, exception);
		}
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#logError(java.lang.String)
	 */
	public void logError(String message) {
		log.log(LogService.LOG_ERROR, message);
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#logError(java.lang.String,
	 *      java.lang.Throwable)
	 */
	public void logError(String message, Throwable exception) {
		log.log(LogService.LOG_ERROR, message, exception);
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#logTraceMessage(java.lang.String)
	 */
	public void logTraceMessage(String message) {
		if(SLPCore.CONFIG.getTraceMessage()) {
			log.log(LogService.LOG_INFO, message);
		}
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#logTraceDrop(java.lang.String)
	 */
	public void logTraceDrop(String message) {
		if(SLPCore.CONFIG.getTraceDrop()) {
			log.log(LogService.LOG_INFO, message);
		}
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#logTraceMessage(java.lang.String)
	 */
	public void logTraceReg(String message) {
		if(SLPCore.CONFIG.getTraceReg()) {
			log.log(LogService.LOG_INFO, message);
		}
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#logWarning(java.lang.String)
	 */
	public void logWarning(String message) {
		log.log(LogService.LOG_WARNING, message);
	}

	/**
	 * 
	 * @see ch.ethz.iks.slp.impl.PlatformAbstraction#logWarning(java.lang.String,
	 *      java.lang.Throwable)
	 */
	public void logWarning(String message, Throwable exception) {
		log.log(LogService.LOG_WARNING, message, exception);
	}

	/**
	 * 
	 * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
	 */
	public void serviceChanged(final ServiceEvent event) {
		switch (event.getType()) {
		case ServiceEvent.REGISTERED:
			log = (LogService) context.getService(event.getServiceReference());
			return;
		case ServiceEvent.UNREGISTERING:
			log = new NullPatternLogService();
		default:
		}
	}
	
	// if no LogService is present, we use a dummy log
	private class NullPatternLogService implements LogService {

		public void log(int level, String message) {
			if(level == LogService.LOG_ERROR || level == LogService.LOG_WARNING) {
				System.err.println(message);
			} else {
				System.out.println(message);
			}
		}

		public void log(int level, String message, Throwable exception) {
			log(level, message + " " + exception.toString());
		}

		public void log(ServiceReference sr, int level, String message) {
			log(null, level, message);
		}

		public void log(ServiceReference sr, int level, String message, Throwable t) {
			log(null, level, message, t);
		}

	}
}

Back to the top