001 /*
002 * Copyright 2011 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 */
016 package org.kuali.ole.utility;
017
018 import static org.apache.commons.lang.StringUtils.isBlank;
019
020 import org.slf4j.Logger;
021 import org.slf4j.LoggerFactory;
022
023 /**
024 * Logic for determining a base directory for Kuali applications by examining system properties and the environment the application is
025 * running in
026 */
027 public class ConfigUtil {
028 private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
029
030 public static final String KUALI_HOME_SYSTEM_PROPERTY = "kuali.home";
031 public static final String KUALI_HOME_ENVIRONMENT_VAR = "KUALI_HOME";
032 public static final String KUALI_HOME_DEFAULT_VALUE = System.getProperty("user.home") + "/.kuali";
033
034 /**
035 * Return the configured value for the <code>kuali.home</code> system property, the <code>KUALI_HOME</code> environment variable, or a
036 * default value for "kuali.home" based on "user.home".
037 *
038 * When this method finishes the system property <code>kuali.home</code> is guaranteed to be set to the same value returned by this
039 * method.
040 */
041 public String getKualiHome() {
042 String kualiHome = getValue(KUALI_HOME_SYSTEM_PROPERTY, KUALI_HOME_ENVIRONMENT_VAR, KUALI_HOME_DEFAULT_VALUE);
043 String systemProperty = System.getProperty(KUALI_HOME_SYSTEM_PROPERTY);
044 if (!kualiHome.equals(systemProperty)) {
045 logger.info("Setting " + KUALI_HOME_SYSTEM_PROPERTY + "=" + kualiHome);
046 System.setProperty(KUALI_HOME_SYSTEM_PROPERTY, kualiHome);
047 }
048 return kualiHome;
049 }
050
051 public String getGroupHome(String group) {
052 return getKualiHome() + "/" + group;
053 }
054
055 public String getApplicationHome(String group, String application) {
056 return getGroupHome(group) + "/" + application;
057 }
058
059 /**
060 * Locate a value by examining system properties and environment variables. If a system property is found, the system property always
061 * wins. Otherwise the logic defers to the environment variable before falling through to the default value.
062 *
063 * @param systemProperty
064 * @param environmentVariable
065 * @param defaultValue
066 * @return
067 */
068 public String getValue(String systemProperty, String environmentVariable, String defaultValue) {
069 if (!isBlank(systemProperty) && !isBlank(System.getProperty(systemProperty))) {
070 return System.getProperty(systemProperty);
071 } else if (!isBlank(environmentVariable) && !isBlank(System.getenv(environmentVariable))) {
072 return System.getenv(environmentVariable);
073 } else {
074 return defaultValue;
075 }
076 }
077 }