annotate src/org/tmatesoft/hg/repo/HgRepositoryLock.java @ 604:c3505001a42a

Use nodeid reverse lookup speedup cache for #isKnown, if available
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 06 May 2013 18:53:04 +0200
parents 3cd3c3d37432
children c56edf42be64 2f1cd1c26de5
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
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;
489
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
29 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
30 import org.tmatesoft.hg.internal.Internals;
486
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 /**
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 * NOT SAFE FOR MULTITHREAD USE!
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 *
489
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
35 * <p>Usage:
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
36 * <pre>
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
37 * HgRepositoryLock lock = hgRepo.getWorkingDirLock();
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
38 * try {
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
39 * // Actually lock the repo
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
40 * lock.acquire();
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
41 * ///
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
42 * // do whatever modifies working directory
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
43 * ...
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
44 * } finally {
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
45 * if (lock.isLocked()) {
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
46 * // this check is needed not to release()
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
47 * // erroneously in case acquire() failed (e.g. due to timeout)
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
48 * lock.release();
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 *
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
51 * </pre>
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
52 *
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 * 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
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 */
489
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
59 @Experimental(reason="Work in progress")
487
db48a77ec8ff Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 486
diff changeset
60 public class HgRepositoryLock {
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 /*
488
45b3b6ca046f Repository locking mechanism is alive
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 487
diff changeset
62 * 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
63 * 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
64 */
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 private final File lockFile;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 private int use = 0;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 private final int timeoutSeconds;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69
487
db48a77ec8ff Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 486
diff changeset
70 HgRepositoryLock(File lock, int timeoutInSeconds) {
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 lockFile = lock;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 timeoutSeconds = timeoutInSeconds;
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 /**
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 * 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
77 * @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
78 */
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 public String readLockInfo() {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 if (lockFile.exists()) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 try {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 byte[] bytes = read(lockFile);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 if (bytes != null && bytes.length > 0) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 return new String(bytes);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 } catch (Exception ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 // deliberately ignored
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 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 return null;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92
489
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
93 /**
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
94 * @return <code>true</code> if we hold the lock
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
95 */
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 public boolean isLocked() {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 return use > 0;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99
489
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
100 /**
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
101 * 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
102 * before throwing {@link HgInvalidStateException} in case lock is not available
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
103 * immediately.
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
104 *
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
105 * <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
106 * calls shall be made.
505
3cd3c3d37432 Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 489
diff changeset
107 * @throws HgRepositoryLockException if failed to grab a lock
489
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
108 */
505
3cd3c3d37432 Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 489
diff changeset
109 public void acquire() throws HgRepositoryLockException {
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 if (use > 0) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 use++;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 return;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 StringBuilder lockDescription = new StringBuilder();
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 lockDescription.append(getHostname());
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 lockDescription.append(':');
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 lockDescription.append(getPid());
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 byte[] bytes = lockDescription.toString().getBytes();
487
db48a77ec8ff Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 486
diff changeset
119 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
120 do {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 synchronized(this) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 try {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 if (!lockFile.exists()) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 write(lockFile, bytes);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 use++;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126 return;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
127 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
128 } catch (IOException ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
129 // deliberately ignored
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
130 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131 try {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132 wait(1000);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133 } catch (InterruptedException ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
134 // deliberately ignored
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 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
137
487
db48a77ec8ff Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 486
diff changeset
138 } 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
139 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
140 throw new HgRepositoryLockException(msg);
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
141 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
142
489
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
143 /**
9c0138cda59a HgRepositoryLock: some javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 488
diff changeset
144 * Release lock we own
505
3cd3c3d37432 Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 489
diff changeset
145 * @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
146 */
505
3cd3c3d37432 Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 489
diff changeset
147 public void release() throws HgRepositoryLockException {
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
148 if (use == 0) {
505
3cd3c3d37432 Use checked exception to indicate fs lock entanglements
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 489
diff changeset
149 throw new HgRepositoryLockException("Lock is not held!");
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
150 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 use--;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
152 if (use > 0) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153 return;
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 // do release
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
156 lockFile.delete();
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
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
159 protected String getHostname() {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 try {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161 return InetAddress.getLocalHost().getHostName();
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 } catch (Exception ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 return "localhost";
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
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
167 protected int getPid() {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
168 try {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
169 // @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
170 if (!Internals.runningOnWindows()) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
171 File f = new File("/proc/self");
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172 if (f.exists()) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
173 // /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
174 return Integer.parseInt(f.getCanonicalFile().getName());
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 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
177 String rtBean = ManagementFactory.getRuntimeMXBean().getName();
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
178 int x;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
179 if ((x = rtBean.indexOf('@')) != -1) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
180 return Integer.parseInt(rtBean.substring(0, x));
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
181 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
182 return -1;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
183 } catch (Exception ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
184 return -1;
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
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
188 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
189 FileOutputStream fos = new FileOutputStream(f);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
190 fos.write(content);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
191 fos.close();
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
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
194 private static byte[] read(File f) throws IOException {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
195 FileChannel fc = new FileInputStream(f).getChannel();
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);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
198 fc.close();
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 }