Skip to content

Commit

Permalink
[MODULES-216] ResourceLoader interface must extend AutoClosable inter…
Browse files Browse the repository at this point in the history
…face.
  • Loading branch information
ropalka committed Sep 10, 2015
1 parent a76225d commit 4ea4141
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 51 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
<properties>
<skip.enforcer>false</skip.enforcer>
<skip.compile>false</skip.compile>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
Expand Down
52 changes: 2 additions & 50 deletions src/main/java/org/jboss/modules/AbstractResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* An abstract resource loader implementation.
*
* @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
public abstract class AbstractResourceLoader implements ResourceLoader {
private static String getDefinedAttribute(Attributes.Name name, Attributes entryAttribute, Attributes mainAttribute) {
Expand Down Expand Up @@ -63,77 +64,28 @@ protected static PackageSpec getPackageSpec(final String name, final Manifest ma
return spec;
}

/**
* Get the name of the root represented by this resource loader. Returns an empty string by default.
*
* @return the name of the root
*/
public String getRootName() {
return "";
}

/**
* Get the class specification for the given class name. If no matching class is found, {@code null} is returned.
* Returns {@code null} by default.
*
* @param fileName the fileName of the class, e.g. for the class <code>org.jboss.modules.ResourceLoader</code> the
* fileName will be <code>org/jboss/modules/ResourceLoader.class</code>
*
* @return the class specification, or {@code null} if the named class is not found
*
* @throws java.io.IOException if an I/O error occurs
*/
public ClassSpec getClassSpec(final String fileName) throws IOException {
return null;
}

/**
* Get the package specification for the given directory name. Always returns a package specification; this method
* cannot be used to test for the existence of a package. A package spec should always be acquired from the same
* resource loader which provided the class specification. The directory name will always be specified using "{@code
* /}" separators. Returns {@code null} by default.
*
* @param name the directory name
*
* @return the package specification
*
* @throws java.io.IOException if an I/O error occurs
*/
public PackageSpec getPackageSpec(final String name) throws IOException {
return null;
}

/**
* Get a resource with the given name. If no such resource is available, {@code null} is returned. The resource name
* will always be specified using "{@code /}" separators for the directory segments. Returns {@code null} by default.
*
* @param name the resource name
*
* @return the resource, or {@code null} if it is not available
*/
public Resource getResource(final String name) {
return null;
}

/**
* Get the absolute physical filesystem path for a library with the given name. The resultant path should be
* path-separated using "{@code /}" characters. Returns {@code null} by default.
*
* @param name the name
*
* @return the path or {@code null} if the library is not present
*/
public String getLibrary(final String name) {
return null;
}

/**
* Get the collection of resource paths. Called one time only when the resource loader is initialized. The paths
* should use "{@code /}" characters to separate the path segments. Returns an empty set by default.
*
* @return the resource paths
*/
public Collection<String> getPaths() {
return Collections.emptySet();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public Collection<String> getPaths() {
public Iterator<Resource> iterateResources(final String startPath, final boolean recursive) {
return PathFilters.filtered(filter, loader.iterateResources(PathUtils.relativize(PathUtils.canonicalize(startPath)), recursive));
}

public void close() {
loader.close();
}

}
8 changes: 8 additions & 0 deletions src/main/java/org/jboss/modules/FilteredResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import java.util.Collection;
import org.jboss.modules.filter.PathFilter;

/**
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
final class FilteredResourceLoader implements ResourceLoader {

private final PathFilter filter;
Expand Down Expand Up @@ -57,4 +61,8 @@ public String getLibrary(final String name) {
public Collection<String> getPaths() {
return loader.getPaths();
}

public void close() {
loader.close();
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/jboss/modules/JarFileResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,19 @@ public Collection<String> getPaths() {
return index;
}

@Override
public void close() {
try {
super.close();
} finally {
try {
jarFile.close();
} catch (IOException e) {
// ignored
}
}
}

static void extractJarPaths(final JarFile jarFile, String relativePath,
final Collection<String> index) {
index.add("");
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/jboss/modules/ResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
* A loader for resources from a specific resource root within a module.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
public interface ResourceLoader {
public interface ResourceLoader extends AutoCloseable {

/**
* Get the name of the root represented by this resource loader.
Expand Down Expand Up @@ -82,4 +83,12 @@ public interface ResourceLoader {
* @return the resource paths
*/
Collection<String> getPaths();

/**
* Closes this resource, relinquishing any underlying resources.
* This method is invoked automatically on objects managed by the
* {@code try}-with-resources statement.
*/
default void close() {}

}

0 comments on commit 4ea4141

Please sign in to comment.