Skip to content

Commit

Permalink
Add logging
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 Jun 13, 2024
1 parent ba95b34 commit 8677ccd
Showing 1 changed file with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.System.Logger;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -58,6 +59,9 @@
import org.glassfish.internal.api.DelegatingClassLoader.ClassFinder;
import org.jvnet.hk2.annotations.Service;

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.TRACE;
import static java.lang.System.Logger.Level.WARNING;
import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE;
import static java.util.Collections.emptyEnumeration;
import static java.util.Collections.enumeration;
Expand All @@ -78,6 +82,8 @@
@Singleton
public class AppLibClassLoaderServiceImpl implements EventListener {

private static final Logger LOG = System.getLogger(AppLibClassLoaderServiceImpl.class.getName());

/**
* Class finders' registry.
* <p>
Expand Down Expand Up @@ -105,10 +111,10 @@ public void event(@RestrictTo(PREPARE_SHUTDOWN_NAME) Event<?> event) {
for (Library library : classFinderRegistry.keySet()) {
if (library.isSnapshot()) {
try {
// Remove library snapshots on exit
// Delete on close application library snapshot
Files.newInputStream(Path.of(library.getURI()), DELETE_ON_CLOSE).close();
} catch (IOException e) {
// do nothing
LOG.log(WARNING, () -> "Could not delete on close application library snapshot " + library.getURI(), e);
}
}
}
Expand Down Expand Up @@ -324,6 +330,8 @@ public Enumeration<URL> findResources(String name) throws IOException {
*/
private static class Library {

private static final Logger LOG = System.getLogger(Library.class.getName());

/**
* Represents a method to read the attributes of an open file.
*/
Expand Down Expand Up @@ -383,7 +391,7 @@ private static class Library {
try {
this.fileInputStream = new FileInputStream(new File(libURI));
} catch (IOException e) {
// do nothing
LOG.log(WARNING, () -> "Could not open input stream for application library " + libURI, e);
}

BasicFileAttributes attributes = null;
Expand Down Expand Up @@ -412,8 +420,8 @@ public URI getURI() {
if (source == null) {
File snapshot = createSnapshot();
if (snapshot != null) {
// Snapshot successfully created.
// Use it URI as a library source.
LOG.log(TRACE, "Created snapshot {0} for application library {1}", snapshot, originalSource);
// Use snapshot URI as a library source.
source = snapshot.toURI();
} else {
// Snapshot creation failed.
Expand All @@ -437,7 +445,7 @@ public void close() {
try {
fileInputStream.close();
} catch (IOException e) {
// do nothing
LOG.log(WARNING, () -> "Could not close input stream for application library " + source, e);
}
}
}
Expand Down Expand Up @@ -506,9 +514,11 @@ private Object getNativeDescriptor(FileInputStream fileInputStream) {
FileDescriptor fileDescriptor = fileInputStream.getFD();
if (fileDescriptor.valid()) {
nativeDescriptor = nativeDescriptorField.get(fileDescriptor);
LOG.log(TRACE, "Returning nativeDescriptor={0} for application library {1}",
nativeDescriptor, source);
}
} catch (IllegalAccessException | IOException e) {
// do nothing
LOG.log(WARNING, () -> "Could not obtain native descriptor for application library " + source, e);
}
}
return nativeDescriptor;
Expand All @@ -521,12 +531,14 @@ private Object getNativeDescriptor(FileInputStream fileInputStream) {
* @return the file attributes or {@code null} if an error occurs
*/
private BasicFileAttributes readAttributes(Object nativeDescriptor) {
LOG.log(DEBUG, "readAttributes(nativeDescriptor={0})", nativeDescriptor);
BasicFileAttributes attributes = null;
if (readAttributesMethod != null) {
try {
attributes = (BasicFileAttributes) readAttributesMethod.invoke(null, nativeDescriptor);
} catch (ReflectiveOperationException e) {
// do nothing
} catch (Exception e) {
LOG.log(WARNING, () -> "Could not read file attributes for nativeDescriptor="
+ nativeDescriptor, e);
}
}
return attributes;
Expand All @@ -540,9 +552,11 @@ private BasicFileAttributes readAttributes(Object nativeDescriptor) {
* @return the file attributes or {@code null} if an error occurs
*/
private BasicFileAttributes readAttributes(URI libURI) {
LOG.log(DEBUG, "readAttributes(libURI={0})", libURI);
try {
return Files.readAttributes(Path.of(libURI), BasicFileAttributes.class);
} catch (IOException e) {
LOG.log(WARNING, () -> "Could not read file attributes for libURI=" + libURI, e);
return null;
}
}
Expand All @@ -555,6 +569,7 @@ private BasicFileAttributes readAttributes(URI libURI) {
* @return an abstract pathname denoting a newly-created snapshot
*/
private File createSnapshot() {
LOG.log(DEBUG, "createSnapshot()");
File snapshot = null;
try {
snapshot = File.createTempFile("applib", ".jar");
Expand All @@ -563,6 +578,7 @@ private File createSnapshot() {
}
snapshot.deleteOnExit();
} catch (IOException e) {
LOG.log(WARNING, () -> "Could not create snapshot for application library " + source, e);
FileUtils.deleteFileMaybe(snapshot);
} finally {
fileInputStream = null;
Expand Down

0 comments on commit 8677ccd

Please sign in to comment.