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