2 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.infra.distro.activation;
19 import com.google.inject.Inject;
20 import com.google.inject.Module;
21 import com.google.inject.Provider;
22 import io.fd.honeycomb.infra.distro.schema.ResourceLoader;
23 import java.util.ArrayList;
24 import java.util.List;
26 import java.util.stream.Collectors;
27 import javax.annotation.Nonnull;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Provides list of active modules for distribution
34 public class ActiveModuleProvider implements Provider<ActiveModules>, ResourceLoader {
36 private static final Logger LOG = LoggerFactory.getLogger(ActiveModuleProvider.class);
39 private ActivationConfig config;
42 public ActiveModules get() {
43 return new ActiveModules(loadActiveModules(
44 aggregateResources(config.getModulesResourcePath())));
48 * Provide unique set of active modules filtered from provided resources
50 static Set<Class<? extends Module>> loadActiveModules(@Nonnull final List<String> moduleNames) {
51 final ClassLoader classLoader = ActiveModuleProvider.class.getClassLoader();
52 LOG.info("Reading active modules configuration for distribution");
54 // process resources to resource modules
55 return moduleNames.stream()
57 .filter(trimmedLine -> trimmedLine.length() != 0)
58 // filter out commented lines
59 .filter(nonEmptyLine -> !nonEmptyLine.startsWith("//"))
62 .map(validLine -> moduleNameToClass(validLine, classLoader))
63 // filters out classes that are not modules
64 .filter(ActiveModuleProvider::filterNonModules)
65 .collect(Collectors.toSet());
69 * Aggregate all resources from provided relative path into a {@code List<String>}
71 public List<String> aggregateResources(final String relativePath) {
72 // must use universal approach of loading from folder/jar
73 // because of memory footprint benchmark
74 return new ArrayList<>(loadResourceContentsOnPath(relativePath));
77 private static boolean filterNonModules(final Class<?> clazz) {
78 final boolean isModule = Module.class.isAssignableFrom(clazz);
80 LOG.warn("Class {} is provided in modules configuration, but is not a Module and will be ignored", clazz);
86 * Loads class by provided name
88 private static Class<? extends Module> moduleNameToClass(final String name,
89 final ClassLoader classLoader) {
91 LOG.debug("Loading module class {}", name);
92 return (Class<? extends Module>) classLoader.loadClass(name);
93 } catch (ClassNotFoundException e) {
94 LOG.error("Unable to convert {} to class, make sure you've provided sources to classpath", name);
95 throw new IllegalStateException(
96 "Unable to convert " + name + " to class, make sure you've provided sources to classpath", e);