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;
29 import io.fd.honeycomb.data.init.DataTreeInitializer;
30 import io.fd.honeycomb.data.init.InitializerRegistry;
31 import io.fd.honeycomb.infra.distro.activation.ActivationModule;
32 import io.fd.honeycomb.infra.distro.activation.ActiveModules;
33 import io.fd.honeycomb.infra.distro.initializer.InitializerPipelineModule;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public final class Main {
40 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
45 public static void main(String[] args) {
46 init(new ActivationModule());
50 * Initialize the Honeycomb with provided modules
52 public static Injector init(final ActivationModule activationModule) {
54 LOG.info("Starting honeycomb");
55 // creating child injector does not work in this case, so just create injector, and does not store ref
56 // to it, or its active modules instance
57 Injector injector = createInjector(ImmutableSet.<Module>builder()
58 .add(activationModule)
59 .addAll(createInjector(activationModule).getInstance(ActiveModules.class).createModuleInstances())
63 injector.getAllBindings().entrySet().stream()
64 .forEach(e -> LOG.trace("Component available under: {} is {}", e.getKey(), e.getValue()));
67 LOG.info("Initializing configuration");
68 injector.getInstance(Key.get(InitializerRegistry.class,
69 Names.named(InitializerPipelineModule.HONEYCOMB_INITIALIZER))).initialize();
70 LOG.info("Configuration initialized successfully");
71 } catch (DataTreeInitializer.InitializeException e) {
72 LOG.error("Unable to initialize configuration", e);
75 LOG.info("Honeycomb started successfully!");
78 } catch (CreationException | ProvisionException | ConfigurationException e) {
79 LOG.error("Failed to initialize Honeycomb components", e);
81 } catch (RuntimeException e) {
82 LOG.error("Unexpected initialization failure", e);
85 // Trigger gc to force collect initial garbage + dedicated classloader