001    package org.shiftone.jrat.core;
002    
003    import org.shiftone.jrat.util.log.Logger;
004    
005    /**
006     * This has a bit of a smell to it.
007     * The problem is the system needs to know how it's being executed.  In the case of
008     * runtime, it needs to read (and possibly column) files.  In the case of desktop, that
009     * would be silly.
010     * The mode can be set multiple times, but once it's read it can not be set.
011     *
012     * @author jeff@shiftone.org (Jeff Drost)
013     */
014    public class Mode {
015    
016        private static final Logger LOG = Logger.getLogger(Mode.class);
017    
018        public static final Mode UNKNOWN = new Mode("unknown", false, 0);
019        public static final Mode DESKTOP = new Mode("desktop", false, 1);
020        public static final Mode RUNTIME = new Mode("runtime", true, 2);
021    
022        private static Mode current = UNKNOWN;
023        private static boolean locked = false;
024    
025        private String name;
026        private boolean environmentLoadingEnabled;
027        private int priority;
028    
029    
030        public Mode(String name, boolean environmentLoadingEnabled, int priority) {
031            this.name = name;
032            this.environmentLoadingEnabled = environmentLoadingEnabled;
033            this.priority = priority;
034        }
035    
036        public String getName() {
037            return name;
038        }
039    
040        public boolean isEnvironmentLoadingEnabled() {
041            return environmentLoadingEnabled;
042        }
043    
044        // todo - fix this
045        public static void set(Mode newMode) {
046    
047            LOG.info("set " + newMode);
048            if (current.priority >= newMode.priority) {
049    
050                return;
051    
052            } else if (locked) {
053    
054                throw new IllegalStateException("mode is locked");
055    
056            } else {
057    
058                if (newMode.priority > current.priority) {
059                    // the mode can be changed to a higher priority mode
060                    current = newMode;
061                }
062    
063            }
064    
065        }
066    
067        public static Mode get() {
068            //LOG.info("get",new Throwable());
069            if (!locked) {
070                LOG.info("locking");
071                locked = true;
072            }
073            return current;
074        }
075    
076    
077        public String toString() {
078            return "Mode[" + name + "]";
079        }
080    }