Update license header in appc-dispatcher files
[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  * 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.dao.util.dbcp;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.apache.commons.dbcp2.BasicDataSource;
29 import org.onap.appc.dao.util.api.DBConnectionPoolService;
30 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
31
32 import java.sql.Connection;
33 import java.sql.SQLException;
34 import java.util.HashMap;
35 import java.util.Map;
36
37 /**
38  * This class provides ability to create database connection pool.
39  */
40 public class DBConnectionPool implements DBConnectionPoolService {
41
42     private final EELFLogger logger = EELFManager.getInstance().getLogger(DBConnectionPool.class);
43     private BasicDataSource dataSource;
44
45     public enum DataSourceStatus {
46         ACTIVE_NUMBER("active_number"),
47         IDLE_NUMBER("idle_number");
48
49         private String attribute;
50
51         DataSourceStatus(String attribute) {
52             this.attribute = attribute;
53         }
54
55         public String getAttribute() {
56             return attribute;
57         }
58     }
59
60     public DBConnectionPool(String connectURI, String username, String password, String driverClass) {
61         this(connectURI, username, password, driverClass, null, null, null, null, null);
62     }
63
64     public DBConnectionPool(String connectURI, String username, String password,
65                             String driverClass, Integer initialSize, Integer maxActive,
66                             Integer maxIdle, Integer maxWait, Integer minIdle) {
67         this.dataSource = getBasicDataSource(connectURI, username, password, driverClass,
68             initialSize, maxActive, maxIdle, maxWait, minIdle);
69     }
70
71     /**
72      * Get a connection from datasource which is thread safe.
73      * {@inheritDoc}
74      */
75     @Override
76     public Connection getConnection() throws DBConnectionPoolException {
77         if (dataSource == null) {
78             throw new DBConnectionPoolException();
79         }
80
81         Connection connection = null;
82         try {
83             connection = dataSource.getConnection();
84         } catch (SQLException e) {
85             logger.error("Get connection failure", e);
86             throw new DBConnectionPoolException(e);
87         }
88
89         if(connection == null){
90             //
91             throw new DBConnectionPoolException("Connection was not created");
92         }
93
94         return connection;
95     }
96
97     /**
98      * Closes and releases all idle connections that are currently stored in the connection pool associated with this
99      * data source.
100      */
101     public void shutdown() {
102         if (dataSource != null) {
103             try {
104                 dataSource.close();
105             } catch (SQLException e) {
106                 logger.error("Datasource cannot be closed normally.", e.getMessage());
107             }
108         }
109
110         dataSource = null;
111     }
112
113     /**
114      * Get datasource status
115      *
116      * @return
117      */
118     public Map<String, Integer> getDataSourceStatus() {
119         Map<String, Integer> map = new HashMap<>(2);
120         map.put(DataSourceStatus.ACTIVE_NUMBER.getAttribute(), dataSource.getNumActive());
121         map.put(DataSourceStatus.IDLE_NUMBER.getAttribute(), dataSource.getNumIdle());
122
123         return map;
124     }
125
126     private BasicDataSource getBasicDataSource(String connectURI, String username, String password,
127                                                String driverClass, Integer initialSize, Integer maxtotal,
128                                                Integer maxIdle, Integer maxWaitMillis, Integer minIdle) {
129         BasicDataSource dataSource = new BasicDataSource();
130         dataSource.setDriverClassName(driverClass);
131         dataSource.setUsername(username);
132         dataSource.setPassword(password);
133         dataSource.setUrl(connectURI);
134
135         if (initialSize != null) {
136             dataSource.setInitialSize(initialSize);
137         }
138         if (maxtotal != null) {
139             dataSource.setMaxTotal(maxtotal);
140         }
141         if (maxIdle != null) {
142             dataSource.setMaxIdle(maxIdle);
143         }
144         if (maxWaitMillis != null) {
145             dataSource.setMaxWaitMillis(maxWaitMillis);
146         }
147         if (minIdle != null) {
148             dataSource.setMinIdle(minIdle);
149         }
150
151         return dataSource;
152     }
153 }