8f36ac52ab05e80c44a6d84278dacbc155d9db63
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / SchemaServiceApp.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.schemaservice;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.onap.aai.exceptions.AAIException;
25 import org.onap.aai.logging.LoggingContext;
26 import org.onap.aai.logging.LoggingContext.StatusCode;
27 import org.onap.aai.schemaservice.config.PropertyPasswordConfiguration;
28 import org.slf4j.MDC;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.SpringApplication;
31 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
32 import org.springframework.boot.autoconfigure.SpringBootApplication;
33 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
34 import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
35 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
36 import org.springframework.core.env.Environment;
37
38 import javax.annotation.PostConstruct;
39 import javax.annotation.PreDestroy;
40 import java.util.Map;
41 import java.util.UUID;
42
43 @SpringBootApplication
44 // Component Scan provides a way to look for spring beans
45 // It only searches beans in the following packages
46 // Any method annotated with @Bean annotation or any class
47 // with @Component, @Configuration, @Service will be picked up
48 @EnableAutoConfiguration(exclude = {
49     DataSourceAutoConfiguration.class,
50     DataSourceTransactionManagerAutoConfiguration.class,
51     HibernateJpaAutoConfiguration.class
52 })
53 public class SchemaServiceApp {
54
55     private static final EELFLogger logger = EELFManager.getInstance().getLogger(SchemaServiceApp.class.getName());
56
57     private static final String APP_NAME = "aai-schema-service";
58     private static Map<String, String> contextMap;
59
60     @Autowired
61     private Environment env;
62
63     public static void main(String[] args) throws AAIException {
64
65         setDefaultProps();
66
67         LoggingContext.save();
68         LoggingContext.component("init");
69         LoggingContext.partnerName("NA");
70         LoggingContext.targetEntity(APP_NAME);
71         LoggingContext.requestId(UUID.randomUUID().toString());
72         LoggingContext.serviceName(APP_NAME);
73         LoggingContext.targetServiceName("contextInitialized");
74         LoggingContext.statusCode(StatusCode.COMPLETE);
75
76
77         SpringApplication app = new SpringApplication(SchemaServiceApp.class);
78         app.setLogStartupInfo(false);
79         app.setRegisterShutdownHook(true);
80         app.addInitializers(new PropertyPasswordConfiguration());
81         Environment env = app.run(args).getEnvironment();
82         MDC.setContextMap(contextMap);
83
84         logger.info(
85             "Application '{}' is running on {}!",
86             env.getProperty("spring.application.name"),
87             env.getProperty("server.port")
88         );
89
90         logger.info("SchemaService MicroService Started");
91
92         System.out.println("SchemaService Microservice Started");
93
94         LoggingContext.restoreIfPossible();
95     }
96
97     public static void setDefaultProps() {
98
99         if (System.getProperty("file.separator") == null) {
100             System.setProperty("file.separator", "/");
101         }
102
103         String currentDirectory = System.getProperty("user.dir");
104
105         if (System.getProperty("AJSC_HOME") == null) {
106             System.setProperty("AJSC_HOME", ".");
107         }
108
109         if (currentDirectory.contains(APP_NAME)) {
110             if (System.getProperty("BUNDLECONFIG_DIR") == null) {
111                 System.setProperty("BUNDLECONFIG_DIR", "src/main/resources");
112             }
113         } else {
114             if (System.getProperty("BUNDLECONFIG_DIR") == null) {
115                 System.setProperty("BUNDLECONFIG_DIR", "aai-schema-service/src/main/resources");
116             }
117         }
118     }
119
120     @PostConstruct
121     private void init() throws AAIException {
122         System.setProperty("org.onap.aai.serverStarted", "false");
123         setDefaultProps();
124
125         LoggingContext.save();
126         LoggingContext.component("init");
127         LoggingContext.partnerName("NA");
128         LoggingContext.targetEntity(APP_NAME);
129         LoggingContext.requestId(UUID.randomUUID().toString());
130         LoggingContext.serviceName(APP_NAME);
131         LoggingContext.targetServiceName("contextInitialized");
132         LoggingContext.statusCode(StatusCode.COMPLETE);
133
134         contextMap = MDC.getCopyOfContextMap();
135         logger.info("SchemaService initialization started...");
136
137         // Setting this property to allow for encoded slash (/) in the path parameter
138         // This is only needed for tomcat keeping this as temporary
139         System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
140
141
142         if (env.acceptsProfiles(Profiles.TWO_WAY_SSL) && env.acceptsProfiles(Profiles.ONE_WAY_SSL)) {
143             logger.warn("You have seriously misconfigured your application");
144         }
145
146         LoggingContext.restoreIfPossible();
147     }
148
149     @PreDestroy
150     public void cleanup() {
151         logger.info("SchemaService shutting down");
152     }
153 }