aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/database
diff options
context:
space:
mode:
authorjcardus <joaquim.cardeira@gmail.com>2020-03-25 17:25:19 +0000
committerjcardus <joaquim.cardeira@gmail.com>2020-03-25 17:25:19 +0000
commitfeac85bad4004fb3e316a88285626e12840398d2 (patch)
treed268a79d63b9d1f27b6e433fedc90de7b427b172 /src/main/java/org/traccar/database
parent7b669e3181f637f06b31b55bd930584c7b08e897 (diff)
parentf3d465abe7f255ec44e976caade642e4c2fd7598 (diff)
downloadtrackermap-server-feac85bad4004fb3e316a88285626e12840398d2.tar.gz
trackermap-server-feac85bad4004fb3e316a88285626e12840398d2.tar.bz2
trackermap-server-feac85bad4004fb3e316a88285626e12840398d2.zip
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'src/main/java/org/traccar/database')
-rw-r--r--src/main/java/org/traccar/database/ActiveDevice.java13
-rw-r--r--src/main/java/org/traccar/database/BaseObjectManager.java60
-rw-r--r--src/main/java/org/traccar/database/CommandsManager.java48
-rw-r--r--src/main/java/org/traccar/database/ConnectionManager.java4
-rw-r--r--src/main/java/org/traccar/database/DeviceManager.java118
-rw-r--r--src/main/java/org/traccar/database/DriversManager.java51
-rw-r--r--src/main/java/org/traccar/database/ExtendedObjectManager.java78
-rw-r--r--src/main/java/org/traccar/database/GroupsManager.java5
-rw-r--r--src/main/java/org/traccar/database/LdapProvider.java36
-rw-r--r--src/main/java/org/traccar/database/SimpleObjectManager.java30
-rw-r--r--src/main/java/org/traccar/database/UsersManager.java6
11 files changed, 330 insertions, 119 deletions
diff --git a/src/main/java/org/traccar/database/ActiveDevice.java b/src/main/java/org/traccar/database/ActiveDevice.java
index 207fc454b..e3ece6ad9 100644
--- a/src/main/java/org/traccar/database/ActiveDevice.java
+++ b/src/main/java/org/traccar/database/ActiveDevice.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2020 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.
@@ -16,7 +16,8 @@
package org.traccar.database;
import io.netty.channel.Channel;
-import org.traccar.NetworkMessage;
+import io.netty.handler.codec.http.HttpRequestDecoder;
+import org.traccar.BasePipelineFactory;
import org.traccar.Protocol;
import org.traccar.model.Command;
@@ -44,12 +45,12 @@ public class ActiveDevice {
return deviceId;
}
- public void sendCommand(Command command) {
- protocol.sendDataCommand(this, command);
+ public boolean supportsLiveCommands() {
+ return BasePipelineFactory.getHandler(channel.pipeline(), HttpRequestDecoder.class) == null;
}
- public void write(Object message) {
- channel.writeAndFlush(new NetworkMessage(message, remoteAddress));
+ public void sendCommand(Command command) {
+ protocol.sendDataCommand(channel, remoteAddress, command);
}
}
diff --git a/src/main/java/org/traccar/database/BaseObjectManager.java b/src/main/java/org/traccar/database/BaseObjectManager.java
index 8bf9ef860..e274e5aba 100644
--- a/src/main/java/org/traccar/database/BaseObjectManager.java
+++ b/src/main/java/org/traccar/database/BaseObjectManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,6 +23,8 @@ import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,6 +34,8 @@ public class BaseObjectManager<T extends BaseModel> {
private static final Logger LOGGER = LoggerFactory.getLogger(BaseObjectManager.class);
+ private final ReadWriteLock lock = new ReentrantReadWriteLock();
+
private final DataManager dataManager;
private Map<Long, T> items;
@@ -43,6 +47,22 @@ public class BaseObjectManager<T extends BaseModel> {
refreshItems();
}
+ protected final void readLock() {
+ lock.readLock().lock();
+ }
+
+ protected final void readUnlock() {
+ lock.readLock().unlock();
+ }
+
+ protected final void writeLock() {
+ lock.writeLock().lock();
+ }
+
+ protected final void writeUnlock() {
+ lock.writeLock().unlock();
+ }
+
protected final DataManager getDataManager() {
return dataManager;
}
@@ -52,12 +72,18 @@ public class BaseObjectManager<T extends BaseModel> {
}
public T getById(long itemId) {
- return items.get(itemId);
+ try {
+ readLock();
+ return items.get(itemId);
+ } finally {
+ readUnlock();
+ }
}
public void refreshItems() {
if (dataManager != null) {
try {
+ writeLock();
Collection<T> databaseItems = dataManager.getObjects(baseClass);
if (items == null) {
items = new ConcurrentHashMap<>(databaseItems.size());
@@ -78,12 +104,19 @@ public class BaseObjectManager<T extends BaseModel> {
}
} catch (SQLException error) {
LOGGER.warn("Error refreshing items", error);
+ } finally {
+ writeUnlock();
}
}
}
protected void addNewItem(T item) {
- items.put(item.getId(), item);
+ try {
+ writeLock();
+ items.put(item.getId(), item);
+ } finally {
+ writeUnlock();
+ }
}
public void addItem(T item) throws SQLException {
@@ -92,7 +125,12 @@ public class BaseObjectManager<T extends BaseModel> {
}
protected void updateCachedItem(T item) {
- items.put(item.getId(), item);
+ try {
+ writeLock();
+ items.put(item.getId(), item);
+ } finally {
+ writeUnlock();
+ }
}
public void updateItem(T item) throws SQLException {
@@ -101,7 +139,12 @@ public class BaseObjectManager<T extends BaseModel> {
}
protected void removeCachedItem(long itemId) {
- items.remove(itemId);
+ try {
+ writeLock();
+ items.remove(itemId);
+ } finally {
+ writeUnlock();
+ }
}
public void removeItem(long itemId) throws SQLException {
@@ -121,7 +164,12 @@ public class BaseObjectManager<T extends BaseModel> {
}
public Set<Long> getAllItems() {
- return items.keySet();
+ try {
+ readLock();
+ return items.keySet();
+ } finally {
+ readUnlock();
+ }
}
}
diff --git a/src/main/java/org/traccar/database/CommandsManager.java b/src/main/java/org/traccar/database/CommandsManager.java
index dc9512d9e..485402807 100644
--- a/src/main/java/org/traccar/database/CommandsManager.java
+++ b/src/main/java/org/traccar/database/CommandsManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -76,7 +76,12 @@ public class CommandsManager extends ExtendedObjectManager<Command> {
} else {
ActiveDevice activeDevice = Context.getConnectionManager().getActiveDevice(deviceId);
if (activeDevice != null) {
- activeDevice.sendCommand(command);
+ if (activeDevice.supportsLiveCommands()) {
+ activeDevice.sendCommand(command);
+ } else {
+ getDeviceQueue(deviceId).add(command);
+ return false;
+ }
} else if (!queueing) {
throw new RuntimeException("Device is not online");
} else {
@@ -142,21 +147,46 @@ public class CommandsManager extends ExtendedObjectManager<Command> {
}
private Queue<Command> getDeviceQueue(long deviceId) {
- if (!deviceQueues.containsKey(deviceId)) {
- deviceQueues.put(deviceId, new ConcurrentLinkedQueue<Command>());
+ Queue<Command> deviceQueue;
+ try {
+ readLock();
+ deviceQueue = deviceQueues.get(deviceId);
+ } finally {
+ readUnlock();
+ }
+ if (deviceQueue != null) {
+ return deviceQueue;
+ } else {
+ try {
+ writeLock();
+ return deviceQueues.computeIfAbsent(deviceId, key -> new ConcurrentLinkedQueue<>());
+ } finally {
+ writeUnlock();
+ }
}
- return deviceQueues.get(deviceId);
}
- public void sendQueuedCommands(ActiveDevice activeDevice) {
- Queue<Command> deviceQueue = deviceQueues.get(activeDevice.getDeviceId());
+ public Collection<Command> readQueuedCommands(long deviceId) {
+ return readQueuedCommands(deviceId, Integer.MAX_VALUE);
+ }
+
+ public Collection<Command> readQueuedCommands(long deviceId, int count) {
+ Queue<Command> deviceQueue;
+ try {
+ readLock();
+ deviceQueue = deviceQueues.get(deviceId);
+ } finally {
+ readUnlock();
+ }
+ Collection<Command> result = new ArrayList<>();
if (deviceQueue != null) {
Command command = deviceQueue.poll();
- while (command != null) {
- activeDevice.sendCommand(command);
+ while (command != null && result.size() < count) {
+ result.add(command);
command = deviceQueue.poll();
}
}
+ return result;
}
}
diff --git a/src/main/java/org/traccar/database/ConnectionManager.java b/src/main/java/org/traccar/database/ConnectionManager.java
index dd0071143..4d43bc71b 100644
--- a/src/main/java/org/traccar/database/ConnectionManager.java
+++ b/src/main/java/org/traccar/database/ConnectionManager.java
@@ -139,10 +139,6 @@ public class ConnectionManager {
}
updateDevice(device);
-
- if (status.equals(Device.STATUS_ONLINE) && !oldStatus.equals(Device.STATUS_ONLINE)) {
- Context.getCommandsManager().sendQueuedCommands(getActiveDevice(deviceId));
- }
}
public Map<Event, Position> updateDeviceState(long deviceId) {
diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java
index fa95adeb2..fe17f7ced 100644
--- a/src/main/java/org/traccar/database/DeviceManager.java
+++ b/src/main/java/org/traccar/database/DeviceManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 - 2019 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2020 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.
@@ -58,11 +58,16 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
public DeviceManager(DataManager dataManager) {
super(dataManager, Device.class);
this.config = Context.getConfig();
- if (devicesByPhone == null) {
- devicesByPhone = new ConcurrentHashMap<>();
- }
- if (devicesByUniqueId == null) {
- devicesByUniqueId = new ConcurrentHashMap<>();
+ try {
+ writeLock();
+ if (devicesByPhone == null) {
+ devicesByPhone = new ConcurrentHashMap<>();
+ }
+ if (devicesByUniqueId == null) {
+ devicesByUniqueId = new ConcurrentHashMap<>();
+ }
+ } finally {
+ writeUnlock();
}
dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000;
lookupGroupsAttribute = config.getBoolean("deviceManager.lookupGroupsAttribute");
@@ -108,11 +113,20 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
@Override
public Device getByUniqueId(String uniqueId) throws SQLException {
- boolean forceUpdate = !devicesByUniqueId.containsKey(uniqueId) && !config.getBoolean("database.ignoreUnknown");
-
+ boolean forceUpdate;
+ try {
+ readLock();
+ forceUpdate = !devicesByUniqueId.containsKey(uniqueId) && !config.getBoolean("database.ignoreUnknown");
+ } finally {
+ readUnlock();
+ }
updateDeviceCache(forceUpdate);
-
- return devicesByUniqueId.get(uniqueId);
+ try {
+ readLock();
+ return devicesByUniqueId.get(uniqueId);
+ } finally {
+ readUnlock();
+ }
}
@Override
@@ -134,7 +148,12 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
}
public Device getDeviceByPhone(String phone) {
- return devicesByPhone.get(phone);
+ try {
+ readLock();
+ return devicesByPhone.get(phone);
+ } finally {
+ readUnlock();
+ }
}
@Override
@@ -176,8 +195,7 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
}
public Set<Long> getAllManagedItems(long userId) {
- Set<Long> result = new HashSet<>();
- result.addAll(getAllUserItems(userId));
+ Set<Long> result = new HashSet<>(getAllUserItems(userId));
for (long managedUserId : Context.getUsersManager().getUserItems(userId)) {
result.addAll(getAllUserItems(managedUserId));
}
@@ -186,34 +204,68 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
@Override
public Set<Long> getManagedItems(long userId) {
- Set<Long> result = new HashSet<>();
- result.addAll(getUserItems(userId));
+ Set<Long> result = new HashSet<>(getUserItems(userId));
for (long managedUserId : Context.getUsersManager().getUserItems(userId)) {
result.addAll(getUserItems(managedUserId));
}
return result;
}
- private void putUniqueDeviceId(Device device) {
- if (devicesByUniqueId == null) {
- devicesByUniqueId = new ConcurrentHashMap<>(getAllItems().size());
+ private void addByUniqueId(Device device) {
+ try {
+ writeLock();
+ if (devicesByUniqueId == null) {
+ devicesByUniqueId = new ConcurrentHashMap<>();
+ }
+ devicesByUniqueId.put(device.getUniqueId(), device);
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ private void removeByUniqueId(String deviceUniqueId) {
+ try {
+ writeLock();
+ if (devicesByUniqueId != null) {
+ devicesByUniqueId.remove(deviceUniqueId);
+ }
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ private void addByPhone(Device device) {
+ try {
+ writeLock();
+ if (devicesByPhone == null) {
+ devicesByPhone = new ConcurrentHashMap<>();
+ }
+ devicesByPhone.put(device.getPhone(), device);
+ } finally {
+ writeUnlock();
}
- devicesByUniqueId.put(device.getUniqueId(), device);
}
- private void putPhone(Device device) {
- if (devicesByPhone == null) {
- devicesByPhone = new ConcurrentHashMap<>(getAllItems().size());
+ private void removeByPhone(String phone) {
+ if (phone == null || phone.isEmpty()) {
+ return;
+ }
+ try {
+ writeLock();
+ if (devicesByPhone != null) {
+ devicesByPhone.remove(phone);
+ }
+ } finally {
+ writeUnlock();
}
- devicesByPhone.put(device.getPhone(), device);
}
@Override
protected void addNewItem(Device device) {
super.addNewItem(device);
- putUniqueDeviceId(device);
+ addByUniqueId(device);
if (device.getPhone() != null && !device.getPhone().isEmpty()) {
- putPhone(device);
+ addByPhone(device);
}
if (Context.getGeofenceManager() != null) {
Position lastPosition = getLastPosition(device.getId());
@@ -234,18 +286,16 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
cachedDevice.setDisabled(device.getDisabled());
cachedDevice.setAttributes(device.getAttributes());
if (!device.getUniqueId().equals(cachedDevice.getUniqueId())) {
- devicesByUniqueId.remove(cachedDevice.getUniqueId());
+ removeByUniqueId(cachedDevice.getUniqueId());
cachedDevice.setUniqueId(device.getUniqueId());
- putUniqueDeviceId(cachedDevice);
+ addByUniqueId(cachedDevice);
}
if (device.getPhone() != null && !device.getPhone().isEmpty()
&& !device.getPhone().equals(cachedDevice.getPhone())) {
String phone = cachedDevice.getPhone();
- if (phone != null && !phone.isEmpty()) {
- devicesByPhone.remove(phone);
- }
+ removeByPhone(phone);
cachedDevice.setPhone(device.getPhone());
- putPhone(cachedDevice);
+ addByPhone(cachedDevice);
}
}
@@ -256,10 +306,8 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
String deviceUniqueId = cachedDevice.getUniqueId();
String phone = cachedDevice.getPhone();
super.removeCachedItem(deviceId);
- devicesByUniqueId.remove(deviceUniqueId);
- if (phone != null && !phone.isEmpty()) {
- devicesByPhone.remove(phone);
- }
+ removeByUniqueId(deviceUniqueId);
+ removeByPhone(phone);
}
positions.remove(deviceId);
}
diff --git a/src/main/java/org/traccar/database/DriversManager.java b/src/main/java/org/traccar/database/DriversManager.java
index 930951460..d111cd643 100644
--- a/src/main/java/org/traccar/database/DriversManager.java
+++ b/src/main/java/org/traccar/database/DriversManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,22 +27,44 @@ public class DriversManager extends ExtendedObjectManager<Driver> {
public DriversManager(DataManager dataManager) {
super(dataManager, Driver.class);
- if (driversByUniqueId == null) {
- driversByUniqueId = new ConcurrentHashMap<>();
+ try {
+ writeLock();
+ if (driversByUniqueId == null) {
+ driversByUniqueId = new ConcurrentHashMap<>();
+ }
+ } finally {
+ writeUnlock();
}
}
- private void putUniqueDriverId(Driver driver) {
- if (driversByUniqueId == null) {
- driversByUniqueId = new ConcurrentHashMap<>(getAllItems().size());
+ private void addByUniqueId(Driver driver) {
+ try {
+ writeLock();
+ if (driversByUniqueId == null) {
+ driversByUniqueId = new ConcurrentHashMap<>();
+ }
+ driversByUniqueId.put(driver.getUniqueId(), driver);
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ private void removeByUniqueId(String driverUniqueId) {
+ try {
+ writeLock();
+ if (driversByUniqueId == null) {
+ driversByUniqueId = new ConcurrentHashMap<>();
+ }
+ driversByUniqueId.remove(driverUniqueId);
+ } finally {
+ writeUnlock();
}
- driversByUniqueId.put(driver.getUniqueId(), driver);
}
@Override
protected void addNewItem(Driver driver) {
super.addNewItem(driver);
- putUniqueDriverId(driver);
+ addByUniqueId(driver);
}
@Override
@@ -50,9 +72,9 @@ public class DriversManager extends ExtendedObjectManager<Driver> {
Driver cachedDriver = getById(driver.getId());
cachedDriver.setName(driver.getName());
if (!driver.getUniqueId().equals(cachedDriver.getUniqueId())) {
- driversByUniqueId.remove(cachedDriver.getUniqueId());
+ removeByUniqueId(cachedDriver.getUniqueId());
cachedDriver.setUniqueId(driver.getUniqueId());
- putUniqueDriverId(cachedDriver);
+ addByUniqueId(cachedDriver);
}
cachedDriver.setAttributes(driver.getAttributes());
}
@@ -63,11 +85,16 @@ public class DriversManager extends ExtendedObjectManager<Driver> {
if (cachedDriver != null) {
String driverUniqueId = cachedDriver.getUniqueId();
super.removeCachedItem(driverId);
- driversByUniqueId.remove(driverUniqueId);
+ removeByUniqueId(driverUniqueId);
}
}
public Driver getDriverByUniqueId(String uniqueId) {
- return driversByUniqueId.get(uniqueId);
+ try {
+ readLock();
+ return driversByUniqueId.get(uniqueId);
+ } finally {
+ readUnlock();
+ }
}
}
diff --git a/src/main/java/org/traccar/database/ExtendedObjectManager.java b/src/main/java/org/traccar/database/ExtendedObjectManager.java
index ceb85b537..93e5820fb 100644
--- a/src/main/java/org/traccar/database/ExtendedObjectManager.java
+++ b/src/main/java/org/traccar/database/ExtendedObjectManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -45,24 +45,45 @@ public abstract class ExtendedObjectManager<T extends BaseModel> extends SimpleO
}
public final Set<Long> getGroupItems(long groupId) {
- if (!groupItems.containsKey(groupId)) {
- groupItems.put(groupId, new HashSet<Long>());
+ try {
+ readLock();
+ Set<Long> result = groupItems.get(groupId);
+ if (result != null) {
+ return new HashSet<>(result);
+ } else {
+ return new HashSet<>();
+ }
+ } finally {
+ readUnlock();
}
- return groupItems.get(groupId);
}
public final Set<Long> getDeviceItems(long deviceId) {
- if (!deviceItems.containsKey(deviceId)) {
- deviceItems.put(deviceId, new HashSet<Long>());
+ try {
+ readLock();
+ Set<Long> result = deviceItems.get(deviceId);
+ if (result != null) {
+ return new HashSet<>(result);
+ } else {
+ return new HashSet<>();
+ }
+ } finally {
+ readUnlock();
}
- return deviceItems.get(deviceId);
}
public Set<Long> getAllDeviceItems(long deviceId) {
- if (!deviceItemsWithGroups.containsKey(deviceId)) {
- deviceItemsWithGroups.put(deviceId, new HashSet<Long>());
+ try {
+ readLock();
+ Set<Long> result = deviceItemsWithGroups.get(deviceId);
+ if (result != null) {
+ return new HashSet<>(result);
+ } else {
+ return new HashSet<>();
+ }
+ } finally {
+ readUnlock();
}
- return deviceItemsWithGroups.get(deviceId);
}
@Override
@@ -74,41 +95,48 @@ public abstract class ExtendedObjectManager<T extends BaseModel> extends SimpleO
public void refreshExtendedPermissions() {
if (getDataManager() != null) {
try {
-
Collection<Permission> databaseGroupPermissions =
getDataManager().getPermissions(Group.class, getBaseClass());
- groupItems.clear();
- for (Permission groupPermission : databaseGroupPermissions) {
- getGroupItems(groupPermission.getOwnerId()).add(groupPermission.getPropertyId());
- }
-
Collection<Permission> databaseDevicePermissions =
getDataManager().getPermissions(Device.class, getBaseClass());
+ writeLock();
+
+ groupItems.clear();
deviceItems.clear();
deviceItemsWithGroups.clear();
+ for (Permission groupPermission : databaseGroupPermissions) {
+ groupItems
+ .computeIfAbsent(groupPermission.getOwnerId(), key -> new HashSet<>())
+ .add(groupPermission.getPropertyId());
+ }
+
for (Permission devicePermission : databaseDevicePermissions) {
- getDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
- getAllDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
+ deviceItems
+ .computeIfAbsent(devicePermission.getOwnerId(), key -> new HashSet<>())
+ .add(devicePermission.getPropertyId());
+ deviceItemsWithGroups
+ .computeIfAbsent(devicePermission.getOwnerId(), key -> new HashSet<>())
+ .add(devicePermission.getPropertyId());
}
for (Device device : Context.getDeviceManager().getAllDevices()) {
long groupId = device.getGroupId();
- while (groupId != 0) {
- getAllDeviceItems(device.getId()).addAll(getGroupItems(groupId));
+ while (groupId > 0) {
+ deviceItemsWithGroups
+ .computeIfAbsent(device.getId(), key -> new HashSet<>())
+ .addAll(groupItems.getOrDefault(groupId, new HashSet<>()));
Group group = Context.getGroupsManager().getById(groupId);
- if (group != null) {
- groupId = group.getGroupId();
- } else {
- groupId = 0;
- }
+ groupId = group != null ? group.getGroupId() : 0;
}
}
} catch (SQLException | ClassNotFoundException error) {
LOGGER.warn("Refresh permissions error", error);
+ } finally {
+ writeUnlock();
}
}
}
diff --git a/src/main/java/org/traccar/database/GroupsManager.java b/src/main/java/org/traccar/database/GroupsManager.java
index d8404c614..81f1968aa 100644
--- a/src/main/java/org/traccar/database/GroupsManager.java
+++ b/src/main/java/org/traccar/database/GroupsManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -95,8 +95,7 @@ public class GroupsManager extends BaseObjectManager<Group> implements Managable
@Override
public Set<Long> getManagedItems(long userId) {
- Set<Long> result = new HashSet<>();
- result.addAll(getUserItems(userId));
+ Set<Long> result = getUserItems(userId);
for (long managedUserId : Context.getUsersManager().getUserItems(userId)) {
result.addAll(getUserItems(managedUserId));
}
diff --git a/src/main/java/org/traccar/database/LdapProvider.java b/src/main/java/org/traccar/database/LdapProvider.java
index d8b5c9f52..a8220ea8e 100644
--- a/src/main/java/org/traccar/database/LdapProvider.java
+++ b/src/main/java/org/traccar/database/LdapProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 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.
@@ -81,7 +81,7 @@ public class LdapProvider {
if (this.adminFilter != null) {
try {
InitialDirContext context = initContext();
- String searchString = adminFilter.replace(":login", accountName);
+ String searchString = adminFilter.replace(":login", encodeForLdap(accountName));
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = context.search(searchBase, searchString, searchControls);
@@ -107,7 +107,7 @@ public class LdapProvider {
private SearchResult lookupUser(String accountName) throws NamingException {
InitialDirContext context = initContext();
- String searchString = searchFilter.replace(":login", accountName);
+ String searchString = searchFilter.replace(":login", encodeForLdap(accountName));
SearchControls searchControls = new SearchControls();
String[] attributeFilter = {idAttribute, nameAttribute, mailAttribute};
@@ -176,4 +176,34 @@ public class LdapProvider {
return false;
}
+ public String encodeForLdap(String input) {
+ if (input == null) {
+ return null;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < input.length(); i++) {
+ char c = input.charAt(i);
+ switch (c) {
+ case '\\':
+ sb.append("\\5c");
+ break;
+ case '*':
+ sb.append("\\2a");
+ break;
+ case '(':
+ sb.append("\\28");
+ break;
+ case ')':
+ sb.append("\\29");
+ break;
+ case '\0':
+ sb.append("\\00");
+ break;
+ default:
+ sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
}
diff --git a/src/main/java/org/traccar/database/SimpleObjectManager.java b/src/main/java/org/traccar/database/SimpleObjectManager.java
index 15dda4520..eb8284d4e 100644
--- a/src/main/java/org/traccar/database/SimpleObjectManager.java
+++ b/src/main/java/org/traccar/database/SimpleObjectManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -42,16 +42,22 @@ public abstract class SimpleObjectManager<T extends BaseModel> extends BaseObjec
@Override
public final Set<Long> getUserItems(long userId) {
- if (!userItems.containsKey(userId)) {
- userItems.put(userId, new HashSet<Long>());
+ try {
+ readLock();
+ Set<Long> result = userItems.get(userId);
+ if (result != null) {
+ return new HashSet<>(result);
+ } else {
+ return new HashSet<>();
+ }
+ } finally {
+ readUnlock();
}
- return userItems.get(userId);
}
@Override
public Set<Long> getManagedItems(long userId) {
- Set<Long> result = new HashSet<>();
- result.addAll(getUserItems(userId));
+ Set<Long> result = getUserItems(userId);
for (long managedUserId : Context.getUsersManager().getUserItems(userId)) {
result.addAll(getUserItems(managedUserId));
}
@@ -71,16 +77,16 @@ public abstract class SimpleObjectManager<T extends BaseModel> extends BaseObjec
public final void refreshUserItems() {
if (getDataManager() != null) {
try {
- if (userItems != null) {
- userItems.clear();
- } else {
- userItems = new ConcurrentHashMap<>();
- }
+ writeLock();
+ userItems = new ConcurrentHashMap<>();
for (Permission permission : getDataManager().getPermissions(User.class, getBaseClass())) {
- getUserItems(permission.getOwnerId()).add(permission.getPropertyId());
+ Set<Long> items = userItems.computeIfAbsent(permission.getOwnerId(), key -> new HashSet<>());
+ items.add(permission.getPropertyId());
}
} catch (SQLException | ClassNotFoundException error) {
LOGGER.warn("Error getting permissions", error);
+ } finally {
+ writeUnlock();
}
}
}
diff --git a/src/main/java/org/traccar/database/UsersManager.java b/src/main/java/org/traccar/database/UsersManager.java
index 576a9e6c7..b741a85b6 100644
--- a/src/main/java/org/traccar/database/UsersManager.java
+++ b/src/main/java/org/traccar/database/UsersManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,7 +16,6 @@
*/
package org.traccar.database;
-import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -73,8 +72,7 @@ public class UsersManager extends SimpleObjectManager<User> {
@Override
public Set<Long> getManagedItems(long userId) {
- Set<Long> result = new HashSet<>();
- result.addAll(getUserItems(userId));
+ Set<Long> result = getUserItems(userId);
result.add(userId);
return result;
}