001/** 002 * Copyright 2010-2014 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 */ 016package org.kuali.common.util.property; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Properties; 021 022import org.kuali.common.util.CollectionUtils; 023import org.kuali.common.util.Encodings; 024import org.kuali.common.util.LocationUtils; 025import org.kuali.common.util.Mode; 026import org.kuali.common.util.ModeUtils; 027import org.kuali.common.util.PropertyUtils; 028import org.kuali.common.util.maven.MavenConstants; 029import org.kuali.common.util.property.processor.GlobalOverrideProcessor; 030import org.kuali.common.util.property.processor.HomeProcessor; 031import org.kuali.common.util.property.processor.OrgProcessor; 032import org.kuali.common.util.property.processor.PathProcessor; 033import org.kuali.common.util.property.processor.PropertyProcessor; 034import org.kuali.common.util.property.processor.VersionProcessor; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037import org.springframework.util.Assert; 038 039/** 040 * @deprecated 041 */ 042@Deprecated 043public class DefaultPropertyLoadContext extends DefaultPropertyContext implements PropertyLoadContext { 044 045 private static final Logger logger = LoggerFactory.getLogger(DefaultPropertyLoadContext.class); 046 047 List<String> locations; 048 String encoding = Encodings.UTF8; 049 String missingLocationsMode = Mode.INFORM.name(); 050 Properties locationHelperProperties; 051 String locationHelperInclude; 052 String locationHelperExclude; 053 List<String> locationHelperIncludes; 054 List<String> locationHelperExcludes; 055 String organizationGroupId; 056 String groupIdProperty = MavenConstants.GROUP_ID_KEY; 057 String versionProperty = MavenConstants.VERSION_KEY; 058 059 @Override 060 public Properties init() { 061 Assert.notNull(helper, "helper is null"); 062 Properties global = getGlobalProperties(locationHelperProperties); 063 this.globalPropertiesMode = resolve(globalPropertiesMode, global); 064 this.missingLocationsMode = resolve(missingLocationsMode, global); 065 logger.info("Global properties mode - " + globalPropertiesMode); 066 logger.info("Missing locations mode - " + missingLocationsMode); 067 this.encoding = resolve(encoding, global); 068 this.organizationGroupId = resolve(organizationGroupId, global); 069 validateGlobalPropertiesMode(globalPropertiesMode); 070 GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode); 071 this.locationHelperProperties = getLocationHelperProperties(locationHelperProperties, gpm); 072 logger.info("Property file encoding - " + encoding); 073 logger.info("Using " + locationHelperProperties.size() + " properties to assist with property loading."); 074 validate(); 075 if (logger.isDebugEnabled()) { 076 PropertyUtils.debug(locationHelperProperties); 077 } 078 Properties p = new Properties(); 079 p.putAll(PropertyUtils.toEmpty(properties)); 080 p.putAll(PropertyUtils.toEmpty(locationHelperProperties)); 081 return p; 082 } 083 084 protected void validateGlobalPropertiesMode(String globalPropertiesMode) { 085 validateResolved(globalPropertiesMode); 086 GlobalPropertiesMode.valueOf(globalPropertiesMode); 087 } 088 089 @Override 090 protected void validate() { 091 validateGlobalPropertiesMode(globalPropertiesMode); 092 validateResolved(encoding); 093 validateResolved(missingLocationsMode); 094 Mode.valueOf(missingLocationsMode); 095 } 096 097 @Override 098 public String getLocation(String location, Properties properties) { 099 String resolvedLocation = getResolvedLocation(location, properties); 100 return getValidatedLocation(resolvedLocation, Mode.valueOf(missingLocationsMode)); 101 } 102 103 protected String getValidatedLocation(String location, Mode missingLocationsMode) { 104 validateResolved(location); 105 if (LocationUtils.exists(location)) { 106 return location; 107 } else { 108 ModeUtils.validate(missingLocationsMode, "Skipping non-existent location - [{}]", location, "Could not locate [" + location + "]"); 109 return null; 110 } 111 } 112 113 protected void validateResolved(String string) { 114 if (PropertyUtils.containsUnresolvedPlaceholder(string)) { 115 throw new IllegalArgumentException("Unable to resolve [" + string + "]"); 116 } 117 } 118 119 protected String getResolvedLocation(String location, Properties properties) { 120 boolean resolve = PropertyUtils.containsUnresolvedPlaceholder(location); 121 if (resolve) { 122 GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode); 123 Properties duplicate = PropertyUtils.getProperties(properties, gpm); 124 List<PropertyProcessor> processors = getLocationProcessors(); 125 for (PropertyProcessor processor : processors) { 126 processor.process(duplicate); 127 } 128 return helper.replacePlaceholders(location, duplicate); 129 } else { 130 return location; 131 } 132 } 133 134 protected Properties getGlobalProperties(Properties properties) { 135 return PropertyUtils.getGlobalProperties(PropertyUtils.toEmpty(properties)); 136 } 137 138 protected Properties getLocationHelperProperties(Properties properties, GlobalPropertiesMode mode) { 139 Properties locationHelperProperties = PropertyUtils.duplicate(PropertyUtils.toEmpty(properties)); 140 List<PropertyProcessor> processors = getLocationHelperProcessors(); 141 for (PropertyProcessor processor : processors) { 142 processor.process(locationHelperProperties); 143 } 144 return locationHelperProperties; 145 } 146 147 protected List<PropertyProcessor> getLocationProcessors() { 148 GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode); 149 List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>(); 150 processors.add(new GlobalOverrideProcessor(gpm)); 151 processors.add(new org.kuali.common.util.property.processor.ResolvePlaceholdersProcessor(helper, gpm)); 152 return processors; 153 } 154 155 protected String getGroupId() { 156 if (groupIdProperty == null) { 157 return null; 158 } else if (locationHelperProperties == null) { 159 return null; 160 } else { 161 return locationHelperProperties.getProperty(groupIdProperty); 162 } 163 } 164 165 protected List<PropertyProcessor> getLocationHelperProcessors() { 166 List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>(); 167 168 this.locationHelperIncludes = CollectionUtils.sortedMerge(locationHelperIncludes, locationHelperInclude); 169 this.locationHelperExcludes = CollectionUtils.sortedMerge(locationHelperExcludes, locationHelperExclude); 170 processors.add(new org.kuali.common.util.property.processor.TrimProcessor(locationHelperIncludes, locationHelperExcludes)); 171 172 String groupId = getGroupId(); 173 if (organizationGroupId != null && groupId != null) { 174 processors.add(new OrgProcessor(organizationGroupId, groupId)); 175 processors.add(new HomeProcessor(organizationGroupId, groupId)); 176 } 177 178 if (groupIdProperty != null) { 179 processors.add(new PathProcessor(groupIdProperty)); 180 } 181 182 if (versionProperty != null) { 183 processors.add(new VersionProcessor(versionProperty)); 184 } 185 186 GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode); 187 processors.add(new GlobalOverrideProcessor(gpm)); 188 processors.add(new org.kuali.common.util.property.processor.ResolvePlaceholdersProcessor(helper, gpm)); 189 190 return processors; 191 } 192 193 public String getMissingLocationsMode() { 194 return missingLocationsMode; 195 } 196 197 public void setMissingLocationsMode(String missingLocationsMode) { 198 this.missingLocationsMode = missingLocationsMode; 199 } 200 201 @Override 202 public List<String> getLocations() { 203 return locations; 204 } 205 206 public void setLocations(List<String> locations) { 207 this.locations = locations; 208 } 209 210 public Properties getLocationHelperProperties() { 211 return locationHelperProperties; 212 } 213 214 public void setLocationHelperProperties(Properties locationHelperProperties) { 215 this.locationHelperProperties = locationHelperProperties; 216 } 217 218 @Override 219 public String getEncoding() { 220 return encoding; 221 } 222 223 public void setEncoding(String encoding) { 224 this.encoding = encoding; 225 } 226 227 public String getOrganizationGroupId() { 228 return organizationGroupId; 229 } 230 231 public void setOrganizationGroupId(String organizationGroupId) { 232 this.organizationGroupId = organizationGroupId; 233 } 234 235 public String getGroupIdProperty() { 236 return groupIdProperty; 237 } 238 239 public void setGroupIdProperty(String groupIdProperty) { 240 this.groupIdProperty = groupIdProperty; 241 } 242 243 public String getVersionProperty() { 244 return versionProperty; 245 } 246 247 public void setVersionProperty(String versionProperty) { 248 this.versionProperty = versionProperty; 249 } 250 251 public List<String> getLocationHelperIncludes() { 252 return locationHelperIncludes; 253 } 254 255 public void setLocationHelperIncludes(List<String> locationHelperIncludes) { 256 this.locationHelperIncludes = locationHelperIncludes; 257 } 258 259 public List<String> getLocationHelperExcludes() { 260 return locationHelperExcludes; 261 } 262 263 public void setLocationHelperExcludes(List<String> locationHelperExcludes) { 264 this.locationHelperExcludes = locationHelperExcludes; 265 } 266 267 public String getLocationHelperInclude() { 268 return locationHelperInclude; 269 } 270 271 public void setLocationHelperInclude(String locationHelperInclude) { 272 this.locationHelperInclude = locationHelperInclude; 273 } 274 275 public String getLocationHelperExclude() { 276 return locationHelperExclude; 277 } 278 279 public void setLocationHelperExclude(String locationHelperExclude) { 280 this.locationHelperExclude = locationHelperExclude; 281 } 282 283}