How to use yafs libraries?
The yafs-lib
is a generic library and it can't be used alone. It must be used with an implementation. Your own implementaiton or the availables: yafs-libFile
and yafs-libFTP
.
The following code uses yafs-libFile
to simply synchronize two hierarchy of files on a file system. It uses the xiss-lib-file-1.3.1.jar
library of Xiss web.
//initializing a synchronizer Synchronizer sync = new Synchronizer(); sync.setHierarchyVisitorFactory(new FileHierarchyVisitorFactory()); sync.setToSyncCheckerFactory(new FileToSyncCheckerFactory()); sync.setSyncActionPerformerFactory(new FileSyncActionPerformerFactory()); File srcDir; File trgDir; //choose source and target directories (...) Source s = new Source(srcDir.toURI()); Target t = new Target(trgDir.toURI()); sync.setSource(s); sync.setTarget(t): try { sync.buildHierarchy(); sync.checkToSync(); if (sync.getActionCount() != 0) { sync.synchronize(); } } catch (IOException e) { e.printStackTrace(); }
It is possible to stnchronize a hierarchy of files from a file system to a FTP server. For this yafs-libFile
and yafs-libFTP
are necessary. The yafs-libFTP-1.0.1.jar
uses commons-net-2.2.jar
Apache's library.
//initializing a synchronizer Synchronizer sync = new Synchronizer(); sync.setHierarchyVisitorFactory(new FileHierarchyVisitorFactory()); sync.setToSyncCheckerFactory(new FTPToSyncCheckerFactory()); sync.setSyncActionPerformerFactory(new FileToFTPSyncActionPerformerFactory()); File srcDir; //choose source directory (...) FTPLocation trgDir; //choose the FTP server and directory in an account trgDir.setHost(... // FTP server host trgDir.setPort(... // if the default port isn't used trgDir.setAccountName(... // account on the FTP server trgDir.setPath(... // path to a directory, the default is the root ('/') of the account Source s = new Source(srcDir.toURI()); Target t = new Target(trgDir.toURI()); sync.setSource(s); sync.setTarget(t): String passwd = null; // get the password of the account on the FTP server if necessary by any way (...) FTPContext context = new FTPContext(sync); try { sync.buildHierarchy(); // build the hierarchy of file from file system context.checkToSync(passwd); // check if sync actions are necessary if (context.getActionCount() != 0) { context.synchronize(passwd); // synchronize files on the FTP server } } catch (IOException e) { e.printStackTrace(); }
In the example above the connection to the FTP server is managed by the FTP context. At each step it opens a new connection to the FTP sever. It is possible to manage yoursef the FTP connection and pass the context of this connection to the synchronizer. The following code is an example to make the two last steps of synchronization in one connection.
Synchroniser sync = new Synchronizer(); (...) After synchronizer intialization and the build of the hierarchy with a source FTPLocation trgDir; //choose the FTP server and directory in an account (...) sync.setTarget(trgDir.toURI()); // use directly the Apache's commons-net FTP client FTPClient ftpClient = new FTPClient(); String passwd = null; // get the password of the account on the FTP server if necessary by any way (...) try { ftpClient.connect(trgDir.getHost(), trgDir.getPort()); int reply = ftpClient.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); System.out.println(" [INFO] Connection refused: "+ftpClient.getReplyString()); return; } System.out.println(" [INFO] Connected: "+ftpClient.getReplyString()); System.out.println(" [INFO] login "+trgDir.getAccountName()); boolean login = ftpClient.login(trgDir.getAccountName(), passwd); if (login) { System.out.println(" [INFO] Login succed!"); } else { System.out.println(" [INFO] Login faild!"); ftpClient.disconnect(); return; } //create a FTPContext without a synchronizer FTPContext context = new FTPContext(null); //initialize the context with the FTPLocation object for the target and the FTPClient context.location = trgDir; context.client = ftpClient; //Set the context to the synchronizer sync.setContext(context); sync.checkToSync(); if (sync.getActionCount() != 0) { sync.synchronize(); } } catch (Exception e) { e.printStackTrace(); }