50826041da09790b612cd88e3c9bd576416e2ba8
[transportpce.git] / tests / honeynode221 / minimal-distribution / src / main / java / io / fd / honeycomb / infra / distro / Main.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.infra.distro;
18
19 import static com.google.inject.Guice.createInjector;
20
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
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;
35
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public final class Main {
40
41     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
42
43     private Main() {
44     }
45
46     public static void main(String[] args) {
47         init(new ActivationModule());
48     }
49
50     /**
51      * Initialize the Honeycomb with provided modules
52      */
53     public static Injector init(final ActivationModule activationModule) {
54         try {
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())
61                     .build());
62
63             // Log all bindings
64             injector.getAllBindings().entrySet().stream()
65                     .forEach(e -> LOG.trace("Component available under: {} is {}", e.getKey(), e.getValue()));
66
67             try {
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);
74             }
75
76
77             LOG.info("Honeycomb started successfully!");
78             return injector;
79         } catch (CreationException | ProvisionException | ConfigurationException e) {
80             LOG.error("Failed to initialize Honeycomb components", e);
81             throw e;
82         } catch (RuntimeException e) {
83             LOG.error("Unexpected initialization failure", e);
84             throw e;
85         } finally {
86             // Trigger gc to force collect initial garbage + dedicated classloader
87             System.gc();
88         }
89     }
90
91 }