[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / util / DmaapConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.onap.dmaap.dbcapi.util;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import java.io.*;
26 import java.security.KeyStore;
27 import java.util.*;
28 import javax.net.ssl.SSLContext;
29 import javax.net.ssl.SSLSocketFactory;
30 import javax.net.ssl.TrustManager;
31 import javax.net.ssl.TrustManagerFactory;
32 import org.onap.dmaap.dbcapi.server.CertificateManager;
33 import org.onap.dmaap.dbcapi.server.JettyServer;
34
35 public class DmaapConfig extends Properties     {
36
37         private static final EELFLogger logger = EELFManager.getInstance().getLogger(DmaapConfig.class);
38         private static final long serialVersionUID = 1L;
39         private static final String CONFIG_FILE_NAME = System.getProperty("ConfigFile", "/opt/app/config/conf/dmaapbc.properties");
40         private static final Properties config = new DmaapConfig();
41
42         public static Properties getConfig() {
43                 return(config);
44         }
45         public static String getConfigFileName() {
46                 return(CONFIG_FILE_NAME);
47         }
48         private DmaapConfig() {
49                 try (InputStream is = new FileInputStream(CONFIG_FILE_NAME)){
50                         load(is);
51                 } catch (Exception e) {
52                         logger.error("Unable to load configuration file " + CONFIG_FILE_NAME);
53                         System.exit(1);
54                 }
55         }
56
57         public static SSLSocketFactory getSSLSocketFactory() {
58                 SSLSocketFactory factory = null;
59                 try {
60                         CertificateManager cm = JettyServer.getCertificateManager();
61                         String truststore = cm.getTrustStoreFile();
62                         KeyStore ts = KeyStore.getInstance(cm.getTrustStoreType());
63                         try (InputStream in = new FileInputStream(truststore)) {
64                                 ts.load(in, cm.getTrustStorePassword().toCharArray());
65                         }
66                         TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
67                         tmf.init(ts);
68                         TrustManager[] tm = tmf.getTrustManagers();
69                         SSLContext sslContext = SSLContext.getInstance("TLS");
70                         sslContext.init(null, tm, null);
71                         factory = sslContext.getSocketFactory();
72                 } catch (Exception e) {
73                         logger.error("Exception thrown trying to get SSLSocketFactory: ", e);
74                 }
75                 return factory;
76         }
77         
78 }