PageRenderTime 163ms CodeModel.GetById 100ms app.highlight 9ms RepoModel.GetById 52ms 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
  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 */
 17package mpv5.db.common;
 18
 19import java.util.Vector;
 20import mpv5.db.objects.User;
 21import mpv5.logging.Log;
 22
 23/**
 24 * This class is used to lock objects to prevent parallel editing.
 25 */
 26public class DatabaseObjectLock {
 27
 28    private boolean LOCKED = false;
 29    private DatabaseObject dbo;
 30    public static Vector<DatabaseObject> lockedObjects = new Vector<DatabaseObject>();
 31
 32    /**
 33     * Creates a new lock for the given DatabaseObject.
 34     * <br/>This does not result in a locked object. Call {@link aquire()} to verify.
 35     * <br/>A Lock may fail if the DO is already locked, or not existing
 36     * @param dbo
 37     */
 38    public DatabaseObjectLock(DatabaseObject dbo) {
 39        this.dbo = dbo;
 40    }
 41
 42    /**
 43     * Checks if this lock is valid
 44     * @return TRUE if this Lock does represent a valid lock - if the DO is locked for the current user
 45     */
 46    public synchronized boolean aquire() {
 47        if (dbo.__getIDS().intValue() > 0 && !dbo.isReadOnly()) {
 48            for (int i = 0; i < lockedObjects.size(); i++) {
 49                DatabaseObject databaseObject = lockedObjects.get(i);
 50                if (databaseObject.equals(dbo)) {
 51                    Log.Debug(this, "Already locked for you: " + dbo);
 52                    return true;
 53                }
 54            }
 55            return lock(dbo);
 56        } else {
 57            return false;
 58        }
 59    }
 60
 61    /**
 62     * Releases this lock, if valid. Does nothing if this lock is not valid at all.
 63     */
 64    public void release() {
 65        if (LOCKED) {
 66            Log.Debug(this, "Releasing item " + dbo);
 67            QueryHandler.instanceOf().clone(Context.getLock()).removeLock(dbo.getContext(), dbo.__getIDS(), mpv5.db.objects.User.getCurrentUser());
 68            lockedObjects.remove(dbo);
 69            LOCKED = false;
 70        }
 71    }
 72
 73    private synchronized boolean lock(DatabaseObject dbo) {
 74        if (!LOCKED) {
 75            try {
 76                Log.Debug(this, "Trying to lock item " + dbo);
 77                QueryHandler.instanceOf().clone(Context.getLock()).insertLock(dbo.getContext(), dbo.__getIDS(), mpv5.db.objects.User.getCurrentUser());
 78                lockedObjects.add(dbo);
 79                LOCKED = true;
 80            } catch (UnableToLockException ex) {
 81                Log.Debug(this, ex.getMessage());
 82//                Log.Debug(this, ex );
 83                LOCKED = false;
 84                dbo.ReadOnly(true);
 85            }
 86        }
 87
 88        return LOCKED;
 89    }
 90
 91    /**
 92     * Releases all the users objects, and all locked objects from this program instance
 93     * @param user
 94     */
 95    public static void releaseAllObjectsFor(User user) {
 96        try {
 97            QueryHandler.instanceOf().clone(Context.getLock()).delete(new String[][]{{"usersids", user.__getIDS().toString(), ""}});
 98            for (int i = 0; i < lockedObjects.size(); i++) {
 99                DatabaseObject databaseObject = lockedObjects.get(i);
100                databaseObject.release();
101            }
102            lockedObjects.removeAllElements();
103        } catch (Exception ex) {
104            Log.Debug(ex);
105        }
106    }
107}