001    /*
002     * $RCSfile: Location.java,v $
003     *
004     * Created on April 2, 2006, 2:43 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2003 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.policy;
025    
026    import gov.bnl.star.offline.scheduler.catalog.*;
027    import java.net.URL;
028    import java.util.Iterator;
029    import java.util.SortedSet;
030    import java.util.StringTokenizer;
031    import java.util.TreeSet;
032    
033    /** A Location is where a file can be: a node, a group of node or the NFS.
034     *
035     * @author Gabriele Carcassi & Pavel Jakl
036     * @version $Revision: 1.10 $ $Date: 2006/11/21 00:41:32 $
037     */
038    public class Location {
039        String location;
040        
041        /** NFS location */
042        private  Location() {
043            location = new String("NFS");
044        }
045        
046        /** Creates a new instance of Location */
047        private Location(String node) {
048            location = new String(node);
049            if (location == null) location = new String("NFS");
050            if (location == "") location = new String("NFS");
051        }
052        
053        public static Location getNFS() {
054            return new Location();
055        }
056        
057        public static Location getNode(String node) {
058            return new Location(node);
059        }
060    
061        public static Location getHPSS(){
062            return new Location("HPSS");
063        }
064        public static Location getLocation(URL url) {
065            return new Location(url.getHost());
066        }
067    
068        public static Location getLocation(PhysicalFile file) {
069            if ("NFS".equals(file.getStorage())) {
070                return getNFS();}
071            if("HPSS".equals(file.getStorage())) {
072                return getHPSS();
073            }
074            return new Location(file.getNode());
075        }
076        
077        public Location mergeLocations(Location loc) {
078            SortedSet set = new TreeSet();
079            StringTokenizer token = new StringTokenizer(location, ",");
080            while (token.hasMoreTokens()) {
081                set.add(token.nextToken());
082            }
083    
084            token = new StringTokenizer(loc.location, ",");
085            while (token.hasMoreTokens()) {
086                set.add(token.nextToken());
087            }
088            
089            String newLoc = null;
090            Iterator iter = set.iterator();
091            while (iter.hasNext()) {
092                if (newLoc == null) {
093                    newLoc = (String) iter.next();
094                } else {
095                    newLoc += "," + iter.next().toString();
096                }
097            }
098            return new Location(newLoc);
099        }
100        
101        public String toString() {
102            return location;
103        }
104        
105        public boolean equals(Object o) {
106            return location.equals(((Location) o).location);
107        }
108        
109        public int hashCode() {
110            return location.hashCode();
111        }
112    
113    }