001    /*
002     * GenericCompositeDispatcher.java
003     *
004     * Created on August 30, 2004, 4:21 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    package gov.bnl.star.offline.scheduler;
024    
025    import java.util.List;
026    import java.util.ArrayList;
027    
028    import gov.bnl.star.offline.scheduler.request.Request;
029    import gov.bnl.star.offline.scheduler.Dispatcher;
030    import gov.bnl.star.offline.scheduler.Job;
031    
032    import gov.bnl.star.offline.scheduler.util.persistent.SessionWriter;
033    import gov.bnl.star.offline.scheduler.util.persistent.TablePrinter;
034    
035    //for xml writer
036    import java.beans.XMLEncoder;
037    import java.beans.PersistenceDelegate;
038    import java.beans.Expression;
039    import java.beans.Encoder;
040    import java.io.BufferedOutputStream;
041    import java.io.FileOutputStream;
042    import java.net.URL;
043    import java.io.PrintStream;
044    
045    
046    
047    
048    /** This class submits all jobs to lower level dispatchers using the dispatcher object the particular job points to.
049     *  @author  Levente Hajdu
050     */
051    public class GenericCompositeDispatcher implements Dispatcher {
052        
053        Dispatcher BackUpDispatcher;
054          
055        public GenericCompositeDispatcher() {
056        }
057        
058       /**This method sets the dispatcher to be used if the jobs AssociatedDispatcher is Null. 
059        Setting this method is optional.   
060       */
061        public void setBackUpDispatcher(Dispatcher dispatcher){
062            BackUpDispatcher = dispatcher;
063        }
064        
065        /**Routs the jobs to the correct dispatcher */
066        public void dispatch(Request request, List jobs){
067            List OneJobList = new ArrayList ();//make a new list, puts the one job into the list dispatched. This is because dispatchs take list of job not jobs thesleves
068            
069            for (int nJob = jobs.size() - 1; nJob != -1; nJob--) { //loop over all jobs
070               Job job = (Job) jobs.get(nJob);
071               OneJobList.add(job);
072               if(job.getAssociatedDispatcher() != null){
073                   
074                   //System.out.println("\n\n" + job.getAssociatedDispatcher().getClass().toString() + "\n\n");
075                   
076                   dispatchersList.add(job.getAssociatedDispatcher());
077                   job.getAssociatedDispatcher().dispatch(request, OneJobList);
078                   OneJobList.clear();
079               }
080               else{ // if job has no Associated Dispatcher
081                   if(BackUpDispatcher != null){//if there is a backup Dispatcher then use it.
082                       System.out.println(job.getJobID() + " has No Associated Dispatcher, trying backup dispatcher!");
083                       BackUpDispatcher.dispatch(request, OneJobList);
084                       OneJobList.clear();
085                   }
086                   else{ //if there is no backup dispatcher just drop the job and print an error
087                       System.out.println(job.getJobID() + " has No Associated Dispatcher, job will not be dispatched !");
088                   }
089               }  
090             
091            }
092               
093            
094            SessionWriter a = new SessionWriter(request);
095            /*
096            //write out the report data
097            TablePrinter table = new TablePrinter();
098            table.addRow("ID"); // make col for job id
099            for(int nJob = 0; nJob != jobs.size(); nJob++){
100                Job job = (Job) jobs.get(nJob);
101                table.addToRow(job.getJobID());
102            }
103            
104            table.addRow("Queue"); // make col for job Queue
105            for(int nJob = 0; nJob != jobs.size(); nJob++){
106                Job job = (Job) jobs.get(nJob);
107                table.addToRow(job.getQueue());
108            }
109            
110            table.addRow("NFiles"); // make col for job Queue
111            for(int nJob = 0; nJob != jobs.size(); nJob++){
112                Job job = (Job) jobs.get(nJob);
113                table.addToRow(job.getInput().size());
114            }
115            
116            table.addRow("estRunTime"); // make col for job Time
117            for(int nJob = 0; nJob != jobs.size(); nJob++){
118                Job job = (Job) jobs.get(nJob);
119                double jobTimeLimit = (((double) job.getInput().size()) / request.getFilesPerHour()) * 60;
120                if((job.getInput().size() == 0)&&(request.getFilesPerHour() != Double.POSITIVE_INFINITY))
121                        jobTimeLimit = 60 / request.getFilesPerHour(); //If the job has a FilesPerHour but no file, then the FilesPerHour is the time the job takes
122                        table.addToRow(String.valueOf(jobTimeLimit) + "min");    
123            }
124            
125            table.addRow("Target"); // make col for job Target
126            for(int nJob = 0; nJob != jobs.size(); nJob++){
127                Job job = (Job) jobs.get(nJob);
128                table.addToRow(job.getTarget() );
129            }
130            
131            table.addRow("SubmitTime"); // make col for job SubmitTime
132            for(int nJob = 0; nJob != jobs.size(); nJob++){
133                Job job = (Job) jobs.get(nJob);
134                if(request.getSimulation())table.addToRow("n/a (Simulation)");
135                else table.addToRow(job.getDispatchTime());
136                
137            }
138                          
139            table.addRow("SubmitSuccessful"); // make col for Successful
140            for(int nJob = 0; nJob != jobs.size(); nJob++){
141                Job job = (Job) jobs.get(nJob);
142                String yn = "no!";
143                if(job.getDispatchSuccessful()) yn = "yes!";        
144                if(request.getSimulation()) yn = "n/a (Simulation)";
145                table.addToRow(yn);
146            }
147            
148            
149            System.out.println("table");
150            System.out.println("\n\n" + table.getTable() + "\n\n");
151           
152            
153            // table.clearTable();
154            
155            */
156            
157      /*      try{
158                    
159                //BufferedOutputStream bufferedOutputStream  
160                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("Test.xml"));
161    
162                PrintStream  out = new PrintStream ( bufferedOutputStream);                   
163    
164                XMLEncoder e = new XMLEncoder(bufferedOutputStream);
165    
166                
167                out.println("<!--");
168                out.println("Levente B. Hajdu");
169                out.println("-->");
170                
171                      
172                //PersistenceDelegate for URL class ~ This tells XMLEncoder how to deal with these objects
173                e.setPersistenceDelegate(URL.class,
174                     new PersistenceDelegate() {
175                            protected Expression instantiate(Object oldInstance,Encoder out) {
176                            return new Expression(oldInstance,oldInstance.getClass(),"new", new Object[]{ oldInstance.toString() });}});
177    
178                e.writeObject(request.getJobs());
179                e.close();      
180                      
181            } catch (Exception e) {
182                      System.out.println("Error with XMLEncoder !!! \n" + e.toString());
183                      
184            }
185             */   /////    
186               
187        }
188        
189        
190        public void retrieveOutput(Request job, List jobs) {
191        }
192        
193        public void Kill(Request request, List jobs) {
194            List OneJobList = new ArrayList ();
195            for (int nJob = jobs.size() - 1; nJob != -1; nJob--) { //loop over all jobs
196               Job job = (Job) jobs.get(nJob);
197               OneJobList.add(job);
198               job.getAssociatedDispatcher().Kill(request, OneJobList);
199            }
200            SessionWriter a = new SessionWriter(request);
201        }    
202         
203        public String Status(Job job, int Processe) {
204            
205            return job.getAssociatedDispatcher().Status(job, Processe);
206        }    
207        
208        
209        private List dispatchersList = new ArrayList(); //A list of all dispacher jobs have been sent to. 
210       
211        public void stop() { //find all the  dispachers that have been used and call there stop members.
212            if(dispatchersList.size() == 0) System.out.println("Zero dispatchers found, job may not have been dispatched yet.");
213            else 
214                for(int i = 0; i != dispatchersList.size(); i++){
215                    Dispatcher dispatcher = (Dispatcher)dispatchersList.get(i);
216                    dispatcher.stop();
217                }
218        }
219        
220        
221        
222        public boolean test(Queue queue) {
223            return true;
224        }    
225        
226        
227    }