0f14e600b9d93f33d8dcd3c303d9beace31747af
[dmaap/dbcapi.git] / 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  * ================================================================================
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.database;
22
23 import java.sql.*;
24 import java.util.*;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.util.concurrent.TimeUnit;
30 import org.onap.dmaap.dbcapi.util.DmaapConfig;
31
32 public class ConnectionFactory  {
33          static final EELFLogger logger = EELFManager.getInstance().getLogger( ConnectionFactory.class );
34          static final EELFLogger appLogger = EELFManager.getInstance().getApplicationLogger();
35          static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
36          static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
37          static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
38          static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
39          static final EELFLogger serverLogger = EELFManager.getInstance().getServerLogger();
40          static final int PREPARE_PSQL_CONNECTION_ATTEMPTS = 5;
41
42         static {
43                 try {
44                         Class.forName("org.postgresql.Driver");
45                 } catch (Exception e) {
46                         logger.error("Unable to load postgres driver " + e, e);
47                 }
48         }
49         private static ConnectionFactory instance = new ConnectionFactory();
50         private String  host;
51         private String  dbname;
52         private String  dbuser;
53         private String  dbcr;
54         private String  schema;
55         
56         public ConnectionFactory() {
57                 Properties p = DmaapConfig.getConfig();
58                 host = p.getProperty("DB.host", "dcae-pstg-write-ftl.domain.notset.com");
59                 dbname = p.getProperty("DB.name", "dmaap");
60                 dbuser = p.getProperty("DB.user", "dmaap_admin");
61                 dbcr = p.getProperty("DB.cred", "test234-ftl");
62                 schema = p.getProperty("DB.schema", "public");
63         }
64         public static ConnectionFactory getDefaultInstance() {
65                 return(instance);
66         }
67         private Connection[] pool = new Connection[5];
68         private int     cur;
69         public Connection get(boolean fresh) throws SQLException {
70                 if (!fresh) {
71                         synchronized(this) {
72                                 if (cur > 0) {
73                                         return(pool[--cur]);
74                                 }
75                         }
76                 }
77                 Properties p = new Properties();
78                 p.put("user", dbuser);
79                 p.put("password", dbcr);
80                 for (int i=1; i<PREPARE_PSQL_CONNECTION_ATTEMPTS; i++){
81                         try{
82                                 return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p));
83                         }catch(SQLException e){
84                                 logger.error("Unable to connect to the postgres server. " + i + "attempt failed. ", e);
85                                 waitFor(1);
86                         }
87                 }
88                 return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p));
89         }
90         public String getSchema() {
91                 return(schema);
92         }
93         public void release(Connection c) {
94                 synchronized(this) {
95                         if (cur < pool.length) {
96                                 pool[cur++] = c;
97                                 return;
98                         }
99                 }
100                 try { 
101                         c.close(); 
102                 } catch (Exception e) {
103                         logger.error("Error", e);
104                 }
105         }
106         private void waitFor(long seconds){
107                 try {
108                         TimeUnit.SECONDS.sleep(seconds);
109                 } catch (InterruptedException e) {
110                         logger.debug("Waiting interrupted. ", e);
111                 }
112         }
113 }