diff options
author | Kevin Sawicki <kevin@github.com> | 2011-12-12 17:49:09 -0800 |
---|---|---|
committer | Kevin Sawicki <kevin@github.com> | 2011-12-18 14:40:42 -0800 |
commit | 656461a9919403755b34b5dcd261a0f42750369d (patch) | |
tree | c50929b29243a2f64934e1070a748a119df10c9d | |
parent | 8c60468ff4cae4a13f57ed4a1822cec7cb2ba22a (diff) | |
download | jgit-656461a9919403755b34b5dcd261a0f42750369d.tar.gz jgit-656461a9919403755b34b5dcd261a0f42750369d.zip |
Add exception class for when locking a file fails
This will allows calling classes to handle lock failures
without checking against the message and will also provide
access to the file that could not be locked.
Change-Id: I95bc59e1330a7af71ae3b0485c4516299193f504
7 files changed, 100 insertions, 8 deletions
diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties index 5bb2800db0..d2933c46ae 100644 --- a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties +++ b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties @@ -56,7 +56,6 @@ cannotExecute=cannot execute: {0} cannotGet=Cannot get {0} cannotListRefs=cannot list refs cannotLock=Cannot lock {0} -cannotLockFile=Cannot lock file {0} cannotLockPackIn=Cannot lock pack in {0} cannotMatchOnEmptyString=Cannot match on empty string. cannotMoveIndexTo=Cannot move index to {0} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java index c57f77ec1d..0a73f59981 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java @@ -116,7 +116,6 @@ public class JGitText extends TranslationBundle { /***/ public String cannotGet; /***/ public String cannotListRefs; /***/ public String cannotLock; - /***/ public String cannotLockFile; /***/ public String cannotLockPackIn; /***/ public String cannotMatchOnEmptyString; /***/ public String cannotMoveIndexTo; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java index 55256d638e..346b3b35f6 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java @@ -62,6 +62,7 @@ import java.util.Arrays; import java.util.Comparator; import org.eclipse.jgit.JGitText; +import org.eclipse.jgit.errors.LockFailedException; import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.UnmergedPathException; import org.eclipse.jgit.events.IndexChangedEvent; @@ -190,7 +191,7 @@ public class DirCache { throws CorruptObjectException, IOException { final DirCache c = new DirCache(indexLocation, fs); if (!c.lock()) - throw new IOException(MessageFormat.format(JGitText.get().cannotLock, indexLocation)); + throw new LockFailedException(indexLocation); try { c.read(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/errors/LockFailedException.java b/org.eclipse.jgit/src/org/eclipse/jgit/errors/LockFailedException.java new file mode 100644 index 0000000000..96a86c4f47 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/errors/LockFailedException.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2011, GitHub Inc. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.errors; + +import java.io.File; +import java.io.IOException; +import java.text.MessageFormat; + +import org.eclipse.jgit.JGitText; + +/** + * An exception occurring when a file cannot be locked + */ +public class LockFailedException extends IOException { + private static final long serialVersionUID = 1L; + + private File file; + + /** + * Construct a CannotLockException for the given file and message + * + * @param file + * file that could not be locked + * @param message + * exception message + */ + public LockFailedException(File file, String message) { + super(message); + this.file = file; + } + + /** + * Construct a CannotLockException for the given file + * + * @param file + * file that could not be locked + */ + public LockFailedException(File file) { + this(file, MessageFormat.format(JGitText.get().cannotLock, file)); + } + + /** + * Get the file that could not be locked + * + * @return file + */ + public File getFile() { + return file; + } +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java index 212646b11d..1cb488d3e8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java @@ -55,6 +55,7 @@ import java.io.IOException; import java.text.MessageFormat; import org.eclipse.jgit.JGitText; +import org.eclipse.jgit.errors.LockFailedException; import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Constants; @@ -172,7 +173,7 @@ public class FileBasedConfig extends StoredConfig { final byte[] out = Constants.encode(toText()); final LockFile lf = new LockFile(getFile(), fs); if (!lf.lock()) - throw new IOException(MessageFormat.format(JGitText.get().cannotLockFile, getFile())); + throw new LockFailedException(getFile()); try { lf.setNeedSnapshot(true); lf.write(out); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryPackParser.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryPackParser.java index 91e50ad9d1..28b3ded286 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryPackParser.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryPackParser.java @@ -58,6 +58,7 @@ import java.util.zip.CRC32; import java.util.zip.Deflater; import org.eclipse.jgit.JGitText; +import org.eclipse.jgit.errors.LockFailedException; import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.CoreConfig; @@ -452,8 +453,9 @@ public class ObjectDirectoryPackParser extends PackParser { // try { if (!keep.lock(lockMessage)) - throw new IOException(MessageFormat.format( - JGitText.get().cannotLockPackIn, finalPack)); + throw new LockFailedException(finalPack, + MessageFormat.format( + JGitText.get().cannotLockPackIn, finalPack)); } catch (IOException e) { cleanupTemporaryFiles(); throw e; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java index 3917ff4716..47b484e39a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java @@ -78,6 +78,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import org.eclipse.jgit.JGitText; +import org.eclipse.jgit.errors.LockFailedException; import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.ObjectWritingException; import org.eclipse.jgit.events.RefsChangedEvent; @@ -562,8 +563,7 @@ public class RefDirectory extends RefDatabase { LockFile lck = new LockFile(packedRefsFile, update.getRepository().getFS()); if (!lck.lock()) - throw new IOException(MessageFormat.format( - JGitText.get().cannotLockFile, packedRefsFile)); + throw new LockFailedException(packedRefsFile); try { PackedRefList cur = readPackedRefs(); int idx = cur.find(name); |