[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / database / ConnectionFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 IBM.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dmaap.dbcapi.database;
23
24 import java.sql.Connection;
25 import java.sql.DriverManager;
26 import java.sql.SQLException;
27 import java.util.Properties;
28 import java.util.concurrent.TimeUnit;
29 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
30 import org.onap.dmaap.dbcapi.util.DmaapConfig;
31
32 public class ConnectionFactory extends BaseLoggingClass {
33
34          static final int PREPARE_PSQL_CONNECTION_ATTEMPTS = 5;
35
36         static {
37                 try {
38                         Class.forName("org.postgresql.Driver");
39                 } catch (Exception e) {
40                         errorLogger.error("Unable to load postgres driver " + e, e);
41                 }
42         }
43         private static ConnectionFactory instance = new ConnectionFactory();
44         private String  host;
45         private String  dbname;
46         private String  dbuser;
47         private String  dbcr;
48         private String  schema;
49         
50         public ConnectionFactory() {
51                 Properties p = DmaapConfig.getConfig();
52                 host = p.getProperty("DB.host", "dcae-pstg-write-ftl.domain.notset.com");
53                 dbname = p.getProperty("DB.name", "dmaap");
54                 dbuser = getValue(p, "DB.user", "dmaap_admin");
55                 dbcr = getValue(p, "DB.cred", "test234-ftl");
56                 schema = p.getProperty("DB.schema", "dmaap_admin");
57         }
58
59         private static String getValue(final Properties props, final String value, final String defaultValue) {
60                 String prop = props.getProperty(value, defaultValue);
61                 if (prop != null && prop.matches("[$][{].*[}]$")) {
62                         return System.getenv(prop.substring(2, prop.length() - 1));
63                 }
64                 return prop;
65         }
66
67         public static ConnectionFactory getDefaultInstance() {
68                 return(instance);
69         }
70         private Connection[] pool = new Connection[5];
71         private int     cur;
72         public Connection get(boolean fresh) throws SQLException {
73                 if (!fresh) {
74                         synchronized(this) {
75                                 if (cur > 0) {
76                                         return(pool[--cur]);
77                                 }
78                         }
79                 }
80                 Properties p = new Properties();
81                 p.put("user", dbuser);
82                 p.put("password", dbcr);
83                 for (int i=1; i<PREPARE_PSQL_CONNECTION_ATTEMPTS; i++){
84                         try{
85                                 return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p));
86                         }catch(SQLException e){
87                                 errorLogger.error("Unable to connect to the postgres server. " + i + "attempt failed. ", e);
88                                 waitFor(1);
89                         }
90                 }
91                 return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p));
92         }
93         public String getSchema() {
94                 return(schema);
95         }
96         public void release(Connection c) {
97                 synchronized(this) {
98                         if (cur < pool.length) {
99                                 pool[cur++] = c;
100                                 return;
101                         }
102                 }
103                 try { 
104                         c.close(); 
105                 } catch (Exception e) {
106                         errorLogger.error("Error", e);
107                 }
108         }
109         private void waitFor(long seconds){
110                 try {
111                         TimeUnit.SECONDS.sleep(seconds);
112                 } catch (InterruptedException e) {
113                         debugLogger.debug("Waiting interrupted. ", e);
114                         Thread.currentThread().interrupt();
115                 }
116         }
117 }