1 16 package com.blandware.atleap.service.core.impl; 17 18 import com.blandware.atleap.model.core.GlobalProperty; 19 import com.blandware.atleap.persistence.core.GlobalPropertyDAO; 20 import com.blandware.atleap.service.core.GlobalPropertyManager; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Properties ; 29 import java.util.Set ; 30 31 41 public class GlobalPropertyManagerImpl extends BaseManagerImpl implements GlobalPropertyManager { 42 43 protected final Log log = LogFactory.getLog(GlobalPropertyManagerImpl.class); 44 45 48 protected GlobalPropertyDAO globalPropertyDAO; 49 50 53 protected Properties staticProperties; 54 55 58 protected Properties dynamicProperties; 59 60 63 protected Properties originalDynamicProperties; 64 65 70 public void setGlobalPropertyDAO(GlobalPropertyDAO dao) { 71 this.globalPropertyDAO = dao; 72 } 73 74 79 public void setStaticProperties(Properties staticProperties) { 80 this.staticProperties = staticProperties; 81 } 82 83 88 public void setDynamicProperties(Properties dynamicProperties) { 89 this.dynamicProperties = dynamicProperties; 90 } 91 92 97 public Properties getStaticProperties() { 98 return staticProperties; 99 } 100 101 106 public Properties getDynamicProperties() { 107 return dynamicProperties; 108 } 109 110 113 public Properties loadDynamicProperties(boolean overrideDynamicValues) { 114 Set intersection = new HashSet (staticProperties.keySet()); 116 intersection.retainAll(dynamicProperties.keySet()); 117 if ( !intersection.isEmpty() ) { 118 if ( log.isWarnEnabled() ) { 120 for ( Iterator i = intersection.iterator(); i.hasNext(); ) { 121 String name = (String ) i.next(); 122 log.warn("Property with name '" + name + "' declared as static and dynamic simultaneously. Static value will never be returned."); 123 } 124 } 125 } 126 127 List existentProperties = listDynamicProperties(); 129 for ( Iterator i = existentProperties.iterator(); i.hasNext(); ) { 130 GlobalProperty globalProperty = (GlobalProperty) i.next(); 131 if ( !this.dynamicProperties.keySet().contains(globalProperty.getName()) ) { 132 globalPropertyDAO.deleteGlobalProperty(globalProperty); 133 } 134 } 135 136 137 Properties properties = new Properties (); 138 139 for ( Iterator i = this.dynamicProperties.entrySet().iterator(); i.hasNext(); ) { 141 Map.Entry property = (Map.Entry ) i.next(); 142 String name = (String ) property.getKey(); 143 String value = (String ) property.getValue(); 144 if (log.isDebugEnabled()) { 145 log.debug("Creating unexistent property with name='" + name + "' and value='" + value + "'"); 146 } 147 GlobalProperty globalProperty = globalPropertyDAO.retrieveGlobalProperty(name); 148 if ( globalProperty == null ) { 149 globalProperty = new GlobalProperty(name, value); 150 globalPropertyDAO.createGlobalProperty(globalProperty); 151 } else if ( overrideDynamicValues && !value.equals(globalProperty.getValue()) ) { 152 globalProperty.setValue(value); 153 globalPropertyDAO.updateGlobalProperty(globalProperty); 154 } else { 155 value = globalProperty.getValue(); 156 if (value == null) { 157 value = ""; 158 } 159 } 160 properties.put(name, value); 161 } 162 return properties; 163 } 164 165 168 public GlobalProperty retrieveGlobalProperty(String propertyName) { 169 return globalPropertyDAO.retrieveGlobalProperty(propertyName); 170 } 171 172 173 176 public void updateProperty(GlobalProperty globalProperty) { 177 globalPropertyDAO.removeFromCache(globalProperty); 179 180 if ( log.isDebugEnabled() ) { 181 log.debug("Updating global property with name '" + globalProperty.getName() + "'..."); 182 } 183 184 globalPropertyDAO.updateGlobalProperty(globalProperty); 185 186 if ( log.isDebugEnabled() ) { 187 log.debug("Global property has been updated successfully"); 188 } 189 } 190 191 193 196 public List listDynamicProperties() { 197 return globalPropertyDAO.listDynamicProperties(); 198 } 199 200 } | Popular Tags |