Example usage for org.eclipse.jgit.errors LockFailedException LockFailedException

List of usage examples for org.eclipse.jgit.errors LockFailedException LockFailedException

Introduction

In this page you can find the example usage for org.eclipse.jgit.errors LockFailedException LockFailedException.

Prototype

public LockFailedException(File file) 

Source Link

Document

Construct a CannotLockException for the given file

Usage

From source file:org.ms123.common.git.FileHolder.java

License:Open Source License

/**
 * Save the configuration as a Git text style configuration file.
 * <p>//from w w  w. j  a  v  a2  s  . c om
 * <b>Warning:</b> Although this method uses the traditional Git file
 * locking approach to protect against concurrent writes of the
 * configuration file, it does not ensure that the file has not been
 * modified since the last read, which means updates performed by other
 * objects accessing the same backing file may be lost.
 *
 * @throws IOException
 *             the file could not be written.
 */
public void save() throws IOException {
    byte[] out1;
    byte[] out2 = new byte[0];
    byte[] outTotal;
    final String text = toText();
    if (utf8Bom) {
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(0xEF);
        bos.write(0xBB);
        bos.write(0xBF);
        bos.write(text.getBytes(UTF8_CHARSET.name()));
        out1 = bos.toByteArray();
    } else {
        out1 = encode(text);
    }
    if (m_content != null) {
        out2 = encode("--\n" + m_content);
    }
    outTotal = concat(out1, out2);
    final LockFile lf = new LockFile(getFile(), fs);
    if (!lf.lock())
        throw new LockFailedException(getFile());
    try {
        lf.setNeedSnapshot(true);
        lf.write(outTotal);
        if (!lf.commit()) {
            throw new IOException(MessageFormat.format(JGitText.get().cannotCommitWriteTo, getFile()));
        }
    } finally {
        lf.unlock();
    }
    snapshot = lf.getCommitSnapshot();
    hash = hash(outTotal);
    fireConfigChangedEvent();
}