diff options
10 files changed, 59 insertions, 48 deletions
diff --git a/src/main/java/org/traccar/BasePipelineFactory.java b/src/main/java/org/traccar/BasePipelineFactory.java index 1f383f211..3eb7011a1 100644 --- a/src/main/java/org/traccar/BasePipelineFactory.java +++ b/src/main/java/org/traccar/BasePipelineFactory.java @@ -22,6 +22,7 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOutboundHandler; import io.netty.channel.ChannelPipeline; import io.netty.handler.timeout.IdleStateHandler; +import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.handler.ComputedAttributesHandler; import org.traccar.handler.CopyAttributesHandler; @@ -58,12 +59,12 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { private final String protocol; private int timeout; - public BasePipelineFactory(TrackerConnector connector, String protocol) { + public BasePipelineFactory(TrackerConnector connector, Config config, String protocol) { this.connector = connector; this.protocol = protocol; - timeout = Context.getConfig().getInteger(Keys.PROTOCOL_TIMEOUT.withPrefix(protocol)); + timeout = config.getInteger(Keys.PROTOCOL_TIMEOUT.withPrefix(protocol)); if (timeout == 0) { - timeout = Context.getConfig().getInteger(Keys.SERVER_TIMEOUT); + timeout = config.getInteger(Keys.SERVER_TIMEOUT); } } diff --git a/src/main/java/org/traccar/BaseProtocolDecoder.java b/src/main/java/org/traccar/BaseProtocolDecoder.java index 5b3f129de..cbcb429b3 100644 --- a/src/main/java/org/traccar/BaseProtocolDecoder.java +++ b/src/main/java/org/traccar/BaseProtocolDecoder.java @@ -17,7 +17,6 @@ package org.traccar; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; -import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.database.CommandsManager; import org.traccar.database.IdentityManager; @@ -46,7 +45,6 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { private final Protocol protocol; - private Config config; private IdentityManager identityManager; private ConnectionManager connectionManager; private StatisticsManager statisticsManager; @@ -57,22 +55,6 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { this.protocol = protocol; } - /** - * Method called when config is initialized. - */ - protected void init() { - } - - public Config getConfig() { - return config; - } - - @Inject - public void setConfig(Config config) { - this.config = config; - init(); - } - public IdentityManager getIdentityManager() { return identityManager; } @@ -115,7 +97,7 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } public String getServer(Channel channel, char delimiter) { - String server = config.getString(Keys.PROTOCOL_SERVER.withPrefix(getProtocolName())); + String server = getConfig().getString(Keys.PROTOCOL_SERVER.withPrefix(getProtocolName())); if (server == null && channel != null) { InetSocketAddress address = (InetSocketAddress) channel.localAddress(); server = address.getAddress().getHostAddress() + ":" + address.getPort(); @@ -124,7 +106,7 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } protected double convertSpeed(double value, String defaultUnits) { - switch (config.getString(getProtocolName() + ".speed", defaultUnits)) { + switch (getConfig().getString(getProtocolName() + ".speed", defaultUnits)) { case "kmh": return UnitsConverter.knotsFromKph(value); case "mps": @@ -222,7 +204,7 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { @Override protected Object handleEmptyMessage(Channel channel, SocketAddress remoteAddress, Object msg) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (config.getBoolean(Keys.DATABASE_SAVE_EMPTY) && deviceSession != null) { + if (getConfig().getBoolean(Keys.DATABASE_SAVE_EMPTY) && deviceSession != null) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); diff --git a/src/main/java/org/traccar/ExtendedObjectDecoder.java b/src/main/java/org/traccar/ExtendedObjectDecoder.java index 46720da52..f79a36c85 100644 --- a/src/main/java/org/traccar/ExtendedObjectDecoder.java +++ b/src/main/java/org/traccar/ExtendedObjectDecoder.java @@ -21,18 +21,38 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.ReferenceCountUtil; +import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.helper.DataConverter; import org.traccar.model.Position; +import javax.inject.Inject; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; import java.util.Collection; public abstract class ExtendedObjectDecoder extends ChannelInboundHandlerAdapter { + private Config config; + + public Config getConfig() { + return config; + } + + @Inject + public void setConfig(Config config) { + this.config = config; + init(); + } + + /** + * Method called when config is initialized. + */ + protected void init() { + } + private void saveOriginal(Object decodedMessage, Object originalMessage) { - if (Context.getConfig().getBoolean(Keys.DATABASE_SAVE_ORIGINAL) && decodedMessage instanceof Position) { + if (getConfig().getBoolean(Keys.DATABASE_SAVE_ORIGINAL) && decodedMessage instanceof Position) { Position position = (Position) decodedMessage; if (originalMessage instanceof ByteBuf) { ByteBuf buf = (ByteBuf) originalMessage; diff --git a/src/main/java/org/traccar/MainEventHandler.java b/src/main/java/org/traccar/MainEventHandler.java index d4a0fae6c..7fff2e13f 100644 --- a/src/main/java/org/traccar/MainEventHandler.java +++ b/src/main/java/org/traccar/MainEventHandler.java @@ -23,6 +23,7 @@ import io.netty.handler.codec.http.HttpRequestDecoder; import io.netty.handler.timeout.IdleStateEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.database.StatisticsManager; import org.traccar.helper.DateUtil; @@ -48,14 +49,13 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter { private final ConnectionManager connectionManager; @Inject - public MainEventHandler(ConnectionManager connectionManager) { + public MainEventHandler(Config config, ConnectionManager connectionManager) { this.connectionManager = connectionManager; - String connectionlessProtocolList = Context.getConfig().getString(Keys.STATUS_IGNORE_OFFLINE); + String connectionlessProtocolList = config.getString(Keys.STATUS_IGNORE_OFFLINE); if (connectionlessProtocolList != null) { connectionlessProtocols.addAll(Arrays.asList(connectionlessProtocolList.split("[, ]"))); } - logAttributes.addAll(Arrays.asList( - Context.getConfig().getString(Keys.LOGGER_ATTRIBUTES).split("[, ]"))); + logAttributes.addAll(Arrays.asList(config.getString(Keys.LOGGER_ATTRIBUTES).split("[, ]"))); } @Override diff --git a/src/main/java/org/traccar/ServerManager.java b/src/main/java/org/traccar/ServerManager.java index f4f6e1ba4..ffb15d8ca 100644 --- a/src/main/java/org/traccar/ServerManager.java +++ b/src/main/java/org/traccar/ServerManager.java @@ -18,6 +18,7 @@ package org.traccar; import com.google.inject.Injector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.helper.ClassScanner; @@ -41,9 +42,10 @@ public class ServerManager implements LifecycleObject { private final Map<String, BaseProtocol> protocolList = new ConcurrentHashMap<>(); @Inject - public ServerManager(Injector injector) throws IOException, URISyntaxException, ReflectiveOperationException { + public ServerManager( + Injector injector, Config config) throws IOException, URISyntaxException, ReflectiveOperationException { for (Class<?> protocolClass : ClassScanner.findSubclasses(BaseProtocol.class, "org.traccar.protocol")) { - if (Context.getConfig().hasKey(Keys.PROTOCOL_PORT.withPrefix(BaseProtocol.nameFromClass(protocolClass)))) { + if (config.hasKey(Keys.PROTOCOL_PORT.withPrefix(BaseProtocol.nameFromClass(protocolClass)))) { BaseProtocol protocol = (BaseProtocol) protocolClass.getDeclaredConstructor().newInstance(); injector.injectMembers(protocol); connectorList.addAll(protocol.getConnectorList()); diff --git a/src/main/java/org/traccar/TrackerClient.java b/src/main/java/org/traccar/TrackerClient.java index 12971849c..5a3c38212 100644 --- a/src/main/java/org/traccar/TrackerClient.java +++ b/src/main/java/org/traccar/TrackerClient.java @@ -54,14 +54,15 @@ public abstract class TrackerClient implements TrackerConnector { } public TrackerClient(String protocol) { + Config config = Context.getConfig(); - secure = Context.getConfig().getBoolean(Keys.PROTOCOL_SSL.withPrefix(protocol)); - interval = Context.getConfig().getLong(Keys.PROTOCOL_INTERVAL.withPrefix(protocol)); - address = Context.getConfig().getString(Keys.PROTOCOL_ADDRESS.withPrefix(protocol)); - port = Context.getConfig().getInteger(Keys.PROTOCOL_PORT.withPrefix(protocol), secure ? 443 : 80); - devices = Context.getConfig().getString(Keys.PROTOCOL_DEVICES.withPrefix(protocol)).split("[, ]"); + secure = config.getBoolean(Keys.PROTOCOL_SSL.withPrefix(protocol)); + interval = config.getLong(Keys.PROTOCOL_INTERVAL.withPrefix(protocol)); + address = config.getString(Keys.PROTOCOL_ADDRESS.withPrefix(protocol)); + port = config.getInteger(Keys.PROTOCOL_PORT.withPrefix(protocol), secure ? 443 : 80); + devices = config.getString(Keys.PROTOCOL_DEVICES.withPrefix(protocol)).split("[, ]"); - BasePipelineFactory pipelineFactory = new BasePipelineFactory(this, protocol) { + BasePipelineFactory pipelineFactory = new BasePipelineFactory(this, config, protocol) { @Override protected void addTransportHandlers(PipelineBuilder pipeline) { try { @@ -78,7 +79,7 @@ public abstract class TrackerClient implements TrackerConnector { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { try { - TrackerClient.this.addProtocolHandlers(pipeline, Context.getConfig()); + TrackerClient.this.addProtocolHandlers(pipeline, config); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/src/main/java/org/traccar/TrackerServer.java b/src/main/java/org/traccar/TrackerServer.java index b279d3479..dd83ca6b0 100644 --- a/src/main/java/org/traccar/TrackerServer.java +++ b/src/main/java/org/traccar/TrackerServer.java @@ -58,11 +58,13 @@ public abstract class TrackerServer implements TrackerConnector { public TrackerServer(boolean datagram, String protocol) { this.datagram = datagram; - secure = Context.getConfig().getBoolean(Keys.PROTOCOL_SSL.withPrefix(protocol)); - address = Context.getConfig().getString(Keys.PROTOCOL_ADDRESS.withPrefix(protocol)); - port = Context.getConfig().getInteger(Keys.PROTOCOL_PORT.withPrefix(protocol)); + Config config = Context.getConfig(); - BasePipelineFactory pipelineFactory = new BasePipelineFactory(this, protocol) { + secure = config.getBoolean(Keys.PROTOCOL_SSL.withPrefix(protocol)); + address = config.getString(Keys.PROTOCOL_ADDRESS.withPrefix(protocol)); + port = config.getInteger(Keys.PROTOCOL_PORT.withPrefix(protocol)); + + BasePipelineFactory pipelineFactory = new BasePipelineFactory(this, config, protocol) { @Override protected void addTransportHandlers(PipelineBuilder pipeline) { try { @@ -77,7 +79,7 @@ public abstract class TrackerServer implements TrackerConnector { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { - TrackerServer.this.addProtocolHandlers(pipeline, Context.getConfig()); + TrackerServer.this.addProtocolHandlers(pipeline, config); } }; diff --git a/src/main/java/org/traccar/api/resource/AttributeResource.java b/src/main/java/org/traccar/api/resource/AttributeResource.java index ab7e43add..43d8a7ccd 100644 --- a/src/main/java/org/traccar/api/resource/AttributeResource.java +++ b/src/main/java/org/traccar/api/resource/AttributeResource.java @@ -16,6 +16,7 @@ */ package org.traccar.api.resource; +import javax.inject.Inject; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.POST; @@ -29,6 +30,7 @@ import javax.ws.rs.core.Response; import org.traccar.Context; import org.traccar.api.ExtendedObjectResource; +import org.traccar.config.Config; import org.traccar.model.Attribute; import org.traccar.model.Device; import org.traccar.model.Position; @@ -43,6 +45,9 @@ import org.traccar.storage.query.Request; @Consumes(MediaType.APPLICATION_JSON) public class AttributeResource extends ExtendedObjectResource<Attribute> { + @Inject + private Config config; + public AttributeResource() { super(Attribute.class); } @@ -57,7 +62,7 @@ public class AttributeResource extends ExtendedObjectResource<Attribute> { new Columns.All(), new Condition.LatestPositions(deviceId))); - Object result = new ComputedAttributesHandler(Context.getConfig(), Context.getIdentityManager(), null) + Object result = new ComputedAttributesHandler(config, Context.getIdentityManager(), null) .computeAttribute(entity, position); if (result != null) { switch (entity.getType()) { diff --git a/src/main/java/org/traccar/handler/GeocoderHandler.java b/src/main/java/org/traccar/handler/GeocoderHandler.java index 614cf97d6..075bdf815 100644 --- a/src/main/java/org/traccar/handler/GeocoderHandler.java +++ b/src/main/java/org/traccar/handler/GeocoderHandler.java @@ -20,7 +20,6 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.traccar.Context; import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.database.IdentityManager; @@ -42,7 +41,7 @@ public class GeocoderHandler extends ChannelInboundHandlerAdapter { Config config, Geocoder geocoder, IdentityManager identityManager) { this.geocoder = geocoder; this.identityManager = identityManager; - ignorePositions = Context.getConfig().getBoolean(Keys.GEOCODER_IGNORE_POSITIONS); + ignorePositions = config.getBoolean(Keys.GEOCODER_IGNORE_POSITIONS); processInvalidPositions = config.getBoolean(Keys.GEOCODER_PROCESS_INVALID_POSITIONS); geocoderReuseDistance = config.getInteger(Keys.GEOCODER_REUSE_DISTANCE, 0); } diff --git a/src/main/java/org/traccar/handler/TimeHandler.java b/src/main/java/org/traccar/handler/TimeHandler.java index c7e5e6e5c..439c076c7 100644 --- a/src/main/java/org/traccar/handler/TimeHandler.java +++ b/src/main/java/org/traccar/handler/TimeHandler.java @@ -19,7 +19,6 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import org.traccar.BaseProtocolDecoder; -import org.traccar.Context; import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.model.Position; @@ -44,7 +43,7 @@ public class TimeHandler extends ChannelInboundHandlerAdapter { } else { useServerTime = false; } - String protocolList = Context.getConfig().getString(Keys.TIME_PROTOCOLS); + String protocolList = config.getString(Keys.TIME_PROTOCOLS); if (protocolList != null) { protocols = new HashSet<>(Arrays.asList(protocolList.split("[, ]"))); } else { |