001    /*
002     * $RCSfile: ResolveFilesystemWildcards.java,v $ 
003     *
004     * Created on October 16, 2002, 1:41 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    package gov.bnl.star.offline.scheduler.util;
024    
025    import gov.bnl.star.offline.scheduler.ComponentLibrary;
026    import java.io.BufferedReader;
027    
028    import java.util.ArrayList;
029    import java.util.logging.Level;
030    import org.apache.log4j.Logger;
031    
032    
033    
034    /** Resolves a wildcard for filenames by executing ls on the local machine.
035     * It is actually csh that resolves the wildcard, so what is actually being
036     * executed is:
037     * <p>
038     * <CODE>csh -c "ls -1 wildcard"</CODE>
039     * <p>
040     * where wildcard is the string passed through the constructor. To retrieve the
041     * result use <CODE>getResult</CODE>.
042     *
043     * @author  Gabriele Carcassi
044     * @version $Revision: 1.13 $ $Date: 2006/11/21 00:41:29 $
045     */
046    public class ResolveFilesystemWildcards extends CommandLineTask {
047        private static String csh = ComponentLibrary.getInstance().getProgramLocation("csh");
048        private static String ls = ComponentLibrary.getInstance().getProgramLocation("ls");
049        /* Notes on wildcards.
050         *
051         * Wildcards are resolved by the XML Parser since URLs can't containt '?'.
052         * It is not clear whether this is something wanted, or if wilcards should
053         * be resolved in the policy, or in the initializer.
054         *
055         * Another notes regards jobs submitted with wildcards that resolve to nothing.
056         * These should be executed, and whoever resolves the wildcard has to
057         * mark the job for non execution. This is done through the hasNoInput property
058         * of JobDescription. It's up to the Policy to decide whether or not to
059         * submit a job with no input.
060         */
061        static private Logger log = Logger.getLogger(ResolveFilesystemWildcards.class.getName());
062    
063        /** Creates a new task to resolve wildcards.
064         * @param wildcard the wildcard to be resolved: ex. /home/user/pippo/*a/test.*
065         */
066        public ResolveFilesystemWildcards(String wildcard) {
067            //TODO this should be factorized with CSHCommandLineTask
068            super(new String[] { csh, "-c", ls + " -1 " + wildcard }, true);
069        }
070    
071        /** Returns all the files matched by the wildcard
072         * @return all the matching files
073         */
074        public String[] getResult() {
075            try {
076                BufferedReader output = getOutputReader();
077    
078                if (output == null) {
079                    return null;
080                }
081    
082                ArrayList result = new ArrayList();
083                String line;
084    
085                while ((line = output.readLine()) != null) {
086                    if (line.endsWith("*")) {
087                        result.add(line.substring(0, line.length()-1));
088                    }
089                    else {
090                        result.add(line);
091                    }
092                }
093    
094                String[] temp = new String[result.size()];
095    
096                for (int n = 0; n < result.size(); n++) {
097                    temp[n] = (String) result.get(n);
098                }
099    
100                return temp;
101            } catch (Exception e) {
102                log.warn("Wildcard resolution unsuccessful", e);
103    
104                return null;
105            }
106        }
107    
108        public static void main(String[] args) {
109            ResolveFilesystemWildcards test = new ResolveFilesystemWildcards(args[1]);
110            System.out.println("Resolving \"" + args[1] + "\": ");
111            test.execute();
112            System.out.println();
113    
114            String[] temp = test.getResult();
115    
116            for (int n = 0; n < temp.length; n++) {
117                System.out.println(temp[n]);
118            }
119        }
120    }