View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.mets.tools;
20  
21  import java.text.MessageFormat;
22  import java.util.Hashtable;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.mycore.common.MCRException;
27  import org.mycore.common.MCRSession;
28  import org.mycore.common.MCRSessionMgr;
29  import org.mycore.datamodel.metadata.MCRObjectID;
30  
31  /**
32   * 
33   * Used to lock the mets editor for a specific Derivate
34   * 
35   * @author Sebastian Hofmann (mcrshofm)
36   */
37  public class MCRMetsLock {
38  
39      private static Hashtable<MCRObjectID, String> metsAccessSessionTable = new Hashtable<>();
40  
41      private static final Logger LOGGER = LogManager.getLogger(MCRMetsLock.class);
42  
43      /**
44       * Checks if a Derivate is locked
45       * @param derivateIdString the derivate id
46       * @return true if the derivate is locked
47       */
48      public static synchronized boolean isLocked(String derivateIdString) {
49          MCRObjectID derivateId = MCRObjectID.getInstance(derivateIdString);
50          if (MCRMetsLock.metsAccessSessionTable.containsKey(derivateId)) {
51              String lastAccessID = MCRMetsLock.metsAccessSessionTable.get(derivateId);
52              MCRSession lastSession = MCRSessionMgr.getSession(lastAccessID);
53              LOGGER.debug(MessageFormat.format("{0} is locked : {1}", derivateIdString, lastSession != null));
54              return lastSession != null;
55          } else {
56              LOGGER.debug(MessageFormat.format("{0} is not locked", derivateIdString));
57              return false;
58          }
59      }
60  
61      /**
62       * Locks a Derivate with the current SessionId
63       * @param derivateIdString the Derivate to lock
64       * @return True if the derivate is locked. False if the derivate could not be locked.
65       */
66      public static synchronized boolean doLock(String derivateIdString) {
67          MCRObjectID derivateId = MCRObjectID.getInstance(derivateIdString);
68          if (isLocked(derivateIdString)
69              && MCRMetsLock.metsAccessSessionTable.get(derivateId) != MCRSessionMgr.getCurrentSessionID()) {
70              LOGGER.info(MessageFormat.format("Could not lock {0}, because its already locked.", derivateIdString));
71              return false;
72          } else {
73              LOGGER.info(MessageFormat.format("{0} is now locked", derivateIdString));
74              MCRMetsLock.metsAccessSessionTable.put(derivateId, MCRSessionMgr.getCurrentSessionID());
75              return true;
76          }
77      }
78  
79      /**
80       * Unlocks a Derivate wich was locked with the current SessionId
81       * @param derivateIdString the id of the derivate
82       * @throws MCRException if the session-id of locker is different from current session-id
83       */
84      public static synchronized void doUnlock(String derivateIdString) throws MCRException {
85          MCRObjectID derivateId = MCRObjectID.getInstance(derivateIdString);
86          if (isLocked(derivateIdString)) {
87              String sessionId = MCRMetsLock.metsAccessSessionTable.get(MCRObjectID.getInstance(derivateIdString));
88              if (MCRSessionMgr.getCurrentSessionID().equals(sessionId)) {
89                  LOGGER.info(MessageFormat.format("{0} is not locked anymore", derivateIdString));
90                  MCRMetsLock.metsAccessSessionTable.remove(derivateId);
91              } else {
92                  LOGGER.error(MessageFormat.format("could not unlock {0} because session id is different",
93                      derivateIdString));
94                  String message = MessageFormat.format(
95                      "Could not unlock {0}, because the session wich locked it was : ''{1}'' "
96                          + "and current sesssion is ''{2}''",
97                      derivateIdString, sessionId,
98                      MCRSessionMgr.getCurrentSessionID());
99                  throw new MCRException(message);
100             }
101         }
102     }
103 
104 }