Merge pull request #1030 from jeremylong/shutdownhook

Add Shutdown Hook to remove h2 lock file
This commit is contained in:
Jeremy Long
2017-12-16 07:40:33 -05:00
committed by GitHub
2 changed files with 75 additions and 10 deletions

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2017 OWASP.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.owasp.dependencycheck.utils;
/**
*
* @author jeremy
*/
public class H2DBCleanupHook extends Thread {
private final H2DBLock lock;
public H2DBCleanupHook(H2DBLock lock) {
this.lock = lock;
}
@Override
public void run() {
lock.release();
}
}

View File

@@ -69,6 +69,8 @@ public class H2DBLock {
*/
private final String magic;
private H2DBCleanupHook hook = null;
/**
* Constructs a new H2DB Lock object with the configured settings.
*
@@ -100,16 +102,7 @@ public class H2DBLock {
try {
final File dir = settings.getDataDirectory();
lockFile = new File(dir, "dc.update.lock");
if (!lockFile.getParentFile().isDirectory() && !lockFile.mkdir()) {
throw new H2DBLockException("Unable to create path to data directory.");
}
if (lockFile.isFile() && getFileAge(lockFile) > 30) {
LOGGER.debug("An old db update lock file was found: {}", lockFile.getAbsolutePath());
if (!lockFile.delete()) {
LOGGER.warn("An old db update lock file was found but the system was unable to delete "
+ "the file. Consider manually deleting {}", lockFile.getAbsolutePath());
}
}
checkState();
int ctr = 0;
do {
try {
@@ -126,6 +119,7 @@ public class H2DBLock {
lock = null;
LOGGER.debug("Another process obtained a lock first ({})", Thread.currentThread().getName());
} else {
addShutdownHook();
final Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LOGGER.debug("Lock file created ({}) {} @ {}", Thread.currentThread().getName(), magic, timestamp.toString());
}
@@ -162,6 +156,24 @@ public class H2DBLock {
}
}
private void checkState() throws H2DBLockException {
if (!lockFile.getParentFile().isDirectory() && !lockFile.mkdir()) {
throw new H2DBLockException("Unable to create path to data directory.");
}
if (lockFile.isFile()) {
if (getFileAge(lockFile) > 30) {
LOGGER.debug("An old db update lock file was found: {}", lockFile.getAbsolutePath());
if (!lockFile.delete()) {
LOGGER.warn("An old db update lock file was found but the system was unable to delete "
+ "the file. Consider manually deleting {}", lockFile.getAbsolutePath());
}
} else {
LOGGER.info("Lock file found `{}`", lockFile);
LOGGER.info("Existing update in progress; waiting for update to complete");
}
}
}
/**
* Releases the lock on the H2 database.
*/
@@ -196,6 +208,7 @@ public class H2DBLock {
}
}
lockFile = null;
removeShutdownHook();
final Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LOGGER.debug("Lock released ({}) {} @ {}", Thread.currentThread().getName(), magic, timestamp.toString());
}
@@ -213,4 +226,22 @@ public class H2DBLock {
LOGGER.debug("Lock file age is {} minutes", time);
return time;
}
private void addShutdownHook() {
if (hook == null) {
hook = new H2DBCleanupHook(this);
Runtime.getRuntime().addShutdownHook(hook);
}
}
private void removeShutdownHook() {
if (hook != null) {
try {
Runtime.getRuntime().removeShutdownHook(hook);
} catch (IllegalStateException ex) {
LOGGER.trace("ignore as we are likely shutting down", ex);
}
hook = null;
}
}
}