Mercurial > jhg
annotate src/org/tmatesoft/hg/repo/HgRepositoryLock.java @ 623:fedc54356091
Update tests for Windows; TestCommit: use copy of a repo (not clone) to preserve old timestamps
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Mon, 20 May 2013 18:35:13 +0200 |
parents | 868b2ffdcd5c |
children | 5afc7eedb3dd |
rev | line source |
---|---|
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
605
c56edf42be64
Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
505
diff
changeset
|
2 * Copyright (c) 2012-2013 TMate Software Ltd |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
3 * |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
7 * |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
11 * GNU General Public License for more details. |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
12 * |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
13 * For information on how to redistribute this software under |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
487
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
486
diff
changeset
|
17 package org.tmatesoft.hg.repo; |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 import java.io.File; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
20 import java.io.FileInputStream; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
21 import java.io.FileOutputStream; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 import java.io.IOException; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
23 import java.lang.management.ManagementFactory; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 import java.net.InetAddress; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 import java.nio.ByteBuffer; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
26 import java.nio.channels.FileChannel; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
27 |
505
3cd3c3d37432
Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
489
diff
changeset
|
28 import org.tmatesoft.hg.core.HgRepositoryLockException; |
487
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
486
diff
changeset
|
29 import org.tmatesoft.hg.internal.Internals; |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 /** |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
32 * <p>Usage: |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
33 * <pre> |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
34 * HgRepositoryLock lock = hgRepo.getWorkingDirLock(); |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
35 * try { |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
36 * // Actually lock the repo |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
37 * lock.acquire(); |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
38 * /// |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
39 * // do whatever modifies working directory |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
40 * ... |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
41 * } finally { |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
42 * if (lock.isLocked()) { |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
43 * // this check is needed not to release() |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
44 * // erroneously in case acquire() failed (e.g. due to timeout) |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
45 * lock.release(); |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
46 * } |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
47 * |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
48 * </pre> |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
49 * |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 * Unlike original mechanism, we don't use symlinks, rather files, as it's easier to implement |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 * |
613
f41dd9a3b8af
Remove few Experimental annotations as the API they've marked graduates
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
605
diff
changeset
|
52 * <p> |
f41dd9a3b8af
Remove few Experimental annotations as the API they've marked graduates
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
605
diff
changeset
|
53 * NOT SAFE FOR MULTITHREAD USE! |
f41dd9a3b8af
Remove few Experimental annotations as the API they've marked graduates
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
605
diff
changeset
|
54 * |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
55 * @see http://code.google.com/p/hg4j/issues/detail?id=35 |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 * @author Artem Tikhomirov |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 * @author TMate Software Ltd. |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 */ |
487
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
486
diff
changeset
|
59 public class HgRepositoryLock { |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 /* |
488
45b3b6ca046f
Repository locking mechanism is alive
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
487
diff
changeset
|
61 * Lock .hg/ except .hg/store/ .hg/wlock (new File(hgRepo.getRepoRoot(),"wlock")) |
45b3b6ca046f
Repository locking mechanism is alive
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
487
diff
changeset
|
62 * Lock .hg/store/ .hg/store/lock (HgRepository.repoPathHelper("lock")) |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 */ |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 private final File lockFile; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 private int use = 0; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 private final int timeoutSeconds; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 |
487
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
486
diff
changeset
|
69 HgRepositoryLock(File lock, int timeoutInSeconds) { |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 lockFile = lock; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
71 timeoutSeconds = timeoutInSeconds; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 /** |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 * Tries to read lock file and supplies hostname:pid (or just pid) information from there |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 * @return <code>null</code> if no lock file available at the moment |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 */ |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 public String readLockInfo() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 if (lockFile.exists()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 byte[] bytes = read(lockFile); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 if (bytes != null && bytes.length > 0) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 return new String(bytes); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 } catch (Exception ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
86 // deliberately ignored |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
87 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
88 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 return null; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
90 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
91 |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
92 /** |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
93 * @return <code>true</code> if we hold the lock |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
94 */ |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 public boolean isLocked() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
96 return use > 0; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
97 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
98 |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
99 /** |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
100 * Perform actual locking. Waits for timeout (if specified at construction time) |
605
c56edf42be64
Commit: update active bookmark with new revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
505
diff
changeset
|
101 * before throwing {@link HgRepositoryLockException} in case lock is not available |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
102 * immediately. |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
103 * |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
104 * <p>Multiple calls are possible, but corresponding number of {@link #release()} |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
105 * calls shall be made. |
505
3cd3c3d37432
Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
489
diff
changeset
|
106 * @throws HgRepositoryLockException if failed to grab a lock |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
107 */ |
505
3cd3c3d37432
Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
489
diff
changeset
|
108 public void acquire() throws HgRepositoryLockException { |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
109 if (use > 0) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
110 use++; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
111 return; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
112 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
113 StringBuilder lockDescription = new StringBuilder(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
114 lockDescription.append(getHostname()); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
115 lockDescription.append(':'); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
116 lockDescription.append(getPid()); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
117 byte[] bytes = lockDescription.toString().getBytes(); |
487
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
486
diff
changeset
|
118 long stopTime = timeoutSeconds < 0 ? -1 : (System.currentTimeMillis() + timeoutSeconds*1000); |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
119 do { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
120 synchronized(this) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
121 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
122 if (!lockFile.exists()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
123 write(lockFile, bytes); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
124 use++; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
125 return; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
126 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
127 } catch (IOException ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
128 // deliberately ignored |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
129 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
130 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
131 wait(1000); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
132 } catch (InterruptedException ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
133 // deliberately ignored |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
134 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
135 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
136 |
487
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
486
diff
changeset
|
137 } while (stopTime == -1/*no timeout*/ || System.currentTimeMillis() <= stopTime); |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
138 String msg = String.format("Failed to aquire lock, waited for %d seconds, present owner: '%s'", timeoutSeconds, readLockInfo()); |
505
3cd3c3d37432
Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
489
diff
changeset
|
139 throw new HgRepositoryLockException(msg); |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
140 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
141 |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
142 /** |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
143 * Release lock we own |
505
3cd3c3d37432
Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
489
diff
changeset
|
144 * @throws HgRepositoryLockException if there's no evidence we do own a lock |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
145 */ |
505
3cd3c3d37432
Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
489
diff
changeset
|
146 public void release() throws HgRepositoryLockException { |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
147 if (use == 0) { |
505
3cd3c3d37432
Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
489
diff
changeset
|
148 throw new HgRepositoryLockException("Lock is not held!"); |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
149 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
150 use--; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
151 if (use > 0) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
152 return; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
153 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
154 // do release |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
155 lockFile.delete(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
156 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
157 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
158 protected String getHostname() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
159 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
160 return InetAddress.getLocalHost().getHostName(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
161 } catch (Exception ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
162 return "localhost"; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
163 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
164 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
165 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
166 protected int getPid() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
167 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
168 // @see http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
169 if (!Internals.runningOnWindows()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
170 File f = new File("/proc/self"); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
171 if (f.exists()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
172 // /proc/self is a symlink to /proc/pid/ directory |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
173 return Integer.parseInt(f.getCanonicalFile().getName()); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
174 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
175 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
176 String rtBean = ManagementFactory.getRuntimeMXBean().getName(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
177 int x; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
178 if ((x = rtBean.indexOf('@')) != -1) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
179 return Integer.parseInt(rtBean.substring(0, x)); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
180 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
181 return -1; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
182 } catch (Exception ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
183 return -1; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
184 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
185 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
186 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
187 private static void write(File f, byte[] content) throws IOException { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
188 FileOutputStream fos = new FileOutputStream(f); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
189 fos.write(content); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
190 fos.close(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
191 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
192 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
193 private static byte[] read(File f) throws IOException { |
619
868b2ffdcd5c
Close FIS, not FileChannel, to clear both references to FileDescriptor right away
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
613
diff
changeset
|
194 FileInputStream fis = new FileInputStream(f); |
868b2ffdcd5c
Close FIS, not FileChannel, to clear both references to FileDescriptor right away
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
613
diff
changeset
|
195 FileChannel fc = fis.getChannel(); |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
196 ByteBuffer bb = ByteBuffer.allocate(Internals.ltoi(fc.size())); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
197 fc.read(bb); |
619
868b2ffdcd5c
Close FIS, not FileChannel, to clear both references to FileDescriptor right away
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
613
diff
changeset
|
198 fis.close(); |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
199 return bb.array(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
200 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
201 } |