diff options
Diffstat (limited to 'infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation')
4 files changed, 0 insertions, 280 deletions
diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationConfig.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationConfig.java deleted file mode 100644 index a17f5d632..000000000 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationConfig.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2016 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.activation; - -import java.util.Optional; -import net.jmob.guice.conf.core.BindConfig; -import net.jmob.guice.conf.core.InjectConfig; -import net.jmob.guice.conf.core.Syntax; - -@BindConfig(value = "activation", syntax = Syntax.JSON) -public class ActivationConfig { - - @InjectConfig("modules-resource-path") - private String modulesResourcePath; - - @InjectConfig("yang-modules-index-path") - private String yangModulesIndexPath; - - public String getModulesResourcePath() { - return Optional.ofNullable(modulesResourcePath).orElse("../modules/"); - } - - public String getYangModulesIndexPath() { - return Optional.ofNullable(yangModulesIndexPath).orElse("../yang-mapping/"); - } - -} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationModule.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationModule.java deleted file mode 100644 index 00f77a66d..000000000 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationModule.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2016 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.activation; - -import com.google.inject.AbstractModule; -import net.jmob.guice.conf.core.ConfigurationModule; - -/** - * Module that provides set of modules activated by distribution and binds this set to be available - */ -public class ActivationModule extends AbstractModule { - @Override - protected void configure() { - install(ConfigurationModule.create()); - requestInjection(ActivationConfig.class); - bind(ActiveModules.class).toProvider(ActiveModuleProvider.class).asEagerSingleton(); - } -} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProvider.java deleted file mode 100644 index d31024ce9..000000000 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProvider.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * 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.activation; - -import com.google.common.collect.ImmutableList; -import com.google.inject.Inject; -import com.google.inject.Module; -import com.google.inject.Provider; -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import javax.annotation.Nonnull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Provides list of active modules for distribution - */ -class ActiveModuleProvider implements Provider<ActiveModules> { - - private static final Logger LOG = LoggerFactory.getLogger(ActiveModuleProvider.class); - - @Inject - private ActivationConfig config; - - @Override - public ActiveModules get() { - return new ActiveModules(loadActiveModules( - aggregateResources(config.getModulesResourcePath(), Thread.currentThread().getContextClassLoader()))); - } - - /** - * Provide unique set of active modules filtered from provided resources - */ - static Set<Class<? extends Module>> loadActiveModules(@Nonnull final List<String> moduleNames) { - final ClassLoader classLoader = ActiveModuleProvider.class.getClassLoader(); - LOG.info("Reading active modules configuration for distribution"); - - // process resources to resource modules - return moduleNames.stream() - .map(String::trim) - .filter(trimmedLine -> trimmedLine.length() != 0) - // filter out commented lines - .filter(nonEmptyLine -> !nonEmptyLine.startsWith("//")) - // filter duplicates - .distinct() - .map(validLine -> moduleNameToClass(validLine, classLoader)) - // filters out classes that are not modules - .filter(ActiveModuleProvider::filterNonModules) - .collect(Collectors.toSet()); - } - - /** - * Aggregate all resources from provided relative path into a {@code List<String>} - */ - public static List<String> aggregateResources(final String relativePath, final ClassLoader classLoader) { - try { - return Collections.list(classLoader.getResources(relativePath)).stream() - .map(ActiveModuleProvider::toURI) - .flatMap(ActiveModuleProvider::folderToFile) - .map(File::toURI) - .map(Paths::get) - // readAll lines and add them to iteration - .flatMap(ActiveModuleProvider::readLines) - .collect(Collectors.toList()); - } catch (IOException e) { - LOG.error("Unable to load resources from relative path {}", relativePath, e); - throw new IllegalStateException("Unable to load resources from relative path " + relativePath, e); - } - } - - private static Stream<File> folderToFile(final URI uri) { - final File[] files = new File(uri).listFiles(File::isFile); - - return files != null - ? ImmutableList.copyOf(files).stream() - : Collections.<File>emptyList().stream(); - } - - private static boolean filterNonModules(final Class<?> clazz) { - final boolean isModule = Module.class.isAssignableFrom(clazz); - if (!isModule) { - LOG.warn("Class {} is provided in modules configuration, but is not a Module and will be ignored", clazz); - } - return isModule; - } - - /** - * Read lines from {@code Path} - */ - private static Stream<String> readLines(final Path path) { - try { - return Files.readAllLines(path).stream(); - } catch (IOException e) { - LOG.error("Unable to read content of {}", path, e); - throw new IllegalStateException("Unable to read content of " + path, e); - } - } - - /** - * Converts {@code URL} to {@code URI} - */ - private static URI toURI(final URL url) { - try { - return url.toURI(); - } catch (URISyntaxException e) { - LOG.error("Unable to convert {} to uri", url); - throw new IllegalStateException("Unable to convert " + url + " to uri", e); - } - } - - /** - * Loads class by provided name - */ - private static Class<? extends Module> moduleNameToClass(final String name, - final ClassLoader classLoader) { - try { - LOG.debug("Loading module class {}", name); - return (Class<? extends Module>) classLoader.loadClass(name); - } catch (ClassNotFoundException e) { - LOG.error("Unable to convert {} to class, make sure you've provided sources to classpath", name); - throw new IllegalStateException( - "Unable to convert " + name + " to class, make sure you've provided sources to classpath", e); - } - } -} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModules.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModules.java deleted file mode 100644 index f729f00b9..000000000 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModules.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2016 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.activation; - -import static java.lang.String.format; - -import com.google.inject.Module; -import java.util.Set; -import java.util.stream.Collectors; - -/** - * Provides static set of active activeModulesClasses - */ -public class ActiveModules { - private final Set<Class<? extends Module>> activeModulesClasses; - - public ActiveModules(final Set<Class<? extends Module>> activeModulesClasses) { - this.activeModulesClasses = activeModulesClasses; - } - - public Set<Class<? extends Module>> getActiveModulesClasses() { - return activeModulesClasses; - } - - public Set<? extends Module> createModuleInstances() { - return activeModulesClasses.stream() - .map(moduleClass -> { - try { - return moduleClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - throw new IllegalStateException(format("Unable to create instance of module %s", moduleClass), - e); - } - }).collect(Collectors.toSet()); - } - - @Override - public String toString() { - return "ActiveModules{" + - "activeModulesClasses=" + activeModulesClasses + - '}'; - } -} |