001    /*
002     * VORS.java
003     *
004     * Created on August 28, 2006, 1:59 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    
025    package gov.bnl.star.offline.scheduler.informationService;
026    
027    import java.io.BufferedReader;
028    import java.io.CharArrayReader;
029    import java.io.IOException;
030    import java.io.InputStream;
031    import java.io.InputStreamReader;
032    import java.net.MalformedURLException;
033    import java.net.URL;
034    import java.util.ArrayList;
035    import java.util.Hashtable;
036    import java.util.List;
037    
038    /**
039     * Implements the Virtual Organization Resource Selector (VORS) information service
040     * @author Levente B. Hajdu
041     */
042    public class VORS implements InformationService {
043        
044        /** Creates a new instance of VORS */
045        public VORS() {
046        }
047        
048        
049        private String siteID = null;
050        private String  host = "http://vors.grid.iu.edu";
051        
052        /** @param siteID a string used to identify the site to the information service **/
053        public void setSiteID(String siteID) { this.siteID = siteID; }
054        /** @return a string used to identify the site with in the  information service **/
055        public String getSiteID() { return siteID; }
056        
057        /** @param service the url of the service **/
058        public void setService(String service) { this.host = service;}
059        /** @return the url of the service **/
060        public String getService() { return host; }
061        
062        
063        public String getSiteInfo(String site, String info) {return getParam(this.host, site, info); } //This just fills in the site
064    
065    
066        public String getAppDir() {   
067            //System.out.println("------------------------app dir --------> " + getParam(this.host, siteID, "app_loc") );
068            //System.out.println("------------------------this.host --------> " + this.host );
069            //System.out.println("------------------------siteID --------> " + siteID );
070            return getParam(this.host, siteID, "app_loc"); 
071        }
072        
073        public String getDataDir() { return getParam(this.host, siteID, "data_loc"); }
074        public String getWNTmpDir() { return getParam(this.host, siteID, "wntmp_loc");  }
075        public String getGKGlobusDir() { return getParam(this.host, siteID, "globus_loc"); }
076        public String getWNClient() { return getParam(this.host, siteID, "osg_grid");   }
077        
078        //not part of interface
079        public List getJobManager(){ return getJobManagersList(this.host, siteID); }
080        
081        
082        public void refreshConnections(){
083            
084            System.out.println("app dir -->"      + getAppDir()   + "\n");
085            System.out.println("data dir -->"     + getDataDir()   + "\n");
086            System.out.println("data wntemp -->"  + getWNTmpDir()   + "\n");
087            System.out.println("getGKGlobusDir -->"      + getGKGlobusDir()   + "\n\n");
088            System.out.println("WNClient -->"      + getWNClient()   + "\n\n");  
089        }
090        
091    
092         private static Hashtable buffer = new Hashtable();
093         
094         
095         
096         private boolean test(String VORSurl, String site, String param){ 
097            //Error trap testing
098            if(VORSurl == null){
099                System.out.println("Error: The VORS I.S. dose not have the host set.");
100                return false;
101            }
102            if(site == null){
103                System.out.println("Error: The VORS I.S. dose not have the host sit set.");
104                return false;
105            }
106           if(param == null){
107                System.out.println("Error: The VORS I.S. dose not have the parameter to recover set");
108                return false;
109            }
110            if(VORSurl.length() == 0){
111                System.out.println("Error: The VORS I.S. dose not have the host set.");
112                return false;
113            }
114            if(site.length() == 0){
115                System.out.println("Error: The VORS I.S. dose not have the host sit set.");
116                return false;
117            }
118            if(param.length() == 0){
119                System.out.println("Error: The VORS I.S. dose not have the parameter to recover set");
120                return false;
121            }
122             
123             return true;
124         }
125         
126         
127         //The job managers are formated differntly, this memeber will need to  be used to recover them
128         public List getJobManagersList(String VORSurl, String site){
129            List jobManagers = new ArrayList();
130             
131            if( !test(VORSurl, site, "jobManagers") ) return null;
132            
133            //Test if this value is aready in memory, if it is just return the value from memory and do not recover the value agian.
134            if(buffer.containsKey(VORSurl + site + "jobManagers")){
135                return (List) buffer.get(VORSurl + site + "jobManagers");
136            }
137            
138            String resId = recoverData(VORSurl + "/cgi-bin/tindex.cgi?grid=OSG", "[0-9]*," + site + ",.*" , "([0-9]*)," + site + ",.*" );    
139            if(resId != null){
140                jobManagers = recoverDataList(VORSurl + "/cgi-bin/tindex.cgi?res=" + resId , ".*jobmanager-.*" , ".*jobmanager-([A-Za-z0-9]*).*");
141                if(jobManagers.size() > 0){
142                    buffer.put(VORSurl + site + "jobManagers", jobManagers); //note: it will not put the same entry in the table more then once because, if the value is already in the table it will never get up to this point
143                    return jobManagers;
144                }else{
145                    System.out.println("Found site site, but could not recover parameter.");
146                    return null;
147                }
148            }else{
149                System.out.println("Could not find site in index");
150                return null;
151            }
152        }
153         
154         
155         
156            
157        public String getParam(String VORSurl, String site, String param){
158            
159            if( !test(VORSurl, site, param) ) return null;
160            
161            //Test if this value is aready in memory, if it is just return the value from memory and do not recover the value agian.
162            if(buffer.containsKey(VORSurl + site + param)){
163                return (String) buffer.get(VORSurl + site + param);
164            }
165            
166            String resId = recoverData(VORSurl + "/cgi-bin/tindex.cgi?grid=OSG", "[0-9]*," + site + ",.*" , "([0-9]*)," + site + ",.*" );    
167            if(resId != null){
168                String data = recoverData(VORSurl + "/cgi-bin/tindex.cgi?res=" + resId , param + "=.*" , param + "=(.*)");
169                if(data != null){
170                    buffer.put(VORSurl + site + param, data); //note: it will not put the same entry in the table more then once because, if the value is already in the table it will never get up to this point
171                    return data;
172                }else{
173                    System.out.println("Found site site, but could not recover parameter.");
174                    return null;
175                }
176            }else{
177                System.out.println("Could not find site in index");
178                return null;
179            }
180        }
181        
182        
183        
184    
185        
186        public String recoverData(String url, String matches, String selectionGroup ){
187            try {
188    
189                BufferedReader in = new BufferedReader(new InputStreamReader((new URL(url)).openStream())); 
190                String entry;
191                String recoveredData = null;
192                while ((entry = in.readLine()) != null){
193                    if(entry.matches(matches)){
194                        return  keywordFilter(entry.replaceAll(selectionGroup, "$1"));
195                    }  
196                }
197                
198                
199            } catch (Exception e){ //Just return null if there was an error recovering the data
200               e.printStackTrace();
201               return null;
202            }
203            return null;
204        }
205        
206        
207          public List recoverDataList(String url, String matches, String selectionGroup ){
208            List recoveredData = new ArrayList();
209            try {
210    
211                BufferedReader in = new BufferedReader(new InputStreamReader((new URL(url)).openStream())); 
212                String entry;
213                
214                while ((entry = in.readLine()) != null){
215                    if(entry.matches(matches)){
216                        
217                        System.out.println("readding line : " + entry);
218                        System.out.println("adding : " + keywordFilter(entry.replaceAll(selectionGroup, "$1")));
219                        
220                        recoveredData.add(keywordFilter(entry.replaceAll(selectionGroup, "$1")));
221                    }  
222                }
223                
224                if(recoveredData != null) return recoveredData;
225                
226            } catch (Exception e){ //Just return null if there was an error recovering the data
227               e.printStackTrace();
228               return recoveredData;
229            }
230            return recoveredData;
231        }
232        
233        
234        /**Used to  filter out keywords that are known to be errors */
235        private static  String keywordFilter(String input){
236            if(input.equals("FAIL")     ) return null;
237            if(input.equals("UNTESTED") ) return null;
238            if(input.equals("") ) return null;        
239            return input;
240        } 
241            
242        
243       
244    }