[SDNC-5] Boron port and bug fixes
[sdnc/core.git] / dblib / provider / src / main / java / org / openecomp / sdnc / sli / resource / dblib / jdbc / MySQLCachedDataSource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 AT&T
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdnc.sli.resource.dblib.jdbc;
22
23 import java.sql.SQLFeatureNotSupportedException;
24 import java.util.Properties;
25
26 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSource;
27 import org.openecomp.sdnc.sli.resource.dblib.DBConfigException;
28 import org.openecomp.sdnc.sli.resource.dblib.config.BaseDBConfiguration;
29 import org.openecomp.sdnc.sli.resource.dblib.config.JDBCConfiguration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
34
35
36  
37
38 /**
39  * @version $Revision: 1.7 $
40  * Change Log
41  * Author         Date     Comments
42  * ============== ======== ====================================================
43  * Rich Tabedzki
44  */
45
46 public class MySQLCachedDataSource extends CachedDataSource
47 {
48         private String dbUserId;
49         private String dbPasswd;
50         private String dbUrl;
51         
52         private String minLimit;
53         private String maxLimit;
54         private String initialLimit;
55         
56         private static final String AS_CONF_ERROR = "AS_CONF_ERROR: ";
57         
58         private static Logger LOGGER = LoggerFactory.getLogger(MySQLCachedDataSource.class);
59
60         /**
61          * @param jdbcElem
62          */
63         public MySQLCachedDataSource(BaseDBConfiguration jdbcElem)
64         {
65                         super(jdbcElem);
66         }
67
68         @Override
69         protected void configure(BaseDBConfiguration xmlElem) throws DBConfigException 
70         {
71                 BaseDBConfiguration jdbcConfig = (BaseDBConfiguration)xmlElem;
72                 if(jdbcConfig.getConnTimeout() > 0){
73                         this.CONN_REQ_TIMEOUT = jdbcConfig.getConnTimeout();
74                 }
75                 if(jdbcConfig.getRequestTimeout() > 0){
76                                 this.DATA_REQ_TIMEOUT = jdbcConfig.getRequestTimeout();
77                 }
78
79         // set connection pool name
80                 String dbConnectionName = jdbcConfig.getDbConnectionName();
81         super.setDbConnectionName(dbConnectionName);
82         // Configure the JDBC connection
83         dbUserId = jdbcConfig.getDbUserId();
84         if (dbUserId == null)
85         {
86                 String errorMsg =  "Invalid XML contents: JDBCConnection missing dbUserId attribute";
87                 LOGGER.error(AS_CONF_ERROR + errorMsg);
88             throw new DBConfigException(errorMsg);
89         }
90
91         dbPasswd = jdbcConfig.getDbPasswd();
92         if (dbPasswd == null)
93         {
94                 String errorMsg =  "Invalid XML contents: JDBCConnection missing dbPasswd attribute";
95                 LOGGER.error(AS_CONF_ERROR + errorMsg);
96             throw new DBConfigException(errorMsg);
97         }
98         /*
99         dbDriver = jdbcConfig.getDbDriver();
100         if (dbDriver == null)
101         {
102                 String errorMsg =  "Invalid XML contents: JDBCConnection missing dbDriver attribute";
103                 LOGGER.error(AS_CONF_ERROR + errorMsg);
104                 throw new ScpTblUpdateError(errorMsg);
105         }
106         */
107
108         minLimit = Integer.toString(jdbcConfig.getDbMinLimit());
109         if (minLimit == null)
110         {
111                 String errorMsg =  "Invalid XML contents: JDBC Connection missing minLimit attribute";
112                 LOGGER.error(AS_CONF_ERROR + errorMsg);
113                 throw new DBConfigException(errorMsg);
114         }
115         maxLimit =  Integer.toString(jdbcConfig.getDbMaxLimit());
116         if (maxLimit == null)
117         {
118                 String errorMsg =  "Invalid XML contents: JDBC Connection missing maxLimit attribute";
119                 LOGGER.error(AS_CONF_ERROR + errorMsg);
120                 throw new DBConfigException(errorMsg);
121         }
122         initialLimit =  Integer.toString(jdbcConfig.getDbInitialLimit());
123         if (initialLimit == null)
124         {
125                 String errorMsg =  "Invalid XML contents: JDBC Connection missing initialLimit attribute";
126                 LOGGER.error(AS_CONF_ERROR + errorMsg);
127                 throw new DBConfigException(errorMsg);
128         }
129
130         dbUrl = jdbcConfig.getDbUrl();
131         if(dbUrl == null){
132                 String errorMsg =  "Invalid XML contents: JDBCConnection missing dbUrl attribute";
133                 LOGGER.error(AS_CONF_ERROR + errorMsg);
134             throw new DBConfigException(errorMsg);
135         }
136         
137                 try {
138                         
139                         MysqlDataSource dataSource = new MysqlDataSource();
140                     dataSource.setUser(dbUserId);
141                     dataSource.setPassword(dbPasswd);
142                     dataSource.setURL(dbUrl);
143 //                  dataSource.setInitialSize(5);
144 //                  dataSource.setMaxTotal(60);
145 //                  dataSource.setMaxActive(100);
146 //                  dataSource.setMaxWait(10000);
147 //                  dataSource.setMaxIdle(10);
148
149                         Properties connAttr = new Properties();
150
151                         connAttr.setProperty("MinLimit", minLimit);
152                         connAttr.setProperty("MaxLimit", maxLimit);
153                         connAttr.setProperty("InitialLimit", initialLimit);
154                         connAttr.setProperty("TRANSACTION_ISOLATION","SERIALIZABLE");
155                         connAttr.setProperty("CONNECTION_TAG", dbConnectionName.toUpperCase()+"_CONNECTION");
156                         connAttr.setProperty("InactivityTimeout", "900");
157                         connAttr.setProperty("AbandonedConnectionTimeout", "600");
158                         connAttr.setProperty("PropertyCheckInterval", "60");
159                         connAttr.setProperty("ValidateConnection", "true");
160                         
161
162                         synchronized(this)
163                         {
164                                 this.ds = dataSource;
165
166                                 initialized = true;
167                                 LOGGER.info("MySQLDataSource <"+dbConnectionName+"> configured successfully. Using URL: "+dbUrl);
168                         }
169
170 //              } catch (SQLException exc) {
171 //                      initialized = false;
172 //                      StringBuffer sb = new StringBuffer();
173 //                      sb.append("Failed to initialize MySQLDataSource<");
174 //                      sb.append(dbConnectionName).append(">. Reason: ");
175 //                      sb.append(exc.getMessage());
176 //                      LOGGER.error("AS_CONF_ERROR: " + sb.toString());
177 ////                    throw new DBConfigException(e.getMessage());
178                 } catch (Exception exc) {
179                 initialized = false;
180                         StringBuffer sb = new StringBuffer();
181                         sb.append("Failed to initialize MySQLCachedDataSource <");
182                         sb.append(dbConnectionName).append(">. Reason: ");
183                         sb.append(exc.getMessage());
184                         LOGGER.error("AS_CONF_ERROR: " + sb.toString());
185 //              throw new DBConfigException(e.getMessage());
186         }
187     }
188
189         public final String getDbUrl()
190         {
191                 return dbUrl;
192         }
193
194         public final String getDbUserId()
195         {
196                 return dbUserId;
197         }
198
199         public final String getDbPasswd()
200         {
201                 return dbPasswd;
202         }
203
204         public static MySQLCachedDataSource createInstance(BaseDBConfiguration config) /*throws Exception*/ {
205                 return new MySQLCachedDataSource(config);
206         }
207         
208         public String toString(){
209                 return getDbConnectionName();
210         }
211
212         public java.util.logging.Logger getParentLogger()
213                         throws SQLFeatureNotSupportedException {
214                 // TODO Auto-generated method stub
215                 return null;
216         }
217 }