All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jeesuite.spring.helper.EnvironmentHelper Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.jeesuite.spring.helper;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;

import com.jeesuite.spring.InstanceFactory;

public class EnvironmentHelper {

	private static Environment environment;
	
	public static String getProperty(String key){
		init();
		return environment == null ? null : environment.getProperty(key);
	}

	public static void init() {
		if(environment == null && InstanceFactory.isInitialized()){
			synchronized (EnvironmentHelper.class) {
				environment = InstanceFactory.getInstance(Environment.class);
			}
		}
	}
	
	public static boolean containsProperty(String key){
		init();
		return environment == null ? false : environment.containsProperty(key);
	}
	
	
	public static Map getAllProperties(String prefix){
		init();
		if(environment == null)return null;
		MutablePropertySources propertySources = ((ConfigurableEnvironment)environment).getPropertySources();
		
		Map properties = new LinkedHashMap();
		for (PropertySource source : propertySources) {
			if(source.getName().startsWith("servlet") || source.getName().startsWith("system")){
				continue;
			}
			if (source instanceof EnumerablePropertySource) {
				for (String name : ((EnumerablePropertySource) source) .getPropertyNames()) {
					boolean match = StringUtils.isEmpty(prefix);
					if(!match){
						match = name.startsWith(prefix);
					}
					if(match){						
						Object value = source.getProperty(name);
						if(value != null){
							properties.put(name, value);
						}
					}
				}
			}
		}
		return Collections.unmodifiableMap(properties);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy