added logic to contact different servers for NABARD and UCB banks

This commit is contained in:
2025-08-09 22:24:42 +05:30
parent cc6eaf5852
commit d1850fd72f
4 changed files with 631 additions and 615 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,5 @@
dependency-reduced-pom.xml dependency-reduced-pom.xml
target/ target/
.classpath
.project
.settings/

10
pom.xml
View File

@@ -28,6 +28,16 @@
<artifactId>ojdbc8</artifactId> <artifactId>ojdbc8</artifactId>
<version>23.5.0.24.07</version> <version>23.5.0.24.07</version>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -1,12 +1,16 @@
package com.Main; package com.Main;
import com.fetchFile.FetchReportFile;
import com.sendFile.protocol.SSH.SSHProtocol;
import java.io.InputStream; import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.fetchFile.FetchReportFile; import org.slf4j.Logger;
import com.sendFile.protocol.SSH.SSHProtocol; import org.slf4j.LoggerFactory;
public class Main { public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) { public static void main(String[] args) {
String user = ""; String user = "";
String password = ""; String password = "";
@@ -35,7 +39,7 @@ public class Main {
InputStream input = Main.class.getClassLoader().getResourceAsStream("config.properties"); InputStream input = Main.class.getClassLoader().getResourceAsStream("config.properties");
Properties prop = new Properties(); Properties prop = new Properties();
if (input == null) { if (input == null) {
System.out.println("Sorry, unable to find config.properties"); logger.error("Sorry, unable to find config.properties");
return; return;
} }
prop.load(input); prop.load(input);
@@ -61,29 +65,29 @@ public class Main {
dbPass = prop.getProperty("DB_PASS"); dbPass = prop.getProperty("DB_PASS");
dbSchema = prop.getProperty("DB_SCHEMA"); dbSchema = prop.getProperty("DB_SCHEMA");
System.out.println("Config Properties read:"); logger.info("Config Properties read:");
System.out.println( logger.info(
"REMOTE_HOST_UCB: " "REMOTE_HOST_UCB: {}"
+ hostUcb + "\nREMOTE_HOST_NABARD: {}"
+ "\nREMOTE_HOST_NABARD: " + "\nREMOTE_USER: {}"
+ hostNabard + "\nREMOTE_PASS: {}"
+ "\nREMOTE_USER: " + "\nREMOTE_PORT: {}"
+ user + "\nREMOTE_FILE_PATH: {}"
+ "\nREMOTE_PASS: " + "\nSLEEP_TIME: {}"
+ password + "\nLOCAL_FOLDER_PATH: {}"
+ "\nREMOTE_PORT: " + "\nTRANSFER_PROTOCOL: {}",
+ port hostUcb,
+ "\nREMOTE_FILE_PATH: " hostNabard,
+ remote_file_path user,
+ "\nSLEEP_TIME: " password,
+ sleep port,
+ "\nLOCAL_FOLDER_PATH: " remote_file_path,
+ local_folder_path sleep,
+ "\nTRANSFER_PROTOCOL: " local_folder_path,
+ transfer_protocol); transfer_protocol);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error("Error reading config file", e);
} }
SSHProtocol send = SSHProtocol send =
@@ -136,32 +140,32 @@ public class Main {
try { try {
while (true) { while (true) {
System.out.println("___________________________________________________"); logger.info("___________________________________________________");
try { try {
send.sendFileSSHUcb(); send.sendFileSSHUcb();
send.sendFileSSHNabard(); send.sendFileSSHNabard();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error("Error sending file", e);
} }
try { try {
fetch.fetchFiles(); fetch.fetchFiles();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error("Error fetching report file", e);
} }
try { try {
fetchFailed.fetchFiles(); fetchFailed.fetchFiles();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error("Error fetching failed file", e);
} }
System.out.println("Sleeping for:" + sleep + " Minutes"); logger.info("Sleeping for: {} Minutes", sleep);
System.out.println("___________________________________________________"); logger.info("___________________________________________________");
TimeUnit.MINUTES.sleep(sleep); TimeUnit.MINUTES.sleep(sleep);
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); logger.error("An unexpected error occurred in the main loop", e);
return; return;
} }
} }

View File

@@ -18,475 +18,474 @@ import java.util.Vector;
import com.moveFile.Movefile; import com.moveFile.Movefile;
public class SSHProtocol { public class SSHProtocol {
String user; String user;
String password; String password;
String hostUcb; String hostUcb;
String hostNabard; String hostNabard;
int port; int port;
String local_fileName; String local_fileName;
String local_folder_path_bkp; String local_folder_path_bkp;
String remote_filePath; String remote_filePath;
String remote_fileName; String remote_fileName;
String archive_path; String archive_path;
String local_folder_path; String local_folder_path;
String dbUrl; String dbUrl;
String dbUsr; String dbUsr;
String dbPass; String dbPass;
String dbSchema; String dbSchema;
public SSHProtocol( public SSHProtocol(
String user, String user,
String password, String password,
String hostUcb, String hostUcb,
String hostNabard, String hostNabard,
int port, int port,
String local_fileName, String local_fileName,
String local_folder_path_bkp, String local_folder_path_bkp,
String remote_filePath, String remote_filePath,
String remote_fileName, String remote_fileName,
String archive_path, String archive_path,
String local_folder_path, String local_folder_path,
String dbUrl, String dbUrl,
String dbUsr, String dbUsr,
String dbPass, String dbPass,
String dbSchema) { String dbSchema) {
this.user = user; this.user = user;
this.password = password; this.password = password;
this.hostUcb = hostUcb; this.hostUcb = hostUcb;
this.hostNabard = hostNabard; this.hostNabard = hostNabard;
this.port = port; this.port = port;
this.local_fileName = local_fileName; this.local_fileName = local_fileName;
this.local_folder_path_bkp = local_folder_path_bkp; this.local_folder_path_bkp = local_folder_path_bkp;
this.remote_filePath = remote_filePath; this.remote_filePath = remote_filePath;
this.remote_fileName = remote_fileName; this.remote_fileName = remote_fileName;
this.archive_path = archive_path; this.archive_path = archive_path;
this.local_folder_path = local_folder_path; this.local_folder_path = local_folder_path;
this.dbUrl = dbUrl; this.dbUrl = dbUrl;
this.dbUsr = dbUsr; this.dbUsr = dbUsr;
this.dbPass = dbPass; this.dbPass = dbPass;
this.dbSchema = dbSchema; this.dbSchema = dbSchema;
} }
public void sendFileSSHUcb() throws SQLException { public void sendFileSSHUcb() throws SQLException {
ChannelSftp sftpChannel = null; ChannelSftp sftpChannel = null;
Connection connection = null; Connection connection = null;
Session session = null; Session session = null;
JSch jsch = new JSch(); JSch jsch = new JSch();
String DBfilename = null; String DBfilename = null;
String bankCode = null; String bankCode = null;
String DBfileType = null; String DBfileType = null;
String DBfileId = null; String DBfileId = null;
String DCCBCode = null; String DCCBCode = null;
String procDate = null; String procDate = null;
DateFormat sdf = new SimpleDateFormat("ddMMyyyy"); DateFormat sdf = new SimpleDateFormat("ddMMyyyy");
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
String dateToday = sdf.format(cal.getTime()); String dateToday = sdf.format(cal.getTime());
ResultSet rs = null; ResultSet rs = null;
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs2 = null; ResultSet rs2 = null;
Statement statement = null; Statement statement = null;
Statement statement2 = 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 {
try { try {
connection.close(); System.out.println("Going to SEND files from remote Server via SSH to UCB Server");
} catch (SQLException e1) { System.out.println(
e1.printStackTrace(); "Creating SSH Session: user@host:port |--->"
} + this.user
} + "@"
} catch (Exception ex) { + this.hostUcb
ex.printStackTrace(); + ":"
} finally { + 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 { try {
sftpChannel.disconnect(); Class.forName("oracle.jdbc.driver.OracleDriver");
session.disconnect(); connection = DriverManager.getConnection(this.dbUrl, this.dbUsr, this.dbPass);
System.out.println("Disconnect SFTP Channel"); connection.setAutoCommit(false);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public void sendFileSSHNabard() throws SQLException { if (connection != null) {
ChannelSftp sftpChannel = null; System.out.println("DB Connected..");
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.");
try { ps = connection.prepareStatement(
Class.forName("oracle.jdbc.driver.OracleDriver"); "select k.bnk_code,k.file_name,k.file_type,k.file_id,k.dccb_code,k.proc_dt from "
connection = DriverManager.getConnection(this.dbUrl, this.dbUsr, this.dbPass); + this.dbSchema
connection.setAutoCommit(false); + ".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) { while (rs.next()) {
System.out.println("DB Connected.."); 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 = System.out.println("Creating SFTP Channel.");
connection.prepareStatement( sftpChannel = (ChannelSftp) session.openChannel("sftp");
"select k.bnk_code,k.file_name,k.file_type,k.file_id,k.dccb_code,k.proc_dt from " sftpChannel.connect();
+ this.dbSchema System.out.println("SFTP Channel created.");
+ ".kcc_sftp_log k where k.proc_stat = 'Y' and k.proc_dt = '" System.out.println(
+ dateToday "SFTP -- Local File Name: "
+ "' and k.remarks = 'FILE GENERATED' AND k.bnk_code IN ('0005', '0021')"); + DBfilename
rs = ps.executeQuery(); + " Remote File Name: "
+ this.remote_fileName);
while (rs.next()) { System.out.println(
int errCount = 0; "Going to create brancheise folder:" + bankCode + " at " + this.remote_filePath);
bankCode = rs.getString(1); sftpChannel.cd(this.remote_filePath);
DBfilename = rs.getString(2); try {
DBfileType = rs.getString(3); sftpChannel.mkdir(bankCode);
DBfileId = rs.getString(4); } catch (Exception ex) {
DCCBCode = rs.getString(5); System.out.println("Folder not created: " + ex.getMessage());
procDate = rs.getString(6); }
connection.commit();
System.out.println("Creating SFTP Channel."); try {
sftpChannel = (ChannelSftp) session.openChannel("sftp"); sftpChannel.cd(bankCode);
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( sftpChannel.put(this.local_folder_path + DBfilename, DBfilename);
"Going to create brancheise folder:" + bankCode + " at " + this.remote_filePath); System.out.println("File copied to remote server Successfully, archiving file");
sftpChannel.cd(this.remote_filePath);
try {
sftpChannel.mkdir(bankCode);
} catch (Exception ex) {
System.out.println("Folder not created: " + ex.getMessage());
}
try { Movefile mv = new Movefile();
sftpChannel.cd(bankCode); mv.movefile(new File(this.local_folder_path + DBfilename), this.archive_path);
System.out.println("File archived to: " + this.archive_path);
sftpChannel.put(this.local_folder_path + DBfilename, DBfilename); } catch (Exception ex) {
System.out.println("File copied to remote server Successfully, archiving file"); ex.printStackTrace();
System.out.println("Error pushing file:" + DBfilename);
Movefile mv = new Movefile(); errCount++;
mv.movefile(new File(this.local_folder_path + DBfilename), this.archive_path); }
System.out.println("File archived to: " + this.archive_path); if (errCount == 0) {
} catch (Exception ex) { 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(); ex.printStackTrace();
System.out.println("Error pushing file:" + DBfilename); } finally {
errCount++;
} try {
if (errCount == 0) { sftpChannel.disconnect();
statement2 = connection.createStatement(); session.disconnect();
rs2 = System.out.println("Disconnect SFTP Channel");
statement2.executeQuery( } catch (Exception e1) {
"update " e1.printStackTrace();
+ 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 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 { try {
connection.close(); System.out.println("Going to SEND files from remote Server via SSH to NABARD server");
} 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<ChannelSftp.LsEntry> 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<ChannelSftp.LsEntry> 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( 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(); try {
session.disconnect(); Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Disconnect SFTP Channel"); connection = DriverManager.getConnection(this.dbUrl, this.dbUsr, this.dbPass);
} catch (Exception e) { connection.setAutoCommit(false);
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(); if (connection != null) {
config.put("StrictHostKeyChecking", "no"); System.out.println("DB Connected..");
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); ps = connection.prepareStatement(
System.out.println(sftpChannel.pwd()); "select k.bnk_code,k.file_name,k.file_type,k.file_id,k.dccb_code,k.proc_dt from "
String rootPath = sftpChannel.pwd(); + this.dbSchema
Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(sftpChannel.pwd()); + ".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( System.out.println("Creating SFTP Channel.");
"Current File/Folder: " + entry.getFilename() + " isDir " + entry.getAttrs().isDir()); sftpChannel = (ChannelSftp) session.openChannel("sftp");
if (entry.getAttrs().isDir()) { sftpChannel.connect();
if (entry.getFilename().equals(".") || entry.getFilename().equals("..")) { System.out.println("SFTP Channel created.");
System.out.println("Skipping . and .."); System.out.println(
continue; "SFTP -- Local File Name: "
} + DBfilename
System.out.println("Found folder: " + entry.getFilename()); + " Remote File Name: "
System.out.println("cd: " + rootPath + "/" + entry.getFilename()); + this.remote_fileName);
sftpChannel.cd(rootPath + "/" + entry.getFilename());
System.out.println(sftpChannel.pwd()); System.out.println(
Vector<ChannelSftp.LsEntry> subDirFileList = sftpChannel.ls(remote_report_pattern); "Going to create brancheise folder:" + bankCode + " at " + this.remote_filePath);
for (ChannelSftp.LsEntry subDirFiles : subDirFileList) { sftpChannel.cd(this.remote_filePath);
if (!(new File(this.local_folder_path_bkp + "/" + subDirFiles.getFilename())) try {
.exists()) { sftpChannel.mkdir(bankCode);
sftpChannel.get( } catch (Exception ex) {
subDirFiles.getFilename(), this.local_fileName + subDirFiles.getFilename()); System.out.println("Folder not created: " + ex.getMessage());
System.out.println( }
"File: " + subDirFiles.getFilename() + " copied to :" + this.local_fileName);
continue; 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( } catch (Exception ex) {
"File: " + subDirFiles.getFilename() + " already present in local path"); ex.printStackTrace();
} } finally {
}
}
sftpChannel.disconnect(); try {
session.disconnect(); sftpChannel.disconnect();
System.out.println("Disconnect SFTP Channel"); session.disconnect();
} catch (Exception e) { System.out.println("Disconnect SFTP Channel");
e.printStackTrace(); } 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<ChannelSftp.LsEntry> 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<ChannelSftp.LsEntry> 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<ChannelSftp.LsEntry> 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<ChannelSftp.LsEntry> 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();
}
} }
}
} }