aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-08-24 14:55:06 +1200
committerGitHub <noreply@github.com>2016-08-24 14:55:06 +1200
commitea0bdea88003f46fd6f6164d5c45e33d53f47c81 (patch)
treed8e51a10e56f7e916cc2bf7486d75c90feb5696e /src/org/traccar
parenta645eb8e2338f11e2c807045b1787a47cd1f1464 (diff)
parentf6484e9808f50aa997f028f9421b05aad6aed8bd (diff)
downloadtraccar-server-ea0bdea88003f46fd6f6164d5c45e33d53f47c81.tar.gz
traccar-server-ea0bdea88003f46fd6f6164d5c45e33d53f47c81.tar.bz2
traccar-server-ea0bdea88003f46fd6f6164d5c45e33d53f47c81.zip
Merge pull request #2242 from Abyss777/users_cache
Implement users cache
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/api/resource/DevicePermissionResource.java4
-rw-r--r--src/org/traccar/api/resource/DeviceResource.java4
-rw-r--r--src/org/traccar/api/resource/GroupPermissionResource.java4
-rw-r--r--src/org/traccar/api/resource/GroupResource.java4
-rw-r--r--src/org/traccar/api/resource/SessionResource.java4
-rw-r--r--src/org/traccar/api/resource/UserResource.java11
-rw-r--r--src/org/traccar/database/PermissionsManager.java54
-rw-r--r--src/org/traccar/notification/NotificationMail.java10
8 files changed, 66 insertions, 29 deletions
diff --git a/src/org/traccar/api/resource/DevicePermissionResource.java b/src/org/traccar/api/resource/DevicePermissionResource.java
index 7de050074..7faa1ab09 100644
--- a/src/org/traccar/api/resource/DevicePermissionResource.java
+++ b/src/org/traccar/api/resource/DevicePermissionResource.java
@@ -37,7 +37,7 @@ public class DevicePermissionResource extends BaseResource {
public Response add(DevicePermission entity) throws SQLException {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getDataManager().linkDevice(entity.getUserId(), entity.getDeviceId());
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().refreshPermissions();
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
}
@@ -48,7 +48,7 @@ public class DevicePermissionResource extends BaseResource {
public Response remove(DevicePermission entity) throws SQLException {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getDataManager().unlinkDevice(entity.getUserId(), entity.getDeviceId());
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().refreshPermissions();
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
}
diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java
index f48df7553..b12ab8c36 100644
--- a/src/org/traccar/api/resource/DeviceResource.java
+++ b/src/org/traccar/api/resource/DeviceResource.java
@@ -59,7 +59,7 @@ public class DeviceResource extends BaseResource {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getDeviceManager().addDevice(entity);
Context.getDataManager().linkDevice(getUserId(), entity.getId());
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().refreshPermissions();
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
}
@@ -84,7 +84,7 @@ public class DeviceResource extends BaseResource {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getPermissionsManager().checkDevice(getUserId(), id);
Context.getDeviceManager().removeDevice(id);
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().refreshPermissions();
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
}
diff --git a/src/org/traccar/api/resource/GroupPermissionResource.java b/src/org/traccar/api/resource/GroupPermissionResource.java
index 1fbf37a2b..94100362b 100644
--- a/src/org/traccar/api/resource/GroupPermissionResource.java
+++ b/src/org/traccar/api/resource/GroupPermissionResource.java
@@ -37,7 +37,7 @@ public class GroupPermissionResource extends BaseResource {
public Response add(GroupPermission entity) throws SQLException {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getDataManager().linkGroup(entity.getUserId(), entity.getGroupId());
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().refreshPermissions();
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
}
@@ -48,7 +48,7 @@ public class GroupPermissionResource extends BaseResource {
public Response remove(GroupPermission entity) throws SQLException {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getDataManager().unlinkGroup(entity.getUserId(), entity.getGroupId());
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().refreshPermissions();
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
}
diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java
index eec28e325..6b722ef6d 100644
--- a/src/org/traccar/api/resource/GroupResource.java
+++ b/src/org/traccar/api/resource/GroupResource.java
@@ -58,7 +58,7 @@ public class GroupResource extends BaseResource {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getDeviceManager().addGroup(entity);
Context.getDataManager().linkGroup(getUserId(), entity.getId());
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().refreshPermissions();
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
}
@@ -83,7 +83,7 @@ public class GroupResource extends BaseResource {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getPermissionsManager().checkGroup(getUserId(), id);
Context.getDeviceManager().removeGroup(id);
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().refreshPermissions();
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
}
diff --git a/src/org/traccar/api/resource/SessionResource.java b/src/org/traccar/api/resource/SessionResource.java
index 49670c1f9..deed70b37 100644
--- a/src/org/traccar/api/resource/SessionResource.java
+++ b/src/org/traccar/api/resource/SessionResource.java
@@ -73,7 +73,7 @@ public class SessionResource extends BaseResource {
}
if (userId != null) {
- return Context.getDataManager().getUser(userId);
+ return Context.getPermissionsManager().getUser(userId);
} else {
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build());
}
@@ -83,7 +83,7 @@ public class SessionResource extends BaseResource {
@POST
public User add(
@FormParam("email") String email, @FormParam("password") String password) throws SQLException {
- User user = Context.getDataManager().login(email, password);
+ User user = Context.getPermissionsManager().login(email, password);
if (user != null) {
request.getSession().setAttribute(USER_ID_KEY, user.getId());
return user;
diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java
index da72c7f47..2d187fe9d 100644
--- a/src/org/traccar/api/resource/UserResource.java
+++ b/src/org/traccar/api/resource/UserResource.java
@@ -41,7 +41,7 @@ public class UserResource extends BaseResource {
@GET
public Collection<User> get() throws SQLException {
Context.getPermissionsManager().checkAdmin(getUserId());
- return Context.getDataManager().getUsers();
+ return Context.getPermissionsManager().getUsers();
}
@PermitAll
@@ -50,8 +50,7 @@ public class UserResource extends BaseResource {
if (!Context.getPermissionsManager().isAdmin(getUserId())) {
Context.getPermissionsManager().checkRegistration(getUserId());
}
- Context.getDataManager().addUser(entity);
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().addUser(entity);
if (Context.getNotificationManager() != null) {
Context.getNotificationManager().refresh();
}
@@ -66,8 +65,7 @@ public class UserResource extends BaseResource {
} else {
Context.getPermissionsManager().checkUser(getUserId(), entity.getId());
}
- Context.getDataManager().updateUser(entity);
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().updateUser(entity);
if (Context.getNotificationManager() != null) {
Context.getNotificationManager().refresh();
}
@@ -78,8 +76,7 @@ public class UserResource extends BaseResource {
@DELETE
public Response remove(@PathParam("id") long id) throws SQLException {
Context.getPermissionsManager().checkUser(getUserId(), id);
- Context.getDataManager().removeUser(id);
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().removeUser(id);
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refreshUserGeofences();
}
diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java
index 3c2cc252b..f5fed978a 100644
--- a/src/org/traccar/database/PermissionsManager.java
+++ b/src/org/traccar/database/PermissionsManager.java
@@ -25,10 +25,12 @@ import org.traccar.model.Server;
import org.traccar.model.User;
import java.sql.SQLException;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
public class PermissionsManager {
@@ -36,7 +38,7 @@ public class PermissionsManager {
private volatile Server server;
- private final Map<Long, User> users = new HashMap<>();
+ private final Map<Long, User> users = new ConcurrentHashMap<>();
private final Map<Long, Set<Long>> groupPermissions = new HashMap<>();
private final Map<Long, Set<Long>> devicePermissions = new HashMap<>();
@@ -73,19 +75,26 @@ public class PermissionsManager {
public PermissionsManager(DataManager dataManager) {
this.dataManager = dataManager;
- refresh();
+ refreshUsers();
+ refreshPermissions();
}
- public final void refresh() {
+ public final void refreshUsers() {
users.clear();
- groupPermissions.clear();
- devicePermissions.clear();
try {
server = dataManager.getServer();
for (User user : dataManager.getUsers()) {
users.put(user.getId(), user);
}
+ } catch (SQLException error) {
+ Log.warning(error);
+ }
+ }
+ public final void refreshPermissions() {
+ groupPermissions.clear();
+ devicePermissions.clear();
+ try {
GroupTree groupTree = new GroupTree(Context.getDeviceManager().getAllGroups(),
Context.getDeviceManager().getAllDevices());
for (GroupPermission permission : dataManager.getGroupPermissions()) {
@@ -176,4 +185,39 @@ public class PermissionsManager {
this.server = server;
}
+ public Collection<User> getUsers() {
+ return users.values();
+ }
+
+ public User getUser(long userId) {
+ return users.get(userId);
+ }
+
+ public void addUser(User user) throws SQLException {
+ dataManager.addUser(user);
+ users.put(user.getId(), user);
+ refreshPermissions();
+ }
+
+ public void updateUser(User user) throws SQLException {
+ dataManager.updateUser(user);
+ users.put(user.getId(), user);
+ refreshPermissions();
+ }
+
+ public void removeUser(long userId) throws SQLException {
+ dataManager.removeUser(userId);
+ users.remove(userId);
+ refreshPermissions();
+ }
+
+ public User login(String email, String password) throws SQLException {
+ User user = dataManager.login(email, password);
+ if (user != null && users.get(user.getId()) != null) {
+ return users.get(user.getId());
+ } else {
+ return null;
+ }
+ }
+
}
diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java
index f9d42968f..f8449e7d2 100644
--- a/src/org/traccar/notification/NotificationMail.java
+++ b/src/org/traccar/notification/NotificationMail.java
@@ -15,7 +15,6 @@
*/
package org.traccar.notification;
-import java.sql.SQLException;
import java.util.Properties;
import javax.mail.Message;
@@ -27,7 +26,6 @@ import javax.mail.internet.MimeMessage;
import org.traccar.Config;
import org.traccar.Context;
-import org.traccar.database.DataManager;
import org.traccar.helper.Log;
import org.traccar.model.Event;
import org.traccar.model.Extensible;
@@ -106,14 +104,13 @@ public final class NotificationMail {
}
public static void sendMailSync(long userId, Event event, Position position) {
- DataManager dataManager = Context.getDataManager();
Properties mailServerProperties;
Session mailSession;
MimeMessage mailMessage;
try {
- User user = dataManager.getUser(userId);
+ User user = Context.getPermissionsManager().getUser(userId);
mailServerProperties = getConfigProperies();
if (!mailServerProperties.containsKey("mail.smtp.host")) {
@@ -130,8 +127,7 @@ public final class NotificationMail {
mailMessage.setFrom(new InternetAddress(mailServerProperties.getProperty("mail.smtp.from")));
}
- mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(
- Context.getDataManager().getUser(userId).getEmail()));
+ mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
mailMessage.setSubject(NotificationFormatter.formatTitle(userId, event, position));
mailMessage.setText(NotificationFormatter.formatMessage(userId, event, position));
@@ -142,7 +138,7 @@ public final class NotificationMail {
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
transport.close();
- } catch (MessagingException | SQLException error) {
+ } catch (MessagingException error) {
Log.warning(error);
}
}