001    /*
002     * $RCSfile: ComponentLibrary.java,v $
003     *
004     * Created on September 25, 2003, 11:47 AM
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;
025    
026    import gov.bnl.star.offline.scheduler.util.persistent.XMLDecoderExceptionListener;
027    
028    import gov.bnl.star.offline.scheduler.bnl.*;
029    import gov.bnl.star.offline.scheduler.catalog.*;
030    import gov.bnl.star.offline.scheduler.Dispatchers.condorg.*;
031    import gov.bnl.star.offline.scheduler.initializer.*;
032    import gov.bnl.star.offline.scheduler.Dispatchers.lsf.*;
033    import gov.bnl.star.offline.scheduler.util.ConfigToolkit;
034    import gov.bnl.star.offline.scheduler.policy.*;
035    import java.beans.*;
036    import java.io.*;
037    import java.util.*;
038    import java.util.logging.Level;
039    import org.apache.log4j.Logger;
040    
041    /**The base class that encapsulates(points to) the configurations object.
042     * @author  carcassi
043     * @version $Revision: 1.22 $ $Date: 2006/11/21 00:41:32 $
044     */
045    public class ComponentLibrary {
046        static private Logger log = Logger.getLogger(ComponentLibrary.class.getName());
047        static private ComponentLibrary theInstance;
048        
049        private Map library;
050        /** Creates a new instance of ComponentLibrary */
051        private ComponentLibrary() {
052        }
053        
054        /**@return The initialized global configuration object.*/
055        public static ComponentLibrary getInstance() {
056            return theInstance;
057        }
058        
059        public static void initLibrary(String xmlFilename) {
060            try {
061                initLibrary(new FileInputStream(xmlFilename));
062            } catch (Exception e) {
063                log.fatal("Couldn't open configuration file", e);
064                // TODO Exception user message
065                throw new RuntimeException("Couldn't open configuration file. Contact scheduler administrator,");
066            }
067        }
068        
069        static void initLibrary(InputStream is) {
070            try {
071                
072                //used for debugging
073                //XMLDecoderExceptionListener MyExceptionListener = new XMLDecoderExceptionListener();
074                //XMLDecoder e = new XMLDecoder(is,null, MyExceptionListener);
075                
076                XMLDecoder e = new XMLDecoder(is);
077                Hashtable map = (Hashtable) e.readObject();
078                e.close();
079                theInstance = new ComponentLibrary();
080                theInstance.library = map;
081            } catch (Exception e) {
082                log.fatal("Couldn't read configuration", e);
083                // TODO Exception user message
084                throw new RuntimeException("Couldn't open configuration file. Contact scheduler administrator,");
085            }
086        }
087        
088        /** Initializes the component library with the old properties file.
089         * This should be removed once the xml configuration is ready.
090         
091        public static void initLibrary() {
092            theInstance = new ComponentLibrary();
093            theInstance.library = new Hashtable();
094            XMLInitializer init = new XMLInitializer();
095            init.setDefaultFileListSyntax(SchedulerProperties.getInstance().getProperty("JobRequestXMLParser.defaults.fileListSyntax"));
096            theInstance.library.put("defaultJobInitializer", init);
097            
098            PassivePolicy policy = new PassivePolicy();
099            policy.setNfsQueue(SchedulerProperties.getInstance().getProperty("PassivePolicy.nfsQueue"));
100            policy.setLocalQueue(SchedulerProperties.getInstance().getProperty("PassivePolicy.localQueue"));
101            policy.setLongQueue(SchedulerProperties.getInstance().getProperty("PassivePolicy.longQueue"));
102            policy.setMinTimeLimitQueue(SchedulerProperties.getInstance().getIntProperty("PassivePolicy.localQueue.minTimeLimit"));
103            policy.setRootdAvailable("true".equals(SchedulerProperties.getInstance().getProperty("PassivePolicy.isRootdAvailable")));
104            theInstance.library.put("defaultPolicy", policy);
105            
106            LSFDispatcher lsfDispatcher = new LSFDispatcher();
107            LSFResourceStrategy res = new LSFResourceStrategy();
108            res.setBase(12);
109            res.setFileFactor(2);
110            res.setMax(50);
111            res.setDataVaultPrefix("/star/data");
112            res.setTrimVaultNumberLeadingZeros(true);
113            res.setResourceNameSyntax("sd$vaultNumber");
114    // No need to change or expand. See above comment on obsolete method
115            res.setSwitchSyntax("\"rusage[$nameEqualValueCommaSeparated]\"");
116            
117            lsfDispatcher.setResourceStrategy(res);
118            lsfDispatcher.setScratchDir(SchedulerProperties.getInstance().getProperty("LSFDispatcher.scratchDir"));
119            lsfDispatcher.setBsubEx(SchedulerProperties.getInstance().getProperty("ProgramLocation.bsub"));
120            lsfDispatcher.setQueueName(SchedulerProperties.getInstance().getProperty("LSFDispatcher.queueName"));
121            lsfDispatcher.setBsubOptions(SchedulerProperties.getInstance().getProperty("LSFDispatcher.bsubOptions"));
122            lsfDispatcher.setMaxAttempts(SchedulerProperties.getInstance().getIntProperty("LSFDispatcher.maxBsubAttempts"));
123            lsfDispatcher.setMsBtwnSuccess(SchedulerProperties.getInstance().getIntProperty("LSFDispatcher.msBtwnSuccess"));
124            lsfDispatcher.setMsBtwnFailure(SchedulerProperties.getInstance().getIntProperty("LSFDispatcher.msBtwnFailure"));
125            theInstance.library.put("defaultDispatcher", lsfDispatcher);
126            
127            XMLInitializer gridInit = new XMLInitializer();
128            gridInit.setDefaultFileListSyntax(SchedulerProperties.getInstance().getProperty("JobRequestXMLParser.defaults.fileListSyntax"));
129            theInstance.library.put("gridInitializer", gridInit);
130            
131            PassivePolicy gridPolicy = new PassivePolicy();
132            gridPolicy.setNfsQueue(SchedulerProperties.getInstance().getProperty("PassivePolicy.nfsQueue"));
133            gridPolicy.setLocalQueue(SchedulerProperties.getInstance().getProperty("PassivePolicy.localQueue"));
134            gridPolicy.setLongQueue(SchedulerProperties.getInstance().getProperty("PassivePolicy.longQueue"));
135            gridPolicy.setMinTimeLimitQueue(SchedulerProperties.getInstance().getIntProperty("PassivePolicy.localQueue.minTimeLimit"));
136            gridPolicy.setRootdAvailable("true".equals(SchedulerProperties.getInstance().getProperty("PassivePolicy.isRootdAvailable")));
137            theInstance.library.put("gridPolicy", gridPolicy);
138            
139            CondorGLSFDispatcher condorgDispatcher = new CondorGLSFDispatcher();
140            LSFResourceStrategy gridres = new LSFResourceStrategy();
141            gridres.setBase(12);
142            gridres.setFileFactor(2);
143            gridres.setMax(50);
144            gridres.setDataVaultPrefix("/star/data");
145            gridres.setTrimVaultNumberLeadingZeros(true);
146            gridres.setResourceNameSyntax("sd$vaultNumber");
147      // No need to change or expand. See above comment on obsolete method
148            
149            gridres.setSwitchSyntax("\"rusage[$nameEqualValueCommaSeparated]\"");
150            condorgDispatcher.setResourceStrategy(gridres);
151            condorgDispatcher.setScratchDir(SchedulerProperties.getInstance().getProperty("LSFDispatcher.scratchDir"));
152            condorgDispatcher.setBsubEx(SchedulerProperties.getInstance().getProperty("ProgramLocation.bsub"));
153            condorgDispatcher.setCondorEx(SchedulerProperties.getInstance().getProperty("ProgramLocation.condor_submit"));
154            condorgDispatcher.setQueueName(SchedulerProperties.getInstance().getProperty("LSFDispatcher.queueName"));
155            condorgDispatcher.setBsubOptions(SchedulerProperties.getInstance().getProperty("LSFDispatcher.bsubOptions"));
156            condorgDispatcher.setMaxAttempts(SchedulerProperties.getInstance().getIntProperty("LSFDispatcher.maxBsubAttempts"));
157            condorgDispatcher.setMsBtwnSuccess(SchedulerProperties.getInstance().getIntProperty("LSFDispatcher.msBtwnSuccess"));
158            condorgDispatcher.setMsBtwnFailure(SchedulerProperties.getInstance().getIntProperty("LSFDispatcher.msBtwnFailure"));
159            theInstance.library.put("gridDispatcher", condorgDispatcher);
160            
161            policy = new PassivePolicy();
162            policy.setNfsQueue(SchedulerProperties.getInstance().getProperty("PassivePolicy.nfsQueue"));
163            policy.setLocalQueue(SchedulerProperties.getInstance().getProperty("PassivePolicy.localQueue"));
164            policy.setLongQueue(SchedulerProperties.getInstance().getProperty("ResonanceGroupPolicy.longQueue"));
165            policy.setMinTimeLimitQueue(SchedulerProperties.getInstance().getIntProperty("PassivePolicy.localQueue.minTimeLimit"));
166            theInstance.library.put("ResonanceGroupPolicy", policy);
167            
168            theInstance.library.put("defaultFileCatalog", new StarCatalog());
169            Map programLocations =  new Hashtable();
170            programLocations.put("csh", SchedulerProperties.getInstance().getProperty("ProgramLocation.csh"));
171            programLocations.put("ls", SchedulerProperties.getInstance().getProperty("ProgramLocation.ls"));
172            // TODO: might want to put these as parameters of the Dispatchers
173            programLocations.put("bsub", SchedulerProperties.getInstance().getProperty("ProgramLocation.bsub"));
174            programLocations.put("condor_submit", SchedulerProperties.getInstance().getProperty("ProgramLocation.condor_submit"));
175            theInstance.library.put("programLocations", programLocations);
176            
177            Map statConf =  new Hashtable();
178            statConf.put("URL", SchedulerProperties.getInstance().getProperty("statistics.URL"));
179            statConf.put("username", SchedulerProperties.getInstance().getProperty("statistics.username"));
180            statConf.put("password", SchedulerProperties.getInstance().getProperty("statistics.password"));
181            theInstance.library.put("statistics", statConf);
182    
183            Map logConf =  new Hashtable();
184            logConf.put("directory", SchedulerProperties.getInstance().getProperty("Log.directory"));
185            logConf.put("level", SchedulerProperties.getInstance().getProperty("Log.level"));
186            theInstance.library.put("log", logConf);
187            
188            CompositeDispatcher bnlDispatcher = new CompositeDispatcher();
189            CondorDispatcher condorDispatcher = new CondorDispatcher();
190            condorDispatcher.setScratchDir(SchedulerProperties.getInstance().getProperty("LSFDispatcher.scratchDir"));
191            condorDispatcher.setCondorEx(SchedulerProperties.getInstance().getProperty("ProgramLocation.condor_submit"));
192            condorDispatcher.setMaxAttempts(SchedulerProperties.getInstance().getIntProperty("CondorDispatcher.maxBsubAttempts"));
193            condorDispatcher.setMsBtwnSuccess(SchedulerProperties.getInstance().getIntProperty("CondorDispatcher.msBtwnSuccess"));
194            condorDispatcher.setMsBtwnFailure(SchedulerProperties.getInstance().getIntProperty("CondorDispatcher.msBtwnFailure"));
195            SimpleDispatcherChooser chooser = new SimpleDispatcherChooser();
196            chooser.setCluster("rcas.rcf.bnl.gov");
197            chooser.setRedirectedCluster("crs.rcf.bnl.gov");
198            chooser.setRedirectedPercentage(0.2);
199            chooser.setQueueToRedirect(SchedulerProperties.getInstance().getProperty("PassivePolicy.nfsQueue"));
200            bnlDispatcher.setChooser(chooser);
201            Map dispatchers = new Hashtable();
202            dispatchers.put("rcas.rcf.bnl.gov", lsfDispatcher);
203            dispatchers.put("crs.rcf.bnl.gov", condorDispatcher);
204            bnlDispatcher.setDispatchers(dispatchers);
205            
206            theInstance.library.put("BNLDispatcher", bnlDispatcher);
207            theInstance.library.put("CRSDispatcher", condorDispatcher);
208            
209    // These 3 lines are a temporary fix to get test to work 
210    // until xml configuration is read properly
211            CSHApplication app = new CSHApplication();
212            app.setMakeExecutableCommand("/bin/chmod +x");
213            
214            theInstance.library.put("CSHApplication", app);
215        }
216    */
217        
218        public static void main(String[] args) {
219            /*initLibrary();
220            try {
221            XMLEncoder e = new XMLEncoder(new BufferedOutputStream( new FileOutputStream("c:\\test.xml")));
222            e.writeObject(theInstance.library);
223            e.close();
224            } catch (Exception e) {
225                e.printStackTrace();
226            }*/
227            
228            initLibrary("C:\\java\\ITDCVS\\scheduler\\res\\dev\\devConfig.xml");
229        }
230        
231        /**Add an object to the configuration table.*/
232        public void addComponent(String name, Object object){
233            library.put(name, object);
234        }
235        
236        /**Get an object from the configuration table.*/
237        public Object getComponent(String componentName) {
238            //This line is used for debugging.
239            //if(! library.containsKey(componentName)) System.out.println("Component Library could not find component \""+ componentName +"\". ");
240            return library.get(componentName);
241        }
242        
243        public JobInitializer getJobInitializer() {
244            return getJobInitializer("defaultJobInitializer");
245        }
246        
247        public JobInitializer getJobInitializer(String jobInitializerName) {
248            return (JobInitializer) getComponent(jobInitializerName);
249        }
250        
251        public Policy getPolicy() {
252            return getPolicy("defaultPolicy");
253        }
254        
255        public Policy getPolicy(String policyName) {
256            return (Policy) getComponent(policyName);
257        }
258        
259        public Dispatcher getDispatcher() {
260            return getDispatcher("defaultDispatcher");
261        }
262    
263        public Dispatcher getDispatcher(String dispatcherName) {
264            return (Dispatcher) getComponent(dispatcherName);
265        }
266        
267        public FileCatalog getDefaultFileCatalog() {
268            return (FileCatalog) getComponent("defaultFileCatalog");
269        }
270        
271        /* Get a program from the config file located to the site from which I am currently submitting. */
272        public String getProgramLocation(String programName) {
273           // return (String) ((Map) getComponent("programLocations")).get(programName);
274            return (String) ConfigToolkit.getToolkit().myLocalSite().getProgramLocations().get(programName);
275        }
276        
277        /* Get a program from the config file for a give site. The two parameters are the site where the program should be looked up and the program name. */
278        public String getProgramLocation(Site site, String programName) {
279           return (String) site.getProgramLocations().get(programName);
280        }    
281            
282        
283        public void forceGridDispatchers(){   
284            List sites = (List) ComponentLibrary.getInstance().getComponent("gridView");
285            for(int i = 0; i != sites.size(); i++ ){
286                List batchSystems = ((Site) sites.get(i)).getBatchSystems();
287                for(int j = 0; j != batchSystems.size(); j++){
288                    BatchSystem batchSystem = (BatchSystem) batchSystems.get(j);
289                    batchSystem.setLocalDispatcher(batchSystem.getGridDispatcher());   
290                }
291            }   
292        }
293        
294        
295        public Map globalObjectTable = new Hashtable();
296        
297        
298    }