d201f399cb6ddf1ff6cf9096c08f576ad957167e
[appc.git] / appc-dispatcher / appc-dispatcher-common / appc-data-access-lib / src / main / java / org / onap / appc / dao / util / dbcp / DBConnectionPool.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dao.util.dbcp;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.apache.commons.dbcp2.BasicDataSource;
31 import org.onap.appc.dao.util.api.DBConnectionPoolService;
32 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
33
34 import java.sql.Connection;
35 import java.sql.SQLException;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 /**
40  * This class provides ability to create database connection pool.
41  */
42 public class DBConnectionPool implements DBConnectionPoolService {
43
44     private final EELFLogger logger = EELFManager.getInstance().getLogger(DBConnectionPool.class);
45     private BasicDataSource dataSource;
46
47     public enum DataSourceStatus {
48         ACTIVE_NUMBER("active_number"),
49         IDLE_NUMBER("idle_number");
50
51         private String attribute;
52
53         DataSourceStatus(String attribute) {
54             this.attribute = attribute;
55         }
56
57         public String getAttribute() {
58             return attribute;
59         }
60     }
61
62     public DBConnectionPool(String connectURI, String username, String password, String driverClass) {
63         this(connectURI, username, password, driverClass, null, null, null, null, null);
64     }
65
66     public DBConnectionPool(String connectURI, String username, String password,
67                             String driverClass, Integer initialSize, Integer maxActive,
68                             Integer maxIdle, Integer maxWait, Integer minIdle) {
69         this.dataSource = getBasicDataSource(connectURI, username, password, driverClass,
70             initialSize, maxActive, maxIdle, maxWait, minIdle);
71     }
72
73     /**
74      * Get a connection from datasource which is thread safe.
75      * {@inheritDoc}
76      */
77     @Override
78     public Connection getConnection() throws DBConnectionPoolException {
79         if (dataSource == null) {
80             throw new DBConnectionPoolException();
81         }
82         Connection connection = null;
83         try {
84             connection = dataSource.getConnection();
85         } catch (SQLException e) {
86             logger.error("Get connection failure", e);
87             throw new DBConnectionPoolException(e);
88         }
89
90         if(connection == null){
91             //
92             throw new DBConnectionPoolException("Connection was not created");
93         }
94
95         return connection;
96     }
97
98     /**
99      * Closes and releases all idle connections that are currently stored in the connection pool associated with this
100      * data source.
101      */
102     public void shutdown() {
103         if (dataSource != null) {
104             try {
105                 dataSource.close();
106             } catch (SQLException e) {
107                 logger.error("Datasource cannot be closed normally.", e.getMessage());
108             }
109         }
110
111         dataSource = null;
112     }
113
114     /**
115      * Get datasource status
116      *
117      * @return
118      */
119     public Map<String, Integer> getDataSourceStatus() {
120         Map<String, Integer> map = new HashMap<>(2);
121         map.put(DataSourceStatus.ACTIVE_NUMBER.getAttribute(), dataSource.getNumActive());
122         map.put(DataSourceStatus.IDLE_NUMBER.getAttribute(), dataSource.getNumIdle());
123
124         return map;
125     }
126
127     protected BasicDataSource getBasicDataSource(String connectURI, String username, String password,
128                                                String driverClass, Integer initialSize, Integer maxtotal,
129                                                Integer maxIdle, Integer maxWaitMillis, Integer minIdle) {
130         BasicDataSource dataSource = new BasicDataSource();
131         dataSource.setDriverClassName(driverClass);
132         dataSource.setUsername(username);
133         dataSource.setPassword(password);
134         dataSource.setUrl(connectURI);
135
136         if (initialSize != null) {
137             dataSource.setInitialSize(initialSize);
138         }
139         if (maxtotal != null) {
140             dataSource.setMaxTotal(maxtotal);
141         }
142         if (maxIdle != null) {
143             dataSource.setMaxIdle(maxIdle);
144         }
145         if (maxWaitMillis != null) {
146             dataSource.setMaxWaitMillis(maxWaitMillis);
147         }
148         if (minIdle != null) {
149             dataSource.setMinIdle(minIdle);
150         }
151
152         return dataSource;
153     }
154 }