📄 lockfile.java
字号:
} /** * Writes the current hearbeat timestamp value to this object's lock * file. <p> * * @throws FileSecurityException possibly never (seek and write are native * methods whose JavaDoc entries do not actually specifiy throwing * <tt>SecurityException</tt>). However, it is conceivable that these * native methods may, in turn, access Java methods that do throw * <tt>SecurityException</tt>. In this case, a * <tt>SecurityException</tt> might be thrown if a required system * property value cannot be accessed, or if a security manager exists * and its <tt>{@link * java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)}</tt> * method denies write access to the file * @throws UnexpectedEndOfFileException if an end of file exception is * thrown while attepting to write the heartbeat timestamp value to * the target file (typically, this cannot happen, but the case is * included to distiguish it from the general IOException case). * @throws UnexpectedFileIOException if the current heartbeat timestamp * value cannot be written due to an underlying I/O error */ private final void writeHeartbeat() throws LockFile.FileSecurityException, LockFile.UnexpectedEndOfFileException, LockFile.UnexpectedFileIOException { try { raf.seek(MAGIC.length); raf.writeLong(System.currentTimeMillis()); } catch (SecurityException ex) { throw new FileSecurityException(this, "writeHeartbeat", ex); } catch (EOFException ex) { throw new UnexpectedEndOfFileException(this, "writeHeartbeat", ex); } catch (IOException ex) { throw new UnexpectedFileIOException(this, "writeHeartbeat", ex); } } /** * Tests whether some other object is "equal to" this one. <p> * * An object is considered equal to a <tt>LockFile</tt> object if and * only if it is not null, it is an instance of <tt>LockFile</tt> and * either it is the identical instance or it has the same lock file. More * formally, is is considered equal if and only if it is not null, it is an * instance of <tt>LockFile</tt>, and the expression: <p> * * <pre> * this == other || * this.file == null ? other.file == null : this.file.equals(other.file); * </pre> * * yeilds true. <p> * * Note that <tt>file</tt> must be a canonical reference to correctly * satisfy this contract. <p> * * @param obj the reference object with which to compare. * @return <tt>true</tt> if this object is equal to the <tt>obj</tt> * argument; <tt>false</tt> otherwise. * @see #hashCode */ public final boolean equals(final Object obj) { if (this == obj) { return true; } else if (obj instanceof LockFile) { LockFile other = (LockFile) obj; return (this.file == null) ? other.file == null : this.file.equals(other.file); } return false; } /** * Retrieves the canonical path of this object's lock file, as a * <tt>String</tt> object. <p> * * @return the canonical path of this object's lock file. */ public final String getCanonicalPath() { return cpath; } /** * Retrieves the hash code value for this object. <p> * * The value is zero if <tt>file</tt> is <tt>null</tt>, else the * <tt>hashCode</tt> of <tt>file</tt>. That is, two <tt>LockFile</tt> * objects have the same <tt>hashCode</tt> value if they refer to the * same lock file. <p> * * Note that <tt>file</tt> must be a canonical reference to correctly * satisfy this contract. <p> * * @return a hash code value for this object. * @see #equals(java.lang.Object) */ public final int hashCode() { return file == null ? 0 : file.hashCode(); } /** * Retrieves whether this object has successfully obtained and is still * holding (has not yet released) a cooperative lock condition on its * lock file. <p> * * <b>Note:</b> <p> * * Due to platform-independence retrictions placed on a JVM, it is quite * possible to successfully acquire a lock condition and yet for the * condition to become invalid while still held. <p> * * For instance, under JVMs with no <tt>java.nio</tt> package or under * operating systems that do not apply mandatory file locking (espcially * mandatory locking that precludes deletion), it is quite possible for * another process or even an uncooperative bit of code running in the same * JVM to overwrite or delete the target lock file while this object holds * a lock condition. <p> * * Because of this, the <tt>isValid()</tt> method is provided in the public * interface in order to allow clients to detect at least a subset of such * situations. <p> * * @return <tt>true</tt> if this object has successfully obtained and is * still holding (has not yet released) a lock condition, else * <tt>false</tt> * @see #isValid */ public final boolean isLocked() { return locked; } /** * Retrieves whether there is potentially already a cooperative lock, * operating system lock or some other situation preventing a cooperative * lock condition from being aquired using the specified path. * * @param path the path to test * @return <tt>true</tt> if there is currently something preventing the * acquisition of a cooperative lock condition using the specified * <tt>path</tt>, else <tt>false</tt> */ public final static boolean isLocked(final String path) { boolean locked = true; try { LockFile lockFile = LockFile.newLockFile(path); lockFile.checkHeartbeat(false); locked = false; } catch (Exception e) {} return locked; } /** * Retrieves whether this object holds a valid lock condition on its * lock file. <p> * * More formally, this method retrieves true if and only if: <p> * * <pre> * isLocked() && file != null && file.exists() && raf != null * </pre> * * @return <tt>true</tt> if this object holds a valid lock condition on its * lock file; else <tt>false</tt> * @throws SecurityException if a required system property value cannot * be accessed, or if a Java security manager exists and its * <tt>checkRead</tt> method denies read access to the lock file; */ public boolean isValid() { return isLocked() && file != null && file.exists() && raf != null; } /** * Retrieves a String representation of this object. <p> * * The String is of the form: <p> * * <pre> * super.toString() + * "[file=" + getCanonicalPath() + * ", exists=" + file.exists() + * ", locked=" + isLocked() + * ", valid=" + isValid() + * ", " + toStringImpl() + * "]"; * </pre> * * * @return a String representation of this object. * @see #toStringImpl * @throws SecurityException if a required system property value cannot * be accessed, or if a security manager exists and its <tt>{@link * java.lang.SecurityManager#checkRead}</tt> method denies * read access to the lock file; */ public String toString() { return new StringBuffer(super.toString()).append("[file =").append( cpath).append(", exists=").append(file.exists()).append( ", locked=").append(isLocked()).append(", valid=").append( isValid()).append(", ").append(toStringImpl()).append( "]").toString(); } /** * Retrieves an implementation-specific tail value for the * <tt>toString()</tt> method. <p> * * The default implementation returns the empty string. * * @return an implementation-specific tail value for the <tt>toString()</tt> * method * @see #toString */ protected String toStringImpl() { return ""; } /** * Retrieves the number of times <tt>checkHeartbeat</tt> may fail before * <tt>pollHeartbeat</tt> fails as a consequence. <p> * * The value is obtained in the following manner: <p> * * <ol> * <li>retries is assigned <tt>POLL_RETRIES_DEFAULT</tt>. * * <li>retries is assigned <tt>Integer.getInteger(POLL_RETRIES_PROPERTY, * retries)</tt> inside a try-catch block to silently ignore any security * exception. * * <li>If retries is less than one (1), retries is assigned one (1). * </ol> * * @return the number of times <tt>checkHeartbeat</tt> may fail before * <tt>pollHeartbeat</tt> fails as a consequence. */ public int getPollHeartbeatRetries() { int retries = POLL_RETRIES_DEFAULT; try { retries = Integer.getInteger( HsqlDatabaseProperties.system_lockfile_poll_retries_property, retries).intValue(); } catch (Exception e) {} if (retries < 1) { retries = 1; } return retries; } /** * Retrieves the interval, in milliseconds, that <tt>pollHeartbeat</tt> * waits between failed invocations of <tt>checkHeartbeat</tt>. * * The value is obtained in the following manner: <p> * * <ol> * <li>interval is assigned <tt>10 + (HEARTBEAT_INTERVAL_PADDED * getPollHeartbeatRetries())</tt> * * <li>interval is assigned <tt>Long.getLong(POLL_INTERVAL_PROPERTY, * interval)</tt>, inside a try-catch block, to silently ignore any security * exception. * * <li>If interval is less than or equal to zero (0), interval is reassigned * <tt>10 + (HEARTBEAT_INTERVAL_PADDED / getPollHeartbeatRetries())</tt> * </ol> * * @return the interval, in milliseconds, that <tt>pollHeartbeat</tt> * waits between failed invocations of <tt>checkHeartbeat</tt> */ public long getPollHeartbeatInterval() { int retries = getPollHeartbeatRetries(); long interval = 10 + (HEARTBEAT_INTERVAL_PADDED / retries); try { interval = Long.getLong(POLL_INTERVAL_PROPERTY, interval).longValue(); } catch (Exception e) {} if (interval <= 0) { interval = 10 + (HEARTBEAT_INTERVAL_PADDED / retries); } return interval; } /** * Polls the underlying lock file to determine if a lock condition * exists. <p> * * Specifically, polls {@link #checkHeartbeat(boolean) checkHeartbeat} at * the configured interval until the check passes, the current p
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -