X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=tests%2Fhoneynode%2F1.2.1%2Fminimal-distribution-core%2Fsrc%2Fmain%2Fjava%2Fio%2Ffd%2Fhoneycomb%2Finfra%2Fdistro%2Fschema%2FResourceLoader.java;fp=tests%2Fhoneynode%2F1.2.1%2Fminimal-distribution-core%2Fsrc%2Fmain%2Fjava%2Fio%2Ffd%2Fhoneycomb%2Finfra%2Fdistro%2Fschema%2FResourceLoader.java;h=99aa4518a4ab88886e20c4ad8bca1a8c292d5642;hb=c764b14f67faf1665f8814db9e5d16ddb342553e;hp=0000000000000000000000000000000000000000;hpb=b90fdacec82f92e07d14ed1df31e3fe53275c676;p=transportpce.git diff --git a/tests/honeynode/1.2.1/minimal-distribution-core/src/main/java/io/fd/honeycomb/infra/distro/schema/ResourceLoader.java b/tests/honeynode/1.2.1/minimal-distribution-core/src/main/java/io/fd/honeycomb/infra/distro/schema/ResourceLoader.java new file mode 100644 index 000000000..99aa4518a --- /dev/null +++ b/tests/honeynode/1.2.1/minimal-distribution-core/src/main/java/io/fd/honeycomb/infra/distro/schema/ResourceLoader.java @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.infra.distro.schema; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; +import static java.lang.String.format; + +import com.google.common.base.Charsets; +import com.google.common.base.Strings; +import com.google.common.io.Resources; +import java.io.File; +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.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; +import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.stream.Collectors; +import org.apache.commons.io.IOUtils; + +/** + * Allows loading content of X amount of files from filesystem or archive + */ +public interface ResourceLoader { + + default Set loadResourceContentsOnPath(final String path) { + final URL folderUrl = getClass().getClassLoader().getResource(path); + checkState(folderUrl != null, "Resources %s not found", path); + + if (ResourceLoaderIml.urlToUri(folderUrl).getScheme().equals("jar")) { + return ResourceLoaderIml.readFromJar(path, folderUrl); + } else { + return ResourceLoaderIml.readFromFolder(folderUrl); + } + + } + + final class ResourceLoaderIml { + + private ResourceLoaderIml() { + throw new UnsupportedOperationException(); + } + + private static Set readFromFolder(final URL folderUrl) { + final File folder = new File(folderUrl.getPath()); + final File[] files = checkNotNull(folder.listFiles(), "No files present on path %s", folderUrl); + return Arrays.stream(files) + .map(ResourceLoaderIml::fileToUrl) + .map(ResourceLoaderIml::urlToContentString) + .flatMap(content -> Arrays.stream(content.split(System.lineSeparator()))) + .filter(ResourceLoaderIml::filterNonEmpty) + .collect(Collectors.toSet()); + } + + private static Set readFromJar(final String path, final URL url) { + final String uriString = urlToUri(url).toString(); + final String fileReference = extractJarFilePath(uriString); + try (JarFile jar = new JarFile(new File(fileReference))) { + return Collections.list(jar.entries()) + .stream() + .filter(jarEntry -> jarEntry.getName().contains(path)) + .map(jarEntry -> getJarEntryStream(jar, jarEntry)) + .map(ResourceLoaderIml::readJarEntryStream) + .flatMap(content -> Arrays.stream(content.split(System.lineSeparator()))) + .filter(ResourceLoaderIml::filterNonEmpty) + .collect(Collectors.toSet()); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private static String extractJarFilePath(final String uriString) { + return uriString.substring(0, uriString.indexOf("!")).replace("jar:file:", ""); + } + + private static boolean filterNonEmpty(final String line) { + return !Strings.isNullOrEmpty(line.trim()); + } + + private static String readJarEntryStream(final InputStream inputStream) { + try { + final String value = IOUtils.toString(inputStream, StandardCharsets.UTF_8); + IOUtils.closeQuietly(inputStream); + return value; + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private static InputStream getJarEntryStream(final JarFile jar, final JarEntry jarEntry) { + try { + return jar.getInputStream(jarEntry); + } catch (IOException e) { + throw new IllegalStateException(format("Unable to get stream for entry %s | jar %s", jar, jarEntry), e); + } + } + + private static URI urlToUri(final URL url) { + try { + return url.toURI(); + } catch (URISyntaxException e) { + throw new IllegalStateException(format("Unable to convert URL %s to URI", url), e); + } + } + + private static String urlToContentString(final URL url) { + try { + return Resources.toString(url, Charsets.UTF_8); + } catch (IOException e) { + throw new IllegalArgumentException("Unable to read resource from: " + url, e); + } + } + + private static URL fileToUrl(final File file) { + try { + return file.toURI().toURL(); + } catch (MalformedURLException e) { + throw new IllegalStateException(e); + } + } + } +}