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.docstore.discovery.util;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.List;
21  import java.util.Properties;
22  
23  import org.kuali.ole.utility.ConfigUtil;
24  import org.kuali.ole.utility.Constants;
25  import org.kuali.ole.utility.PropertyUtils;
26  import org.kuali.ole.utility.ResourceUtil;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * 
32   */
33  public class PropertyUtil {
34  	private static final Logger LOG = LoggerFactory.getLogger(PropertyUtil.class);
35  	ResourceUtil resourceUtil = new ResourceUtil();
36  	ConfigUtil configUtil = new ConfigUtil();
37  	private static PropertyUtil propertyUtil = null;
38  	private static String DEFAULT_PROPERTIES_FILENAME = "ole-discovery.properties";
39  	private static String DEFAULT_PROPERTIES_LOCATION = "classpath:" + DEFAULT_PROPERTIES_FILENAME;
40  	private static String SYSTEM_PROPERTY = "discovery.properties.file";
41  	private static String DOCSEARCH_URL_PROPERTY = "docSearchURL";
42  
43  	public static synchronized PropertyUtil getPropertyUtil() {
44  		if (propertyUtil == null) {
45  			propertyUtil = new PropertyUtil();
46  		}
47  		return propertyUtil;
48  	}
49  
50  	private Properties props;
51  
52  	private PropertyUtil() {
53  		try {
54  			String externalConfigFile = getExternalConfigFile();
55  			List<String> locations = PropertyUtils.getLocations(DEFAULT_PROPERTIES_LOCATION, SYSTEM_PROPERTY, externalConfigFile);
56  			props = PropertyUtils.getAppEnvironmentProperties(locations);
57  			LOG.info(DOCSEARCH_URL_PROPERTY + "=" + props.getProperty(DOCSEARCH_URL_PROPERTY));
58  		} catch (IOException e) {
59  			throw new IllegalStateException("Exception loading properties", e);
60  		}
61  	}
62  
63  	public String getProperty(String key) {
64  		return props.getProperty(key);
65  	}
66  
67  	public Properties getProperties() {
68  		Properties p = new Properties();
69  		p.putAll(props);
70  		return p;
71  	}
72  
73  	public String getExternalConfigFile() {
74  		StringBuilder sb = new StringBuilder();
75  		sb.append(configUtil.getApplicationHome(Constants.KUALI_GROUP, Constants.OLE_DOCSTORE_APP));
76  		sb.append(File.separator + Constants.OLE_DISCOVERY_APP);
77  		sb.append(File.separator + "properties" + File.separator);
78  		sb.append(DEFAULT_PROPERTIES_FILENAME);
79  		return sb.toString();
80  	}
81  }