View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.spring.env;
17  
18  import java.util.Properties;
19  
20  import org.kuali.common.util.Ascii;
21  import org.kuali.common.util.Assert;
22  import org.kuali.common.util.FormatUtils;
23  import org.kuali.common.util.PropertyUtils;
24  import org.springframework.core.env.Environment;
25  
26  import com.google.common.base.Optional;
27  
28  public class EnvUtils {
29  
30  	public static final Optional<EnvironmentService> ABSENT = Optional.absent();
31  
32  	private static final String ENV_PREFIX = "env";
33  
34  	/**
35  	 * If the environment contains a string under this key, convert it into a long signifying bytes
36  	 * 
37  	 * <pre>
38  	 *   file.size=10m   (file that is 10 megabytes)
39  	 *   disk.size=100g  (disk that is 100 gigabytes)
40  	 * </pre>
41  	 */
42  	public static long getBytes(EnvironmentService env, String key, long provided) {
43  		if (env.containsProperty(key)) {
44  			String size = env.getString(key);
45  			long bytes = FormatUtils.getBytes(size);
46  			return bytes;
47  		} else {
48  			return provided;
49  		}
50  	}
51  
52  	private static Environment instance;
53  
54  	/**
55  	 * Return an environment that uses system properties / environment variables
56  	 */
57  	public synchronized static Environment getDefaultEnvironment() {
58  		if (instance == null) {
59  			Properties global = PropertyUtils.getGlobalProperties();
60  			instance = new PropertiesEnvironment(global);
61  		}
62  		return instance;
63  	}
64  
65  	/**
66  	 * <pre>
67  	 *  foo.bar    -> env.FOO_BAR
68  	 *  foo.barBaz -> env.FOO_BAR_BAZ
69  	 * </pre>
70  	 */
71  	public static String getEnvironmentVariableKey(String key) {
72  		Assert.noBlanks(key);
73  		// Add a prefix, change to upper case and return
74  		return ENV_PREFIX + "." + toUnderscore(key).toUpperCase();
75  	}
76  	
77  	/**
78  	 * <pre>
79  	 *  foo.bar    -> foo_bar
80  	 *  foo.barBaz -> foo_bar_baz
81  	 * </pre>
82  	 */
83  	public static String toUnderscore(String key) {
84  		char[] chars = key.toCharArray();
85  		StringBuilder sb = new StringBuilder();
86  		char prevChar = 0;
87  		for (char c : chars) {
88  			if (c == '.') {
89  				// Convert dots into dashes
90  				sb.append('_');
91  			} else if (Ascii.isUpperCase(c) && Ascii.isLowerCase(prevChar)) {
92  				// Insert an underscore every time there is a transition from a lower case char to an upper case char
93  				sb.append('_');
94  				sb.append(c);
95  			} else {
96  				// Just append the char
97  				sb.append(c);
98  			}
99  			// Keep track of the previous char
100 			prevChar = c;
101 		}
102 		return sb.toString();
103 	}
104 
105 }