wiki:GRITSFileLocking

File Locking in GRITS MS Plugins

The classes for the file locking are in "org.grits.toolbox.entry.ms" plugin inside org.grits.toolbox.entry.ms.property package.

In order to use the file locking mechanism, the lock file first needs to be read (loaded in FileLockManager object), then new files can be added or any existing files' lock status can be changed (through FileLockManager) and finally the lock file (FileLockManager) should be written back (saved) for the changes to take effect.

Loading a lock file for an Entry

You need the Entry and the property (MassSpecProperty) to access the lock file.

    Entry entry = getEntryForCurrentViewer();
    MassSpecProperty pp = getEntryMassSpecProperty();
    try {
        fileLockManager = FileLockingUtils.readLockFile(pp.getLockFilePath(entry));
    } catch (IOException | JAXBException e) {
        logger.error("Cannot load the lock file", e);
	fileLockManager = null;
    }

Locking a file

There are two methods in FileLockManager to lock a given file. If you have the Entry (object) trying to lock the file (using the file) you can pass that as an argument. Or if you only have the name and id of the entry but not the whole Entry object, you can use the other version (PropertyReader, for example, cannot have the Entry object yet).

       /**
	 * increments the lock count of the file given by the fileName
	 * @param fileName name of the file to be locked
	 * @param entry Entry that uses the file
	 */
	public void lockFile(String fileName, Entry entry) {
		FileLock existing = findFile(fileName);
		if (existing == null) {
			existing = new FileLock();
			existing.setFilename(fileName);
			fileList.add(existing);
		}
		existing.addEntry(entry);
		existing.incrementLock();
	}

	/**
	 * This version is used if you only have "id" and the "name" for the entry, not the Entry object
	 * 
	 * @param fileName file to be locked
	 * @param entry LockEntry object with entry id and name
	 */
	public void lockFile(String fileName, LockEntry entry) {
		FileLock existing = findFile(fileName);
		if (existing == null) {
			existing = new FileLock();
			existing.setFilename(fileName);
			fileList.add(existing);
		}
		existing.addEntry(entry);
		existing.incrementLock();
	}

	

See if a file is already locked (in use)

       /**
	 * returns whether the given file is already locked
	 * @param filename name of the file to be checked
	 * @return true if the file is locked, false otherwise or if the file is not found 
	 */
	public boolean isFileLocked(String filename) {
		FileLock existing = findFile(filename);
		if (existing != null)
			return existing.isLocked();
		return false;
	}

Release the lock for a given file

       /**
	 * decrements the lock count for the given file
	 * This version is used if you only have "id" and the "name" for the entry, not the Entry object
	 * 
	 * @param fileName name of the file to be unlocked
	 * @param usedBy entry that no longer uses the file
	 */
	public void removeLock(String fileName, LockEntry usedBy) {
		FileLock existing = findFile(fileName);
		if (existing != null) {
			existing.decrementLock();
			existing.removeEntry (usedBy);
		}
	}
	
	/**
	 * decrements the lock count for the given file
	 * 
	 * @param fileName name of the file to be unlocked
	 * @param entry entry that no longer uses the file
	 */
	public void removeLock(String fileName, Entry entry) {
		FileLock existing = findFile(fileName);
		if (existing != null) {
			existing.decrementLock();
			existing.deleteEntry (entry);
		}
	}

Saving the updated lock file

FileLockingUtils.writeLockFile(fileLockManager, pp.getLockFilePath(entry));
Last modified 4 years ago Last modified on 08/23/2017 10:41:33 PM