1 16 package com.blandware.atleap.webapp.util.core; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.model.core.GlobalProperty; 20 import com.blandware.atleap.service.core.GlobalPropertyManager; 21 import com.blandware.atleap.service.exception.BeanNotFoundException; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.springframework.context.ApplicationContext; 25 import org.springframework.web.context.support.WebApplicationContextUtils; 26 27 import javax.servlet.ServletContext ; 28 import java.util.Properties ; 29 30 37 public final class GlobalProperties { 38 39 42 private static final String INSTANCE_KEY = "com.blandware.atleap.webapp.util.GlobalProperties.INSTANCE"; 43 44 47 private transient final Log log = LogFactory.getLog(GlobalProperties.class); 48 49 52 private Properties staticProperties; 53 54 57 private Properties dynamicProperties; 58 59 62 private GlobalPropertyManager globalPropertyManager = null; 63 64 69 private GlobalProperties(ServletContext servletContext) { 70 ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 71 globalPropertyManager = (GlobalPropertyManager) applicationContext.getBean(Constants.GLOBAL_PROPERTY_MANAGER_BEAN); 72 synchronizeCache(); 73 } 74 75 81 public static GlobalProperties getInstance(ServletContext servletContext) { 82 GlobalProperties globalProperties = (GlobalProperties) servletContext.getAttribute(INSTANCE_KEY); 83 if ( globalProperties == null ) { 84 globalProperties = new GlobalProperties(servletContext); 85 servletContext.setAttribute(INSTANCE_KEY, globalProperties); 86 } 87 return globalProperties; 88 } 89 90 93 public void synchronizeCache() { 94 staticProperties = globalPropertyManager.getStaticProperties(); 95 dynamicProperties = globalPropertyManager.loadDynamicProperties(false); 96 97 if ( log.isInfoEnabled() ) { 98 log.info("Global properties synchronized"); 99 } 100 } 101 102 111 public String getString(String propertyName) { 112 return getString(propertyName, null); 113 } 114 115 116 127 public String getString(String propertyName, String defaultValue) { 128 String value = dynamicProperties.getProperty(propertyName); 129 if ( value == null ) { 130 value = staticProperties.getProperty(propertyName); 131 } 132 return value != null ? value : defaultValue; 133 } 134 135 147 public Integer getInteger(String propertyName) { 148 return getInteger(propertyName, null); 149 } 150 151 165 public Integer getInteger(String propertyName, int defaultValue) { 166 return getInteger(propertyName, new Integer (defaultValue)); 167 } 168 169 183 public Integer getInteger(String propertyName, Integer defaultValue) { 184 String value = null; 185 if ( defaultValue != null ) { 186 value = getString(propertyName, defaultValue.toString()); 187 } else { 188 value = getString(propertyName, null); 189 } 190 191 Integer result = null; 192 if ( value != null ) { 193 try { 194 result = Integer.valueOf(value); 195 } catch ( Exception e ) { 196 if ( log.isErrorEnabled() ) { 197 log.error("Error while retrieving integer value of property '" + propertyName 198 + "'. Failed to convert string '" + value 199 + "' to java.lang.Integer. Cause: " 200 + e.getClass().getName() + ": " + e.getMessage()); 201 } 202 result = defaultValue; 203 } 204 } 205 return result; 206 } 207 208 215 public void updateProperty(GlobalProperty globalProperty) throws BeanNotFoundException { 216 globalPropertyManager.updateProperty(globalProperty); 217 dynamicProperties.put(globalProperty.getName(), globalProperty.getValue()); 218 } 219 220 227 public GlobalProperty retrieveGlobalProperty(String name) { 228 return globalPropertyManager.retrieveGlobalProperty(name); 229 } 230 231 234 public void reloadProperties() { 235 dynamicProperties = globalPropertyManager.loadDynamicProperties(true); 236 } 237 238 } 239 | Popular Tags |