001    /*
002     * $RCSfile: PhysicalFile.java,v $
003     *
004     * Created on December 13, 2002, 1:24 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    
024    package gov.bnl.star.offline.scheduler.catalog;
025    
026    import java.net.URL;
027    import java.util.*;
028    
029    /** Represnt a file returned by the Catalog. This object is basically read only,
030     * and it's used to compare different copies of the same logical file.
031     *
032     * @author  Gabriele Carcassi & Pavel Jakl & Levente Hajdu
033     * @version $Revision: 1.11 $ $Date: 2006/11/21 00:41:31 $
034     */
035    public class PhysicalFile implements java.io.Serializable{
036        
037        private Object logicalID;
038        private String storage;
039        private String node;
040        private String path;
041        private String filename;
042        private URL url;
043        private Map attributes;
044        
045        public PhysicalFile(){
046            //System.out.println("New PhysicalFile");
047        }
048        
049        /** Creates a new instance of PhysicalFile
050         * @param logicalID the logical name of the file
051         * @param storage the type of storage (NFS, local, ...)
052         * @param node the node (es. rcas6023.rhic.bnl.gov)
053         * @param path the path to the file
054         * @param filename the filename
055         */
056        public PhysicalFile(Object logicalID, String storage, String node, String path, String filename, Map attributes) {
057            this.logicalID = logicalID;
058            this.storage = storage;
059            this.node = node;
060            this.path = path;
061            this.filename = filename;
062            if (attributes == null) {
063                this.attributes = new Hashtable();
064            } else {
065                this.attributes = attributes;
066            }
067        }
068        
069        public PhysicalFile(URL url) {
070            logicalID = url;
071            if ((url.getHost() != null) && (!url.getHost().trim().equals(""))) {
072                storage = "local";
073                node = url.getHost();
074            } else {
075                storage = "NFS";
076            }
077            String tempPath = url.getPath();
078            int lastSlash = url.getPath().lastIndexOf('/');
079            path = url.getPath().substring(0, lastSlash);
080            filename = url.getPath().substring(lastSlash + 1);
081            attributes = new Hashtable();
082        }
083        
084      /*  public PhysicalFile(String file){ //lh
085           try{
086                URL url = new URL(file);
087    
088                logicalID = url;
089                if ((url.getHost() != null) && (!url.getHost().trim().equals(""))) {
090                    storage = "local";
091                    node = url.getHost();
092                } else {
093                    storage = "NFS";
094                }
095                String tempPath = url.getPath();
096                int lastSlash = url.getPath().lastIndexOf('/');
097                path = url.getPath().substring(0, lastSlash);
098                filename = url.getPath().substring(lastSlash + 1);
099                attributes = new Hashtable();
100           
101           } catch (Exception e) {} 
102        }
103        */
104        public void setAttribute(String attr, Object value) {
105            attributes.put(attr, value);
106        }
107    
108        public Object getAtribute(String attr) {
109            return attributes.get(attr);
110        }
111        
112        public void setAttributes(Map attributes){
113            this.attributes = attributes;
114        }
115        
116        public Map getAttributes(){ //Added for sessionWriter // \LBH
117            return attributes;
118        }
119        
120        public Map setAttributes(){
121            //System.out.println("\ngetting Attributes");
122            return attributes;
123        }
124        
125        /** Returns the logical name of the file
126         * @return the logical name
127         */    
128        public Object getLogicalID() {
129            //System.out.println("\nGetting logical file Id ->" + logicalID);
130            return logicalID;
131        }
132       
133        
134        
135        public void setLogicalID(String logicalID) {
136            this.logicalID = logicalID;
137        }
138        
139        
140        /** Returns the storage type.
141         * @return a String describing the storage type (local, NFS, HPSS, ...)
142         */    
143        public String getStorage() {
144            return storage;
145        }
146        
147        public void setStorage(String storage) {
148            this.storage = storage;
149        }
150        
151        /** Returns the node where the file resides.
152         * @return null if the file is not on local disk
153         */    
154        public String getNode() {
155            return node;
156        }
157        
158        public void setNode(String node) {
159            this.node = node;
160        }
161        
162        /** Returns the path to the file.
163         * @return the path to the file.
164         */    
165        public String getPath() {
166            return path;
167        }
168        
169        public void setPath(String path) {
170            this.path = path;
171        }
172        
173        
174        /** Returns the filename.
175         * @return the filename
176         */    
177        public String getFilename() {
178            return filename;
179        }
180        
181        public void setFilename(String filename) {
182            this.filename = filename;
183        }
184        
185        
186        public String toString(){ //LH
187            return asURL().toString();
188        }
189        
190    
191        
192        
193        /** Returns the formatted URL for the input file.
194         * @return the URL describing the input file
195         */    
196        public URL asURL() {
197            if (url == null) {
198                url = createURLFromCatalogTokens();
199            }
200            return url;
201        }
202        
203        private URL createURLFromCatalogTokens() {
204            if (storage != null) {
205                if (storage.equals("NFS")) {
206                    try {
207                        return new URL("file:" + path + "/" + filename);
208                    } catch (Exception e) {
209                        e.printStackTrace();
210                        
211                        return null;
212                    }
213                }
214                
215                if (storage.equals("local")) {
216                    try {
217                        return new URL("file://" + node + path + "/" + filename);
218                    } catch (Exception e) {
219                        e.printStackTrace();
220                        
221                        return null;
222                    }
223                }
224            }
225            
226            return null;
227        }
228        
229    }