annotate src/org/tmatesoft/hg/repo/HgRepositoryLock.java @ 487:db48a77ec8ff

Access to reposiotry lock mechanism via HgRepositoryLock
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 13 Aug 2012 18:11:47 +0200
parents src/org/tmatesoft/hg/internal/Lock.java@d740edfff563
children 45b3b6ca046f
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
487
db48a77ec8ff Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 486
diff changeset
28 import org.tmatesoft.hg.internal.Internals;
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29
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 * NOT SAFE FOR MULTITHREAD USE!
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 * 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
34 *
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 * @author Artem Tikhomirov
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 * @author TMate Software Ltd.
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 */
487
db48a77ec8ff Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 486
diff changeset
38 public class HgRepositoryLock {
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 /*
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 * Lock .hg/ except .hg/store/ .hg/wlock (HgRepository.repoPathHelper("wlock"))
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 * Lock .hg/store/ .hg/store/lock (HgRepository.repoPathHelper("store/lock") ???)
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 */
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 private final File lockFile;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 private int use = 0;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 private final int timeoutSeconds;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47
487
db48a77ec8ff Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 486
diff changeset
48 HgRepositoryLock(File lock, int timeoutInSeconds) {
486
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 lockFile = lock;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 timeoutSeconds = timeoutInSeconds;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 /**
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 * 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
55 * @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
56 */
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 public String readLockInfo() {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 if (lockFile.exists()) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 try {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 byte[] bytes = read(lockFile);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 if (bytes != null && bytes.length > 0) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 return new String(bytes);
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 } catch (Exception ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 // deliberately ignored
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 return null;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 public boolean isLocked() {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 return use > 0;
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 public void acquire() {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 if (use > 0) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 use++;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 return;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 StringBuilder lockDescription = new StringBuilder();
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 lockDescription.append(getHostname());
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 lockDescription.append(':');
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 lockDescription.append(getPid());
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 byte[] bytes = lockDescription.toString().getBytes();
487
db48a77ec8ff Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 486
diff changeset
85 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
86 do {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 synchronized(this) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 try {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 if (!lockFile.exists()) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 write(lockFile, bytes);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 use++;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 return;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 } catch (IOException ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 // deliberately ignored
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 try {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 wait(1000);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 } catch (InterruptedException ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 // deliberately ignored
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103
487
db48a77ec8ff Access to reposiotry lock mechanism via HgRepositoryLock
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 486
diff changeset
104 } 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
105 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
106 throw new HgInvalidStateException(msg);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 public void release() {
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 throw new HgInvalidStateException("");
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 use--;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 if (use > 0) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 return;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 // do release
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 lockFile.delete();
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 protected String getHostname() {
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 return InetAddress.getLocalHost().getHostName();
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 } catch (Exception ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 return "localhost";
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 }
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 protected int getPid() {
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 // @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
132 if (!Internals.runningOnWindows()) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133 File f = new File("/proc/self");
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
134 if (f.exists()) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
135 // /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
136 return Integer.parseInt(f.getCanonicalFile().getName());
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
137 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
138 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
139 String rtBean = ManagementFactory.getRuntimeMXBean().getName();
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
140 int x;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
141 if ((x = rtBean.indexOf('@')) != -1) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
142 return Integer.parseInt(rtBean.substring(0, x));
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
143 }
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
144 return -1;
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145 } catch (Exception ex) {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 return -1;
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 }
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 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
151 FileOutputStream fos = new FileOutputStream(f);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
152 fos.write(content);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153 fos.close();
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 private static byte[] read(File f) throws IOException {
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
157 FileChannel fc = new FileInputStream(f).getChannel();
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
158 ByteBuffer bb = ByteBuffer.allocate(Internals.ltoi(fc.size()));
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
159 fc.read(bb);
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 fc.close();
d740edfff563 Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161 return bb.array();
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 }