-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,521 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.smallrye.common</groupId> | ||
<artifactId>smallrye-common-parent</artifactId> | ||
<version>2.5.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>smallrye-common-resource</artifactId> | ||
|
||
<name>SmallRye Common: Resources</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>smallrye-common-constraint</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
27 changes: 27 additions & 0 deletions
27
resource/src/main/java/io/smallrye/common/resource/EmptyDirectoryStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.smallrye.common.resource; | ||
|
||
import java.nio.file.DirectoryStream; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* An empty directory stream. | ||
*/ | ||
public final class EmptyDirectoryStream<T> implements DirectoryStream<T> { | ||
private static final EmptyDirectoryStream<Object> INSTANCE = new EmptyDirectoryStream<>(); | ||
|
||
private EmptyDirectoryStream() { | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> EmptyDirectoryStream<T> instance() { | ||
return (EmptyDirectoryStream<T>) INSTANCE; | ||
} | ||
|
||
public Iterator<T> iterator() { | ||
return Collections.emptyIterator(); | ||
} | ||
|
||
public void close() { | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
resource/src/main/java/io/smallrye/common/resource/JarFileResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package io.smallrye.common.resource; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.attribute.FileTime; | ||
import java.time.Instant; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
/** | ||
* A resource representing an entry in a JAR file. | ||
*/ | ||
public final class JarFileResource extends Resource { | ||
|
||
private final URL base; | ||
private final JarFile jarFile; | ||
private final JarEntry jarEntry; | ||
private URL url; | ||
|
||
JarFileResource(final URL base, final JarFile jarFile, final JarEntry jarEntry) { | ||
super(jarEntry.getName()); | ||
this.base = base; | ||
this.jarFile = jarFile; | ||
this.jarEntry = jarEntry; | ||
} | ||
|
||
public URL url() { | ||
URL url = this.url; | ||
if (url == null) { | ||
try { | ||
// todo: Java 20+: URL.of(new URI("jar", null, base.toURI().toASCIIString() + "!/" + pathName()), | ||
// new ResourceURLStreamHandler(this)); | ||
url = this.url = new URL(null, | ||
new URI("jar", null, base.toURI().toASCIIString() + "!/" + pathName()).toASCIIString(), | ||
new ResourceURLStreamHandler(this)); | ||
} catch (MalformedURLException | URISyntaxException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
return url; | ||
} | ||
|
||
public boolean isDirectory() { | ||
return jarEntry.isDirectory(); | ||
} | ||
|
||
public DirectoryStream<Resource> openDirectoryStream() { | ||
return new DirectoryStream<Resource>() { | ||
Enumeration<JarEntry> entries; | ||
|
||
public Iterator<Resource> iterator() { | ||
if (entries == null) { | ||
entries = jarFile.entries(); | ||
return new Iterator<Resource>() { | ||
Resource next; | ||
|
||
public boolean hasNext() { | ||
while (next == null) { | ||
if (!entries.hasMoreElements()) { | ||
return false; | ||
} | ||
JarEntry e = entries.nextElement(); | ||
if (e.getName().startsWith(jarEntry.getName())) { | ||
next = new JarFileResource(base, jarFile, e); | ||
break; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public Resource next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
Resource next = this.next; | ||
this.next = null; | ||
return next; | ||
} | ||
}; | ||
} | ||
throw new IllegalStateException(); | ||
} | ||
|
||
public void close() { | ||
entries = Collections.emptyEnumeration(); | ||
} | ||
}; | ||
} | ||
|
||
public Instant modifiedTime() { | ||
FileTime fileTime = jarEntry.getLastModifiedTime(); | ||
return fileTime == null ? null : fileTime.toInstant(); | ||
} | ||
|
||
public InputStream openStream() throws IOException { | ||
return jarFile.getInputStream(jarEntry); | ||
} | ||
|
||
public long size() { | ||
return jarEntry.getSize(); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
resource/src/main/java/io/smallrye/common/resource/JarFileResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.smallrye.common.resource; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
/** | ||
* A resource loader which corresponds to a JAR file. | ||
*/ | ||
public final class JarFileResourceLoader implements ResourceLoader { | ||
private final URL base; | ||
private final JarFile jarFile; | ||
private File tempFile; | ||
|
||
/** | ||
* Construct a new instance. | ||
* | ||
* @param jarPath the path of the JAR file (must not be {@code null}) | ||
* @throws IOException if opening the JAR file fails for some reason | ||
*/ | ||
public JarFileResourceLoader(final Path jarPath) throws IOException { | ||
this.base = jarPath.toUri().toURL(); | ||
jarFile = new JarFile(jarPath.toFile()); | ||
} | ||
|
||
/** | ||
* Construct a new instance from a JAR file contained within a resource. | ||
* | ||
* @param resource the resource of the JAR file (must not be {@code null}) | ||
* @throws IOException if opening the JAR file fails for some reason | ||
*/ | ||
public JarFileResourceLoader(final Resource resource) throws IOException { | ||
// todo: this will be replaced with a version which opens the file in-place from a buffer | ||
base = resource.url(); | ||
JarFile jf = null; | ||
if (resource instanceof PathResource pr) | ||
try { | ||
// avoid using a temp file, if possible | ||
jf = new JarFile(pr.path().toFile(), true, JarFile.OPEN_READ, JarFile.runtimeVersion()); | ||
} catch (UnsupportedOperationException ignored) { | ||
} | ||
if (jf == null) { | ||
tempFile = File.createTempFile("jar-", ""); | ||
try { | ||
try (InputStream is = resource.openStream()) { | ||
Files.copy(is, tempFile.toPath()); | ||
} | ||
jf = new JarFile(tempFile, true, JarFile.OPEN_READ, JarFile.runtimeVersion()); | ||
} catch (Throwable t) { | ||
try { | ||
Files.delete(tempFile.toPath()); | ||
} catch (IOException ignored) { | ||
} | ||
throw t; | ||
} | ||
} | ||
jarFile = jf; | ||
} | ||
|
||
public Resource findResource(final String path) { | ||
String canonPath = ResourceUtils.canonicalizeRelativePath(path); | ||
JarEntry jarEntry = jarFile.getJarEntry(canonPath); | ||
return new JarFileResource(base, jarFile, jarEntry); | ||
} | ||
|
||
public void close() { | ||
try { | ||
jarFile.close(); | ||
} catch (IOException ignored) { | ||
} | ||
if (tempFile != null) | ||
try { | ||
Files.delete(tempFile.toPath()); | ||
} catch (IOException ignored) { | ||
} finally { | ||
tempFile = null; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
resource/src/main/java/io/smallrye/common/resource/MappedDirectoryStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.smallrye.common.resource; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.DirectoryStream; | ||
import java.util.Iterator; | ||
import java.util.function.Function; | ||
|
||
import io.smallrye.common.constraint.Assert; | ||
|
||
/** | ||
* A directory stream to map one kind of entry to another. | ||
*/ | ||
public final class MappedDirectoryStream<T, R> implements DirectoryStream<R> { | ||
private final DirectoryStream<T> delegate; | ||
private final Function<T, R> mappingFunction; | ||
|
||
/** | ||
* Construct a new instance. | ||
* | ||
* @param delegate the delegate stream (must not be {@code null}) | ||
* @param mappingFunction the mapping function (must not be {@code null}) | ||
*/ | ||
public MappedDirectoryStream(final DirectoryStream<T> delegate, final Function<T, R> mappingFunction) { | ||
this.delegate = Assert.checkNotNullParam("delegate", delegate); | ||
this.mappingFunction = Assert.checkNotNullParam("mappingFunction", mappingFunction); | ||
} | ||
|
||
public Iterator<R> iterator() { | ||
Iterator<T> itr = delegate.iterator(); | ||
return new Iterator<R>() { | ||
public boolean hasNext() { | ||
return itr.hasNext(); | ||
} | ||
|
||
public R next() { | ||
return mappingFunction.apply(itr.next()); | ||
} | ||
}; | ||
} | ||
|
||
public void close() throws IOException { | ||
delegate.close(); | ||
} | ||
} |
Oops, something went wrong.