diff --git a/.gitignore b/.gitignore index ca37d98..92efd5c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ dependency-reduced-pom.xml target/ +.classpath +.project +.settings/ diff --git a/pom.xml b/pom.xml index f918a6f..d988a5c 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,16 @@ ojdbc8 23.5.0.24.07 + + org.slf4j + slf4j-api + 1.7.32 + + + org.slf4j + slf4j-simple + 1.7.32 + diff --git a/src/main/java/com/Main/Main.java b/src/main/java/com/Main/Main.java index 59ad228..5df2fd8 100644 --- a/src/main/java/com/Main/Main.java +++ b/src/main/java/com/Main/Main.java @@ -1,168 +1,172 @@ -package com.Main; - -import java.io.InputStream; -import java.util.Properties; -import java.util.concurrent.TimeUnit; -import com.fetchFile.FetchReportFile; -import com.sendFile.protocol.SSH.SSHProtocol; - -public class Main { - public static void main(String[] args) { - String user = ""; - String password = ""; - String hostUcb = ""; - String hostNabard = ""; - String local_folder_path = ""; - String local_archive_path = ""; - String local_report_path = ""; - String local_report_bkp_path = ""; - String remote_file_path = ""; - String remote_report_path = ""; - String remote_report_pattern = ""; - String transfer_protocol = ""; - String remote_failed_path = ""; - String local_failed_path = ""; - String local_fileName = ""; - String remote_fileName = ""; - String dbUrl = ""; - String dbUsr = ""; - String dbPass = ""; - String dbSchema = ""; - int port = 22; - long sleep = 5L; - - try { - InputStream input = Main.class.getClassLoader().getResourceAsStream("config.properties"); - Properties prop = new Properties(); - if (input == null) { - System.out.println("Sorry, unable to find config.properties"); - return; - } - prop.load(input); - - hostUcb = prop.getProperty("REMOTE_HOST_UCB"); - hostNabard = prop.getProperty("REMOTE_HOST_NABARD"); - user = prop.getProperty("REMOTE_USER"); - password = prop.getProperty("REMOTE_PASS"); - port = Integer.parseInt(prop.getProperty("REMOTE_PORT")); - sleep = Long.parseLong(prop.getProperty("SLEEP_TIME_MINS")); - local_folder_path = prop.getProperty("LOCAL_FOLDER_PATH"); - local_archive_path = prop.getProperty("ARCHIVE_FOLDER_PATH"); - local_report_path = prop.getProperty("LOCAL_REPORT_PATH"); - local_report_bkp_path = prop.getProperty("LOCAL_REPORT_BKP_PATH"); - remote_file_path = prop.getProperty("REMOTE_INPUT_FILE_PATH"); - remote_report_path = prop.getProperty("REMOTE_OUTPUT_FILE_PATH"); - transfer_protocol = prop.getProperty("TRANSFER_PROTOCOL"); - remote_report_pattern = prop.getProperty("REMOTE_REPORT_PATTERN"); - remote_failed_path = prop.getProperty("REMOTE_FAILURE_FILE_PATH"); - local_failed_path = prop.getProperty("LOCAL_FAILED_PATH"); - dbUrl = prop.getProperty("DB_URL"); - dbUsr = prop.getProperty("DB_USER"); - dbPass = prop.getProperty("DB_PASS"); - dbSchema = prop.getProperty("DB_SCHEMA"); - - System.out.println("Config Properties read:"); - System.out.println( - "REMOTE_HOST_UCB: " - + hostUcb - + "\nREMOTE_HOST_NABARD: " - + hostNabard - + "\nREMOTE_USER: " - + user - + "\nREMOTE_PASS: " - + password - + "\nREMOTE_PORT: " - + port - + "\nREMOTE_FILE_PATH: " - + remote_file_path - + "\nSLEEP_TIME: " - + sleep - + "\nLOCAL_FOLDER_PATH: " - + local_folder_path - + "\nTRANSFER_PROTOCOL: " - + transfer_protocol); - - } catch (Exception e) { - e.printStackTrace(); - } - - SSHProtocol send = - new SSHProtocol( - user, - password, - hostUcb, - hostNabard, - port, - local_fileName, - "", - remote_file_path, - remote_fileName, - local_archive_path, - local_folder_path, - dbUrl, - dbUsr, - dbPass, - dbSchema); - FetchReportFile fetch = - new FetchReportFile( - user, - password, - hostUcb, - hostNabard, - local_report_path, - local_report_bkp_path, - remote_report_path, - transfer_protocol, - port, - remote_report_pattern, - dbUrl, - dbUsr, - dbPass); - FetchReportFile fetchFailed = - new FetchReportFile( - user, - password, - hostUcb, - hostNabard, - local_failed_path, - "", - remote_failed_path, - transfer_protocol, - port, - remote_report_pattern, - dbUrl, - dbUsr, - dbPass); - - try { - while (true) { - System.out.println("___________________________________________________"); - try { - send.sendFileSSHUcb(); - send.sendFileSSHNabard(); - } catch (Exception e) { - e.printStackTrace(); - } - try { - fetch.fetchFiles(); - } catch (Exception e) { - e.printStackTrace(); - } - try { - fetchFailed.fetchFiles(); - } catch (Exception e) { - e.printStackTrace(); - } - - System.out.println("Sleeping for:" + sleep + " Minutes"); - System.out.println("___________________________________________________"); - TimeUnit.MINUTES.sleep(sleep); - } - - } catch (Exception e) { - - System.out.println(e); - return; - } - } -} +package com.Main; + +import com.fetchFile.FetchReportFile; +import com.sendFile.protocol.SSH.SSHProtocol; +import java.io.InputStream; +import java.util.Properties; +import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Main { + private static final Logger logger = LoggerFactory.getLogger(Main.class); + + public static void main(String[] args) { + String user = ""; + String password = ""; + String hostUcb = ""; + String hostNabard = ""; + String local_folder_path = ""; + String local_archive_path = ""; + String local_report_path = ""; + String local_report_bkp_path = ""; + String remote_file_path = ""; + String remote_report_path = ""; + String remote_report_pattern = ""; + String transfer_protocol = ""; + String remote_failed_path = ""; + String local_failed_path = ""; + String local_fileName = ""; + String remote_fileName = ""; + String dbUrl = ""; + String dbUsr = ""; + String dbPass = ""; + String dbSchema = ""; + int port = 22; + long sleep = 5L; + + try { + InputStream input = Main.class.getClassLoader().getResourceAsStream("config.properties"); + Properties prop = new Properties(); + if (input == null) { + logger.error("Sorry, unable to find config.properties"); + return; + } + prop.load(input); + + hostUcb = prop.getProperty("REMOTE_HOST_UCB"); + hostNabard = prop.getProperty("REMOTE_HOST_NABARD"); + user = prop.getProperty("REMOTE_USER"); + password = prop.getProperty("REMOTE_PASS"); + port = Integer.parseInt(prop.getProperty("REMOTE_PORT")); + sleep = Long.parseLong(prop.getProperty("SLEEP_TIME_MINS")); + local_folder_path = prop.getProperty("LOCAL_FOLDER_PATH"); + local_archive_path = prop.getProperty("ARCHIVE_FOLDER_PATH"); + local_report_path = prop.getProperty("LOCAL_REPORT_PATH"); + local_report_bkp_path = prop.getProperty("LOCAL_REPORT_BKP_PATH"); + remote_file_path = prop.getProperty("REMOTE_INPUT_FILE_PATH"); + remote_report_path = prop.getProperty("REMOTE_OUTPUT_FILE_PATH"); + transfer_protocol = prop.getProperty("TRANSFER_PROTOCOL"); + remote_report_pattern = prop.getProperty("REMOTE_REPORT_PATTERN"); + remote_failed_path = prop.getProperty("REMOTE_FAILURE_FILE_PATH"); + local_failed_path = prop.getProperty("LOCAL_FAILED_PATH"); + dbUrl = prop.getProperty("DB_URL"); + dbUsr = prop.getProperty("DB_USER"); + dbPass = prop.getProperty("DB_PASS"); + dbSchema = prop.getProperty("DB_SCHEMA"); + + logger.info("Config Properties read:"); + logger.info( + "REMOTE_HOST_UCB: {}" + + "\nREMOTE_HOST_NABARD: {}" + + "\nREMOTE_USER: {}" + + "\nREMOTE_PASS: {}" + + "\nREMOTE_PORT: {}" + + "\nREMOTE_FILE_PATH: {}" + + "\nSLEEP_TIME: {}" + + "\nLOCAL_FOLDER_PATH: {}" + + "\nTRANSFER_PROTOCOL: {}", + hostUcb, + hostNabard, + user, + password, + port, + remote_file_path, + sleep, + local_folder_path, + transfer_protocol); + + } catch (Exception e) { + logger.error("Error reading config file", e); + } + + SSHProtocol send = + new SSHProtocol( + user, + password, + hostUcb, + hostNabard, + port, + local_fileName, + "", + remote_file_path, + remote_fileName, + local_archive_path, + local_folder_path, + dbUrl, + dbUsr, + dbPass, + dbSchema); + FetchReportFile fetch = + new FetchReportFile( + user, + password, + hostUcb, + hostNabard, + local_report_path, + local_report_bkp_path, + remote_report_path, + transfer_protocol, + port, + remote_report_pattern, + dbUrl, + dbUsr, + dbPass); + FetchReportFile fetchFailed = + new FetchReportFile( + user, + password, + hostUcb, + hostNabard, + local_failed_path, + "", + remote_failed_path, + transfer_protocol, + port, + remote_report_pattern, + dbUrl, + dbUsr, + dbPass); + + try { + while (true) { + logger.info("___________________________________________________"); + try { + send.sendFileSSHUcb(); + send.sendFileSSHNabard(); + } catch (Exception e) { + logger.error("Error sending file", e); + } + try { + fetch.fetchFiles(); + } catch (Exception e) { + logger.error("Error fetching report file", e); + } + try { + fetchFailed.fetchFiles(); + } catch (Exception e) { + logger.error("Error fetching failed file", e); + } + + logger.info("Sleeping for: {} Minutes", sleep); + logger.info("___________________________________________________"); + TimeUnit.MINUTES.sleep(sleep); + } + + } catch (Exception e) { + + logger.error("An unexpected error occurred in the main loop", e); + return; + } + } +} diff --git a/src/main/java/com/sendFile/protocol/SSH/SSHProtocol.java b/src/main/java/com/sendFile/protocol/SSH/SSHProtocol.java index f4ecf1d..064665b 100644 --- a/src/main/java/com/sendFile/protocol/SSH/SSHProtocol.java +++ b/src/main/java/com/sendFile/protocol/SSH/SSHProtocol.java @@ -18,475 +18,474 @@ import java.util.Vector; import com.moveFile.Movefile; public class SSHProtocol { - String user; - String password; - String hostUcb; - String hostNabard; - int port; - String local_fileName; - String local_folder_path_bkp; - String remote_filePath; - String remote_fileName; - String archive_path; - String local_folder_path; - String dbUrl; - String dbUsr; - String dbPass; - String dbSchema; + String user; + String password; + String hostUcb; + String hostNabard; + int port; + String local_fileName; + String local_folder_path_bkp; + String remote_filePath; + String remote_fileName; + String archive_path; + String local_folder_path; + String dbUrl; + String dbUsr; + String dbPass; + String dbSchema; - public SSHProtocol( - String user, - String password, - String hostUcb, - String hostNabard, - int port, - String local_fileName, - String local_folder_path_bkp, - String remote_filePath, - String remote_fileName, - String archive_path, - String local_folder_path, - String dbUrl, - String dbUsr, - String dbPass, - String dbSchema) { - this.user = user; - this.password = password; - this.hostUcb = hostUcb; - this.hostNabard = hostNabard; - this.port = port; - this.local_fileName = local_fileName; - this.local_folder_path_bkp = local_folder_path_bkp; - this.remote_filePath = remote_filePath; - this.remote_fileName = remote_fileName; - this.archive_path = archive_path; - this.local_folder_path = local_folder_path; - this.dbUrl = dbUrl; - this.dbUsr = dbUsr; - this.dbPass = dbPass; - this.dbSchema = dbSchema; - } + public SSHProtocol( + String user, + String password, + String hostUcb, + String hostNabard, + int port, + String local_fileName, + String local_folder_path_bkp, + String remote_filePath, + String remote_fileName, + String archive_path, + String local_folder_path, + String dbUrl, + String dbUsr, + String dbPass, + String dbSchema) { + this.user = user; + this.password = password; + this.hostUcb = hostUcb; + this.hostNabard = hostNabard; + this.port = port; + this.local_fileName = local_fileName; + this.local_folder_path_bkp = local_folder_path_bkp; + this.remote_filePath = remote_filePath; + this.remote_fileName = remote_fileName; + this.archive_path = archive_path; + this.local_folder_path = local_folder_path; + this.dbUrl = dbUrl; + this.dbUsr = dbUsr; + this.dbPass = dbPass; + this.dbSchema = dbSchema; + } - public void sendFileSSHUcb() throws SQLException { - ChannelSftp sftpChannel = null; - Connection connection = null; - Session session = null; - JSch jsch = new JSch(); - String DBfilename = null; - String bankCode = null; - String DBfileType = null; - String DBfileId = null; - String DCCBCode = null; - String procDate = null; - DateFormat sdf = new SimpleDateFormat("ddMMyyyy"); - Calendar cal = Calendar.getInstance(); - String dateToday = sdf.format(cal.getTime()); - ResultSet rs = null; - PreparedStatement ps = null; - ResultSet rs2 = null; - Statement statement = null; - Statement statement2 = null; - try { - System.out.println("Going to SEND files from remote Server via SSH to UCB Server"); - System.out.println( - "Creating SSH Session: user@host:port |--->" - + this.user - + "@" - + this.hostUcb - + ":" - + this.port); - session = jsch.getSession(this.user, this.hostUcb, this.port); - session.setPassword(this.password); - Properties config = new Properties(); - config.put("StrictHostKeyChecking", "no"); - session.setConfig(config); - System.out.println("Establishing Connection..."); - session.connect(); - System.out.println("Connection established."); - - try { - Class.forName("oracle.jdbc.driver.OracleDriver"); - connection = DriverManager.getConnection(this.dbUrl, this.dbUsr, this.dbPass); - connection.setAutoCommit(false); - - if (connection != null) { - System.out.println("DB Connected.."); - } - - ps = - connection.prepareStatement( - "select k.bnk_code,k.file_name,k.file_type,k.file_id,k.dccb_code,k.proc_dt from " - + this.dbSchema - + ".kcc_sftp_log k where k.proc_stat = 'Y' and k.proc_dt = '" - + dateToday - + "' and k.remarks = 'FILE GENERATED' AND k.bnk_code NOT IN ('0005', '0021')"); - rs = ps.executeQuery(); - - while (rs.next()) { - int errCount = 0; - bankCode = rs.getString(1); - DBfilename = rs.getString(2); - DBfileType = rs.getString(3); - DBfileId = rs.getString(4); - DCCBCode = rs.getString(5); - procDate = rs.getString(6); - connection.commit(); - - System.out.println("Creating SFTP Channel."); - sftpChannel = (ChannelSftp) session.openChannel("sftp"); - sftpChannel.connect(); - System.out.println("SFTP Channel created."); - System.out.println( - "SFTP -- Local File Name: " - + DBfilename - + " Remote File Name: " - + this.remote_fileName); - - System.out.println( - "Going to create brancheise folder:" + bankCode + " at " + this.remote_filePath); - sftpChannel.cd(this.remote_filePath); - try { - sftpChannel.mkdir(bankCode); - } catch (Exception ex) { - System.out.println("Folder not created: " + ex.getMessage()); - } - - try { - sftpChannel.cd(bankCode); - - sftpChannel.put(this.local_folder_path + DBfilename, DBfilename); - System.out.println("File copied to remote server Successfully, archiving file"); - - Movefile mv = new Movefile(); - mv.movefile(new File(this.local_folder_path + DBfilename), this.archive_path); - System.out.println("File archived to: " + this.archive_path); - } catch (Exception ex) { - ex.printStackTrace(); - System.out.println("Error pushing file:" + DBfilename); - errCount++; - } - if (errCount == 0) { - statement2 = connection.createStatement(); - rs2 = - statement2.executeQuery( - "update " - + this.dbSchema - + ".KCC_SFTP_LOG set proc_stat='S',sftp_remarks='FILE" - + " SENT',FILE_SFTP_SND_TIME= to_date(to_char(SYSTIMESTAMP," - + " 'DD-MON-RRHH24:MI:SS'),'DD-MON-RRHH24:MI:SS') where file_name='" - + DBfilename - + "' and BNK_CODE='" - + bankCode - + "' and file_id='" - + DBfileId - + "' and DCCB_CODE='" - + DCCBCode - + "' and PROC_DT='" - + procDate - + "' "); - connection.commit(); - rs2.close(); - System.out.println("File successfully pushed:" + DBfilename); - } - } - } catch (Exception ex) { - ex.printStackTrace(); - } finally { + public void sendFileSSHUcb() throws SQLException { + ChannelSftp sftpChannel = null; + Connection connection = null; + Session session = null; + JSch jsch = new JSch(); + String DBfilename = null; + String bankCode = null; + String DBfileType = null; + String DBfileId = null; + String DCCBCode = null; + String procDate = null; + DateFormat sdf = new SimpleDateFormat("ddMMyyyy"); + Calendar cal = Calendar.getInstance(); + String dateToday = sdf.format(cal.getTime()); + ResultSet rs = null; + PreparedStatement ps = null; + ResultSet rs2 = null; + Statement statement = null; + Statement statement2 = null; try { - connection.close(); - } catch (SQLException e1) { - e1.printStackTrace(); - } - } - } catch (Exception ex) { - ex.printStackTrace(); - } finally { + System.out.println("Going to SEND files from remote Server via SSH to UCB Server"); + System.out.println( + "Creating SSH Session: user@host:port |--->" + + this.user + + "@" + + this.hostUcb + + ":" + + this.port); + session = jsch.getSession(this.user, this.hostUcb, this.port); + session.setPassword(this.password); + Properties config = new Properties(); + config.put("StrictHostKeyChecking", "no"); + session.setConfig(config); + System.out.println("Establishing Connection..."); + session.connect(); + System.out.println("Connection established."); - try { - sftpChannel.disconnect(); - session.disconnect(); - System.out.println("Disconnect SFTP Channel"); - } catch (Exception e1) { - e1.printStackTrace(); - } - } - } + try { + Class.forName("oracle.jdbc.driver.OracleDriver"); + connection = DriverManager.getConnection(this.dbUrl, this.dbUsr, this.dbPass); + connection.setAutoCommit(false); - public void sendFileSSHNabard() throws SQLException { - ChannelSftp sftpChannel = null; - Connection connection = null; - Session session = null; - JSch jsch = new JSch(); - String DBfilename = null; - String bankCode = null; - String DBfileType = null; - String DBfileId = null; - String DCCBCode = null; - String procDate = null; - DateFormat sdf = new SimpleDateFormat("ddMMyyyy"); - Calendar cal = Calendar.getInstance(); - String dateToday = sdf.format(cal.getTime()); - ResultSet rs = null; - PreparedStatement ps = null; - ResultSet rs2 = null; - Statement statement = null; - Statement statement2 = null; - try { - System.out.println("Going to SEND files from remote Server via SSH to NABARD server"); - System.out.println( - "Creating SSH Session: user@host:port |--->" - + this.user - + "@" - + this.hostNabard - + ":" - + this.port); - session = jsch.getSession(this.user, this.hostNabard, this.port); - session.setPassword(this.password); - Properties config = new Properties(); - config.put("StrictHostKeyChecking", "no"); - session.setConfig(config); - System.out.println("Establishing Connection..."); - session.connect(); - System.out.println("Connection established."); + if (connection != null) { + System.out.println("DB Connected.."); + } - try { - Class.forName("oracle.jdbc.driver.OracleDriver"); - connection = DriverManager.getConnection(this.dbUrl, this.dbUsr, this.dbPass); - connection.setAutoCommit(false); + ps = connection.prepareStatement( + "select k.bnk_code,k.file_name,k.file_type,k.file_id,k.dccb_code,k.proc_dt from " + + this.dbSchema + + ".kcc_sftp_log k where k.proc_stat = 'Y' and k.proc_dt = '" + + dateToday + + "' and k.remarks = 'FILE GENERATED' AND k.bnk_code NOT IN ('0005', '0021', '0016', '0018', '0001', '0004', '0007')"); + rs = ps.executeQuery(); - if (connection != null) { - System.out.println("DB Connected.."); - } + while (rs.next()) { + int errCount = 0; + bankCode = rs.getString(1); + DBfilename = rs.getString(2); + DBfileType = rs.getString(3); + DBfileId = rs.getString(4); + DCCBCode = rs.getString(5); + procDate = rs.getString(6); + connection.commit(); - ps = - connection.prepareStatement( - "select k.bnk_code,k.file_name,k.file_type,k.file_id,k.dccb_code,k.proc_dt from " - + this.dbSchema - + ".kcc_sftp_log k where k.proc_stat = 'Y' and k.proc_dt = '" - + dateToday - + "' and k.remarks = 'FILE GENERATED' AND k.bnk_code IN ('0005', '0021')"); - rs = ps.executeQuery(); + System.out.println("Creating SFTP Channel."); + sftpChannel = (ChannelSftp) session.openChannel("sftp"); + sftpChannel.connect(); + System.out.println("SFTP Channel created."); + System.out.println( + "SFTP -- Local File Name: " + + DBfilename + + " Remote File Name: " + + this.remote_fileName); - while (rs.next()) { - int errCount = 0; - bankCode = rs.getString(1); - DBfilename = rs.getString(2); - DBfileType = rs.getString(3); - DBfileId = rs.getString(4); - DCCBCode = rs.getString(5); - procDate = rs.getString(6); - connection.commit(); + System.out.println( + "Going to create brancheise folder:" + bankCode + " at " + this.remote_filePath); + sftpChannel.cd(this.remote_filePath); + try { + sftpChannel.mkdir(bankCode); + } catch (Exception ex) { + System.out.println("Folder not created: " + ex.getMessage()); + } - System.out.println("Creating SFTP Channel."); - sftpChannel = (ChannelSftp) session.openChannel("sftp"); - sftpChannel.connect(); - System.out.println("SFTP Channel created."); - System.out.println( - "SFTP -- Local File Name: " - + DBfilename - + " Remote File Name: " - + this.remote_fileName); + try { + sftpChannel.cd(bankCode); - System.out.println( - "Going to create brancheise folder:" + bankCode + " at " + this.remote_filePath); - sftpChannel.cd(this.remote_filePath); - try { - sftpChannel.mkdir(bankCode); - } catch (Exception ex) { - System.out.println("Folder not created: " + ex.getMessage()); - } + sftpChannel.put(this.local_folder_path + DBfilename, DBfilename); + System.out.println("File copied to remote server Successfully, archiving file"); - try { - sftpChannel.cd(bankCode); - - sftpChannel.put(this.local_folder_path + DBfilename, DBfilename); - System.out.println("File copied to remote server Successfully, archiving file"); - - Movefile mv = new Movefile(); - mv.movefile(new File(this.local_folder_path + DBfilename), this.archive_path); - System.out.println("File archived to: " + this.archive_path); - } catch (Exception ex) { + Movefile mv = new Movefile(); + mv.movefile(new File(this.local_folder_path + DBfilename), this.archive_path); + System.out.println("File archived to: " + this.archive_path); + } catch (Exception ex) { + ex.printStackTrace(); + System.out.println("Error pushing file:" + DBfilename); + errCount++; + } + if (errCount == 0) { + statement2 = connection.createStatement(); + rs2 = statement2.executeQuery( + "update " + + this.dbSchema + + ".KCC_SFTP_LOG set proc_stat='S',sftp_remarks='FILE" + + " SENT',FILE_SFTP_SND_TIME= to_date(to_char(SYSTIMESTAMP," + + " 'DD-MON-RRHH24:MI:SS'),'DD-MON-RRHH24:MI:SS') where file_name='" + + DBfilename + + "' and BNK_CODE='" + + bankCode + + "' and file_id='" + + DBfileId + + "' and DCCB_CODE='" + + DCCBCode + + "' and PROC_DT='" + + procDate + + "' "); + connection.commit(); + rs2.close(); + System.out.println("File successfully pushed:" + DBfilename); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + try { + connection.close(); + } catch (SQLException e1) { + e1.printStackTrace(); + } + } + } catch (Exception ex) { ex.printStackTrace(); - System.out.println("Error pushing file:" + DBfilename); - errCount++; - } - if (errCount == 0) { - statement2 = connection.createStatement(); - rs2 = - statement2.executeQuery( - "update " - + this.dbSchema - + ".KCC_SFTP_LOG set proc_stat='S',sftp_remarks='FILE" - + " SENT',FILE_SFTP_SND_TIME= to_date(to_char(SYSTIMESTAMP," - + " 'DD-MON-RRHH24:MI:SS'),'DD-MON-RRHH24:MI:SS') where file_name='" - + DBfilename - + "' and BNK_CODE='" - + bankCode - + "' and file_id='" - + DBfileId - + "' and DCCB_CODE='" - + DCCBCode - + "' and PROC_DT='" - + procDate - + "' "); - connection.commit(); - rs2.close(); - System.out.println("File successfully pushed:" + DBfilename); - } + } finally { + + try { + sftpChannel.disconnect(); + session.disconnect(); + System.out.println("Disconnect SFTP Channel"); + } catch (Exception e1) { + e1.printStackTrace(); + } } - } catch (Exception ex) { - ex.printStackTrace(); - } finally { + } + + public void sendFileSSHNabard() throws SQLException { + ChannelSftp sftpChannel = null; + Connection connection = null; + Session session = null; + JSch jsch = new JSch(); + String DBfilename = null; + String bankCode = null; + String DBfileType = null; + String DBfileId = null; + String DCCBCode = null; + String procDate = null; + DateFormat sdf = new SimpleDateFormat("ddMMyyyy"); + Calendar cal = Calendar.getInstance(); + String dateToday = sdf.format(cal.getTime()); + ResultSet rs = null; + PreparedStatement ps = null; + ResultSet rs2 = null; + Statement statement = null; + Statement statement2 = null; try { - connection.close(); - } catch (SQLException e1) { - e1.printStackTrace(); - } - } - } catch (Exception ex) { - ex.printStackTrace(); - } finally { - - try { - sftpChannel.disconnect(); - session.disconnect(); - System.out.println("Disconnect SFTP Channel"); - } catch (Exception e1) { - e1.printStackTrace(); - } - } - } - - public void fetchFileSSHUcb(String remote_report_pattern) { - try { - System.out.println("Going to FETCH files from remote Server"); - JSch jsch = new JSch(); - System.out.println( - "Creating SSH Session: user@host:port |--->" - + this.user - + "@" - + this.hostUcb - + ":" - + this.port); - Session session = jsch.getSession(this.user, this.hostNabard, this.port); - session.setPassword(this.password); - - Properties config = new Properties(); - config.put("StrictHostKeyChecking", "no"); - session.setConfig(config); - System.out.println("Establishing Connection..."); - session.connect(); - System.out.println("Connection established."); - System.out.println("Creating SFTP Channel."); - ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); - sftpChannel.connect(); - System.out.println("SFTP Channel created."); - - sftpChannel.cd(this.remote_filePath); - System.out.println(sftpChannel.pwd()); - String rootPath = sftpChannel.pwd(); - Vector list = sftpChannel.ls(sftpChannel.pwd()); - - for (ChannelSftp.LsEntry entry : list) { - - System.out.println( - "Current File/Folder: " + entry.getFilename() + " isDir " + entry.getAttrs().isDir()); - if (entry.getAttrs().isDir()) { - if (entry.getFilename().equals(".") || entry.getFilename().equals("..")) { - System.out.println("Skipping . and .."); - continue; - } - System.out.println("Found folder: " + entry.getFilename()); - System.out.println("cd: " + rootPath + "/" + entry.getFilename()); - sftpChannel.cd(rootPath + "/" + entry.getFilename()); - System.out.println(sftpChannel.pwd()); - Vector subDirFileList = sftpChannel.ls(remote_report_pattern); - for (ChannelSftp.LsEntry subDirFiles : subDirFileList) { - if (!(new File(this.local_folder_path_bkp + "/" + subDirFiles.getFilename())) - .exists()) { - sftpChannel.get( - subDirFiles.getFilename(), this.local_fileName + subDirFiles.getFilename()); - System.out.println( - "File: " + subDirFiles.getFilename() + " copied to :" + this.local_fileName); - continue; - } + System.out.println("Going to SEND files from remote Server via SSH to NABARD server"); System.out.println( - "File: " + subDirFiles.getFilename() + " already present in local path"); - } - } - } + "Creating SSH Session: user@host:port |--->" + + this.user + + "@" + + this.hostNabard + + ":" + + this.port); + session = jsch.getSession(this.user, this.hostNabard, this.port); + session.setPassword(this.password); + Properties config = new Properties(); + config.put("StrictHostKeyChecking", "no"); + session.setConfig(config); + System.out.println("Establishing Connection..."); + session.connect(); + System.out.println("Connection established."); - sftpChannel.disconnect(); - session.disconnect(); - System.out.println("Disconnect SFTP Channel"); - } catch (Exception e) { - e.printStackTrace(); - } - } - public void fetchFileSSHNabard(String remote_report_pattern) { - try { - System.out.println("Going to FETCH files from remote Server"); - JSch jsch = new JSch(); - System.out.println( - "Creating SSH Session: user@host:port |--->" - + this.user - + "@" - + this.hostNabard - + ":" - + this.port); - Session session = jsch.getSession(this.user, this.hostNabard, this.port); - session.setPassword(this.password); + try { + Class.forName("oracle.jdbc.driver.OracleDriver"); + connection = DriverManager.getConnection(this.dbUrl, this.dbUsr, this.dbPass); + connection.setAutoCommit(false); - Properties config = new Properties(); - config.put("StrictHostKeyChecking", "no"); - session.setConfig(config); - System.out.println("Establishing Connection..."); - session.connect(); - System.out.println("Connection established."); - System.out.println("Creating SFTP Channel."); - ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); - sftpChannel.connect(); - System.out.println("SFTP Channel created."); + if (connection != null) { + System.out.println("DB Connected.."); + } - sftpChannel.cd(this.remote_filePath); - System.out.println(sftpChannel.pwd()); - String rootPath = sftpChannel.pwd(); - Vector list = sftpChannel.ls(sftpChannel.pwd()); + ps = connection.prepareStatement( + "select k.bnk_code,k.file_name,k.file_type,k.file_id,k.dccb_code,k.proc_dt from " + + this.dbSchema + + ".kcc_sftp_log k where k.proc_stat = 'Y' and k.proc_dt = '" + + dateToday + + "' and k.remarks = 'FILE GENERATED' AND k.bnk_code IN ('0005', '0021', '0016', '0018', '0001', '0004', '0007')"); + rs = ps.executeQuery(); - for (ChannelSftp.LsEntry entry : list) { + while (rs.next()) { + int errCount = 0; + bankCode = rs.getString(1); + DBfilename = rs.getString(2); + DBfileType = rs.getString(3); + DBfileId = rs.getString(4); + DCCBCode = rs.getString(5); + procDate = rs.getString(6); + connection.commit(); - System.out.println( - "Current File/Folder: " + entry.getFilename() + " isDir " + entry.getAttrs().isDir()); - if (entry.getAttrs().isDir()) { - if (entry.getFilename().equals(".") || entry.getFilename().equals("..")) { - System.out.println("Skipping . and .."); - continue; - } - System.out.println("Found folder: " + entry.getFilename()); - System.out.println("cd: " + rootPath + "/" + entry.getFilename()); - sftpChannel.cd(rootPath + "/" + entry.getFilename()); - System.out.println(sftpChannel.pwd()); - Vector subDirFileList = sftpChannel.ls(remote_report_pattern); - for (ChannelSftp.LsEntry subDirFiles : subDirFileList) { - if (!(new File(this.local_folder_path_bkp + "/" + subDirFiles.getFilename())) - .exists()) { - sftpChannel.get( - subDirFiles.getFilename(), this.local_fileName + subDirFiles.getFilename()); - System.out.println( - "File: " + subDirFiles.getFilename() + " copied to :" + this.local_fileName); - continue; + System.out.println("Creating SFTP Channel."); + sftpChannel = (ChannelSftp) session.openChannel("sftp"); + sftpChannel.connect(); + System.out.println("SFTP Channel created."); + System.out.println( + "SFTP -- Local File Name: " + + DBfilename + + " Remote File Name: " + + this.remote_fileName); + + System.out.println( + "Going to create brancheise folder:" + bankCode + " at " + this.remote_filePath); + sftpChannel.cd(this.remote_filePath); + try { + sftpChannel.mkdir(bankCode); + } catch (Exception ex) { + System.out.println("Folder not created: " + ex.getMessage()); + } + + try { + sftpChannel.cd(bankCode); + + sftpChannel.put(this.local_folder_path + DBfilename, DBfilename); + System.out.println("File copied to remote server Successfully, archiving file"); + + Movefile mv = new Movefile(); + mv.movefile(new File(this.local_folder_path + DBfilename), this.archive_path); + System.out.println("File archived to: " + this.archive_path); + } catch (Exception ex) { + ex.printStackTrace(); + System.out.println("Error pushing file:" + DBfilename); + errCount++; + } + if (errCount == 0) { + statement2 = connection.createStatement(); + rs2 = statement2.executeQuery( + "update " + + this.dbSchema + + ".KCC_SFTP_LOG set proc_stat='S',sftp_remarks='FILE" + + " SENT',FILE_SFTP_SND_TIME= to_date(to_char(SYSTIMESTAMP," + + " 'DD-MON-RRHH24:MI:SS'),'DD-MON-RRHH24:MI:SS') where file_name='" + + DBfilename + + "' and BNK_CODE='" + + bankCode + + "' and file_id='" + + DBfileId + + "' and DCCB_CODE='" + + DCCBCode + + "' and PROC_DT='" + + procDate + + "' "); + connection.commit(); + rs2.close(); + System.out.println("File successfully pushed:" + DBfilename); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + try { + connection.close(); + } catch (SQLException e1) { + e1.printStackTrace(); + } } - System.out.println( - "File: " + subDirFiles.getFilename() + " already present in local path"); - } - } - } + } catch (Exception ex) { + ex.printStackTrace(); + } finally { - sftpChannel.disconnect(); - session.disconnect(); - System.out.println("Disconnect SFTP Channel"); - } catch (Exception e) { - e.printStackTrace(); + try { + sftpChannel.disconnect(); + session.disconnect(); + System.out.println("Disconnect SFTP Channel"); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + } + + public void fetchFileSSHUcb(String remote_report_pattern) { + try { + String local_folder_path_bkp = "/home/ec2-user/RUPAYKCCFILES/reportFiles_bkp/"; + System.out.println("Going to FETCH files from remote Server"); + JSch jsch = new JSch(); + System.out.println( + "Creating SSH Session: user@host:port |--->" + + this.user + + "@" + + this.hostUcb + + ":" + + this.port); + Session session = jsch.getSession(this.user, this.hostUcb, this.port); + session.setPassword(this.password); + + Properties config = new Properties(); + config.put("StrictHostKeyChecking", "no"); + session.setConfig(config); + System.out.println("Establishing Connection..."); + session.connect(); + System.out.println("Connection established."); + System.out.println("Creating SFTP Channel."); + ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); + sftpChannel.connect(); + System.out.println("SFTP Channel created."); + + sftpChannel.cd(this.remote_filePath); + System.out.println(sftpChannel.pwd()); + String rootPath = sftpChannel.pwd(); + Vector list = sftpChannel.ls(sftpChannel.pwd()); + + for (ChannelSftp.LsEntry entry : list) { + + System.out.println( + "Current File/Folder: " + entry.getFilename() + " isDir " + entry.getAttrs().isDir()); + if (entry.getAttrs().isDir()) { + if (entry.getFilename().equals(".") || entry.getFilename().equals("..")) { + System.out.println("Skipping . and .."); + continue; + } + System.out.println("Found folder: " + entry.getFilename()); + + System.out.println("cd: " + rootPath + "/" + entry.getFilename()); + System.out.println(sftpChannel.pwd()); + sftpChannel.cd(rootPath + "/" + entry.getFilename()); + Vector subDirFileList = sftpChannel + .ls(sftpChannel.pwd() + "/" + remote_report_pattern); + for (ChannelSftp.LsEntry subDirFiles : subDirFileList) { + if (!(new File(local_folder_path_bkp + "/" + subDirFiles.getFilename())).exists()) { + sftpChannel.get( + subDirFiles.getFilename(), this.local_fileName + subDirFiles.getFilename()); + System.out.println( + "File: " + subDirFiles.getFilename() + " copied to :" + this.local_fileName); + continue; + } + System.out.println( + "File: " + subDirFiles.getFilename() + " already present in local path"); + } + } + } + + sftpChannel.disconnect(); + session.disconnect(); + System.out.println("Disconnect SFTP Channel"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void fetchFileSSHNabard(String remote_report_pattern) { + try { + System.out.println("Going to FETCH files from remote Server"); + JSch jsch = new JSch(); + System.out.println( + "Creating SSH Session: user@host:port |--->" + + this.user + + "@" + + this.hostNabard + + ":" + + this.port); + Session session = jsch.getSession(this.user, this.hostNabard, this.port); + session.setPassword(this.password); + + Properties config = new Properties(); + config.put("StrictHostKeyChecking", "no"); + session.setConfig(config); + System.out.println("Establishing Connection..."); + session.connect(); + System.out.println("Connection established."); + System.out.println("Creating SFTP Channel."); + ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); + sftpChannel.connect(); + System.out.println("SFTP Channel created."); + + sftpChannel.cd(this.remote_filePath); + System.out.println(sftpChannel.pwd()); + String rootPath = sftpChannel.pwd(); + Vector list = sftpChannel.ls(sftpChannel.pwd()); + + for (ChannelSftp.LsEntry entry : list) { + + System.out.println( + "Current File/Folder: " + entry.getFilename() + " isDir " + entry.getAttrs().isDir()); + if (entry.getAttrs().isDir()) { + if (entry.getFilename().equals(".") || entry.getFilename().equals("..")) { + System.out.println("Skipping . and .."); + continue; + } + System.out.println("Found folder: " + entry.getFilename()); + System.out.println("cd: " + rootPath + "/" + entry.getFilename()); + sftpChannel.cd(rootPath + "/" + entry.getFilename()); + System.out.println(sftpChannel.pwd()); + Vector subDirFileList = sftpChannel.ls(remote_report_pattern); + for (ChannelSftp.LsEntry subDirFiles : subDirFileList) { + if (!(new File(this.local_folder_path_bkp + "/" + subDirFiles.getFilename())) + .exists()) { + sftpChannel.get( + subDirFiles.getFilename(), this.local_fileName + subDirFiles.getFilename()); + System.out.println( + "File: " + subDirFiles.getFilename() + " copied to :" + this.local_fileName); + continue; + } + System.out.println( + "File: " + subDirFiles.getFilename() + " already present in local path"); + } + } + } + + sftpChannel.disconnect(); + session.disconnect(); + System.out.println("Disconnect SFTP Channel"); + } catch (Exception e) { + e.printStackTrace(); + } } - } }