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