Mercurial > hg4j
annotate src/org/tmatesoft/hg/repo/HgRepositoryLock.java @ 494:2743641f2f12
Defect: use of 0 as configuration value for mapio boundary results in every file being memmap-ed
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 18 Oct 2012 19:51:07 +0200 |
parents | 9c0138cda59a |
children | 3cd3c3d37432 |
rev | line source |
---|---|
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012 TMate Software Ltd |
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 |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
28 import org.tmatesoft.hg.internal.Experimental; |
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 /** |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 * NOT SAFE FOR MULTITHREAD USE! |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
33 * |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
34 * <p>Usage: |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
35 * <pre> |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
36 * HgRepositoryLock lock = hgRepo.getWorkingDirLock(); |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
37 * try { |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
38 * // Actually lock the repo |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
39 * lock.acquire(); |
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 * // do whatever modifies working directory |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
42 * ... |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
43 * } finally { |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
44 * if (lock.isLocked()) { |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
45 * // this check is needed not to release() |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
46 * // erroneously in case acquire() failed (e.g. due to timeout) |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
47 * lock.release(); |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
48 * } |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
49 * |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
50 * </pre> |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
51 * |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 * 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
|
53 * |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
54 * @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
|
55 * @author Artem Tikhomirov |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 * @author TMate Software Ltd. |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 */ |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
58 @Experimental(reason="Work in progress") |
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) |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
101 * before throwing {@link HgInvalidStateException} in case lock is not available |
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. |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
106 */ |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
107 public void acquire() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
108 if (use > 0) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
109 use++; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
110 return; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
111 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
112 StringBuilder lockDescription = new StringBuilder(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
113 lockDescription.append(getHostname()); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
114 lockDescription.append(':'); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
115 lockDescription.append(getPid()); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
116 byte[] bytes = lockDescription.toString().getBytes(); |
487
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
486
diff
changeset
|
117 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
|
118 do { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
119 synchronized(this) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
120 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
121 if (!lockFile.exists()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
122 write(lockFile, bytes); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
123 use++; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
124 return; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
125 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
126 } catch (IOException ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
127 // deliberately ignored |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
128 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
129 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
130 wait(1000); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
131 } catch (InterruptedException ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
132 // deliberately ignored |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
133 } |
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 |
487
db48a77ec8ff
Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
486
diff
changeset
|
136 } 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
|
137 String msg = String.format("Failed to aquire lock, waited for %d seconds, present owner: '%s'", timeoutSeconds, readLockInfo()); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
138 throw new HgInvalidStateException(msg); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
139 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
140 |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
141 /** |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
142 * Release lock we own |
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
143 */ |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
144 public void release() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
145 if (use == 0) { |
489
9c0138cda59a
HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
488
diff
changeset
|
146 throw new HgInvalidStateException("Lock is not held!"); |
486
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
147 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
148 use--; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
149 if (use > 0) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
150 return; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
151 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
152 // do release |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
153 lockFile.delete(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
154 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
155 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
156 protected String getHostname() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
157 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
158 return InetAddress.getLocalHost().getHostName(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
159 } catch (Exception ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
160 return "localhost"; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
161 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
162 } |
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 protected int getPid() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
165 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
166 // @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
|
167 if (!Internals.runningOnWindows()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
168 File f = new File("/proc/self"); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
169 if (f.exists()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
170 // /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
|
171 return Integer.parseInt(f.getCanonicalFile().getName()); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
172 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
173 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
174 String rtBean = ManagementFactory.getRuntimeMXBean().getName(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
175 int x; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
176 if ((x = rtBean.indexOf('@')) != -1) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
177 return Integer.parseInt(rtBean.substring(0, x)); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
178 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
179 return -1; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
180 } catch (Exception ex) { |
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 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
183 } |
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 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
|
186 FileOutputStream fos = new FileOutputStream(f); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
187 fos.write(content); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
188 fos.close(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
189 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
190 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
191 private static byte[] read(File f) throws IOException { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
192 FileChannel fc = new FileInputStream(f).getChannel(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
193 ByteBuffer bb = ByteBuffer.allocate(Internals.ltoi(fc.size())); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
194 fc.read(bb); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
195 fc.close(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
196 return bb.array(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
197 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
198 } |