![JAR search and dependency download from the Maven repository](/logo.png)
org.daisy.pipeline.braille.common.BundledResourcePath Maven / Gradle / Ivy
package org.daisy.pipeline.braille.common;
import java.io.File;
import java.net.URI;
import java.net.URL;
import static java.nio.file.Files.createTempDirectory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import static com.google.common.base.Predicates.alwaysTrue;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import org.daisy.common.file.URLs;
import static org.daisy.pipeline.braille.common.util.Iterators.partition;
import static org.daisy.pipeline.braille.common.util.Predicates.matchesGlobPattern;
import static org.daisy.pipeline.braille.common.util.Tuple2;
import static org.daisy.pipeline.braille.common.util.Files.normalize;
public class BundledResourcePath extends AbstractResourcePath {
protected static final String IDENTIFIER = "identifier";
protected static final String PATH = "path";
protected static final String UNPACK = "unpack";
protected static final String EXECUTABLES = "executables";
protected static final String INCLUDES = "includes";
private URI identifier = null;
private URL path = null;
private File unpackDir = null;
private boolean unpacking = false;
private boolean executables = false;
/* The included resources as relative paths */
private Collection resources = null;
protected void activate(final Map,?> properties, Class> context) throws IllegalArgumentException {
if (properties.get(IDENTIFIER) == null || properties.get(IDENTIFIER).toString().isEmpty()) {
throw new IllegalArgumentException(IDENTIFIER + " property must not be empty"); }
String identifierAsString = properties.get(IDENTIFIER).toString();
if (!identifierAsString.endsWith("/")) identifierAsString += "/";
try { identifier = URLs.asURI(identifierAsString); }
catch (Exception e) {
throw new IllegalArgumentException(IDENTIFIER + " could not be parsed into a URI"); }
if (!identifier.isAbsolute())
throw new IllegalArgumentException(IDENTIFIER + " must be an absolute URI");
if (properties.get(PATH) == null || properties.get(PATH).toString().isEmpty()) {
throw new IllegalArgumentException(PATH + " property must not be empty"); }
String pathAsRelativeFilePath = properties.get(PATH).toString();
path = URLs.getResourceFromJAR(pathAsRelativeFilePath, context);
if (path == null)
throw new IllegalArgumentException("Resource path at location " + pathAsRelativeFilePath + " could not be found");
final Predicate includes =
(properties.get(INCLUDES) != null
&& !properties.get(INCLUDES).toString().isEmpty()
&& !properties.get(INCLUDES).toString().equals("*")) ?
Predicates.compose(
matchesGlobPattern(properties.get(INCLUDES).toString()),
URI::getPath) :
alwaysTrue();
Function> getFilePaths = new Function>() {
public Collection apply(String path) {
Tuple2,Collection> entries = partition(
URLs.listResourcesFromJAR(path, context),
s -> s.endsWith("/"));
Collection files = new ArrayList();
files.addAll(entries._2);
for (String folder : entries._1) files.addAll(apply(folder));
return files; }};
resources = new ImmutableList.Builder()
.addAll(
Collections2.filter(
Collections2.transform(
getFilePaths.apply(pathAsRelativeFilePath),
s -> URLs.relativize(URLs.asURI(path), URLs.asURI(URLs.getResourceFromJAR(s, context)))),
includes))
.build();
if (properties.get(UNPACK) != null && (Boolean)properties.get(UNPACK))
unpacking = true;
if (properties.get(EXECUTABLES) != null && (Boolean)properties.get(EXECUTABLES))
executables = true;
}
public URI getIdentifier() {
return identifier;
}
protected boolean containsResource(URI resource) {
return resources.contains(resource);
}
protected Collection listResources() {
return resources;
}
protected URL getBasePath() {
return path;
}
protected boolean isUnpacking() {
return unpacking;
}
@Override
protected File makeUnpackDir() {
if (!unpacking)
return null;
if (unpackDir == null) {
File tmpDirectory; {
try {
tmpDirectory = createTempDirectory("pipeline-").toFile(); }
catch (Exception e) {
throw new RuntimeException("Could not create temporary directory", e); }
tmpDirectory.deleteOnExit();
}
unpackDir = normalize(tmpDirectory); }
return unpackDir;
}
protected boolean isExecutable(URI resources) {
return executables;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy