001    /*
002     * $RCSfile: SchedulerProperties.java,v $ 
003     *
004     * Created on November 12, 2002, 11:09 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    package gov.bnl.star.offline.scheduler;
024    
025    import java.util.Hashtable;
026    import java.util.MissingResourceException;
027    import java.util.ResourceBundle;
028    import java.util.logging.Level;
029    import org.apache.log4j.Logger;
030    
031    
032    /** Manages the scheduler runtime properties and the scheduler.properties files
033     * in which they are stored.
034     * <p>
035     * Properties are first looked into the scheduler.properties file. If they don't
036     * appear there, it falls back to gov/bnl/star/offline/scheduler/scheduler.properties.
037     * Therefore, the second file contains the default settings, and will be packages
038     * inside the jar. While the first will be in the file system to be edited by the
039     * scheduler administrator
040     *
041     * @author  Gabriele Carcassi
042     * @version $Revision: 1.10 $ $Date: 2006/11/21 00:41:32 $
043     * @deprecated
044     */
045    public class SchedulerProperties {
046        static private Logger log = Logger.getLogger(SchedulerProperties.class.getName());
047        private static SchedulerProperties singleton = new SchedulerProperties();
048        private ResourceBundle properties;
049        private ResourceBundle defaultProperties;
050        private Hashtable propertyCache = new Hashtable();
051        
052        /** Creates a new instance of SchedulerProperties */
053        private SchedulerProperties() {
054            loadBundles();
055        }
056        
057        /** Returns the instance of the property manager.
058         */
059        public static SchedulerProperties getInstance() {
060            return singleton;
061        }
062        
063        /** Returns the property with the specified name. First, it looks in the
064         * user specific file. If it fails, looks into the default file. If also
065         * that fails, an error is displayed and the application exits.
066         */
067        public String getProperty(String name) {
068            String cached = (String) propertyCache.get(name);
069            
070            if (cached != null) {
071                return cached;
072            }
073            
074            if (properties != null) {
075                try {
076                    cached = properties.getString(name);
077                    propertyCache.put(name, cached);
078                    log.info("Retrieved custom value for " + name + ": " + cached);
079                    return cached;
080                } catch (MissingResourceException e) {
081                }
082            }
083            
084            try {
085                cached = defaultProperties.getString(name);
086                propertyCache.put(name, cached);
087                log.info("Retrieved default value for " + name + ": " + cached);
088                
089                return cached;
090            } catch (MissingResourceException e) {
091                log.fatal("No value found for property " + name);
092                System.out.println(
093                "Configuration error. Contanct scheduler administrator.");
094                System.out.println("No value found for property " + name);
095                System.exit(-1);
096            }
097            
098            return null;
099        }
100        
101        public int getIntProperty(String name) {
102            String prop = getProperty(name);
103            
104            if (prop != null) {
105                try {
106                    return Integer.parseInt(prop);
107                } catch (Exception e) {
108                    log.fatal("Incorrect value " + prop + " found for property " + name);
109                    System.out.println(
110                    "Configuration error. Contanct scheduler administrator.");
111                    System.out.println("Incorrect value " + prop + " found for property " + name);
112                    System.exit(-1);
113                }
114            }
115            log.fatal("No value found for property " + name);
116            System.out.println(
117            "Configuration error. Contanct scheduler administrator.");
118            System.out.println("No value found for property " + name);
119            System.exit(-1);
120            return 0;
121        }
122        
123        private void loadBundles() {
124            log.info("Loading scheduler properties");
125            
126            try {
127                defaultProperties = ResourceBundle.getBundle(
128                "gov/bnl/star/offline/scheduler/scheduler");
129                log.info("Default scheduler.properties loaded");
130            } catch (MissingResourceException e) {
131                log.fatal("Can't find the default scheduler setting. Aborting", e);
132                System.out.println("Can't find the default scheduler setting.");
133                System.out.println( "Check inside scheduler.jar for gov/bnl/star/offline/scheduler/scheduler.properties");
134                System.exit(-1);
135            }
136            
137            try {
138                properties = ResourceBundle.getBundle("scheduler");
139                log.info("Custom scheduler.properties found");
140            } catch (MissingResourceException e) {
141                log.info("No custom scheduler.properties found");
142            }
143        }
144    }