2ad581e69f5a47fc435814b86d5cebb6dbb005a5
[platform/core/security/suspicious-activity-monitor.git] / server / src / main / java / com / samsung / samserver / SamserverApp.java
1 /*
2  * In Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6 package com.samsung.samserver;
7
8 import com.samsung.samserver.config.ApplicationProperties;
9 import com.samsung.samserver.config.DefaultProfileUtil;
10
11 import io.github.jhipster.config.JHipsterConstants;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.springframework.beans.factory.annotation.Qualifier;
16 import org.springframework.boot.SpringApplication;
17 import org.springframework.boot.actuate.autoconfigure.*;
18 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
19 import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
20 import org.springframework.boot.context.properties.EnableConfigurationProperties;
21 import org.springframework.context.annotation.ComponentScan;
22 import org.springframework.core.env.Environment;
23 import springfox.documentation.builders.PathSelectors;
24 import springfox.documentation.spring.web.plugins.Docket;
25
26 import javax.annotation.PostConstruct;
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import java.util.Arrays;
30 import java.util.Collection;
31
32 @ComponentScan
33 @EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class})
34 @EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class})
35 public class SamserverApp {
36
37     private static final Logger log = LoggerFactory.getLogger(SamserverApp.class);
38
39     private final Environment env;
40
41     private final Docket managementDocket;
42     private final Docket apiDocket;
43
44     public SamserverApp(Environment env,
45                         @Qualifier("swaggerSpringfoxManagementDocket")Docket managementDocket,
46                         @Qualifier("swaggerSpringfoxApiDocket") Docket apiDocket) {
47         this.env = env;
48         this.managementDocket = managementDocket;
49         this.apiDocket = apiDocket;
50     }
51
52     /**
53      * Initializes samserver.
54      * <p>
55      * Spring profiles can be configured with a program arguments --spring.profiles.active=your-active-profile
56      * <p>
57      * You can find more information on how profiles work with JHipster on <a href="http://www.jhipster.tech/profiles/">http://www.jhipster.tech/profiles/</a>.
58      */
59     @PostConstruct
60     public void initApplication() {
61         Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
62         if (activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_PRODUCTION)) {
63             log.error("You have misconfigured your application! It should not run " +
64                 "with both the 'dev' and 'prod' profiles at the same time.");
65         }
66         if (activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_CLOUD)) {
67             log.error("You have misconfigured your application! It should not " +
68                 "run with both the 'dev' and 'cloud' profiles at the same time.");
69         }
70         managementDocket.enable(false);
71         apiDocket.select().paths(PathSelectors.regex("/api/device-service.*")).build();
72     }
73
74     /**
75      * Main method, used to run the application.
76      *
77      * @param args the command line arguments
78      * @throws UnknownHostException if the local host name could not be resolved into an address
79      */
80     public static void main(String[] args) throws UnknownHostException {
81         SpringApplication app = new SpringApplication(SamserverApp.class);
82         DefaultProfileUtil.addDefaultProfile(app);
83         Environment env = app.run(args).getEnvironment();
84         String protocol = "http";
85         if (env.getProperty("server.ssl.key-store") != null) {
86             protocol = "https";
87         }
88         log.info("\n----------------------------------------------------------\n\t" +
89                 "Application '{}' is running! Access URLs:\n\t" +
90                 "Local: \t\t{}://localhost:{}\n\t" +
91                 "External: \t{}://{}:{}\n\t" +
92                 "Profile(s): \t{}\n----------------------------------------------------------",
93             env.getProperty("spring.application.name"),
94             protocol,
95             env.getProperty("server.port"),
96             protocol,
97             InetAddress.getLocalHost().getHostAddress(),
98             env.getProperty("server.port"),
99             env.getActiveProfiles());
100     }
101 }