001    /*
002     * ConfigToolkit.java
003     *
004     * Created on July 25, 2005, 3:36 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.util;
025    
026    import gov.bnl.star.offline.scheduler.AccessMethod;
027    import gov.bnl.star.offline.scheduler.Job;
028    import gov.bnl.star.offline.scheduler.LocalAccessPoint;
029    import java.util.Hashtable;
030    import java.util.List;
031    import java.util.Map;
032    import java.util.logging.FileHandler;
033    import java.util.logging.Handler;
034    import java.util.logging.Level;
035    import org.apache.log4j.Logger;
036    
037    import gov.bnl.star.offline.scheduler.ComponentLibrary;
038    import gov.bnl.star.offline.scheduler.Site;
039    import gov.bnl.star.offline.scheduler.BatchSystem;
040    import gov.bnl.star.offline.scheduler.Queue;
041    import gov.bnl.star.offline.scheduler.GateKeeperAccessPoint;
042    
043    /**
044     * Toolkit with  pre-written methods to help work with the SUMS configuration. 
045     * @author  Levente Hajdu
046     */
047    public class ConfigToolkit {
048        static private Logger log = Logger.getLogger(ConfigToolkit.class.getName());
049        String localSiteName; 
050        static ConfigToolkit configToolkit;
051        
052        public static ConfigToolkit getToolkit(){
053            if(configToolkit == null) configToolkit = new ConfigToolkit();
054            return configToolkit;
055        }
056        
057        
058        
059        /** Creates a new instance of ConfigToolkit */
060        public ConfigToolkit() {
061            if(ComponentLibrary.getInstance() == null) cantFind("Could not load ComponentLibrary");
062            localSiteName = (String) ComponentLibrary.getInstance().getComponent("localSite");
063            if(localSiteName == null) cantFind("Could not read site name. Please check that the submission script calling the scheduler jar has the \"site\" option set correctly.");
064        }
065        
066        
067        
068        public GateKeeperAccessPoint myLocalFileGateKeeper(){
069            return myLocalSite().getFileTransferGatekeeper();  
070        }
071        
072        /** Test if a queue is local to the site.
073         * If the queue is local it will return true else return false.
074         * If the "-u grid" option is used is set it will always return false. **/
075        public boolean isLocalQueue(Queue queue){
076            
077            
078            if( null != ComponentLibrary.getInstance().getComponent("SUMS_FLAG_FORCE_GRID") ) return false;
079            
080            /*
081            if (queue.getBatchSystem().getSite().getSiteName().compareTo(localSiteName)==0){return true;}
082            return false;
083            */
084            
085            //Test that the queue has at lest one access method, else holt
086            List access = queue.getAccessMethods();
087            if(access.size() == 0) throw new RuntimeException("The queue \"" + queue.getID() + "\" has no access method. Please ask an administrator to correct to scheduler config file.");
088                  
089            access = ((AccessMethod) access.get(0)).getAccessPoints();            
090            if(access.size() == 0) throw new RuntimeException("The accessMethod(0) of queue \"" + queue.getID() + "\" has no access accessPoints. Please ask an administrator to correct the scheduler config file.");
091            
092            Site queueSite = ((LocalAccessPoint) access.get(0)).getSite();
093            if(queueSite == null) throw new RuntimeException("SUMS found an AccessPoint config ertry with NULL site.  Please ask your SUMS administrator to correct the scheduler config file.");
094            
095            if(queueSite.getSiteName().equals(localSiteName)) return true;
096            
097            else return false;
098        
099        }
100        
101        /** @return the local site object from the config file **/
102        public Site myLocalSite(){
103            List sites = (List) ComponentLibrary.getInstance().getComponent("gridView");
104            if(sites == null) cantFind("Could not find gridView site list in config file.");
105     
106            for(int i = 0; i != sites.size(); i++){
107               Site site = (Site) sites.get(i);
108               if(site.getSiteName()!= null)
109                    if(site.getSiteName().compareTo(localSiteName) == 0) return site;  
110            }
111       
112            cantFind("Can't fine local site object with siteName=\""+ localSiteName +"\" in config file !!");
113            return null; //It should never get to this point (This is just, used to help the compiler)
114        }
115        
116        /** @return ture if the local site exists in the config file **/
117        public boolean localSiteExists(){
118            List sites = (List) ComponentLibrary.getInstance().getComponent("gridView");
119            if(sites == null) cantFind("Could not find gridView site list in config file.");
120            
121            for(int i = 0; i != sites.size(); i++){
122               Site site = (Site) sites.get(i);
123               
124               ///////////
125               //System.out.println(site.getSiteName() + "--->" + localSiteName);
126               //////////
127               
128               if(site.getSiteName()!= null){
129                   
130                   //System.out.println(site.getSiteName() + "--->" + localSiteName + "----->" + site.getSiteName().compareTo(localSiteName));
131                    if(site.getSiteName().compareTo(localSiteName) == 0) return true; 
132               }
133            }
134            
135            return false;
136        }
137        
138      
139         private void cantFind(String error){
140             log.fatal(error); 
141             System.out.println(error);
142             throw new RuntimeException(error);
143         }
144         
145         
146         public void setFlag(String falgName){
147             ComponentLibrary.getInstance().addComponent(falgName, "true");
148         }
149        
150         public boolean isFlagSet(String flag){
151             if( ComponentLibrary.getInstance().getComponent(flag) == null) return false;
152             else return true; 
153         }
154         
155         
156        
157         public void addGlobalObject(String name, Object object){  
158             ComponentLibrary.getInstance().globalObjectTable.put(name, object);   
159         }
160         public Object getGlobalObject(String name){ 
161             if(!  ComponentLibrary.getInstance().globalObjectTable.containsKey(name) ){ 
162                 System.out.println("Global object table could not find key " + name);
163                 Thread.dumpStack();
164                 return null;
165             }
166             return  ComponentLibrary.getInstance().globalObjectTable.get(name);   
167         }
168         
169         
170         
171         public static String findProgram(String name, Job job){ 
172             return  findProgram( name, job.getAccessPoint().getSite() );
173         }
174         
175         
176         public static String findProgram(String name, Site site){
177             
178             Map programTable = site.getProgramLocations();
179             
180             if(programTable != null){
181                 if(programTable.containsKey(name)){
182                     return (String) programTable.get(name);    
183                 }
184             }
185             
186             
187             
188             //If it is not the sites ProgramLocations look it up in the GlobalProgramLocations config table
189             Map globalProgramLocations = (Map)  ComponentLibrary.getInstance().getComponent("GlobalProgramLocations");
190             
191             if(globalProgramLocations != null){
192                 if(globalProgramLocations.containsKey(name)) return (String) globalProgramLocations.get(name);   
193             }else{
194                 System.out.println("Error : Could not find \"GlobalProgramLocations\" in config file.");
195             }
196             
197                 
198             throw new RuntimeException("The config toolkit could not find the program named \"" + name + "\" in the config file." + 
199                     "Submitting of jobs has been halted because jobs may not be valide.\n" +
200                     "site = \"" + site.getSiteName() + " "); //test that this point has not already been added to a site
201             
202         } 
203         
204         
205         
206         
207    }