1 /*
2 * Copyright 2011 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.ole.utility;
17
18 import static org.apache.commons.lang.StringUtils.isBlank;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24 * Logic for determining a base directory for Kuali applications by examining system properties and the environment the application is
25 * running in
26 */
27 public class ConfigUtil {
28 private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
29
30 public static final String KUALI_HOME_SYSTEM_PROPERTY = "kuali.home";
31 public static final String KUALI_HOME_ENVIRONMENT_VAR = "KUALI_HOME";
32 public static final String KUALI_HOME_DEFAULT_VALUE = System.getProperty("user.home") + "/.kuali";
33
34 /**
35 * Return the configured value for the <code>kuali.home</code> system property, the <code>KUALI_HOME</code> environment variable, or a
36 * default value for "kuali.home" based on "user.home".
37 *
38 * When this method finishes the system property <code>kuali.home</code> is guaranteed to be set to the same value returned by this
39 * method.
40 */
41 public String getKualiHome() {
42 String kualiHome = getValue(KUALI_HOME_SYSTEM_PROPERTY, KUALI_HOME_ENVIRONMENT_VAR, KUALI_HOME_DEFAULT_VALUE);
43 String systemProperty = System.getProperty(KUALI_HOME_SYSTEM_PROPERTY);
44 if (!kualiHome.equals(systemProperty)) {
45 logger.info("Setting " + KUALI_HOME_SYSTEM_PROPERTY + "=" + kualiHome);
46 System.setProperty(KUALI_HOME_SYSTEM_PROPERTY, kualiHome);
47 }
48 return kualiHome;
49 }
50
51 public String getGroupHome(String group) {
52 return getKualiHome() + "/" + group;
53 }
54
55 public String getApplicationHome(String group, String application) {
56 return getGroupHome(group) + "/" + application;
57 }
58
59 /**
60 * Locate a value by examining system properties and environment variables. If a system property is found, the system property always
61 * wins. Otherwise the logic defers to the environment variable before falling through to the default value.
62 *
63 * @param systemProperty
64 * @param environmentVariable
65 * @param defaultValue
66 * @return
67 */
68 public String getValue(String systemProperty, String environmentVariable, String defaultValue) {
69 if (!isBlank(systemProperty) && !isBlank(System.getProperty(systemProperty))) {
70 return System.getProperty(systemProperty);
71 } else if (!isBlank(environmentVariable) && !isBlank(System.getenv(environmentVariable))) {
72 return System.getenv(environmentVariable);
73 } else {
74 return defaultValue;
75 }
76 }
77 }