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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* Copyright 2018 - 2022 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr353.JSR353Module;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Scopes;
import com.google.inject.name.Names;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timer;
import org.apache.velocity.app.VelocityEngine;
import org.eclipse.jetty.util.URIUtil;
import org.traccar.broadcast.BroadcastService;
import org.traccar.broadcast.MulticastBroadcastService;
import org.traccar.broadcast.NullBroadcastService;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.database.LdapProvider;
import org.traccar.database.StatisticsManager;
import org.traccar.forward.EventForwarder;
import org.traccar.forward.EventForwarderJson;
import org.traccar.forward.EventForwarderKafka;
import org.traccar.forward.EventForwarderMqtt;
import org.traccar.forward.PositionForwarder;
import org.traccar.forward.PositionForwarderJson;
import org.traccar.forward.PositionForwarderKafka;
import org.traccar.forward.PositionForwarderRedis;
import org.traccar.forward.PositionForwarderUrl;
import org.traccar.geocoder.AddressFormat;
import org.traccar.geocoder.BanGeocoder;
import org.traccar.geocoder.BingMapsGeocoder;
import org.traccar.geocoder.FactualGeocoder;
import org.traccar.geocoder.GeoapifyGeocoder;
import org.traccar.geocoder.GeocodeFarmGeocoder;
import org.traccar.geocoder.GeocodeXyzGeocoder;
import org.traccar.geocoder.Geocoder;
import org.traccar.geocoder.GisgraphyGeocoder;
import org.traccar.geocoder.GoogleGeocoder;
import org.traccar.geocoder.HereGeocoder;
import org.traccar.geocoder.LocationIqGeocoder;
import org.traccar.geocoder.MapQuestGeocoder;
import org.traccar.geocoder.MapTilerGeocoder;
import org.traccar.geocoder.MapboxGeocoder;
import org.traccar.geocoder.MapmyIndiaGeocoder;
import org.traccar.geocoder.NominatimGeocoder;
import org.traccar.geocoder.OpenCageGeocoder;
import org.traccar.geocoder.PositionStackGeocoder;
import org.traccar.geocoder.TestGeocoder;
import org.traccar.geocoder.TomTomGeocoder;
import org.traccar.geolocation.GeolocationProvider;
import org.traccar.geolocation.GoogleGeolocationProvider;
import org.traccar.geolocation.MozillaGeolocationProvider;
import org.traccar.geolocation.OpenCellIdGeolocationProvider;
import org.traccar.geolocation.UnwiredGeolocationProvider;
import org.traccar.handler.GeocoderHandler;
import org.traccar.handler.GeolocationHandler;
import org.traccar.handler.SpeedLimitHandler;
import org.traccar.helper.ObjectMapperContextResolver;
import org.traccar.helper.SanitizerModule;
import org.traccar.mail.LogMailManager;
import org.traccar.mail.MailManager;
import org.traccar.mail.SmtpMailManager;
import org.traccar.session.cache.CacheManager;
import org.traccar.sms.HttpSmsClient;
import org.traccar.sms.SmsManager;
import org.traccar.sms.SnsSmsClient;
import org.traccar.speedlimit.OverpassSpeedLimitProvider;
import org.traccar.speedlimit.SpeedLimitProvider;
import org.traccar.storage.DatabaseStorage;
import org.traccar.storage.Storage;
import org.traccar.web.WebServer;
import javax.annotation.Nullable;
import javax.inject.Singleton;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
public class MainModule extends AbstractModule {
private final String configFile;
public MainModule(String configFile) {
this.configFile = configFile;
}
@Override
protected void configure() {
bindConstant().annotatedWith(Names.named("configFile")).to(configFile);
bind(Config.class).asEagerSingleton();
bind(Storage.class).to(DatabaseStorage.class).in(Scopes.SINGLETON);
bind(Timer.class).to(HashedWheelTimer.class).in(Scopes.SINGLETON);
}
@Singleton
@Provides
public static ObjectMapper provideObjectMapper(Config config) {
ObjectMapper objectMapper = new ObjectMapper();
if (config.getBoolean(Keys.WEB_SANITIZE)) {
objectMapper.registerModule(new SanitizerModule());
}
objectMapper.registerModule(new JSR353Module());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return objectMapper;
}
@Singleton
@Provides
public static Client provideClient(ObjectMapperContextResolver objectMapperContextResolver) {
return ClientBuilder.newClient().register(objectMapperContextResolver);
}
@Singleton
@Provides
public static SmsManager provideSmsManager(Config config, Client client) {
if (config.hasKey(Keys.SMS_HTTP_URL)) {
return new HttpSmsClient(config, client);
} else if (config.hasKey(Keys.SMS_AWS_REGION)) {
return new SnsSmsClient(config);
}
return null;
}
@Singleton
@Provides
public static MailManager provideMailManager(Config config, StatisticsManager statisticsManager) {
if (config.getBoolean(Keys.MAIL_DEBUG)) {
return new LogMailManager();
} else {
return new SmtpMailManager(config, statisticsManager);
}
}
@Singleton
@Provides
public static LdapProvider provideLdapProvider(Config config) {
if (config.hasKey(Keys.LDAP_URL)) {
return new LdapProvider(config);
}
return null;
}
@Provides
public static WebServer provideWebServer(Injector injector, Config config) {
if (config.hasKey(Keys.WEB_PORT)) {
return new WebServer(injector, config);
}
return null;
}
@Singleton
@Provides
public static Geocoder provideGeocoder(Config config, Client client, StatisticsManager statisticsManager) {
if (config.getBoolean(Keys.GEOCODER_ENABLE)) {
String type = config.getString(Keys.GEOCODER_TYPE, "google");
String url = config.getString(Keys.GEOCODER_URL);
String id = config.getString(Keys.GEOCODER_ID);
String key = config.getString(Keys.GEOCODER_KEY);
String language = config.getString(Keys.GEOCODER_LANGUAGE);
String formatString = config.getString(Keys.GEOCODER_FORMAT);
AddressFormat addressFormat = formatString != null ? new AddressFormat(formatString) : new AddressFormat();
int cacheSize = config.getInteger(Keys.GEOCODER_CACHE_SIZE);
Geocoder geocoder;
switch (type) {
case "test":
geocoder = new TestGeocoder();
break;
case "nominatim":
geocoder = new NominatimGeocoder(client, url, key, language, cacheSize, addressFormat);
break;
case "locationiq":
geocoder = new LocationIqGeocoder(client, url, key, language, cacheSize, addressFormat);
break;
case "gisgraphy":
geocoder = new GisgraphyGeocoder(client, url, cacheSize, addressFormat);
break;
case "mapquest":
geocoder = new MapQuestGeocoder(client, url, key, cacheSize, addressFormat);
break;
case "opencage":
geocoder = new OpenCageGeocoder(client, url, key, language, cacheSize, addressFormat);
break;
case "bingmaps":
geocoder = new BingMapsGeocoder(client, url, key, cacheSize, addressFormat);
break;
case "factual":
geocoder = new FactualGeocoder(client, url, key, cacheSize, addressFormat);
break;
case "geocodefarm":
geocoder = new GeocodeFarmGeocoder(client, key, language, cacheSize, addressFormat);
break;
case "geocodexyz":
geocoder = new GeocodeXyzGeocoder(client, key, cacheSize, addressFormat);
break;
case "ban":
geocoder = new BanGeocoder(client, cacheSize, addressFormat);
break;
case "here":
geocoder = new HereGeocoder(client, url, id, key, language, cacheSize, addressFormat);
break;
case "mapmyindia":
geocoder = new MapmyIndiaGeocoder(client, url, key, cacheSize, addressFormat);
break;
case "tomtom":
geocoder = new TomTomGeocoder(client, url, key, cacheSize, addressFormat);
break;
case "positionstack":
geocoder = new PositionStackGeocoder(client, key, cacheSize, addressFormat);
break;
case "mapbox":
geocoder = new MapboxGeocoder(client, key, cacheSize, addressFormat);
break;
case "maptiler":
geocoder = new MapTilerGeocoder(client, key, cacheSize, addressFormat);
break;
case "geoapify":
geocoder = new GeoapifyGeocoder(client, key, language, cacheSize, addressFormat);
break;
default:
geocoder = new GoogleGeocoder(client, key, language, cacheSize, addressFormat);
break;
}
geocoder.setStatisticsManager(statisticsManager);
return geocoder;
}
return null;
}
@Singleton
@Provides
public static GeolocationProvider provideGeolocationProvider(Config config, Client client) {
if (config.getBoolean(Keys.GEOLOCATION_ENABLE)) {
String type = config.getString(Keys.GEOLOCATION_TYPE, "mozilla");
String url = config.getString(Keys.GEOLOCATION_URL);
String key = config.getString(Keys.GEOLOCATION_KEY);
switch (type) {
case "google":
return new GoogleGeolocationProvider(client, key);
case "opencellid":
return new OpenCellIdGeolocationProvider(client, url, key);
case "unwired":
return new UnwiredGeolocationProvider(client, url, key);
default:
return new MozillaGeolocationProvider(client, key);
}
}
return null;
}
@Singleton
@Provides
public static SpeedLimitProvider provideSpeedLimitProvider(Config config, Client client) {
if (config.getBoolean(Keys.SPEED_LIMIT_ENABLE)) {
String type = config.getString(Keys.SPEED_LIMIT_TYPE, "overpass");
String url = config.getString(Keys.SPEED_LIMIT_URL);
switch (type) {
case "overpass":
default:
return new OverpassSpeedLimitProvider(client, url);
}
}
return null;
}
@Singleton
@Provides
public static GeolocationHandler provideGeolocationHandler(
Config config, @Nullable GeolocationProvider geolocationProvider, CacheManager cacheManager,
StatisticsManager statisticsManager) {
if (geolocationProvider != null) {
return new GeolocationHandler(config, geolocationProvider, cacheManager, statisticsManager);
}
return null;
}
@Singleton
@Provides
public static GeocoderHandler provideGeocoderHandler(
Config config, @Nullable Geocoder geocoder, CacheManager cacheManager) {
if (geocoder != null) {
return new GeocoderHandler(config, geocoder, cacheManager);
}
return null;
}
@Singleton
@Provides
public static SpeedLimitHandler provideSpeedLimitHandler(@Nullable SpeedLimitProvider speedLimitProvider) {
if (speedLimitProvider != null) {
return new SpeedLimitHandler(speedLimitProvider);
}
return null;
}
@Singleton
@Provides
public static BroadcastService provideBroadcastService(
Config config, ObjectMapper objectMapper) throws IOException {
if (config.hasKey(Keys.BROADCAST_ADDRESS)) {
return new MulticastBroadcastService(config, objectMapper);
}
return new NullBroadcastService();
}
@Singleton
@Provides
public static EventForwarder provideEventForwarder(Config config, Client client, ObjectMapper objectMapper) {
if (config.hasKey(Keys.EVENT_FORWARD_URL)) {
String forwardType = config.getString(Keys.EVENT_FORWARD_TYPE);
switch (forwardType) {
case "kafka":
return new EventForwarderKafka(config, objectMapper);
case "mqtt":
return new EventForwarderMqtt(config, objectMapper);
default:
return new EventForwarderJson(config, client);
}
}
return null;
}
@Singleton
@Provides
public static PositionForwarder providePositionForwarder(Config config, Client client, ObjectMapper objectMapper) {
if (config.hasKey(Keys.FORWARD_URL)) {
switch (config.getString(Keys.FORWARD_TYPE)) {
case "json":
return new PositionForwarderJson(config, client, objectMapper);
case "kafka":
return new PositionForwarderKafka(config, objectMapper);
case "redis":
return new PositionForwarderRedis(config, objectMapper);
default:
return new PositionForwarderUrl(config, client, objectMapper);
}
}
return null;
}
@Singleton
@Provides
public static VelocityEngine provideVelocityEngine(Config config) {
Properties properties = new Properties();
properties.setProperty("resource.loader.file.path", config.getString(Keys.TEMPLATES_ROOT) + "/");
if (config.hasKey(Keys.WEB_URL)) {
properties.setProperty("web.url", config.getString(Keys.WEB_URL).replaceAll("/$", ""));
} else {
String address;
try {
address = config.getString(Keys.WEB_ADDRESS, InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
address = "localhost";
}
String url = URIUtil.newURI("http", address, config.getInteger(Keys.WEB_PORT), "", "");
properties.setProperty("web.url", url);
}
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init(properties);
return velocityEngine;
}
}
|