aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r--src/org/traccar/database/ConnectionManager.java26
-rw-r--r--src/org/traccar/database/DataManager.java34
-rw-r--r--src/org/traccar/database/IdentityManager.java4
-rw-r--r--src/org/traccar/database/PermissionsManager.java22
-rw-r--r--src/org/traccar/database/QueryBuilder.java42
5 files changed, 64 insertions, 64 deletions
diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java
index 1aa0dbc57..c34428af5 100644
--- a/src/org/traccar/database/ConnectionManager.java
+++ b/src/org/traccar/database/ConnectionManager.java
@@ -34,7 +34,7 @@ public class ConnectionManager {
private final Map<Long, ActiveDevice> activeDevices = new HashMap<>();
private final Map<Long, Position> positions = new HashMap<>();
private final Map<Long, Set<DataCacheListener>> listeners = new HashMap<>();
-
+
public ConnectionManager(DataManager dataManager) {
if (dataManager != null) {
try {
@@ -50,7 +50,7 @@ public class ConnectionManager {
public void setActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) {
activeDevices.put(deviceId, new ActiveDevice(deviceId, protocol, channel, remoteAddress));
}
-
+
public void removeActiveDevice(Channel channel) {
for (ActiveDevice activeDevice : activeDevices.values()) {
if (activeDevice.getChannel() == channel) {
@@ -73,52 +73,52 @@ public class ConnectionManager {
}
}
}
-
+
public Position getLastPosition(long deviceId) {
return positions.get(deviceId);
}
-
+
public synchronized Collection<Position> getInitialState(Collection<Long> devices) {
-
+
List<Position> result = new LinkedList<>();
-
+
for (long device : devices) {
if (positions.containsKey(device)) {
result.add(positions.get(device));
}
}
-
+
return result;
}
-
+
public static interface DataCacheListener {
public void onUpdate(Position position);
}
-
+
public void addListener(Collection<Long> devices, DataCacheListener listener) {
for (long deviceId : devices) {
addListener(deviceId, listener);
}
}
-
+
public synchronized void addListener(long deviceId, DataCacheListener listener) {
if (!listeners.containsKey(deviceId)) {
listeners.put(deviceId, new HashSet<DataCacheListener>());
}
listeners.get(deviceId).add(listener);
}
-
+
public void removeListener(Collection<Long> devices, DataCacheListener listener) {
for (long deviceId : devices) {
removeListener(deviceId, listener);
}
}
-
+
public synchronized void removeListener(long deviceId, DataCacheListener listener) {
if (!listeners.containsKey(deviceId)) {
listeners.put(deviceId, new HashSet<DataCacheListener>());
}
listeners.get(deviceId).remove(listener);
}
-
+
}
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 27d03ca08..053eb9814 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -47,11 +47,11 @@ import org.traccar.web.JsonConverter;
public class DataManager implements IdentityManager {
private static final long DEFAULT_REFRESH_DELAY = 300;
-
+
private final Config config;
-
+
private DataSource dataSource;
-
+
private final Map<Long, Device> devicesById = new HashMap<>();
private final Map<String, Device> devicesByUniqueId = new HashMap<>();
private long devicesLastUpdate;
@@ -65,13 +65,13 @@ public class DataManager implements IdentityManager {
devicesRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000;
}
-
+
public DataSource getDataSource() {
return dataSource;
}
private void initDatabase() throws Exception {
-
+
String jndiName = config.getString("database.jndi");
if (jndiName != null) {
@@ -110,7 +110,7 @@ public class DataManager implements IdentityManager {
dataSource = ds;
}
}
-
+
@Override
public Device getDeviceById(long id) {
return devicesById.get(id);
@@ -132,7 +132,7 @@ public class DataManager implements IdentityManager {
return devicesByUniqueId.get(uniqueId);
}
-
+
private String getQuery(String key) {
String query = config.getString(key);
if (query == null) {
@@ -158,10 +158,10 @@ public class DataManager implements IdentityManager {
}
}
if (exist) {
-
+
String schemaVersionQuery = getQuery("database.selectSchemaVersion");
if (schemaVersionQuery != null) {
-
+
Schema schema = QueryBuilder.create(dataSource, schemaVersionQuery).executeQuerySingle(new Schema());
int version = 0;
@@ -174,7 +174,7 @@ public class DataManager implements IdentityManager {
throw new RuntimeException();
}
}
-
+
return;
}
@@ -196,7 +196,7 @@ public class DataManager implements IdentityManager {
mockData(admin.getId());
}
}
-
+
private void mockData(long userId) {
if (config.getBoolean("database.mock")) {
try {
@@ -224,7 +224,7 @@ public class DataManager implements IdentityManager {
position.setLatitude(-36.8932371);
position.setLongitude(174.7743053);
addPosition(position);
-
+
updateLatestPosition(position);
} catch (SQLException error) {
@@ -256,7 +256,7 @@ public class DataManager implements IdentityManager {
.setObject(user)
.executeUpdate());
}
-
+
public void updateUser(User user) throws SQLException {
QueryBuilder.create(dataSource, getQuery("database.updateUser"))
.setObject(user)
@@ -289,26 +289,26 @@ public class DataManager implements IdentityManager {
.setLong("userId", userId)
.executeQuery(new Device());
}
-
+
public void addDevice(Device device) throws SQLException {
device.setId(QueryBuilder.create(dataSource, getQuery("database.insertDevice"), true)
.setObject(device)
.executeUpdate());
}
-
+
public void updateDevice(Device device) throws SQLException {
QueryBuilder.create(dataSource, getQuery("database.updateDevice"))
.setObject(device)
.executeUpdate();
}
-
+
public void removeDevice(Device device) throws SQLException {
QueryBuilder.create(dataSource, getQuery("database.deleteDevice"))
.setObject(device)
.executeUpdate();
AsyncServlet.sessionRefreshDevice(device.getId());
}
-
+
public void linkDevice(long userId, long deviceId) throws SQLException {
QueryBuilder.create(dataSource, getQuery("database.linkDevice"))
.setLong("userId", userId)
diff --git a/src/org/traccar/database/IdentityManager.java b/src/org/traccar/database/IdentityManager.java
index 84f246363..b24bf79ed 100644
--- a/src/org/traccar/database/IdentityManager.java
+++ b/src/org/traccar/database/IdentityManager.java
@@ -18,9 +18,9 @@ package org.traccar.database;
import org.traccar.model.Device;
public interface IdentityManager {
-
+
public Device getDeviceById(long id);
-
+
public Device getDeviceByUniqueId(String uniqueId) throws Exception;
}
diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java
index cfbbb9a39..a38a29c32 100644
--- a/src/org/traccar/database/PermissionsManager.java
+++ b/src/org/traccar/database/PermissionsManager.java
@@ -26,25 +26,25 @@ import org.traccar.model.Permission;
import org.traccar.model.User;
public class PermissionsManager {
-
+
private final DataManager dataManager;
-
+
private final Map<Long, User> users = new HashMap<>();
-
+
private final Map<Long, Set<Long>> permissions = new HashMap<>();
-
+
private Set<Long> getNotNull(long userId) {
if (!permissions.containsKey(userId)) {
permissions.put(userId, new HashSet<Long>());
}
return permissions.get(userId);
}
-
+
public PermissionsManager(DataManager dataManager) {
this.dataManager = dataManager;
refresh();
}
-
+
public final void refresh() {
users.clear();
permissions.clear();
@@ -59,27 +59,27 @@ public class PermissionsManager {
Log.warning(error);
}
}
-
+
public void checkAdmin(long userId) throws SecurityException {
if (!users.containsKey(userId) || !users.get(userId).getAdmin()) {
throw new SecurityException("Admin access required");
}
}
-
+
public void checkUser(long userId, long otherUserId) throws SecurityException {
if (userId != otherUserId) {
checkAdmin(userId);
}
}
-
+
public Collection<Long> allowedDevices(long userId) {
return getNotNull(userId);
}
-
+
public void checkDevice(long userId, long deviceId) throws SecurityException {
if (!getNotNull(userId).contains(deviceId)) {
throw new SecurityException("Device access denied");
}
}
-
+
}
diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java
index cee27a413..7bec25ee5 100644
--- a/src/org/traccar/database/QueryBuilder.java
+++ b/src/org/traccar/database/QueryBuilder.java
@@ -42,13 +42,13 @@ import org.traccar.model.Factory;
import org.traccar.model.MiscFormatter;
public class QueryBuilder {
-
+
private final Map<String, List<Integer>> indexMap = new HashMap<>();
private Connection connection;
private PreparedStatement statement;
private final String query;
private final boolean returnGeneratedKeys;
-
+
private QueryBuilder(DataSource dataSource, String query, boolean returnGeneratedKeys) throws SQLException {
this.query = query;
this.returnGeneratedKeys = returnGeneratedKeys;
@@ -67,7 +67,7 @@ public class QueryBuilder {
}
}
}
-
+
private static String parse(String query, Map<String, List<Integer>> paramMap) {
int length = query.length();
@@ -135,7 +135,7 @@ public class QueryBuilder {
public static QueryBuilder create(DataSource dataSource, String query, boolean returnGeneratedKeys) throws SQLException {
return new QueryBuilder(dataSource, query, returnGeneratedKeys);
}
-
+
private List<Integer> indexes(String name) {
name = name.toLowerCase();
List<Integer> result = indexMap.get(name);
@@ -144,7 +144,7 @@ public class QueryBuilder {
}
return result;
}
-
+
public QueryBuilder setBoolean(String name, boolean value) throws SQLException {
for (int i : indexes(name)) {
try {
@@ -157,7 +157,7 @@ public class QueryBuilder {
}
return this;
}
-
+
public QueryBuilder setInteger(String name, int value) throws SQLException {
for (int i : indexes(name)) {
try {
@@ -170,7 +170,7 @@ public class QueryBuilder {
}
return this;
}
-
+
public QueryBuilder setLong(String name, long value) throws SQLException {
for (int i : indexes(name)) {
try {
@@ -183,7 +183,7 @@ public class QueryBuilder {
}
return this;
}
-
+
public QueryBuilder setDouble(String name, double value) throws SQLException {
for (int i : indexes(name)) {
try {
@@ -196,7 +196,7 @@ public class QueryBuilder {
}
return this;
}
-
+
public QueryBuilder setString(String name, String value) throws SQLException {
for (int i : indexes(name)) {
try {
@@ -213,7 +213,7 @@ public class QueryBuilder {
}
return this;
}
-
+
public QueryBuilder setDate(String name, Date value) throws SQLException {
for (int i : indexes(name)) {
try {
@@ -230,11 +230,11 @@ public class QueryBuilder {
}
return this;
}
-
+
public QueryBuilder setObject(Object object) throws SQLException {
-
+
Method[] methods = object.getClass().getMethods();
-
+
for (Method method : methods) {
if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
String name = method.getName().substring(3);
@@ -262,14 +262,14 @@ public class QueryBuilder {
}
}
}
-
+
return this;
}
-
+
private interface ResultSetProcessor<T> {
public void process(T object, ResultSet resultSet) throws SQLException;
}
-
+
public <T extends Factory> T executeQuerySingle(T prototype) throws SQLException {
Collection<T> result = executeQuery(prototype);
if (!result.isEmpty()) {
@@ -278,12 +278,12 @@ public class QueryBuilder {
return null;
}
}
-
+
public <T extends Factory> Collection<T> executeQuery(T prototype) throws SQLException {
List<T> result = new LinkedList<>();
-
+
if (query != null) {
-
+
try {
try (ResultSet resultSet = statement.executeQuery()) {
@@ -406,7 +406,7 @@ public class QueryBuilder {
}
public long executeUpdate() throws SQLException {
-
+
if (query != null) {
try {
statement.executeUpdate();
@@ -423,5 +423,5 @@ public class QueryBuilder {
}
return 0;
}
-
+
}