Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2010-2013 The Kuali Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ecl2.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kuali.common.util.spring;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.codehaus.plexus.util.StringUtils;
import org.jasypt.util.text.TextEncryptor;
import org.kuali.common.util.Assert;
import org.kuali.common.util.EncUtils;
import org.kuali.common.util.EncryptionStrength;
import org.kuali.common.util.LocationUtils;
import org.kuali.common.util.LoggerLevel;
import org.kuali.common.util.LoggerUtils;
import org.kuali.common.util.Project;
import org.kuali.common.util.ProjectUtils;
import org.kuali.common.util.PropertyUtils;
import org.kuali.common.util.Str;
import org.kuali.common.util.execute.Executable;
import org.kuali.common.util.execute.SpringExecutable;
import org.kuali.common.util.property.Constants;
import org.kuali.common.util.property.ProjectProperties;
import org.kuali.common.util.property.processor.ResolvePlaceholdersProcessor;
import org.kuali.common.util.service.DefaultSpringService;
import org.kuali.common.util.service.PropertySourceContext;
import org.kuali.common.util.service.SpringContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
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.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.util.PropertyPlaceholderHelper;
public class SpringUtils {
private static final Logger logger = LoggerFactory.getLogger(SpringUtils.class);
// Configure a helper that will fail on any unresolved placeholders
private static final PropertyPlaceholderHelper HELPER = new PropertyPlaceholderHelper("${", "}", ":", false);
public static Executable getSpringExecutable(Environment env, boolean skip, PropertySource> ps, List> annotatedClasses) {
/**
* This line creates a property source containing 100% of the properties needed by Spring to resolve any/all placeholders. It will be the only property source available to
* Spring so it needs to include system properties and environment variables
*/
PropertySourceContext psc = new PropertySourceContext(ps, true);
// Setup the Spring context
SpringContext context = new SpringContext();
context.setAnnotatedClasses(annotatedClasses);
context.setPropertySourceContext(psc);
// Load the context
SpringExecutable se = new SpringExecutable();
se.setService(new DefaultSpringService());
se.setContext(context);
se.setSkip(skip);
return se;
}
public static boolean getBoolean(Environment env, String key, boolean defaultValue) {
String value = getProperty(env, key, defaultValue + "");
return new Boolean(value);
}
public static PropertySource> getPropertySource(String name, List pps) {
// Load them from disk
Properties source = PropertyUtils.load(pps);
// Prepare them so they are ready for use
prepareContextProperties(source);
// Return a PropertySource backed by the properties
return new PropertiesPropertySource(name, source);
}
public static String getRequiredResolvedProperty(Properties properties, String key) {
return getRequiredResolvedProperty(properties, key, null);
}
public static String getRequiredResolvedProperty(Properties properties, String key, String defaultValue) {
String value = properties.getProperty(key);
value = StringUtils.isBlank(value) ? defaultValue : value;
if (StringUtils.isBlank(value)) {
throw new IllegalArgumentException("[" + key + "] is not set");
} else {
return HELPER.replacePlaceholders(value, properties);
}
}
/**
* Process the properties passed in so they are ready for use by a Spring context.
*
* 1 - Override with system/environment properties
* 2 - Decrypt any ENC(...) values
* 3 - Resolve all property values throwing an exception if any are unresolvable.
*/
public static void prepareContextProperties(Properties properties) {
// Override with system/environment properties
properties.putAll(PropertyUtils.getGlobalProperties());
// Are we decrypting property values?
boolean decrypt = new Boolean(getRequiredResolvedProperty(properties, "properties.decrypt", "false"));
if (decrypt) {
// If they asked to decrypt, a password is required
String password = getRequiredResolvedProperty(properties, "properties.enc.password");
// Strength is optional (defaults to BASIC)
String defaultStrength = EncryptionStrength.BASIC.name();
String strength = getRequiredResolvedProperty(properties, "properties.enc.strength", defaultStrength);
EncryptionStrength es = EncryptionStrength.valueOf(strength);
TextEncryptor decryptor = EncUtils.getTextEncryptor(es, password);
PropertyUtils.decrypt(properties, decryptor);
}
// Are we resolving placeholders?
boolean resolve = new Boolean(getRequiredResolvedProperty(properties, "properties.resolve", "true"));
if (resolve) {
ResolvePlaceholdersProcessor rpp = new ResolvePlaceholdersProcessor();
rpp.setHelper(HELPER);
rpp.process(properties);
}
}
/**
* Converts a GAV into Spring's classpath style notation for the default project properties context.
*
*
*/
public static String getDefaultPropertyContextLocation(String gav) {
Assert.hasText(gav, "gav has no text");
Project p = ProjectUtils.getProject(gav);
return "classpath:" + Str.getPath(p.getGroupId()) + "/" + p.getArtifactId() + "-properties-context.xml";
}
/**
* Make sure all of the locations actually exist
*/
public static void validateExists(List locations) {
StringBuilder sb = new StringBuilder();
for (String location : locations) {
if (!LocationUtils.exists(location)) {
sb.append("Location [" + location + "] does not exist\n");
}
}
if (sb.length() > 0) {
throw new IllegalArgumentException(sb.toString());
}
}
public static ConfigurableApplicationContext getContextWithPreRegisteredBeans(String id, String displayName, List beanNames, List