0704014df7bfc7a768a88e6c5977d4f0d3810069
[appc.git] / appc-dispatcher / appc-dispatcher-common / appc-data-access-lib / src / main / java / org / openecomp / appc / dao / util / DefaultJdbcConnectionFactory.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.openecomp.appc.dao.util;
26
27 import java.sql.Connection;
28 import java.sql.DriverManager;
29 import java.sql.SQLException;
30
31 public abstract class DefaultJdbcConnectionFactory implements JdbcConnectionFactory {
32
33     private static boolean driverRegistered = false;
34
35     private String jdbcURL;
36     private String jdbcUserName;
37     private String jdbcPassword;
38
39     public void setJdbcURL(String jdbcURL) {
40         this.jdbcURL = jdbcURL;
41     }
42
43     public void setJdbcUserName(String jdbcUserName) {
44         this.jdbcUserName = jdbcUserName;
45     }
46
47     public void setJdbcPassword(String jdbcPassword) {
48         this.jdbcPassword = jdbcPassword;
49     }
50
51
52     protected abstract void registedDriver() throws SQLException;
53
54     @Override
55     public Connection openDbConnection() {
56         try {
57             if(!driverRegistered) {
58                 registedDriver();
59                 driverRegistered = true;
60             }
61             return DriverManager.getConnection(jdbcURL, jdbcUserName, jdbcPassword);
62         } catch(SQLException e) {
63             throw new JdbcRuntimeException(Messages.EXP_JDBC_CONNECT.format(jdbcURL), e);
64         }
65     }
66
67     @Override
68     public void closeDbConnection(Connection connection) {
69         try {
70             connection.close();
71         } catch(SQLException e) {
72             throw new JdbcRuntimeException(Messages.EXP_JDBC_DISCONNECT.format(jdbcURL), e);
73         }
74     }
75 }