001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.spring.env;
017
018import java.util.Properties;
019
020import org.kuali.common.util.Ascii;
021import org.kuali.common.util.Assert;
022import org.kuali.common.util.FormatUtils;
023import org.kuali.common.util.PropertyUtils;
024import org.springframework.core.env.Environment;
025
026import com.google.common.base.Optional;
027
028public class EnvUtils {
029
030        public static final Optional<EnvironmentService> ABSENT = Optional.absent();
031
032        private static final String ENV_PREFIX = "env";
033
034        /**
035         * If the environment contains a string under this key, convert it into a long signifying bytes
036         * 
037         * <pre>
038         *   file.size=10m   (file that is 10 megabytes)
039         *   disk.size=100g  (disk that is 100 gigabytes)
040         * </pre>
041         */
042        public static long getBytes(EnvironmentService env, String key, long provided) {
043                if (env.containsProperty(key)) {
044                        String size = env.getString(key);
045                        long bytes = FormatUtils.getBytes(size);
046                        return bytes;
047                } else {
048                        return provided;
049                }
050        }
051
052        private static Environment instance;
053
054        /**
055         * Return an environment that uses system properties / environment variables
056         */
057        public synchronized static Environment getDefaultEnvironment() {
058                if (instance == null) {
059                        Properties global = PropertyUtils.getGlobalProperties();
060                        instance = new PropertiesEnvironment(global);
061                }
062                return instance;
063        }
064
065        /**
066         * <pre>
067         *  foo.bar    -> env.FOO_BAR
068         *  foo.barBaz -> env.FOO_BAR_BAZ
069         * </pre>
070         */
071        public static String getEnvironmentVariableKey(String key) {
072                Assert.noBlanks(key);
073                // Add a prefix, change to upper case and return
074                return ENV_PREFIX + "." + toUnderscore(key).toUpperCase();
075        }
076        
077        /**
078         * <pre>
079         *  foo.bar    -> foo_bar
080         *  foo.barBaz -> foo_bar_baz
081         * </pre>
082         */
083        public static String toUnderscore(String key) {
084                char[] chars = key.toCharArray();
085                StringBuilder sb = new StringBuilder();
086                char prevChar = 0;
087                for (char c : chars) {
088                        if (c == '.') {
089                                // Convert dots into dashes
090                                sb.append('_');
091                        } else if (Ascii.isUpperCase(c) && Ascii.isLowerCase(prevChar)) {
092                                // Insert an underscore every time there is a transition from a lower case char to an upper case char
093                                sb.append('_');
094                                sb.append(c);
095                        } else {
096                                // Just append the char
097                                sb.append(c);
098                        }
099                        // Keep track of the previous char
100                        prevChar = c;
101                }
102                return sb.toString();
103        }
104
105}