001    /*
002     * $RCSfile: OutputFile.java,v $ 
003     *
004     * Created on December 19, 2002, 4:48 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;
024    
025    import java.net.URI;
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    import java.util.logging.Level;
030    import org.apache.log4j.Logger;
031    
032    
033    /** Represent an Output file description. It can be either a file, a wildcard or
034     * a directory. It also contains the target where the output should be copied.
035     * <p>
036     * This representation is only temporary, and it is an evolution to the URL used
037     * as input files. To make everything consistent, input and output representation
038     * should be reviewed to separate definition, and actions such as the file copy.
039     * It might be more than needed, and that is why it hasn't been yet done.
040     *
041     * @author  Gabriele Carcassi
042     * @version $Revision: 1.10 $ $Date: 2006/11/21 00:41:32 $
043     */
044    public class OutputFile {
045        static private Logger log = Logger.getLogger(OutputFile.class.getName());
046    
047        /** source/destination represents a file */
048        public static final int FILE = 0;
049    
050        /** source/destination represents a wildcard */
051        public static final int WILDCARD = 1;
052    
053        /** source/destination represents a directory */
054        public static final int DIRECTORY = 2;
055        
056        /** no destination is set, using actions */
057        public static final int NONE = -1;
058        
059        private String fromScratch;
060        private String toUrl;
061        private URI toURL;
062        private List actionList = new ArrayList();
063            
064            public OutputFile(){};
065    
066        /** Creates a new description for the output files.
067         * @param fromScratch relative path in the scratch directory
068         * @param toURL URL represnting the location where to copy the output at the end of the job
069         */
070        public OutputFile(String fromScratch, String toURL) {
071            this.fromScratch = fromScratch;
072                    this.toUrl = toURL;
073    
074            if (toURL != null) {
075                try {
076                    this.toURL = new URI(toURL);
077                } catch (Exception e) {
078                    log.fatal("Can't create a valid URL from " + toURL, e);
079                    throw new RuntimeException(toURL + " is not a valid URL");
080                }
081            }
082    
083            // Some validation checks
084            if ((getDestType() == FILE) && (getSourceType() != FILE)) {
085                log.fatal("Can't move " + fromScratch + " onto " + toURL + ": the destination is a file");
086                throw new RuntimeException("Can't move " + fromScratch + " onto " +
087                    toURL + ": the destination is a file");
088            }
089    
090            if (getDestType() == WILDCARD) {
091                log.fatal("Destination " + toURL + " is a wildcard");
092                throw new RuntimeException("Can't move " + fromScratch + " onto " + toURL + ": the destination is a wildcard");
093            }
094    
095            if ((toURL != null) && (this.toURL.getHost() != null) &&
096                    (this.toURL.getScheme() != "file")) {
097                log.fatal("Destination " + toURL + " is not allowed: only network files are");
098                throw new RuntimeException(toURL + " is not valid: should be \"file:/path/\" or \"file:/path/file\" ");
099            }
100        }
101    
102        /** Returns the type of source: file, wildcard or directory.
103         * @return FILE, WILDCARD or DIRECTORY
104         */
105        public int getSourceType() {
106            return getPathType(fromScratch);
107        }
108    
109        /** Returns the type of source: file or directory.
110         * @return FILE or DIRECTORY
111         */
112        public int getDestType() {
113            if (toURL == null) return NONE;
114            return getPathType(toURL.getPath());
115        }
116    
117        /** Returns the relative path of the output in the scratch directory
118         * @return a relative path in the scratch directory
119         */
120        public String getFromScratch() {
121            return fromScratch;
122        }
123        
124            public void setFromScratch(String fromScratch) {
125            this.fromScratch = fromScratch;
126        }
127     
128        /** Returns the list of actions to be done on this output. Actions include
129         * copies or registration to the file catalog. The list return is modifiable,
130         * so to add or remove an action simply get the list and modify it.
131         */
132        public List getActionList() {
133            return actionList;
134        }
135        
136        public void setActionList(List actionList) {
137            this.actionList = actionList;
138        }
139    
140        /** Returns the URL reprenting the location in which to copy the output after the
141         * job finishes.
142         * @return a URL
143         */
144        public URI getToURL() {
145            return toURL;
146        }
147    
148        public String getToURLString(){
149            return this.toUrl;
150        }
151        
152        public void setToURLString(String XtoURL){
153            this.toUrl = XtoURL;
154            if (XtoURL != null) {
155                try {
156                    this.toURL = new URI(XtoURL);
157                } catch (Exception e) {
158                    log.fatal("Can't create a valid URL from " + toURL, e);
159                    throw new RuntimeException(XtoURL + " is not a valid URL");
160                }
161            }
162        }
163        
164        private int getPathType(String path) {
165            if (path.endsWith("/")) {
166                return DIRECTORY;
167            }
168    
169            if (path.indexOf('*') != -1) {
170                return WILDCARD;
171            }
172    
173            if (path.indexOf('?') != -1) {
174                return WILDCARD;
175            }
176    
177            return FILE;
178        }
179    }