001    /*
002     * ThreadSafe.java
003     *
004     * Created on June 19, 2006, 8:30 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.util.FilesystemToolkit;
027    import java.net.URL;
028    import java.net.URI;
029    import org.apache.log4j.Logger;
030    
031    /** The class is used to call function that many not retrun and hang SUMS 
032     * @author lbhajdu
033     */
034    public class ThreadSafeFilesystemToolkit extends Thread  {
035        
036        static private Logger log = Logger.getLogger(ThreadSafeFilesystemToolkit.class.getName());
037        
038        /** Creates a new instance of ThreadSafe */
039        public ThreadSafeFilesystemToolkit() {   }
040        
041        public boolean done = false;
042        public boolean isDone(){ return done;}
043        
044        
045        private boolean doCheckIfDirExists = false;
046        private boolean doCheckIfFileExists = false;
047        
048        /** This function should not be called it is used only used international by this class, and must be public.   **/
049        public void run(){
050    
051                if(doCheckIfDirExists){
052                    output = FilesystemToolkit.checkIfDirExists(this.dir, this.hostAllowed);
053                    done = true;
054                }
055                else if(doCheckIfFileExists){
056                    output = FilesystemToolkit.checkIfFileExists(this.file, this.hostAllowed); 
057                    done = true;
058                }
059                else{
060                    log.error(this.getClass().getName() + ".start() (or run()) was called, however no opp was called.");
061                    done = true;
062                }
063    
064        }
065        
066        
067        private boolean output = false;
068        public boolean getOutput(){ return output; }
069         
070        
071        
072        
073        private boolean hostAllowed = false;
074        
075        private URL file;
076         /** This function should not be called it is used only used international by this class, and must be public.   **/
077        public void checkIfFileExists(URL file, boolean hostAllowed) {
078            doCheckIfDirExists = false;
079            doCheckIfFileExists = true;
080            this.file = file;
081            this.hostAllowed = hostAllowed;
082            done = false;
083        }
084        
085        private URL dir;  
086        /** This function should not be called it is used only used international by this class, and must be public.   **/
087        public void checkIfDirExists(URL dir, boolean hostAllowed) {
088            doCheckIfFileExists = false;
089            doCheckIfDirExists = true;
090            this.dir = dir;
091            this.hostAllowed = hostAllowed;
092            done = false;
093        }
094        
095        
096        public boolean threadSafeCheckIfFileExists(URL file, boolean hostAllowed) {
097            
098            if(file == null) System.out.println("file is null ...");
099            
100            ThreadSafeFilesystemToolkit toolkit = new ThreadSafeFilesystemToolkit();
101            toolkit.checkIfFileExists(file, hostAllowed);
102            return runThread(toolkit);
103        }
104        
105        
106        public boolean threadSafeCheckIfDirExists(URL dir, boolean hostAllowed) {
107            ThreadSafeFilesystemToolkit toolkit = new ThreadSafeFilesystemToolkit();
108            toolkit.checkIfDirExists(dir, hostAllowed);
109            return runThread(toolkit);
110        }
111        
112        
113        /** Start and watch the thread when do return output, if it runs out of time stop it, log the error and return false. **/
114        private boolean runThread(ThreadSafeFilesystemToolkit toolkit){
115          try {
116                
117              
118                toolkit.start();
119                
120                for(int i=1; i != 3000; i ++){ //<-- this gives 15sec to find the file
121                    Thread.yield();
122                    Thread.sleep(10);
123                    if(toolkit.isDone()){
124                        return toolkit.output;
125                    }                
126                } //if it did not finish it will exit this loop
127                
128                
129                try {
130                    toolkit.stop(); //if it did not stop by now it needs to be killed.
131                } finally {}
132                
133                
134                log.error("Stopping file system tool kit thread, the because it timed-out.");
135                System.out.println("Warning Checking file timed-out, file system may be slow, if this is an error try submitting with \"star-submit -u ie myjob.xml\":");
136                
137                  
138            } catch (InterruptedException ex) {
139    
140                    ex.printStackTrace();
141                    System.out.println("The process to checked the existence of a file was killed:");
142                    log.error("Process to checked the existence of a file was killed !!!!!");
143                    try {
144                        toolkit.stop(); //at this point we don't know what the state may be, so lets just try and stop it, however it is most liklly already stopped
145                    } finally {}
146                    return false;
147            } 
148            
149            
150            return false; 
151        }
152        
153         
154    }