diff --git a/wise-webapp/src/main/java/com/wisemapping/listener/UnlockOnExpireListener.java b/wise-webapp/src/main/java/com/wisemapping/listener/UnlockOnExpireListener.java new file mode 100644 index 00000000..6fd5edcf --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/listener/UnlockOnExpireListener.java @@ -0,0 +1,44 @@ +package com.wisemapping.listener; + + +import com.wisemapping.exceptions.AccessDeniedSecurityException; +import com.wisemapping.exceptions.LockException; +import com.wisemapping.model.User; +import com.wisemapping.security.Utils; +import com.wisemapping.service.LockManager; +import com.wisemapping.service.MindmapService; +import org.jetbrains.annotations.NotNull; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionListener; + +public class UnlockOnExpireListener implements HttpSessionListener { + + @Override + public void sessionCreated(@NotNull HttpSessionEvent event) { + + } + + @Override + public void sessionDestroyed(@NotNull HttpSessionEvent event) { + + final ServletContext servletContext = event.getSession().getServletContext(); + final WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); + final MindmapService mindmapService = (MindmapService) wc.getBean("mindmapService"); + final LockManager lockManager = mindmapService.getLockManager(); + + final User user = Utils.getUser(false); + if (user != null) { + try { + lockManager.unlockAll(user); + } catch (LockException e) { + e.printStackTrace(); + } catch (AccessDeniedSecurityException e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/wise-webapp/src/main/java/com/wisemapping/service/LockManager.java b/wise-webapp/src/main/java/com/wisemapping/service/LockManager.java index daed4a95..9a5be030 100644 --- a/wise-webapp/src/main/java/com/wisemapping/service/LockManager.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/LockManager.java @@ -34,13 +34,12 @@ public interface LockManager { void unlock(@NotNull Mindmap mindmap, @NotNull User user) throws LockException, AccessDeniedSecurityException; + void unlockAll(@NotNull User user) throws LockException, AccessDeniedSecurityException; + boolean isLockedBy(@NotNull Mindmap mindmap, @NotNull User collaborator); @NotNull LockInfo lock(@NotNull Mindmap mindmap, @NotNull User user, long session) throws WiseMappingException; - @NotNull - LockInfo lock(@NotNull Mindmap mindmap, @NotNull User user) throws WiseMappingException; - long generateSession(); } diff --git a/wise-webapp/src/main/java/com/wisemapping/service/LockManagerImpl.java b/wise-webapp/src/main/java/com/wisemapping/service/LockManagerImpl.java index ca8da61e..68d80fd2 100644 --- a/wise-webapp/src/main/java/com/wisemapping/service/LockManagerImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/LockManagerImpl.java @@ -79,6 +79,17 @@ class LockManagerImpl implements LockManager { return result; } + @Override + public void unlockAll(@NotNull final User user) throws LockException, AccessDeniedSecurityException { + final Set mapIds = lockInfoByMapId.keySet(); + for (final Integer mapId : mapIds) { + final LockInfo lockInfo = lockInfoByMapId.get(mapId); + if (lockInfo.getUser().identityEquality(user)) { + unlock(mapId); + } + } + } + @Override public void unlock(@NotNull Mindmap mindmap, @NotNull User user) throws LockException, AccessDeniedSecurityException { if (isLocked(mindmap) && !isLockedBy(mindmap, user)) { @@ -93,6 +104,8 @@ class LockManagerImpl implements LockManager { } private void unlock(int mapId) { + System.out.println("Unlocking:"+mapId); + logger.debug("Unlock map id:" + mapId); lockInfoByMapId.remove(mapId); } @@ -108,12 +121,6 @@ class LockManagerImpl implements LockManager { } - @Override - @NotNull - public LockInfo lock(@NotNull Mindmap mindmap, @NotNull User user) throws WiseMappingException { - return this.lock(mindmap, user, System.nanoTime()); - } - @Override public long generateSession() { return System.nanoTime(); @@ -135,7 +142,7 @@ class LockManagerImpl implements LockManager { // Update timeout only... logger.debug("Update timestamp:" + mindmap.getId()); updateExpirationTimeout(mindmap, user); - // result.setSession(session); + // result.setSession(session); } else { logger.debug("Lock map id:" + mindmap.getId()); result = new LockInfo(user, mindmap, session); diff --git a/wise-webapp/src/main/webapp/WEB-INF/web.xml b/wise-webapp/src/main/webapp/WEB-INF/web.xml index 1bc58ccb..50979cbf 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/web.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/web.xml @@ -47,6 +47,9 @@ com.wisemapping.service.HibernateAppListener + + com.wisemapping.listener.UnlockOnExpireListener + hibernate