Files
wisemapping-open-source/wise-api/src/main/java/com/wisemapping/service/LockManagerImpl.java

144 lines
5.0 KiB
Java
Raw Normal View History

2012-09-30 17:15:01 -03:00
/*
2022-03-17 18:47:34 -03:00
* Copyright [2022] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* 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.
*/
2012-09-30 17:15:01 -03:00
package com.wisemapping.service;
import com.wisemapping.exceptions.AccessDeniedSecurityException;
import com.wisemapping.exceptions.LockException;
import com.wisemapping.model.CollaborationRole;
import com.wisemapping.model.Mindmap;
2024-02-17 11:18:43 -08:00
import com.wisemapping.model.Account;
2012-09-30 17:15:01 -03:00
import org.jetbrains.annotations.NotNull;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2012-09-30 17:15:01 -03:00
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
2012-09-30 17:15:01 -03:00
import java.util.concurrent.ConcurrentHashMap;
class LockManagerImpl implements LockManager {
2022-03-22 08:35:47 -03:00
private static final int ONE_MINUTE_MILLISECONDS = 1000 * 60;
private final Map<Integer, LockInfo> lockInfoByMapId;
private final static Timer expirationTimer = new Timer();
final private static Logger logger = LogManager.getLogger();
2012-09-30 17:15:01 -03:00
@Override
public boolean isLocked(@NotNull Mindmap mindmap) {
return this.getLockInfo(mindmap) != null;
}
@Override
public LockInfo getLockInfo(@NotNull Mindmap mindmap) {
return lockInfoByMapId.get(mindmap.getId());
}
2013-04-07 12:27:45 -03:00
@Override
2024-02-17 11:18:43 -08:00
public void unlockAll(@NotNull final Account user) throws LockException, AccessDeniedSecurityException {
2013-04-07 12:27:45 -03:00
final Set<Integer> mapIds = lockInfoByMapId.keySet();
for (final Integer mapId : mapIds) {
final LockInfo lockInfo = lockInfoByMapId.get(mapId);
if (lockInfo.getUser().identityEquality(user)) {
unlock(mapId);
}
}
}
2012-09-30 17:15:01 -03:00
@Override
2024-02-17 11:18:43 -08:00
public void unlock(@NotNull Mindmap mindmap, @NotNull Account user) throws LockException, AccessDeniedSecurityException {
2022-03-23 23:32:19 -03:00
verifyHasLock(mindmap, user);
2012-09-30 17:15:01 -03:00
this.unlock(mindmap.getId());
}
private void unlock(int mapId) {
logger.debug("Unlock map id:" + mapId);
lockInfoByMapId.remove(mapId);
}
@Override
2024-02-17 11:18:43 -08:00
public boolean isLockedBy(@NotNull Mindmap mindmap, @NotNull Account collaborator) {
2012-09-30 17:15:01 -03:00
boolean result = false;
final LockInfo lockInfo = this.getLockInfo(mindmap);
2012-11-14 20:33:42 -03:00
if (lockInfo != null && lockInfo.getUser().identityEquality(collaborator)) {
2012-09-30 17:15:01 -03:00
result = true;
}
return result;
}
2012-11-13 21:01:04 -03:00
2012-11-14 20:44:59 -03:00
@Override
public long generateSession() {
return System.nanoTime();
}
@NotNull
2022-03-23 10:56:57 -03:00
@Override
2024-02-17 11:18:43 -08:00
public LockInfo lock(@NotNull Mindmap mindmap, @NotNull Account user) throws LockException {
2012-09-30 17:15:01 -03:00
if (isLocked(mindmap) && !isLockedBy(mindmap, user)) {
2022-03-23 10:56:57 -03:00
throw LockException.createLockLost(mindmap, user, this);
2012-09-30 17:15:01 -03:00
}
2022-03-23 23:32:19 -03:00
// Do I need to create a new lock ?
LockInfo result = lockInfoByMapId.get(mindmap.getId());
2022-03-23 23:32:19 -03:00
if (result == null) {
logger.debug("Creating new lock for map id:" + mindmap.getId());
result = new LockInfo(user, mindmap);
lockInfoByMapId.put(mindmap.getId(), result);
2012-09-30 17:15:01 -03:00
}
2022-03-23 23:32:19 -03:00
// Update timestamp ...
logger.debug("Updating timeout:" + result);
result.updateTimeout();
return result;
2012-09-30 17:15:01 -03:00
}
2024-02-17 11:18:43 -08:00
private void verifyHasLock(@NotNull Mindmap mindmap, @NotNull Account user) throws LockException, AccessDeniedSecurityException {
2022-03-23 23:32:19 -03:00
// Only editor can have lock ...
if (!mindmap.hasPermissions(user, CollaborationRole.EDITOR)) {
throw new AccessDeniedSecurityException(mindmap.getId(), user);
}
// Is the lock assigned to the user ...
if (isLocked(mindmap) && !isLockedBy(mindmap, user)) {
throw LockException.createLockLost(mindmap, user, this);
}
}
public LockManagerImpl() {
2022-03-16 23:16:34 -03:00
lockInfoByMapId = new ConcurrentHashMap<>();
expirationTimer.schedule(new TimerTask() {
@Override
public void run() {
2022-03-22 08:35:47 -03:00
synchronized (this) {
logger.debug("Lock expiration scheduler started. Current locks:" + lockInfoByMapId.keySet());
// Search for expired sessions and remove them ....
lockInfoByMapId.
keySet().
stream().
filter(mapId -> lockInfoByMapId.get(mapId).isExpired()).
forEach(mapId -> unlock(mapId));
}
2022-03-16 23:16:34 -03:00
}
}, ONE_MINUTE_MILLISECONDS, ONE_MINUTE_MILLISECONDS);
2012-09-30 17:15:01 -03:00
}
}