comparison src/org/tmatesoft/hg/internal/CompleteRepoLock.java @ 617:65c01508f002

Rollback support for commands that modify repository. Strategy to keep complete copy of a file being changed
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 15 May 2013 20:10:09 +0200
parents
children
comparison
equal deleted inserted replaced
616:5e0313485eef 617:65c01508f002
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program 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; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Error;
20
21 import org.tmatesoft.hg.core.HgRepositoryLockException;
22 import org.tmatesoft.hg.repo.HgRepository;
23 import org.tmatesoft.hg.repo.HgRepositoryLock;
24 import org.tmatesoft.hg.util.LogFacility;
25
26 /**
27 * Helper to lock both storage and working directory
28 *
29 * @author Artem Tikhomirov
30 * @author TMate Software Ltd.
31 */
32 public final class CompleteRepoLock {
33
34 private final HgRepository repo;
35 private HgRepositoryLock wdLock, storeLock;
36
37 public CompleteRepoLock(HgRepository hgRepo) {
38 repo = hgRepo;
39 }
40
41 public void acquire() throws HgRepositoryLockException {
42 wdLock = repo.getWorkingDirLock();
43 storeLock = repo.getStoreLock();
44 wdLock.acquire();
45 try {
46 storeLock.acquire();
47 } catch (HgRepositoryLockException ex) {
48 try {
49 wdLock.release();
50 } catch (HgRepositoryLockException e2) {
51 final LogFacility log = repo.getSessionContext().getLog();
52 log.dump(getClass(), Error, e2, "Nested exception ignored once failed to acquire store lock");
53 }
54 throw ex;
55 }
56
57 }
58
59 public void release() throws HgRepositoryLockException {
60 try {
61 storeLock.release();
62 } catch (HgRepositoryLockException ex) {
63 try {
64 wdLock.release();
65 } catch (HgRepositoryLockException e2) {
66 final LogFacility log = repo.getSessionContext().getLog();
67 log.dump(getClass(), Error, e2, "Nested exception ignored when releasing working directory lock");
68 }
69 throw ex;
70 }
71 wdLock.release();
72 }
73 }