Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 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 |
|
| 22 |
|
| 23 | package org.onap.osam.dao;
|
| 24 |
|
| 25 | import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
|
| 26 |
|
| 27 | import java.sql.*;
|
| 28 |
|
| 29 |
|
| 30 | public class FnAppDoaImpl {
|
| 31 |
|
| 32 | static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FnAppDoaImpl.class);
|
| 33 |
|
| 34 | public int getProfileCount(String driver, String URL, String username, String password) {
|
| 35 | Connection dbc = null;
|
| 36 | PreparedStatement pst = null;
|
| 37 | ResultSet rs = null;
|
| 38 | String q = null;
|
| 39 | int count = 0;
|
| 40 | try {
|
| 41 | dbc = getConnection(URL,username,password);
|
| 42 | logger.debug(EELFLoggerDelegate.debugLogger, "getConnection:::"+ dbc);
|
| 43 | q = "select count(*) from fn_app";
|
| 44 | pst = dbc.prepareStatement(q);
|
| 45 | rs = pst.executeQuery();
|
| 46 |
|
| 47 | if (rs.next())
|
| 48 | count = rs.getInt(1);
|
| 49 | } catch(Exception ex) {
|
| 50 | logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
|
| 51 | } finally {
|
| 52 | cleanup(rs,pst,dbc);
|
| 53 | }
|
| 54 | logger.debug(EELFLoggerDelegate.debugLogger, "count:::"+ count);
|
| 55 | return count;
|
| 56 | }
|
| 57 |
|
| 58 | public static Connection getConnection(String url, String username, String password) throws SQLException {
|
| 59 | java.sql.Connection con=null;
|
| 60 |
|
| 61 | if( url!=null && username!=null && password!=null ){
|
| 62 | con = DriverManager.getConnection(url, username, password);
|
| 63 | }
|
| 64 |
|
| 65 | logger.info("Connection Successful");
|
| 66 |
|
| 67 | return con;
|
| 68 |
|
| 69 | }
|
| 70 |
|
| 71 | public static void cleanup(ResultSet rs, PreparedStatement st, Connection c) {
|
| 72 | if (rs != null) {
|
| 73 | closeResultSet(rs);
|
| 74 | }
|
| 75 | if (st != null) {
|
| 76 | closePreparedStatement(st);
|
| 77 | }
|
| 78 | if (c != null) {
|
| 79 | rollbackAndCloseConnection(c);
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | private static void rollbackAndCloseConnection(Connection c) {
|
| 84 | try {
|
| 85 | c.rollback();
|
| 86 | } catch (Exception e) {
|
| 87 | if (logger != null)
|
| 88 | logger.error("Error when trying to rollback connection", e);
|
| 89 | }
|
| 90 | try {
|
| 91 | c.close();
|
| 92 | } catch (Exception e) {
|
| 93 | if (logger != null)
|
| 94 | logger.error("Error when trying to close connection", e);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | private static void closePreparedStatement(PreparedStatement st) {
|
| 99 | try {
|
| 100 | st.close();
|
| 101 | } catch (Exception e) {
|
| 102 | if (logger != null)
|
| 103 | logger.error("Error when trying to close statement", e);
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | private static void closeResultSet(ResultSet rs) {
|
| 108 | try {
|
| 109 | rs.close();
|
| 110 | } catch (Exception e) {
|
| 111 | if (logger != null)
|
| 112 | logger.error("Error when trying to close result set", e);
|
| 113 | }
|
| 114 | }
|
| 115 | }
|