Mercurial > hg4j
annotate src/org/tmatesoft/hg/internal/Lock.java @ 486:d740edfff563
Provisional support for Mercurial lock mechanism
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 10 Aug 2012 21:03:03 +0200 |
parents | |
children |
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 */ |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
17 package org.tmatesoft.hg.internal; |
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 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 import org.tmatesoft.hg.repo.HgInvalidStateException; |
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 */ |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 public class Lock { |
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 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 public Lock(File lock) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49 this(lock, 10); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 } |
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 public Lock(File lock, int timeoutInSeconds) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 lockFile = lock; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 timeoutSeconds = timeoutInSeconds; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 } |
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 /** |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 * 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
|
59 * @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
|
60 */ |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 public String readLockInfo() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
62 if (lockFile.exists()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 byte[] bytes = read(lockFile); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 if (bytes != null && bytes.length > 0) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 return new String(bytes); |
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 } catch (Exception ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 // deliberately ignored |
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 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 return null; |
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 boolean isLocked() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 return use > 0; |
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 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 public void acquire() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 if (use > 0) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 use++; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 return; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 StringBuilder lockDescription = new StringBuilder(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 lockDescription.append(getHostname()); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
86 lockDescription.append(':'); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
87 lockDescription.append(getPid()); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
88 byte[] bytes = lockDescription.toString().getBytes(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 long stopTime = System.currentTimeMillis() + timeoutSeconds*1000; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
90 do { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
91 synchronized(this) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
92 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
93 if (!lockFile.exists()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
94 write(lockFile, bytes); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 use++; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
96 return; |
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 } catch (IOException ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
99 // deliberately ignored |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
100 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
101 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
102 wait(1000); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
103 } catch (InterruptedException ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 // deliberately ignored |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
106 } |
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 } while (System.currentTimeMillis() <= stopTime); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
109 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
|
110 throw new HgInvalidStateException(msg); |
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 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
113 public void release() { |
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 throw new HgInvalidStateException(""); |
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 use--; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
118 if (use > 0) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
119 return; |
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 // do release |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
122 lockFile.delete(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
123 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
124 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
125 protected String getHostname() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
126 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
127 return InetAddress.getLocalHost().getHostName(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
128 } catch (Exception ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
129 return "localhost"; |
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 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
132 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
133 protected int getPid() { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
134 try { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
135 // @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
|
136 if (!Internals.runningOnWindows()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
137 File f = new File("/proc/self"); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
138 if (f.exists()) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
139 // /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
|
140 return Integer.parseInt(f.getCanonicalFile().getName()); |
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 } |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
143 String rtBean = ManagementFactory.getRuntimeMXBean().getName(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
144 int x; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
145 if ((x = rtBean.indexOf('@')) != -1) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
146 return Integer.parseInt(rtBean.substring(0, x)); |
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 return -1; |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
149 } catch (Exception ex) { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
150 return -1; |
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 } |
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 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
|
155 FileOutputStream fos = new FileOutputStream(f); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
156 fos.write(content); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
157 fos.close(); |
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 |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
160 private static byte[] read(File f) throws IOException { |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
161 FileChannel fc = new FileInputStream(f).getChannel(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
162 ByteBuffer bb = ByteBuffer.allocate(Internals.ltoi(fc.size())); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
163 fc.read(bb); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
164 fc.close(); |
d740edfff563
Provisional support for Mercurial lock mechanism
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
165 return bb.array(); |
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 } |