2 * Copyright (c) 2016 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;
19 import static com.google.inject.Guice.createInjector;
21 import com.google.common.collect.ImmutableSet;
22 import com.google.inject.ConfigurationException;
23 import com.google.inject.CreationException;
24 import com.google.inject.Injector;
25 import com.google.inject.Key;
26 import com.google.inject.Module;
27 import com.google.inject.ProvisionException;
28 import com.google.inject.name.Names;
30 import io.fd.honeycomb.data.init.DataTreeInitializer;
31 import io.fd.honeycomb.data.init.InitializerRegistry;
32 import io.fd.honeycomb.infra.distro.activation.ActivationModule;
33 import io.fd.honeycomb.infra.distro.activation.ActiveModules;
34 import io.fd.honeycomb.infra.distro.initializer.InitializerPipelineModule;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public final class Main {
41 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
46 public static void main(String[] args) {
47 init(new ActivationModule());
51 * Initialize the Honeycomb with provided modules
53 public static Injector init(final ActivationModule activationModule) {
55 LOG.info("Starting honeycomb");
56 // creating child injector does not work in this case, so just create injector, and does not store ref
57 // to it, or its active modules instance
58 Injector injector = createInjector(ImmutableSet.<Module>builder()
59 .add(activationModule)
60 .addAll(createInjector(activationModule).getInstance(ActiveModules.class).createModuleInstances())
64 injector.getAllBindings().entrySet().stream()
65 .forEach(e -> LOG.trace("Component available under: {} is {}", e.getKey(), e.getValue()));
68 LOG.info("Initializing configuration");
69 injector.getInstance(Key.get(InitializerRegistry.class,
70 Names.named(InitializerPipelineModule.HONEYCOMB_INITIALIZER))).initialize();
71 LOG.info("Configuration initialized successfully");
72 } catch (DataTreeInitializer.InitializeException e) {
73 LOG.error("Unable to initialize configuration", e);
77 LOG.info("Honeycomb started successfully!");
79 } catch (CreationException | ProvisionException | ConfigurationException e) {
80 LOG.error("Failed to initialize Honeycomb components", e);
82 } catch (RuntimeException e) {
83 LOG.error("Unexpected initialization failure", e);
86 // Trigger gc to force collect initial garbage + dedicated classloader