View Javadoc

1   package net.sf.prefixedproperties.spring.environmentfactories;
2   
3   import java.util.Collections;
4   import java.util.List;
5   
6   import org.apache.commons.lang.StringUtils;
7   
8   import net.sf.prefixedproperties.spring.EnvironmentFactory;
9   
10  /**
11   * Takes the first environment from the given list of EnvironmentFactories which is not null.<br>
12   * The order of the list will be the priority.<br>
13   * 
14   * You could build up an cascaded setup where first you look for an property in an properties file, than for an system property and than you use a static one.
15   * 
16   */
17  public class FallbackEnvironmentFactory implements EnvironmentFactory {
18  
19  	private List<EnvironmentFactory> environmentFactories = Collections.emptyList();
20  	
21  	@Override
22  	public String getEnvironment() {
23  		for (EnvironmentFactory environmentFactory : getEnvironmentFactories()) {
24  			String environment = environmentFactory.getEnvironment();
25  			if (StringUtils.isNotBlank(environment)){
26  				return environment;
27  			}
28  		}
29  		return null;
30  	}
31  
32  	public List<EnvironmentFactory> getEnvironmentFactories() {
33  		return environmentFactories;
34  	}
35  
36  	public void setEnvironmentFactories(List<EnvironmentFactory> environmentFactories) {
37  		this.environmentFactories = environmentFactories;
38  	}
39  
40  }