2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.dao.util;
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;
34 import java.sql.Connection;
35 import java.sql.SQLException;
39 * This class implements
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
46 * The singleton instance of this class has been instantiated by blueprint.
47 * An example is shown in the {@link DBConnectionPoolService}
49 public class AppcDatabaseConnectionPool implements DBConnectionPoolService {
51 private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcDatabaseConnectionPool.class);
53 private DBConnectionPool dbConnectionPool;
54 private String dbName;
56 public AppcDatabaseConnectionPool() {
60 public AppcDatabaseConnectionPool(String dbUrl, String userName, String password, String jdbcDriver) {
61 dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
65 * Injected by blueprint
69 public void setDbName(String dbName) {
74 * Bean init method used by blueprint
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);
83 dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
85 // a simple health check
86 Connection connection = null;
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.");
94 if (connection != null) {
97 } catch (SQLException e) {
98 logger.error("DB connection cannot be closed:", e.getMessage());
104 * Bean destroy method used by blueprint
106 public void destroy() {
107 if (dbConnectionPool != null) {
108 dbConnectionPool.shutdown();
113 * Get the connection from connection pool.
115 * @return Connection. If the provided db information is not correct,
116 * the return value might be null.
119 public Connection getConnection() throws DBConnectionPoolException {
120 return dbConnectionPool.getConnection();
124 * Get dbcp status like active_status.
126 * More details about status of DBConnectionPool,
127 * go check {@link org.onap.appc.dao.util.dbcp.DBConnectionPool#getDataSourceStatus()}
129 * @return a map contains some dbcp information.
132 public Map<String, Integer> getDataSourceStatus() {
133 return dbConnectionPool.getDataSourceStatus();
136 private String getConnectionProperty(Configuration configuration, PropertyPattern propertyPattern) {
137 String property = configuration.getProperty(String.format(propertyPattern.getPattern(), dbName), "");
141 private String getJDBCDriver(Configuration configuration) {
142 return configuration.getProperty(PropertyPattern.DRIVER.getPattern(), "");