001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.io.test;
031
032import java.io.File;
033
034import org.kepler.io.DirectoryListing;
035import org.kepler.io.FileInfo;
036import org.kepler.ssh.ExecException;
037import org.kepler.ssh.SshExec;
038
039/* Test DirectoryListing utility on a local directory */
040public class TestDirectoryListingRemote {
041
042        public static void main(String[] arg) throws ExecException {
043
044                String target = arg.length > 0 ? arg[0] : "localhost";
045                String dir = arg.length > 1 ? arg[1] : "";
046                String pattern = arg.length > 2 ? arg[2] : null;
047                System.out.println("Directory to be watched: " + target + ":" + dir
048                                + "   with pattern: " + pattern);
049
050                // use identity file if exist for ssh connections
051                File id = new File(System.getProperty("user.home") + File.separator
052                                + ".ssh" + File.separator + "id_rsa");
053                if (id.isFile()) {
054                        SshExec ssh = new SshExec("dummy", "localhost"); // host@user will
055                                                                                                                                // be never used
056                                                                                                                                // in this
057                                                                                                                                // object...
058                        ssh.addIdentity(id.getAbsolutePath()); // Only this is what we need!
059                } else
060                        System.out.println("You have to type password at connection\n");
061
062                String patterns[] = new String[1];
063                patterns[0] = pattern;
064                DirectoryListing dl = new DirectoryListing(target, dir, patterns);
065
066                dl.list(); // initial list
067                FileInfo[] files = dl.getList();
068
069                System.out.println("File list at first read (" + files.length
070                                + " files):");
071                System.out.println(print(files));
072
073                System.out.println("Now sleep for 15 seconds, and read again. "
074                                + "Meanwhile add new files to the directory");
075
076                try {
077                        java.lang.Thread.sleep(15000);
078                } catch (InterruptedException ex) {
079                }
080                ;
081
082                dl.list(); // second list
083                files = dl.getList();
084
085                System.out.println("File list after second read (" + files.length
086                                + " files):");
087                System.out.println(print(files));
088
089                files = dl.getNewFiles(false);
090                System.out.println("New files (" + files.length + " files):");
091                System.out.println(print(files));
092
093        }
094
095        private static StringBuffer print(FileInfo[] files) {
096                StringBuffer sb = new StringBuffer("");
097                if (files != null && files.length > 0) {
098                        for (int i = 0; i < files.length; i++) {
099                                sb.append(files[i].getName() + "\t" + files[i].getSize() + "\t"
100                                                + files[i].getDate());
101                                if (i < files.length - 1)
102                                        sb.append("\n");
103                        }
104                }
105                return sb;
106        }
107
108}