Sunteți pe pagina 1din 4

public static void main(String[] args) { // TODO code application logic here LogManager logMgr = LogManager.

getInstance(); Logger logger = logMgr.getLogger(); IniPropertiesManager iniMgr = IniPropertiesManager.getInstance(); //System.out.println(String.format("Hostname:%s", new Object[]{iniMgr.ge tHostname()})); //System.out.println(String.format("INI file is:%s",new Object[]{iniMgr. getInifile()})); logger.info(String.format("host:%s", new Object[]{iniMgr.getProperty("sf tp", "host")})); FtpManager ftpMgr = FtpManager.getInstance(); ftpMgr.downloadFiles(iniMgr.getProperty("ftp", "source.location"), iniMg r.getProperty("ftp", "target.location")); ftpMgr.disconnect(); SftpManager sftpMgr = SftpManager.getInstance(); File locDir = new File(iniMgr.getProperty("ftp", "target.location")); File[] flist = locDir.listFiles(); for (File f : flist) { logger.info("Entered into the folder " + f.getAbsolutePath()); if (f.isDirectory() && f.getName().endsWith("in")) { logger.info("Folder ending with in"); File[] flist2 = f.listFiles(); ArrayList<String> files = new ArrayList<String>(); for(File f2:flist2) { files.add(f2.getAbsolutePath()); } String destfile = iniMgr.getProperty("filesystem", f.getName()); if(destfile==null || destfile.length()==0) { destfile = iniMgr.getProperty("filesystem", "defaultfile"); } String destfile2 = f.getAbsolutePath() + AppUtil.pathSep() + des

// // // //

tfile; //sftpMgr.uploadFiles(iniMgr.getProperty("sftp", "target.locatio n")+"/"+f.getName(), files); String sftpDir = iniMgr.getProperty("sftp", "target.location") + "/" + f.getName(); ArrayList<String> remotefiles = sftpMgr.getFilesList(sftpDir); boolean found = false; if(remotefiles!=null) for (String f3 : remotefiles) { if (f3.equalsIgnoreCase(destfile)) { found = true; break; } } ArrayList<String> filecontents = new ArrayList<String>(); if (found) { String tempFile1 = iniMgr.getProperty("ftp", "target.locatio n") + AppUtil.pathSep() + "temp_" + System.currentTimeMillis() + ".tmp"; try { FileOutputStream fout = new FileOutputStream(tempFile1); try { sftpMgr.getCSftp().get(sftpDir + "/" + destfile, fou t); try { fout.close(); } catch (IOException ex) {

logger.error("IO Exception while closing file " + tempFile1 + ": " + ex.getMessage()); } try { filecontents.add(readFileAsString(tempFile1)); logger.info("Added remote file " + destfile + " contents into " + tempFile1); logger.info("Added contents of file " + tempFile 1 + " to buffer"); if (!(new File(tempFile1).delete())) { logger.warn("Deleting file " + tempFile1 + " On JVM exit "); new File(tempFile1).deleteOnExit(); } } catch (IOException ex) { logger.error("IO Exception while reading content s of file " + tempFile1); } } catch (SftpException ex) { logger.error("Failed to download file from server " + destfile + ": " + ex.getMessage()); } } catch (FileNotFoundException ex) { logger.error("File not found exception for " + tempFile1 ); } } flist2 = f.listFiles(); // Implement solution for recon files for (File locFile : flist2) { if (locFile.getName().toUpperCase().startsWith("RECON")) { { FileInputStream fis2 = null; try { sftpMgr.getCSftp().cd(sftpDir); fis2 = new FileInputStream(locFile.getAbsolutePa th()); logger.info("Uploading file " + locFile.getAbsol utePath() + " into sftp server"); sftpMgr.getCSftp().put(fis2, locFile.getName()); //fis2.close(); } catch (FileNotFoundException ex) { logger.error("File not found exception on recon file " + locFile.getAbsolutePath()); } catch (SftpException ex) { logger.error("SFTP Exception while uploading rec on file " + locFile.getAbsolutePath()); } finally { try { fis2.close(); logger.info("Deleting local recon file " + l ocFile.getAbsolutePath()); if (!(locFile.delete())) { logger.info("Deleting file " + destfile2 + " on JVM exit"); locFile.deleteOnExit(); }

} catch (IOException ex) { logger.error("Exception raised while closing recon file " + locFile.getAbsolutePath()); } } } continue; } if (!locFile.getName().toUpperCase().startsWith("PDS")) { continue; } if (!locFile.getName().equalsIgnoreCase(destfile)) { String contents; try { contents = readFileAsString(locFile.getAbsolutePath( )); if (contents != null) { filecontents.add(contents); logger.info("Added contents of file " + locFile. getName() + " to buffer"); if (!locFile.delete()) { logger.warn("Deleting file " + locFile.getAb solutePath() + " on JVM exit"); locFile.deleteOnExit(); } } else { logger.warn("Contents of file " + locFile.getAbs olutePath() + " returned as null"); } } catch (IOException ex) { logger.error("IO Exception while reading contents of file " + locFile.getAbsolutePath()); } } } if (filecontents.size() > 0) { try { FileWriter fw = new FileWriter(destfile2); logger.info("Writing contents of buffer to " + destfile2 ); for (String contents : filecontents) { //logger.info("Writing contents \n\""+contents+"\"\n into file"); fw.write(contents); } fw.close(); String remotefile = destfile; try { sftpMgr.getCSftp().cd(sftpDir); FileInputStream fis2 = new FileInputStream(destfile2 ); logger.info("Uploading file " + destfile2 + " into s ftp server"); sftpMgr.getCSftp().put(fis2, destfile); fis2.close(); logger.info("Deleting local file " + destfile2);

if (!(new File(destfile2).delete())) { logger.info("Deleting file " + destfile2 + " on JVM exit"); new File(destfile2).deleteOnExit(); } } catch (SftpException ex) { logger.error("Exception raised while uploading file " + destfile2 + ": " + ex.getMessage()); } } catch (IOException ex) { logger.error("IO Exception raised while creating file on system " + destfile2 + ": " + ex.getMessage()); } } } } sftpMgr.quit(); }

S-ar putea să vă placă și