PageRenderTime 151ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/db/common/DatabaseObjectLock.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 107 lines | 61 code | 9 blank | 37 comment | 8 complexity | 4b71ddd98b64cd3ac7f0a6c0caf95890 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.db.common;
  18. import java.util.Vector;
  19. import mpv5.db.objects.User;
  20. import mpv5.logging.Log;
  21. /**
  22. * This class is used to lock objects to prevent parallel editing.
  23. */
  24. public class DatabaseObjectLock {
  25. private boolean LOCKED = false;
  26. private DatabaseObject dbo;
  27. public static Vector<DatabaseObject> lockedObjects = new Vector<DatabaseObject>();
  28. /**
  29. * Creates a new lock for the given DatabaseObject.
  30. * <br/>This does not result in a locked object. Call {@link aquire()} to verify.
  31. * <br/>A Lock may fail if the DO is already locked, or not existing
  32. * @param dbo
  33. */
  34. public DatabaseObjectLock(DatabaseObject dbo) {
  35. this.dbo = dbo;
  36. }
  37. /**
  38. * Checks if this lock is valid
  39. * @return TRUE if this Lock does represent a valid lock - if the DO is locked for the current user
  40. */
  41. public synchronized boolean aquire() {
  42. if (dbo.__getIDS().intValue() > 0 && !dbo.isReadOnly()) {
  43. for (int i = 0; i < lockedObjects.size(); i++) {
  44. DatabaseObject databaseObject = lockedObjects.get(i);
  45. if (databaseObject.equals(dbo)) {
  46. Log.Debug(this, "Already locked for you: " + dbo);
  47. return true;
  48. }
  49. }
  50. return lock(dbo);
  51. } else {
  52. return false;
  53. }
  54. }
  55. /**
  56. * Releases this lock, if valid. Does nothing if this lock is not valid at all.
  57. */
  58. public void release() {
  59. if (LOCKED) {
  60. Log.Debug(this, "Releasing item " + dbo);
  61. QueryHandler.instanceOf().clone(Context.getLock()).removeLock(dbo.getContext(), dbo.__getIDS(), mpv5.db.objects.User.getCurrentUser());
  62. lockedObjects.remove(dbo);
  63. LOCKED = false;
  64. }
  65. }
  66. private synchronized boolean lock(DatabaseObject dbo) {
  67. if (!LOCKED) {
  68. try {
  69. Log.Debug(this, "Trying to lock item " + dbo);
  70. QueryHandler.instanceOf().clone(Context.getLock()).insertLock(dbo.getContext(), dbo.__getIDS(), mpv5.db.objects.User.getCurrentUser());
  71. lockedObjects.add(dbo);
  72. LOCKED = true;
  73. } catch (UnableToLockException ex) {
  74. Log.Debug(this, ex.getMessage());
  75. // Log.Debug(this, ex );
  76. LOCKED = false;
  77. dbo.ReadOnly(true);
  78. }
  79. }
  80. return LOCKED;
  81. }
  82. /**
  83. * Releases all the users objects, and all locked objects from this program instance
  84. * @param user
  85. */
  86. public static void releaseAllObjectsFor(User user) {
  87. try {
  88. QueryHandler.instanceOf().clone(Context.getLock()).delete(new String[][]{{"usersids", user.__getIDS().toString(), ""}});
  89. for (int i = 0; i < lockedObjects.size(); i++) {
  90. DatabaseObject databaseObject = lockedObjects.get(i);
  91. databaseObject.release();
  92. }
  93. lockedObjects.removeAllElements();
  94. } catch (Exception ex) {
  95. Log.Debug(ex);
  96. }
  97. }
  98. }