aee3ff1b2ef09c2761a61bbab8fb3b2440190454
[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 org.onap.dmaap.dbcapi.util.DmaapConfig;
30
31 public class ConnectionFactory  {
32          static final EELFLogger logger = EELFManager.getInstance().getLogger( ConnectionFactory.class );
33          static final EELFLogger appLogger = EELFManager.getInstance().getApplicationLogger();
34          static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
35          static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
36          static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
37          static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
38          static final EELFLogger serverLogger = EELFManager.getInstance().getServerLogger();
39
40         static {
41                 try {
42                         Class.forName("org.postgresql.Driver");
43                 } catch (Exception e) {
44                         logger.error("Unable to load postgres driver " + e, e);
45                 }
46         }
47         private static ConnectionFactory instance = new ConnectionFactory();
48         private String  host;
49         private String  dbname;
50         private String  dbuser;
51         private String  dbcr;
52         private String  schema;
53         
54         public ConnectionFactory() {
55                 Properties p = DmaapConfig.getConfig();
56                 host = p.getProperty("DB.host", "dcae-pstg-write-ftl.domain.notset.com");
57                 dbname = p.getProperty("DB.name", "dmaap");
58                 dbuser = p.getProperty("DB.user", "dmaap_admin");
59                 dbcr = p.getProperty("DB.cred", "test234-ftl");
60                 schema = p.getProperty("DB.schema", "public");
61         }
62         public static ConnectionFactory getDefaultInstance() {
63                 return(instance);
64         }
65         private Connection[] pool = new Connection[5];
66         private int     cur;
67         public Connection get(boolean fresh) throws SQLException {
68                 if (!fresh) {
69                         synchronized(this) {
70                                 if (cur > 0) {
71                                         return(pool[--cur]);
72                                 }
73                         }
74                 }
75                 Properties p = new Properties();
76                 p.put("user", dbuser);
77                 p.put("password", dbcr);
78                 return(DriverManager.getConnection("jdbc:postgresql://" + host + "/" + dbname, p));
79         }
80         public String getSchema() {
81                 return(schema);
82         }
83         public void release(Connection c) {
84                 synchronized(this) {
85                         if (cur < pool.length) {
86                                 pool[cur++] = c;
87                                 return;
88                         }
89                 }
90                 try { 
91                         c.close(); 
92                 } catch (Exception e) {
93                         logger.error("Error", e);
94                 }
95         }
96 }