7f4b59baae4a783d9f809950b4d7ceef9f55124f
[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 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dao.util.dbcp;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.apache.commons.dbcp2.BasicDataSource;
30 import org.onap.appc.dao.util.api.DBConnectionPoolService;
31 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
32
33 import java.sql.Connection;
34 import java.sql.SQLException;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 /**
39  * This class provides ability to create database connection pool.
40  */
41 public class DBConnectionPool implements DBConnectionPoolService {
42
43     private final EELFLogger logger = EELFManager.getInstance().getLogger(DBConnectionPool.class);
44     private BasicDataSource dataSource;
45
46     public enum DataSourceStatus {
47         ACTIVE_NUMBER("active_number"),
48         IDLE_NUMBER("idle_number");
49
50         private String attribute;
51
52         DataSourceStatus(String attribute) {
53             this.attribute = attribute;
54         }
55
56         public String getAttribute() {
57             return attribute;
58         }
59     }
60
61     public DBConnectionPool(String connectURI, String username, String password, String driverClass) {
62         this(connectURI, username, password, driverClass, null, null, null, null, null);
63     }
64
65     public DBConnectionPool(String connectURI, String username, String password,
66                             String driverClass, Integer initialSize, Integer maxActive,
67                             Integer maxIdle, Integer maxWait, Integer minIdle) {
68         this.dataSource = getBasicDataSource(connectURI, username, password, driverClass,
69             initialSize, maxActive, maxIdle, maxWait, minIdle);
70     }
71
72     /**
73      * Get a connection from datasource which is thread safe.
74      * {@inheritDoc}
75      */
76     @Override
77     public Connection getConnection() throws DBConnectionPoolException {
78         if (dataSource == null) {
79             throw new DBConnectionPoolException();
80         }
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     private 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 }