Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8f126f02bb3c259f210e0f1cb04c0739332e768 (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
/*******************************************************************************
 *  Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr)
 *  7 Colonel Roche 31077 Toulouse - France
 *  
 *  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
 *  
 *  Initial Contributors:
 *      Thierry Monteil : Project manager, technical co-manager
 *      Mahdi Ben Alaya : Technical co-manager
 *      Samir Medjiah : Technical co-manager
 *      Khalil Drira : Strategy expert
 *      Guillaume Garzone : Developer
 *      François Aïssaoui : Developer
 *
 *   New contributors :
 *******************************************************************************/
package org.eclipse.om2m.binding.mqtt;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.om2m.binding.mqtt.util.DataMapperRegistry;
import org.eclipse.om2m.binding.service.RestClientService;
import org.eclipse.om2m.core.service.CseService;
import org.eclipse.om2m.datamapping.service.DataMapperService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

public class Activator implements BundleActivator {

	private static BundleContext context;

	static BundleContext getContext() {
		return context;
	}

	private static final Log LOGGER = LogFactory.getLog(Activator.class);

	/** {@link DataMapperServiceTracker} reference */
	private static ServiceTracker<DataMapperService, DataMapperService> dataMapperServiceTracker;
	/** {@link CseService} reference */
	private static ServiceTracker<CseService, CseService> cseServiceTracker;

	/** MQTT Request Handler that connects to the MQTT Broker */
	private static MqttRequestHandler mqttRequestHandler;

	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		
		// Listening on Cse Service
		cseServiceTracker = new ServiceTracker<CseService, CseService>(
				bundleContext, CseService.class,
				new CseServiceTrackerCustomizer());
		cseServiceTracker.open();

		// Listening on DataMapper Service
		dataMapperServiceTracker = new ServiceTracker<DataMapperService, DataMapperService>(
				bundleContext, DataMapperService.class,
				new DataMapperServiceTracker());
		dataMapperServiceTracker.open();
		
		// Registering RestClientService of MQTT
		getContext().registerService(RestClientService.class, new MqttRestClient(), null);
	}

	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
		if (cseServiceTracker != null) {
			cseServiceTracker.close();
			cseServiceTracker = null;
		}
		if (dataMapperServiceTracker != null) {
			dataMapperServiceTracker.close();
			dataMapperServiceTracker = null;
		}
		if (mqttRequestHandler != null){
			mqttRequestHandler.close();
			mqttRequestHandler = null;
		}
	}

	private static class CseServiceTrackerCustomizer implements
			ServiceTrackerCustomizer<CseService, CseService> {

		@Override
		public CseService addingService(ServiceReference<CseService> reference) {
			if (reference == null) {
				return null;
			}
			Object service = Activator.getContext().getService(reference);
			if (service != null && service instanceof CseService) {
				LOGGER.debug("New CseService discovered");
				CseService cse = (CseService) service;
				MqttRequestHandler.setCseService(cse);
				if (mqttRequestHandler == null) {
					new Thread() {
						public void run() {
							LOGGER.info("Creating MQTT Request Handler");
							mqttRequestHandler = new MqttRequestHandler();
						};
					}.start();
				}
				return cse;
			}
			return null;
		}

		@Override
		public void modifiedService(ServiceReference<CseService> reference,
				CseService service) {
			if (service != null) {
				LOGGER.info("CseService modified");
				MqttRequestHandler.setCseService(service);
			}
		}

		@Override
		public void removedService(ServiceReference<CseService> reference,
				CseService service) {
			MqttRequestHandler.setCseService(null);
		}

	}

	private static class DataMapperServiceTracker implements
			ServiceTrackerCustomizer<DataMapperService, DataMapperService> {

		@Override
		public DataMapperService addingService(
				ServiceReference<DataMapperService> reference) {
			if (reference == null) {
				return null;
			}
			Object service = Activator.getContext().getService(reference);
			if (service != null && service instanceof DataMapperService) {
				DataMapperService dms = (DataMapperService) service;
				LOGGER.debug("New DataMapper Service discovered: "
						+ dms.getServiceDataType());
				DataMapperRegistry.register(dms);
				return dms;
			}
			return null;
		}

		@Override
		public void modifiedService(
				ServiceReference<DataMapperService> reference,
				DataMapperService service) {
			if (service != null) {
				DataMapperRegistry.register(service);
			}
		}

		@Override
		public void removedService(
				ServiceReference<DataMapperService> reference,
				DataMapperService service) {
			if (service != null) {
				DataMapperRegistry.remove(service);
			}
		}

	}

}

Back to the top