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;
037
038/* Test DirectoryListing utility on a local directory */
039public class TestDirectoryListingLocal {
040
041        public static void main(String[] arg) throws ExecException {
042
043                String dir = arg.length > 0 ? arg[0] : System.getProperty("user.dir");
044                String pattern = arg.length > 1 ? arg[1] : null;
045                System.out.println("Directory to be watched = " + dir
046                                + "   with pattern: " + pattern);
047
048                String patterns[] = new String[1];
049                patterns[0] = pattern;
050                DirectoryListing dl = new DirectoryListing(new File(dir), patterns);
051
052                dl.list(); // initial list
053                FileInfo[] files = dl.getList();
054
055                System.out.println("File list at first read (" + files.length
056                                + " files):");
057                System.out.println(print(files));
058
059                System.out.println("Now sleep for 15 seconds, and read again. "
060                                + "Meanwhile add new files to the directory");
061
062                try {
063                        java.lang.Thread.sleep(15000);
064                } catch (InterruptedException ex) {
065                }
066                ;
067
068                dl.list(); // second list
069                files = dl.getList();
070
071                System.out.println("File list after second read (" + files.length
072                                + " files):");
073                System.out.println(print(files));
074
075                files = dl.getNewFiles(false);
076                System.out.println("New files (" + files.length + " files):");
077                System.out.println(print(files));
078
079        }
080
081        private static StringBuffer print(FileInfo[] files) {
082                StringBuffer sb = new StringBuffer("");
083                if (files != null && files.length > 0) {
084                        for (int i = 0; i < files.length; i++) {
085                                sb.append(files[i].getName() + "\t" + files[i].getSize() + "\t"
086                                                + files[i].getDate());
087                                if (i < files.length - 1)
088                                        sb.append("\n");
089                        }
090                }
091                return sb;
092        }
093
094}