fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 1.2.1 / 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 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;
36
37
38 public final class Main {
39
40     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
41
42     private Main() {
43     }
44
45     public static void main(String[] args) {
46         init(new ActivationModule());
47     }
48
49     /**
50      * Initialize the Honeycomb with provided modules
51      */
52     public static Injector init(final ActivationModule activationModule) {
53         try {
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())
60                     .build());
61
62             // Log all bindings
63             injector.getAllBindings().entrySet().stream()
64                     .forEach(e -> LOG.trace("Component available under: {} is {}", e.getKey(), e.getValue()));
65
66             try {
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);
73             }
74
75             LOG.info("Honeycomb started successfully!");
76
77             return injector;
78         } catch (CreationException | ProvisionException | ConfigurationException e) {
79             LOG.error("Failed to initialize Honeycomb components", e);
80             throw e;
81         } catch (RuntimeException e) {
82             LOG.error("Unexpected initialization failure", e);
83             throw e;
84         } finally {
85             // Trigger gc to force collect initial garbage + dedicated classloader
86             System.gc();
87         }
88     }
89
90 }