Change code in appc dispatcher for new LCMs in R6
[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-2019 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  * ============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, String driverClass,
66             Integer initialSize, Integer maxActive, 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         Connection connection = null;
81         try {
82             connection = dataSource.getConnection();
83         } catch (SQLException e) {
84             logger.error("Get connection failure", e);
85             throw new DBConnectionPoolException(e);
86         }
87
88         if (connection == null) {
89             // Don't necessarily know why, but can report. . . .
90             throw new DBConnectionPoolException("Connection was not created");
91         }
92
93         return connection;
94     }
95
96     /**
97      * Closes and releases all idle connections that are currently stored in the connection pool associated with this
98      * data source.
99      */
100     public void shutdown() {
101         if (dataSource != null) {
102             try {
103                 dataSource.close();
104             } catch (SQLException e) {
105                 logger.error("Datasource cannot be closed normally.", e.getMessage());
106             }
107         }
108
109         dataSource = null;
110     }
111
112     /**
113      * Get datasource status
114      *
115      * @return
116      */
117     public Map<String, Integer> getDataSourceStatus() {
118         Map<String, Integer> map = new HashMap<>(2);
119         map.put(DataSourceStatus.ACTIVE_NUMBER.getAttribute(), dataSource.getNumActive());
120         map.put(DataSourceStatus.IDLE_NUMBER.getAttribute(), dataSource.getNumIdle());
121
122         return map;
123     }
124
125     protected BasicDataSource getBasicDataSource(String connectURI, String username, String password,
126             String driverClass, Integer initialSize, Integer maxTotal, Integer maxIdle, Integer maxWaitMillis,
127             Integer minIdle) {
128         BasicDataSource dataSource = new BasicDataSource();
129         dataSource.setDriverClassName(driverClass);
130         dataSource.setUsername(username);
131         dataSource.setPassword(password);
132         dataSource.setUrl(connectURI);
133
134         if (initialSize != null) {
135             dataSource.setInitialSize(initialSize);
136         }
137         if (maxTotal != null) {
138             dataSource.setMaxTotal(maxTotal);
139         }
140         if (maxIdle != null) {
141             dataSource.setMaxIdle(maxIdle);
142         }
143         if (maxWaitMillis != null) {
144             dataSource.setMaxWaitMillis(maxWaitMillis);
145         }
146         if (minIdle != null) {
147             dataSource.setMinIdle(minIdle);
148         }
149
150         return dataSource;
151     }
152 }