1f4a75911da26dc4c1e739309d7ce9c78bca9051
[appc.git] / appc-dispatcher / appc-dispatcher-common / appc-data-access-lib / src / main / java / org / onap / appc / dao / util / AppcDatabaseConnectionPool.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;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.onap.appc.configuration.Configuration;
31 import org.onap.appc.configuration.ConfigurationFactory;
32 import org.onap.appc.dao.util.api.DBConnectionPoolService;
33 import org.onap.appc.dao.util.dbcp.DBConnectionPool;
34 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
35
36 import java.sql.Connection;
37 import java.sql.SQLException;
38 import java.util.Map;
39
40 /**
41  * This class implements
42  *
43  * @see org.onap.appc.dao.util.dbcp.DBConnectionPool
44  * that provides concrete implemenation of accessing appc database which basic setup
45  * data would be got from global configuration.
46  * @see org.onap.appc.configuration.Configuration
47  * <p>
48  * The singleton instance of this class has been instantiated by blueprint.
49  * An example is shown in the {@link DBConnectionPoolService}
50  */
51 public class AppcDatabaseConnectionPool implements DBConnectionPoolService {
52
53     private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcDatabaseConnectionPool.class);
54
55     private DBConnectionPool dbConnectionPool;
56     private String dbName;
57
58     public AppcDatabaseConnectionPool() {
59         // do nothing
60     }
61
62     public AppcDatabaseConnectionPool(String dbUrl, String userName, String password, String jdbcDriver) {
63         dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
64     }
65
66     /**
67      * Injected by blueprint
68      *
69      * @param dbName
70      */
71     public void setDbName(String dbName) {
72         this.dbName = dbName;
73     }
74
75     /**
76      * Bean init method used by blueprint
77      */
78     public void init() {
79         Configuration configuration = ConfigurationFactory.getConfiguration();
80         String dbUrl = getConnectionProperty(configuration, PropertyPattern.DBURL);
81         String userName = getConnectionProperty(configuration, PropertyPattern.USERNAME);
82         String password = getConnectionProperty(configuration, PropertyPattern.PASSWORD);
83         String jdbcDriver = getJDBCDriver(configuration);
84
85         dbConnectionPool = getDBConnectionPool(dbUrl, userName, password, jdbcDriver);
86         // a simple health check
87         Connection connection = null;
88         try {
89             connection = dbConnectionPool.getConnection();
90         } catch (DBConnectionPoolException e) {
91             logger.error("DB connection pool is created failed." +
92                 "Please make sure the provided information is correct.");
93         }
94
95         if (connection != null) {
96             try {
97                 connection.close();
98             } catch (SQLException e) {
99                 logger.error("DB connection cannot be closed:", e.getMessage());
100             }
101         }
102     }
103
104     /**
105      * Bean destroy method used by blueprint
106      */
107     public void destroy() {
108         if (dbConnectionPool != null) {
109             dbConnectionPool.shutdown();
110         }
111     }
112
113     /**
114      * Get the connection from connection pool.
115      *
116      * @return Connection. If the provided db information is not correct,
117      * the return value might be null.
118      */
119     @Override
120     public Connection getConnection() throws DBConnectionPoolException {
121         return dbConnectionPool.getConnection();
122     }
123
124     /**
125      * Get dbcp status like active_status.
126      * <p>
127      * More details about status of DBConnectionPool,
128      * go check {@link org.onap.appc.dao.util.dbcp.DBConnectionPool#getDataSourceStatus()}
129      *
130      * @return a map contains some dbcp information.
131      */
132     @Override
133     public Map<String, Integer> getDataSourceStatus() {
134         return dbConnectionPool.getDataSourceStatus();
135     }
136
137     private String getConnectionProperty(Configuration configuration, PropertyPattern propertyPattern) {
138         String property = configuration.getProperty(String.format(propertyPattern.getPattern(), dbName), "");
139         return property;
140     }
141
142     private String getJDBCDriver(Configuration configuration) {
143         return configuration.getProperty(PropertyPattern.DRIVER.getPattern(), "");
144     }
145
146     protected DBConnectionPool getDBConnectionPool(String dbUrl, String userName, String password, String jdbcDriver) {
147         return new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
148     }
149 }