001    /*
002     * SessionReader.java
003     *
004     * Created on September 10, 2004, 4:50 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2003-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.persistent;
025    import gov.bnl.star.offline.scheduler.SchedulerCommandLine;
026    import java.beans.XMLDecoder;
027    import java.io.BufferedInputStream;
028    import java.io.BufferedReader;
029    import java.io.FileInputStream;
030    import java.io.FileReader;
031    import java.util.List;
032    
033    import gov.bnl.star.offline.scheduler.request.Request;
034    import gov.bnl.star.offline.scheduler.Job;
035    
036    
037    /** Reads the session xml file back into an object.
038     * @author  Levente Hajdu
039     */
040    public class SessionReader {
041        
042        Request request;
043        
044        /** Creates a new instance of SessionReader */
045        public SessionReader(String file) {
046            openNewSession(file);  
047               
048        }
049        
050        /** Test the the file was biult with the same version of SUMS as the 
051         *   current version. If it is not dispay an error and exit.
052         *
053         *  @param file the name of the .session.xml
054         **/
055        public static void testVersionMatch(String file){
056            
057            String fileVersion = null;
058            
059            try{
060    
061               BufferedReader reader = new BufferedReader(new FileReader(file));
062               String version;
063               while(( version=reader.readLine()) != null){
064                   if(version.matches("<!-- *SUMS_Version_String=\".*\" *-->")      ){ //this staring can be found in the "SessionWriter" class"
065                       fileVersion = version.replaceAll("<!-- *SUMS_Version_String=\"(.*)\" *-->","$1").trim();          
066                   }
067               }
068               
069            } catch (Exception e) {return;} //if the file can not be openned, we do not need to exit here as it is done later in the code
070            
071            
072            
073            if(fileVersion == null){
074                //throw new RuntimeException("You are using the wrong version of SUMS to resubmit your job. Please use : \"" + fileVersion + "\"");
075                
076                System.out.println("Warning: Could not verify the version of SUMS that generated the session file (" + file + "). There is no way to tell if you are using the correct version of SUMS !! The cause is because this file was generated before version marking existed.\n" + 
077                                    "Proceeding may corrupt this file beyond repair. Please be certain you are using the session file with the same version of SUMS that made this file. \nPress \"y\" to continue." );
078                try {
079                    if (System.in.read() == 'y') return;
080                } catch (Exception e) {}
081                
082                System.out.println("User asked to exit");
083                System.exit(1);
084                
085            
086            }
087            else if(! SchedulerCommandLine.getProgramName().trim().equals( fileVersion )){ //if version is known to  be worng 
088                
089                            System.out.println("Warning \"" + SchedulerCommandLine.getProgramName().trim() + "\" != \"" + fileVersion + "\"");
090                            System.out.println("You are using the wrong version of SUMS to resubmit your job. The file (" + file + ") was generated with " + fileVersion + ". Please use : \"" + fileVersion + "\" to resubmit." );
091                            throw new RuntimeException("Wrong SUMS version use " + fileVersion);
092                      
093            }  
094    
095            return;
096        }
097        
098        
099        
100        public void openNewSession(String file){
101            try{
102                
103              
104               //XMLDecoder d;
105               testVersionMatch(file); //test that we are using the same version of SUMS the file was encoded with.
106           
107               XMLDecoderExceptionListener MyExceptionListener = new XMLDecoderExceptionListener();
108               XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream(file)),null, MyExceptionListener);
109              
110               request = (Request) d.readObject();
111               d.close();
112               
113            } catch (Exception e) {
114                      System.out.println("Could not open session : " + file);
115            } 
116        }
117        
118        public void close(){
119            request = null;
120        }
121        
122        public boolean isOpen(){
123            if (request != null) return true;
124            else return false;
125        }
126        
127        public Request getRequest(){
128            return request;
129        }
130        
131        public List getJobs(){
132            if(isOpen())
133                return request.getJobs();
134            else
135                return null;
136        }
137         
138    }