2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dao.util;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.appc.configuration.Configuration;
30 import org.onap.appc.configuration.ConfigurationFactory;
31 import org.onap.appc.dao.util.api.DBConnectionPoolService;
32 import org.onap.appc.dao.util.dbcp.DBConnectionPool;
33 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
35 import java.sql.Connection;
36 import java.sql.SQLException;
40 * This class implements
42 * @see org.onap.appc.dao.util.dbcp.DBConnectionPool
43 * that provides concrete implemenation of accessing appc database which basic setup
44 * data would be got from global configuration.
45 * @see org.onap.appc.configuration.Configuration
47 * The singleton instance of this class has been instantiated by blueprint.
48 * An example is shown in the {@link DBConnectionPoolService}
50 public class AppcDatabaseConnectionPool implements DBConnectionPoolService {
52 private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcDatabaseConnectionPool.class);
54 private DBConnectionPool dbConnectionPool;
55 private String dbName;
57 public AppcDatabaseConnectionPool() {
61 public AppcDatabaseConnectionPool(String dbUrl, String userName, String password, String jdbcDriver) {
62 dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
66 * Injected by blueprint
70 public void setDbName(String dbName) {
75 * Bean init method used by blueprint
78 Configuration configuration = ConfigurationFactory.getConfiguration();
79 String dbUrl = getConnectionProperty(configuration, PropertyPattern.DBURL);
80 String userName = getConnectionProperty(configuration, PropertyPattern.USERNAME);
81 String password = getConnectionProperty(configuration, PropertyPattern.PASSWORD);
82 String jdbcDriver = getJDBCDriver(configuration);
84 dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
86 // a simple health check
87 Connection connection = null;
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.");
95 if (connection != null) {
98 } catch (SQLException e) {
99 logger.error("DB connection cannot be closed:", e.getMessage());
105 * Bean destroy method used by blueprint
107 public void destroy() {
108 if (dbConnectionPool != null) {
109 dbConnectionPool.shutdown();
114 * Get the connection from connection pool.
116 * @return Connection. If the provided db information is not correct,
117 * the return value might be null.
120 public Connection getConnection() throws DBConnectionPoolException {
121 return dbConnectionPool.getConnection();
125 * Get dbcp status like active_status.
127 * More details about status of DBConnectionPool,
128 * go check {@link org.onap.appc.dao.util.dbcp.DBConnectionPool#getDataSourceStatus()}
130 * @return a map contains some dbcp information.
133 public Map<String, Integer> getDataSourceStatus() {
134 return dbConnectionPool.getDataSourceStatus();
137 private String getConnectionProperty(Configuration configuration, PropertyPattern propertyPattern) {
138 String property = configuration.getProperty(String.format(propertyPattern.getPattern(), dbName), "");
142 private String getJDBCDriver(Configuration configuration) {
143 return configuration.getProperty(PropertyPattern.DRIVER.getPattern(), "");