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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/*
* Copyright 2018 - 2023 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.jsonp.JSONPModule;
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.traccar.broadcast.BroadcastService;
import org.traccar.broadcast.MulticastBroadcastService;
import org.traccar.broadcast.RedisBroadcastService;
import org.traccar.broadcast.NullBroadcastService;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.database.LdapProvider;
import org.traccar.database.OpenIdProvider;
import org.traccar.database.StatisticsManager;
import org.traccar.forward.EventForwarder;
import org.traccar.forward.EventForwarderJson;
import org.traccar.forward.EventForwarderAmqp;
import org.traccar.forward.EventForwarderKafka;
import org.traccar.forward.EventForwarderMqtt;
import org.traccar.forward.PositionForwarder;
import org.traccar.forward.PositionForwarderJson;
import org.traccar.forward.PositionForwarderAmqp;
import org.traccar.forward.PositionForwarderKafka;
import org.traccar.forward.PositionForwarderRedis;
import org.traccar.forward.PositionForwarderUrl;
import org.traccar.forward.PositionForwarderMqtt;
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.PlusCodesGeocoder;
import org.traccar.geocoder.TomTomGeocoder;
import org.traccar.geocoder.GeocodeJsonGeocoder;
import org.traccar.geolocation.GeolocationProvider;
import org.traccar.geolocation.GoogleGeolocationProvider;
import org.traccar.geolocation.OpenCellIdGeolocationProvider;
import org.traccar.geolocation.UnwiredGeolocationProvider;
import org.traccar.handler.CopyAttributesHandler;
import org.traccar.handler.FilterHandler;
import org.traccar.handler.GeocoderHandler;
import org.traccar.handler.GeolocationHandler;
import org.traccar.handler.SpeedLimitHandler;
import org.traccar.handler.TimeHandler;
import org.traccar.helper.ObjectMapperContextResolver;
import org.traccar.helper.WebHelper;
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.MemoryStorage;
import org.traccar.storage.Storage;
import org.traccar.web.WebServer;
import org.traccar.api.security.LoginService;
import jakarta.annotation.Nullable;
import jakarta.inject.Singleton;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
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(Timer.class).to(HashedWheelTimer.class).in(Scopes.SINGLETON);
}
@Singleton
@Provides
public static Storage provideStorage(Injector injector, Config config) {
if (config.getBoolean(Keys.DATABASE_MEMORY)) {
return injector.getInstance(MemoryStorage.class);
} else {
return injector.getInstance(DatabaseStorage.class);
}
}
@Singleton
@Provides
public static ObjectMapper provideObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JSONPModule());
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;
}
@Singleton
@Provides
public static OpenIdProvider provideOpenIDProvider(
Config config, LoginService loginService, ObjectMapper objectMapper
) throws InterruptedException, IOException, URISyntaxException {
if (config.hasKey(Keys.OPENID_CLIENT_ID)) {
return new OpenIdProvider(config, loginService, HttpClient.newHttpClient(), objectMapper);
}
return null;
}
@Provides
public static WebServer provideWebServer(Injector injector, Config config) {
if (config.getInteger(Keys.WEB_PORT) > 0) {
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);
String url = config.getString(Keys.GEOCODER_URL);
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 "pluscodes":
geocoder = new PlusCodesGeocoder();
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, 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;
case "geocodejson":
geocoder = new GeocodeJsonGeocoder(client, url, 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, "google");
String url = config.getString(Keys.GEOLOCATION_URL);
String key = config.getString(Keys.GEOLOCATION_KEY);
switch (type) {
case "opencellid":
return new OpenCellIdGeolocationProvider(client, url, key);
case "unwired":
return new UnwiredGeolocationProvider(client, url, key);
default:
return new GoogleGeolocationProvider(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(config, 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 CopyAttributesHandler provideCopyAttributesHandler(Config config, CacheManager cacheManager) {
if (config.getBoolean(Keys.PROCESSING_COPY_ATTRIBUTES_ENABLE)) {
return new CopyAttributesHandler(config, cacheManager);
}
return null;
}
@Singleton
@Provides
public static FilterHandler provideFilterHandler(
Config config, CacheManager cacheManager, Storage storage, StatisticsManager statisticsManager) {
if (config.getBoolean(Keys.FILTER_ENABLE)) {
return new FilterHandler(config, cacheManager, storage, statisticsManager);
}
return null;
}
@Singleton
@Provides
public static TimeHandler provideTimeHandler(Config config) {
if (config.hasKey(Keys.TIME_OVERRIDE)) {
return new TimeHandler(config);
}
return null;
}
@Singleton
@Provides
public static BroadcastService provideBroadcastService(
Config config, ObjectMapper objectMapper) throws IOException {
if (config.hasKey(Keys.BROADCAST_TYPE)) {
switch (config.getString(Keys.BROADCAST_TYPE)) {
case "multicast":
return new MulticastBroadcastService(config, objectMapper);
case "redis":
return new RedisBroadcastService(config, objectMapper);
default:
break;
}
}
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 "amqp":
return new EventForwarderAmqp(config, objectMapper);
case "kafka":
return new EventForwarderKafka(config, objectMapper);
case "mqtt":
return new EventForwarderMqtt(config, objectMapper);
case "json":
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 "amqp":
return new PositionForwarderAmqp(config, objectMapper);
case "kafka":
return new PositionForwarderKafka(config, objectMapper);
case "mqtt":
return new PositionForwarderMqtt(config, objectMapper);
case "redis":
return new PositionForwarderRedis(config, objectMapper);
case "url":
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) + "/");
properties.setProperty("web.url", WebHelper.retrieveWebUrl(config));
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init(properties);
return velocityEngine;
}
}
|