Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Aug 2, 2023
1 parent d60cdfc commit 0a0feb6
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 182 deletions.
Expand Up @@ -64,6 +64,8 @@ class JarFileManager implements Closeable {

private final ScheduledExecutorService scheduler = newScheduledThreadPool(1, new JarFileManagerThreadFactory());
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Lock readLock = lock.readLock();
private final Lock writeLock = lock.writeLock();

private volatile long lastJarFileAccess;
private ScheduledFuture<?> unusedJarsCheck;
Expand All @@ -72,9 +74,8 @@ class JarFileManager implements Closeable {


void addJarFile(File file) {
Lock writeLock = lock.writeLock();
writeLock.lock();
try {
writeLock.lock();
files.add(new JarResource(file));
} finally {
writeLock.unlock();
Expand All @@ -89,9 +90,8 @@ JarFile[] getJarFiles() {
if (!isJarsOpen() && !openJARs()) {
return null;
}
Lock readLock = lock.readLock();
readLock.lock();
try {
readLock.lock();
lastJarFileAccess = System.currentTimeMillis();
return files.stream().map(r -> r.jarFile).toArray(JarFile[]::new);
} finally {
Expand All @@ -101,9 +101,8 @@ JarFile[] getJarFiles() {


File[] getJarRealFiles() {
Lock readLock = lock.readLock();
readLock.lock();
try {
readLock.lock();
return files.stream().map(r -> r.file).toArray(File[]::new);
} finally {
readLock.unlock();
Expand All @@ -122,9 +121,8 @@ ResourceEntry findResource(String name, String path, File loaderDir, boolean ant
if (!isJarsOpen() && !openJARs()) {
return null;
}
final Lock readLock = lock.readLock();
readLock.lock();
try {
readLock.lock();
lastJarFileAccess = System.currentTimeMillis();
for (JarResource jarResource : files) {
final JarFile jarFile = jarResource.jarFile;
Expand Down Expand Up @@ -159,9 +157,8 @@ void extractResources(File loaderDir, String canonicalLoaderDir) {
if (resourcesExtracted) {
return;
}
Lock readLock = lock.readLock();
readLock.lock();
try {
readLock.lock();
for (JarResource jarResource : files) {
extractResource(jarResource.jarFile, loaderDir, canonicalLoaderDir);
}
Expand All @@ -177,9 +174,8 @@ void extractResources(File loaderDir, String canonicalLoaderDir) {
*/
void closeJarFiles() {
LOG.log(DEBUG, "closeJarFiles()");
Lock writeLock = lock.writeLock();
writeLock.lock();
try {
writeLock.lock();
lastJarFileAccess = 0L;
closeJarFiles(files);
} finally {
Expand All @@ -202,9 +198,8 @@ public void close() throws IOException {
*/
private boolean openJARs() {
LOG.log(DEBUG, "openJARs()");
Lock writeLock = lock.writeLock();
writeLock.lock();
try {
writeLock.lock();
if (isJarsOpen()) {
return true;
}
Expand Down Expand Up @@ -237,8 +232,7 @@ private boolean isJarsOpen() {
}


private ResourceEntry createResourceEntry(String name, File file, JarFile jarFile, JarEntry jarEntry,
String entryPath) {
private ResourceEntry createResourceEntry(String name, File file, JarFile jarFile, JarEntry jarEntry, String entryPath) {
final URL codeBase;
try {
codeBase = file.getCanonicalFile().toURI().toURL();
Expand Down Expand Up @@ -276,29 +270,28 @@ private ResourceEntry createResourceEntry(String name, File file, JarFile jarFil

private static void extractResource(JarFile jarFile, File loaderDir, String pathPrefix) {
LOG.log(DEBUG, "extractResource(jarFile={0}, loaderDir={1}, pathPrefix={2})", jarFile, loaderDir, pathPrefix);
Iterator<JarEntry> jarEntries = jarFile.versionedStream().iterator();
Iterator<JarEntry> jarEntries = jarFile.versionedStream()
.filter(jarEntry -> !jarEntry.isDirectory() && !jarEntry.getName().endsWith(".class")).iterator();
while (jarEntries.hasNext()) {
JarEntry jarEntry = jarEntries.next();
if (!jarEntry.isDirectory() && !jarEntry.getName().endsWith(".class")) {
File resourceFile = new File(loaderDir, jarEntry.getName());
try {
if (!resourceFile.getCanonicalPath().startsWith(pathPrefix)) {
throw new IllegalArgumentException(getString(LogFacade.ILLEGAL_JAR_PATH, jarEntry.getName()));
}
} catch (IOException ioe) {
throw new IllegalArgumentException(
getString(LogFacade.VALIDATION_ERROR_JAR_PATH, jarEntry.getName()), ioe);
}
if (!FileUtils.mkdirsMaybe(resourceFile.getParentFile())) {
LOG.log(WARNING, LogFacade.UNABLE_TO_CREATE, resourceFile.getParentFile());
File resourceFile = new File(loaderDir, jarEntry.getName());
try {
if (!resourceFile.getCanonicalPath().startsWith(pathPrefix)) {
throw new IllegalArgumentException(getString(LogFacade.ILLEGAL_JAR_PATH, jarEntry.getName()));
}
} catch (IOException ioe) {
throw new IllegalArgumentException(
getString(LogFacade.VALIDATION_ERROR_JAR_PATH, jarEntry.getName()), ioe);
}
if (!FileUtils.mkdirsMaybe(resourceFile.getParentFile())) {
LOG.log(WARNING, LogFacade.UNABLE_TO_CREATE, resourceFile.getParentFile());
}

try (InputStream is = jarFile.getInputStream(jarEntry);
FileOutputStream os = new FileOutputStream(resourceFile)) {
FileUtils.copy(is, os, Long.MAX_VALUE);
} catch (IOException e) {
LOG.log(DEBUG, "Failed to copy entry " + jarEntry, e);
}
try (InputStream is = jarFile.getInputStream(jarEntry);
FileOutputStream os = new FileOutputStream(resourceFile)) {
FileUtils.copy(is, os, Long.MAX_VALUE);
} catch (IOException e) {
LOG.log(DEBUG, "Failed to copy entry " + jarEntry, e);
}
}
}
Expand Down

0 comments on commit 0a0feb6

Please sign in to comment.