Include impacted changes for APPC-346,APPC-348
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dao.util;
26
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;
34
35 import java.sql.Connection;
36 import java.sql.SQLException;
37 import java.util.Map;
38
39 /**
40  * This class implements
41  *
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
46  * <p>
47  * The singleton instance of this class has been instantiated by blueprint.
48  * An example is shown in the {@link DBConnectionPoolService}
49  */
50 public class AppcDatabaseConnectionPool implements DBConnectionPoolService {
51     enum PropertyPattern {
52         DBURL("org.onap.appc.db.url.%s"),
53         USERNAME("org.onap.appc.db.user.%s"),
54         PASSWORD("org.onap.appc.db.pass.%s"),
55         DRIVER("org.onap.appc.db.jdbc.driver");
56
57         private String pattern;
58
59         PropertyPattern(String pattern) {
60             this.pattern = pattern;
61         }
62
63         public String getPattern() {
64             return pattern;
65         }
66     }
67
68     private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcDatabaseConnectionPool.class);
69
70     private DBConnectionPool dbConnectionPool;
71     private String dbName;
72
73     public AppcDatabaseConnectionPool() {
74         // do nothing
75     }
76
77     public AppcDatabaseConnectionPool(String dbUrl, String userName, String password, String jdbcDriver) {
78         dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
79     }
80
81     /**
82      * Injected by blueprint
83      *
84      * @param dbName
85      */
86     public void setDbName(String dbName) {
87         this.dbName = dbName;
88     }
89
90     /**
91      * Bean init method used by blueprint
92      */
93     public void init() {
94         Configuration configuration = ConfigurationFactory.getConfiguration();
95         String dbUrl = getConnectionProperty(configuration, PropertyPattern.DBURL);
96         String userName = getConnectionProperty(configuration, PropertyPattern.USERNAME);
97         String password = getConnectionProperty(configuration, PropertyPattern.PASSWORD);
98         String jdbcDriver = getJDBCDriver(configuration);
99
100         dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
101
102         // a simple health check
103         Connection connection = null;
104         try {
105             connection = dbConnectionPool.getConnection();
106         } catch (DBConnectionPoolException e) {
107             logger.error("DB connection pool is created failed." +
108                 "Please make sure the provided information is correct.");
109         }
110
111         if (connection != null) {
112             try {
113                 connection.close();
114             } catch (SQLException e) {
115                 logger.error("DB connection cannot be closed:", e.getMessage());
116             }
117         }
118     }
119
120     /**
121      * Bean destroy method used by blueprint
122      */
123     public void destroy() {
124         if (dbConnectionPool != null) {
125             dbConnectionPool.shutdown();
126         }
127     }
128
129     /**
130      * Get the connection from connection pool.
131      *
132      * @return Connection. If the provided db information is not correct,
133      * the return value might be null.
134      */
135     @Override
136     public Connection getConnection() throws DBConnectionPoolException {
137         return dbConnectionPool.getConnection();
138     }
139
140     /**
141      * Get dbcp status like active_status.
142      * <p>
143      * More details about status of DBConnectionPool,
144      * go check {@link org.onap.appc.dao.util.dbcp.DBConnectionPool#getDataSourceStatus()}
145      *
146      * @return a map contains some dbcp information.
147      */
148     @Override
149     public Map<String, Integer> getDataSourceStatus() {
150         return dbConnectionPool.getDataSourceStatus();
151     }
152
153     private String getConnectionProperty(Configuration configuration, PropertyPattern propertyPattern) {
154         String property = configuration.getProperty(String.format(propertyPattern.getPattern(), dbName), "");
155         return property;
156     }
157
158     private String getJDBCDriver(Configuration configuration) {
159         return configuration.getProperty(PropertyPattern.DRIVER.getPattern(), "");
160     }
161 }