mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-01-14 07:43:40 +01:00
removed unused concurrency package
Former-commit-id: c2162dd0f9144400b464535ac4104c98cf6f1da6
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* This file is part of dependency-check-core.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Copyright (c) 2013 Jeremy Long. All Rights Reserved.
|
||||
*/
|
||||
package org.owasp.dependencycheck.concurrency;
|
||||
|
||||
/**
|
||||
* If thrown, indicates that a problem occurred when locking a directory.
|
||||
*
|
||||
* @author Jeremy Long <jeremy.long@owasp.org>
|
||||
*/
|
||||
public class DirectoryLockException extends Exception {
|
||||
|
||||
/**
|
||||
* Default serial version UID.
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Constructs a new Directory Lock Exception.
|
||||
*/
|
||||
public DirectoryLockException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new Directory Lock Exception.
|
||||
*
|
||||
* @param msg the message describing the exception
|
||||
*/
|
||||
public DirectoryLockException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new Directory Lock Exception.
|
||||
*
|
||||
* @param ex the cause of the exception
|
||||
*/
|
||||
public DirectoryLockException(Throwable ex) {
|
||||
super(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new Directory Lock Exception.
|
||||
*
|
||||
* @param msg the message describing the exception
|
||||
* @param ex the cause of the exception
|
||||
*/
|
||||
public DirectoryLockException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
}
|
||||
@@ -1,249 +0,0 @@
|
||||
/*
|
||||
* This file is part of dependency-check-core.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Copyright (c) 2013 Jeremy Long. All Rights Reserved.
|
||||
*/
|
||||
package org.owasp.dependencycheck.concurrency;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.channels.AsynchronousCloseException;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.nio.channels.FileLockInterruptionException;
|
||||
import java.nio.channels.NonWritableChannelException;
|
||||
import java.nio.channels.OverlappingFileLockException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Implements a spin lock on a given directory. If the lock cannot be obtained, the process will "spin" waiting for an
|
||||
* opportunity to obtain the lock requested.
|
||||
*
|
||||
* @author Jeremy Long <jeremy.long@owasp.org>
|
||||
*/
|
||||
public class DirectorySpinLock implements Closeable /*, AutoCloseable*/ {
|
||||
|
||||
/**
|
||||
* The name of the lock file.
|
||||
*/
|
||||
public static final String LOCK_NAME = "data.lock";
|
||||
/**
|
||||
* The maximum wait period used when attempting to obtain a lock.
|
||||
*/
|
||||
public static final int MAX_SPIN = 100;
|
||||
/**
|
||||
* The file channel used to perform the lock.
|
||||
*/
|
||||
private FileChannel channel = null;
|
||||
/**
|
||||
* The file used to perform the lock.
|
||||
*/
|
||||
private File lockFile = null;
|
||||
/**
|
||||
* The lock object.
|
||||
*/
|
||||
private FileLock lock = null;
|
||||
/**
|
||||
* The maximum number of seconds that the spin lock will wait while trying to obtain a lock.
|
||||
*/
|
||||
private long maxWait = MAX_SPIN;
|
||||
|
||||
/**
|
||||
* Get the maximum wait time, in seconds, that the spin lock will wait while trying to obtain a lock.
|
||||
*
|
||||
* @return the number of seconds the spin lock will wait
|
||||
*/
|
||||
public long getMaxWait() {
|
||||
return maxWait / 2; //sleep is for 500, so / 2
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maximum wait time, in seconds, that the spin lock will wait while trying to obtain a lock.
|
||||
*
|
||||
* @param maxWait the number of seconds the spin lock will wait
|
||||
*/
|
||||
public void setMaxWait(long maxWait) {
|
||||
this.maxWait = maxWait * 2; //sleep is for 500, so * 2
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new spin lock on the given directory.
|
||||
*
|
||||
* @param directory the directory to monitor/lock
|
||||
* @throws InvalidDirectoryException thrown if there is an issue with the directory provided
|
||||
* @throws DirectoryLockException thrown there is an issue obtaining a handle to the lock file
|
||||
*/
|
||||
public DirectorySpinLock(File directory) throws InvalidDirectoryException, DirectoryLockException {
|
||||
checkDirectory(directory);
|
||||
lockFile = new File(directory, LOCK_NAME);
|
||||
RandomAccessFile file = null;
|
||||
try {
|
||||
file = new RandomAccessFile(lockFile, "rw");
|
||||
} catch (FileNotFoundException ex) {
|
||||
throw new DirectoryLockException("Lock file not found", ex);
|
||||
}
|
||||
channel = file.getChannel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to obtain an exclusive lock; an exception is thrown if the lock could not be obtained. This method may
|
||||
* block for a few seconds if a lock cannot be obtained.
|
||||
*
|
||||
* @throws DirectoryLockException thrown if there is an exception obtaining the lock
|
||||
*/
|
||||
public void obtainSharedLock() throws DirectoryLockException {
|
||||
obtainLock(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to obtain an exclusive lock; an exception is thrown if the lock could not be obtained. This method may
|
||||
* block for a few seconds if a lock cannot be obtained.
|
||||
*
|
||||
* @throws DirectoryLockException thrown if there is an exception obtaining the lock
|
||||
*/
|
||||
public void obtainExclusiveLock() throws DirectoryLockException {
|
||||
obtainLock(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to obtain a lock; an exception is thrown if the lock could not be obtained. This method may block for a
|
||||
* few seconds if a lock cannot be obtained.
|
||||
*
|
||||
* @param shared true if the lock is shared, otherwise false
|
||||
* @param maxWait the maximum time to wait, in seconds, while trying to obtain the lock
|
||||
* @throws DirectoryLockException thrown if there is an exception obtaining the lock
|
||||
*/
|
||||
protected void obtainLock(boolean shared, long maxWait) throws DirectoryLockException {
|
||||
setMaxWait(maxWait);
|
||||
obtainLock(shared);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to obtain a lock; an exception is thrown if the lock could not be obtained. This method may block for a
|
||||
* few seconds if a lock cannot be obtained.
|
||||
*
|
||||
* @param shared true if the lock is shared, otherwise false
|
||||
* @throws DirectoryLockException thrown if there is an exception obtaining the lock
|
||||
*/
|
||||
protected void obtainLock(boolean shared) throws DirectoryLockException {
|
||||
if (lock != null) {
|
||||
release();
|
||||
}
|
||||
if (channel == null) {
|
||||
throw new DirectoryLockException("Unable to create lock, no file channel exists");
|
||||
}
|
||||
int count = 0;
|
||||
Exception lastException = null;
|
||||
while (lock == null && count++ < maxWait) {
|
||||
try {
|
||||
lock = channel.lock(0, Long.MAX_VALUE, shared);
|
||||
} catch (AsynchronousCloseException ex) {
|
||||
lastException = ex;
|
||||
} catch (ClosedChannelException ex) {
|
||||
lastException = ex;
|
||||
} catch (FileLockInterruptionException ex) {
|
||||
lastException = ex;
|
||||
} catch (OverlappingFileLockException ex) {
|
||||
lastException = ex;
|
||||
} catch (NonWritableChannelException ex) {
|
||||
lastException = ex;
|
||||
} catch (IOException ex) {
|
||||
lastException = ex;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
if (lock == null) {
|
||||
if (lastException == null) {
|
||||
throw new DirectoryLockException("Unable to obtain lock");
|
||||
} else {
|
||||
throw new DirectoryLockException("Unable to obtain lock", lastException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a few simple rudimentary checks on the specified directory. Specifically, does the file exist and is it
|
||||
* a directory.
|
||||
*
|
||||
* @param directory the File object to inspect
|
||||
* @throws InvalidDirectoryException thrown if the directory is null or is not a directory
|
||||
*/
|
||||
private void checkDirectory(File directory) throws InvalidDirectoryException {
|
||||
if (directory == null) {
|
||||
throw new InvalidDirectoryException("Unable to obtain lock on a null File");
|
||||
}
|
||||
if (!directory.isDirectory()) {
|
||||
final String msg = String.format("File, '%s', does not exist or is not a directory", directory.getAbsolutePath());
|
||||
throw new InvalidDirectoryException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases any locks and closes the underlying channel.
|
||||
*
|
||||
* @throws IOException if an IO Exception occurs
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
release();
|
||||
// TODO uncomment this once support for 1.6 is dropped.
|
||||
// if (lock != null) {
|
||||
// try {
|
||||
// lock.close();
|
||||
// } catch (IOException ex) {
|
||||
// Logger.getLogger(DirectorySpinLock.class.getName()).log(Level.FINEST, "Unable to close file lock due to IO Exception", ex);
|
||||
// }
|
||||
// }
|
||||
if (channel != null) {
|
||||
try {
|
||||
channel.close();
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(DirectorySpinLock.class.getName()).log(Level.FINEST, "Unable to close the channel for the file lock", ex);
|
||||
}
|
||||
}
|
||||
if (lockFile != null) {
|
||||
if (lockFile.exists()) {
|
||||
/* yes, this delete could fail which is totally fine. The other
|
||||
* thread holding the lock while delete it.
|
||||
*/
|
||||
lockFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the lock. Any exceptions that are thrown by the underlying lock during the release are ignored.
|
||||
*/
|
||||
public void release() {
|
||||
if (lock != null) {
|
||||
try {
|
||||
lock.release();
|
||||
} catch (ClosedChannelException ex) {
|
||||
Logger.getLogger(DirectorySpinLock.class.getName()).log(Level.FINEST, "Unable to release file lock", ex);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(DirectorySpinLock.class.getName()).log(Level.FINEST, "Unable to release file lock due to IO Exception", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* This file is part of dependency-check-core.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Copyright (c) 2013 Jeremy Long. All Rights Reserved.
|
||||
*/
|
||||
package org.owasp.dependencycheck.concurrency;
|
||||
|
||||
/**
|
||||
* If thrown, indicates that there is a problem with a directory.
|
||||
*
|
||||
* @author Jeremy Long <jeremy.long@owasp.org>
|
||||
*/
|
||||
public class InvalidDirectoryException extends Exception {
|
||||
|
||||
/**
|
||||
* Default serial version UID.
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Constructs a new Invalid Directory Exception.
|
||||
*/
|
||||
public InvalidDirectoryException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new Invalid Directory Exception.
|
||||
*
|
||||
* @param msg the message describing the exception
|
||||
*/
|
||||
public InvalidDirectoryException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new Invalid Directory Exception.
|
||||
*
|
||||
* @param ex the cause of the exception
|
||||
*/
|
||||
public InvalidDirectoryException(Throwable ex) {
|
||||
super(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new Invalid Directory Exception.
|
||||
*
|
||||
* @param msg the message describing the exception
|
||||
* @param ex the cause of the exception
|
||||
*/
|
||||
public InvalidDirectoryException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
/**
|
||||
* <html>
|
||||
* <head>
|
||||
* <title>org.owasp.dependencycheck.concurrency</title>
|
||||
* </head>
|
||||
* <body>
|
||||
* Contains classes used to create shared and exclusive locks on directories.
|
||||
* </body>
|
||||
* </html>
|
||||
*/
|
||||
package org.owasp.dependencycheck.concurrency;
|
||||
@@ -1,114 +0,0 @@
|
||||
/*
|
||||
* This file is part of dependency-check-core.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Copyright (c) 2013 Jeremy Long. All Rights Reserved.
|
||||
*/
|
||||
package org.owasp.dependencycheck.concurrency;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jeremy Long <jeremy.long@owasp.org>
|
||||
*/
|
||||
public class DirectorySpinLockTest {
|
||||
|
||||
public DirectorySpinLockTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of obtainSharedLock method, of class DirectorySpinLock. Specifically, this test uses the SpinLockTask to
|
||||
* obtain an exclusive lock that is held for 5 seconds. We then try to obtain a shared lock while that task is
|
||||
* running. It should take longer then 5 seconds to obtain the shared lock.
|
||||
*/
|
||||
@Test
|
||||
public void testObtainSharedLock_withContention() throws Exception {
|
||||
URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||
File directory = new File(location.getFile());
|
||||
DirectorySpinLock instance = new DirectorySpinLock(directory);
|
||||
SpinLockTask task = new SpinLockTask(directory, 5000, false, 2);
|
||||
long start = System.currentTimeMillis();
|
||||
task.run();
|
||||
instance.obtainSharedLock();
|
||||
long end = System.currentTimeMillis();
|
||||
instance.close();
|
||||
if (task.getException() != null) {
|
||||
throw task.getException();
|
||||
}
|
||||
long timeElapsed = end - start;
|
||||
assertTrue("no lock contention occurred?", timeElapsed >= 5000);
|
||||
//no exceptions means everything worked.
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of obtainSharedLock method, of class DirectorySpinLock. This method obtains two shared locks by using the
|
||||
* SpinLockTask to obtain a lock in another thread.
|
||||
*/
|
||||
@Test
|
||||
public void testObtainSharedLock() throws Exception {
|
||||
URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||
File directory = new File(location.getFile());
|
||||
DirectorySpinLock instance = new DirectorySpinLock(directory);
|
||||
SpinLockTask task = new SpinLockTask(directory, 1000, true, 2);
|
||||
task.run();
|
||||
instance.obtainSharedLock();
|
||||
instance.close();
|
||||
if (task.getException() != null) {
|
||||
throw task.getException();
|
||||
}
|
||||
//no exceptions means everything worked.
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of obtainExclusiveLock method, of class DirectorySpinLock.
|
||||
*/
|
||||
@Test
|
||||
public void testObtainExclusiveLock() throws Exception {
|
||||
URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||
File directory = new File(location.getFile());
|
||||
DirectorySpinLock instance = new DirectorySpinLock(directory);
|
||||
SpinLockTask task = new SpinLockTask(directory, 1000, true, 1);
|
||||
instance.obtainExclusiveLock();
|
||||
task.run();
|
||||
instance.close();
|
||||
assertNotNull("No exception thrown due to exclusive lock failure?", task.getException());
|
||||
assertEquals("Incorrect exception when obtaining exclusive lock", "Unable to obtain lock", task.getException().getMessage());
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* This file is part of dependency-check-core.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Copyright (c) 2013 Jeremy Long. All Rights Reserved.
|
||||
*/
|
||||
package org.owasp.dependencycheck.concurrency;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A simple task that obtains a lock on a directory. This is used in testing of the shared and exclusive locks.
|
||||
*
|
||||
* @author Jeremy Long <jeremy.long@owasp.org>
|
||||
*/
|
||||
public class SpinLockTask implements Runnable {
|
||||
|
||||
DirectorySpinLock lock = null;
|
||||
int holdLockFor;
|
||||
long maxWait;
|
||||
boolean shared;
|
||||
private Exception exception = null;
|
||||
|
||||
/**
|
||||
* Get the value of exception
|
||||
*
|
||||
* @return the value of exception
|
||||
*/
|
||||
public Exception getException() {
|
||||
return exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of exception
|
||||
*
|
||||
* @param exception new value of exception
|
||||
*/
|
||||
public void setException(Exception exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
public SpinLockTask(File directory, int holdLockFor, boolean shared, long maxWait) throws InvalidDirectoryException, DirectoryLockException {
|
||||
this.holdLockFor = holdLockFor;
|
||||
this.shared = shared;
|
||||
this.maxWait = maxWait;
|
||||
lock = new DirectorySpinLock(directory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
lock.obtainLock(shared, maxWait);
|
||||
Thread.sleep(holdLockFor);
|
||||
} catch (DirectoryLockException ex) {
|
||||
exception = ex;
|
||||
} catch (InterruptedException ex) {
|
||||
exception = ex;
|
||||
} finally {
|
||||
if (lock != null) {
|
||||
try {
|
||||
lock.close();
|
||||
} catch (IOException ex) {
|
||||
exception = ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user