Skip to content
Snippets Groups Projects
Commit 74e6b3e0 authored by Nikolaus Krismer's avatar Nikolaus Krismer
Browse files

reworked config file retrieval for dataset configurations

parent 980fb4c5
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ import it.unibz.inf.isochrone.util.ResourceHelper; ...@@ -17,6 +17,7 @@ import it.unibz.inf.isochrone.util.ResourceHelper;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
...@@ -26,6 +27,7 @@ import java.util.Arrays; ...@@ -26,6 +27,7 @@ import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -35,7 +37,9 @@ import java.util.regex.Pattern; ...@@ -35,7 +37,9 @@ import java.util.regex.Pattern;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -77,7 +81,8 @@ public class ConfigDataset { ...@@ -77,7 +81,8 @@ public class ConfigDataset {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigDataset.class); private static final Logger LOGGER = LoggerFactory.getLogger(ConfigDataset.class);
private static final Collection<String> ALL_DATASETS = getAvailableDatasets(true); private static final Collection<String> ALL_DATASETS = getAvailableDatasets(true);
private static final String DS_CONFIG_PATTERN = ".*config\\_(.*)\\.xml"; private static final String DS_CONFIG_EXTENSION = "xml";
private static final String DS_CONFIG_PATTERN = ".*config\\_(.*)\\." + DS_CONFIG_EXTENSION;
private static Map<String, ConfigDataset> instanceMap = new HashMap<>(); private static Map<String, ConfigDataset> instanceMap = new HashMap<>();
@PropertyValue("client.tArrival") @PropertyValue("client.tArrival")
...@@ -361,23 +366,56 @@ public class ConfigDataset { ...@@ -361,23 +366,56 @@ public class ConfigDataset {
protected static Collection<String> getAvailableDatasets(final boolean filterInvalid) { protected static Collection<String> getAvailableDatasets(final boolean filterInvalid) {
LOGGER.info("Getting available datasets"); LOGGER.info("Getting available datasets");
final ClassLoader cLoader = Thread.currentThread().getContextClassLoader();
final Pattern dsConfigPattern = Pattern.compile(DS_CONFIG_PATTERN); final Pattern dsConfigPattern = Pattern.compile(DS_CONFIG_PATTERN);
URL r = null;
InputStream r2 = null;
Collection<String> resources = null; Collection<String> resources = null;
try { try {
final URL r = Thread.currentThread().getContextClassLoader().getResource("/"); // find resources on disk and add to available ones
if (r == null) { r = cLoader.getResource("/");
resources = ResourceHelper.getResources(dsConfigPattern); if (r != null) {
} else { final Collection<File> files = FileUtils.listFiles(new File(r.getFile()), new String[] {DS_CONFIG_EXTENSION}, true);
final Collection<File> files = FileUtils.listFiles(new File(r.getFile()), null, false);
resources = new ArrayList<>(files.size()); resources = new ArrayList<>(files.size());
for (File file : files) { for (File file : files) {
resources.add(file.getName()); final String filename = file.getName();
if (dsConfigPattern.matcher(filename).matches()) {
resources.add(filename);
}
}
}
// find resources on classpath (in jar files...) and add to available ones
r2 = cLoader.getResourceAsStream("/");
if (r2 != null) {
final Collection<String> files = IOUtils.readLines(r2, Charsets.UTF_8);
for (String filename : files) {
if (dsConfigPattern.matcher(filename).matches()) {
resources.add(filename);
}
} }
} }
// find resources on webapp resources (in tomcat lib folder, in WEB-INF...) and add to available ones
final Enumeration<URL> e = cLoader.getResources("../lib");
if (e != null) {
final Collection<URL> webappResources = Collections.list(e);
final Collection<String> files = ResourceHelper.findFilesInResources(webappResources, dsConfigPattern);
resources.addAll(files);
}
} catch (IOException e) { } catch (IOException e) {
resources = null; resources = null;
LOGGER.warn("Datasets could not be read. There might be no configured datasets available!"); LOGGER.warn("Datasets could not be read. There might be no configured datasets available!");
} finally {
if (r2 != null) {
try {
r2.close();
} catch (IOException e) {
LOGGER.debug("Could not close input stream after searching for config files in classpath");
}
}
} }
if (resources == null || resources.size() <= 0) { if (resources == null || resources.size() <= 0) {
......
...@@ -2,103 +2,90 @@ package it.unibz.inf.isochrone.util; ...@@ -2,103 +2,90 @@ package it.unibz.inf.isochrone.util;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.net.URL;
import java.util.Collection; import java.util.Collection;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Gets available resources (from the classpath) by a regular expression pattern.
*/
public final class ResourceHelper { public final class ResourceHelper {
private static final String EXTENSION_JAR = ".jar";
private static final String PROPERTY_CP = "java.class.path";
// Constructor private ResourceHelper() { }
private ResourceHelper() {}
// Public static methods
/** /**
* Gets all resources matching the given pattern. * Gets all resources matching the given pattern from the path elements given.
* *
* @param pattern the pattern to match against (as string) * @param fileNamePattern the pattern to match against
* @return the resources in the order they are found * @return the resources in the order they are found
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
public static Collection<String> getResources(final String pattern) throws IOException { public static Set<String> findFilesInResources(final Collection<URL> classPathElements, final Pattern fileNamePattern) throws IOException {
return getResources(Pattern.compile(pattern)); final String[] retval = new String[classPathElements.size()];
}
/** int i = 0;
* Gets all resources matching the given pattern. for (final URL element : classPathElements) {
* retval[i++] = element.getFile();
* @param pattern the pattern to match against
* @return the resources in the order they are found
* @throws IOException if an I/O error occurs
*/
public static Collection<String> getResources(final Pattern pattern) throws IOException {
final Collection<String> retval = new ArrayList<>();
final String classPath = System.getProperty("java.class.path", ".");
final String[] classPathElements = classPath.split(File.pathSeparator);
for (final String element : classPathElements) {
retval.addAll(getResources(element, pattern));
} }
return retval; return findFilesInResources(retval, fileNamePattern);
} }
// Private static methods public static Set<String> findFilesInResources(final String[] pathElements, final Pattern fileNamePattern) throws IOException {
Set<String> result = new HashSet<String>();
for (String element : pathElements) {
final File newFile = new File(element);
if (newFile.isDirectory()) {
result.addAll(findResourceInDirectory(newFile, fileNamePattern));
} else {
result.addAll(findResourceInFile(newFile, fileNamePattern));
}
}
private static Collection<String> getResources(final String element, final Pattern pattern) throws IOException { return result;
final Collection<String> retval = new ArrayList<>(); }
final File file = new File(element); public static Set<String> findFilesInClassPath(final Pattern fileNamePattern) throws IOException {
if (file.isDirectory()) { final String classPath = System.getProperty(PROPERTY_CP);
retval.addAll(getResourcesFromDirectory(file, pattern)); final String[] pathElements = classPath.split(File.pathSeparator);
} else {
retval.addAll(getResourcesFromJarFile(file, pattern));
}
return retval; return findFilesInResources(pathElements, fileNamePattern);
} }
private static Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) throws IOException { private static Collection<String> findResourceInFile(final File resourceFile, final Pattern fileNamePattern) throws IOException {
final Collection<String> retval = new ArrayList<>(); final Set<String> result = new HashSet<String>();
try (final ZipFile zf = new ZipFile(file)) { if (resourceFile.canRead() && resourceFile.getAbsolutePath().endsWith(EXTENSION_JAR)) {
final Enumeration<? extends ZipEntry> e = zf.entries(); try (final JarFile jarFile = new JarFile(resourceFile)) {
while (e.hasMoreElements()) { final Enumeration<JarEntry> entries = jarFile.entries();
final ZipEntry ze = e.nextElement(); while (entries.hasMoreElements()) {
final String fileName = ze.getName(); final JarEntry singleEntry = entries.nextElement();
final boolean accept = pattern.matcher(fileName).matches(); if (fileNamePattern.matcher(singleEntry.getName()).matches()) {
if (accept) { result.add(jarFile.getName() + File.separatorChar + singleEntry.getName());
retval.add(fileName); }
} }
} }
} catch (final IOException ioe) {
throw ioe;
} }
return retval; return result;
} }
private static Collection<String> getResourcesFromDirectory(final File directory, final Pattern pattern) throws IOException { private static Set<String> findResourceInDirectory(final File directory, final Pattern fileNamePattern) throws IOException {
final Collection<String> retval = new ArrayList<>(); final Set<String> result = new HashSet<String>();
final File[] files = directory.listFiles();
final File[] fileList = directory.listFiles(); for (final File currentFile : files) {
for (final File file : fileList) { if (fileNamePattern.matcher(currentFile.getAbsolutePath()).matches()) {
if (file.isDirectory()) { result.add(currentFile.getAbsolutePath());
retval.addAll(getResourcesFromDirectory(file, pattern)); } else if (currentFile.isDirectory()) {
result.addAll(findResourceInDirectory(currentFile, fileNamePattern));
} else { } else {
final String fileName = file.getCanonicalPath(); result.addAll(findResourceInFile(currentFile, fileNamePattern));
final boolean accept = pattern.matcher(fileName).matches();
if (accept) {
retval.add(fileName);
}
} }
} }
return retval; return result;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment