mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-25 18:41:44 +01:00
Merge pull request #1030 from jeremylong/shutdownhook
Add Shutdown Hook to remove h2 lock file
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -69,6 +69,8 @@ public class H2DBLock {
|
|||||||
*/
|
*/
|
||||||
private final String magic;
|
private final String magic;
|
||||||
|
|
||||||
|
private H2DBCleanupHook hook = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new H2DB Lock object with the configured settings.
|
* Constructs a new H2DB Lock object with the configured settings.
|
||||||
*
|
*
|
||||||
@@ -100,16 +102,7 @@ public class H2DBLock {
|
|||||||
try {
|
try {
|
||||||
final File dir = settings.getDataDirectory();
|
final File dir = settings.getDataDirectory();
|
||||||
lockFile = new File(dir, "dc.update.lock");
|
lockFile = new File(dir, "dc.update.lock");
|
||||||
if (!lockFile.getParentFile().isDirectory() && !lockFile.mkdir()) {
|
checkState();
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int ctr = 0;
|
int ctr = 0;
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
@@ -126,6 +119,7 @@ public class H2DBLock {
|
|||||||
lock = null;
|
lock = null;
|
||||||
LOGGER.debug("Another process obtained a lock first ({})", Thread.currentThread().getName());
|
LOGGER.debug("Another process obtained a lock first ({})", Thread.currentThread().getName());
|
||||||
} else {
|
} else {
|
||||||
|
addShutdownHook();
|
||||||
final Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
final Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||||
LOGGER.debug("Lock file created ({}) {} @ {}", Thread.currentThread().getName(), magic, timestamp.toString());
|
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.
|
* Releases the lock on the H2 database.
|
||||||
*/
|
*/
|
||||||
@@ -196,6 +208,7 @@ public class H2DBLock {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
lockFile = null;
|
lockFile = null;
|
||||||
|
removeShutdownHook();
|
||||||
final Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
final Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
||||||
LOGGER.debug("Lock released ({}) {} @ {}", Thread.currentThread().getName(), magic, timestamp.toString());
|
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);
|
LOGGER.debug("Lock file age is {} minutes", time);
|
||||||
return 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user