001package org.kuali.common.util.spring.env;
002
003import java.util.Properties;
004
005import org.kuali.common.util.Ascii;
006import org.kuali.common.util.Assert;
007import org.kuali.common.util.FormatUtils;
008import org.kuali.common.util.PropertyUtils;
009import org.springframework.core.env.Environment;
010
011import com.google.common.base.Optional;
012
013public class EnvUtils {
014
015        public static final Optional<EnvironmentService> ABSENT = Optional.absent();
016
017        private static final String ENV_PREFIX = "env";
018
019        /**
020         * If the environment contains a string under this key, convert it into a long signifying bytes
021         * 
022         * <pre>
023         *   file.size=10m   (file that is 10 megabytes)
024         *   disk.size=100g  (disk that is 100 gigabytes)
025         * </pre>
026         */
027        public static long getBytes(EnvironmentService env, String key, long provided) {
028                if (env.containsProperty(key)) {
029                        String size = env.getString(key);
030                        long bytes = FormatUtils.getBytes(size);
031                        return bytes;
032                } else {
033                        return provided;
034                }
035        }
036
037        private static Environment instance;
038
039        /**
040         * Return an environment that uses system properties / environment variables
041         */
042        public synchronized static Environment getDefaultEnvironment() {
043                if (instance == null) {
044                        Properties global = PropertyUtils.getGlobalProperties();
045                        instance = new PropertiesEnvironment(global);
046                }
047                return instance;
048        }
049
050        /**
051         * <pre>
052         *  foo.bar    -> env.FOO_BAR
053         *  foo.barBaz -> env.FOO_BAR_BAZ
054         * </pre>
055         */
056        public static String getEnvironmentVariableKey(String key) {
057                Assert.noBlanks(key);
058                // Add a prefix, change to upper case and return
059                return ENV_PREFIX + "." + toUnderscore(key).toUpperCase();
060        }
061        
062        /**
063         * <pre>
064         *  foo.bar    -> foo_bar
065         *  foo.barBaz -> foo_bar_baz
066         * </pre>
067         */
068        public static String toUnderscore(String key) {
069                char[] chars = key.toCharArray();
070                StringBuilder sb = new StringBuilder();
071                char prevChar = 0;
072                for (char c : chars) {
073                        if (c == '.') {
074                                // Convert dots into dashes
075                                sb.append('_');
076                        } else if (Ascii.isUpperCase(c) && Ascii.isLowerCase(prevChar)) {
077                                // Insert an underscore every time there is a transition from a lower case char to an upper case char
078                                sb.append('_');
079                                sb.append(c);
080                        } else {
081                                // Just append the char
082                                sb.append(c);
083                        }
084                        // Keep track of the previous char
085                        prevChar = c;
086                }
087                return sb.toString();
088        }
089
090}