View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.spring;
17  
18  import java.util.Collections;
19  import java.util.List;
20  
21  import org.kuali.common.util.CollectionUtils;
22  import org.kuali.common.util.spring.service.PropertySourceContext;
23  import org.kuali.common.util.spring.service.SpringContext;
24  import org.kuali.common.util.spring.service.SpringService;
25  import org.springframework.core.env.PropertySource;
26  
27  public class SpringExecUtils {
28  
29  	/**
30  	 * Return an executable that resolves all placeholder values against <code>source</code>.
31  	 */
32  	public static SpringExecutable getSpringExecutable(SpringService service, PropertySource<?> source, Class<?> annotatedClass, List<String> activeProfiles) {
33  		SpringContext context = getSinglePropertySourceContext(source);
34  		context.setActiveProfiles(activeProfiles);
35  		context.setAnnotatedClasses(CollectionUtils.asList(annotatedClass));
36  		return new SpringExecutable(service, context);
37  	}
38  
39  	/**
40  	 * Return an executable that resolves all placeholder values against <code>source</code>.
41  	 */
42  	public static SpringExecutable getSpringExecutable(SpringService service, PropertySource<?> source, Class<?> annotatedClass) {
43  		return getSpringExecutable(service, source, annotatedClass, Collections.<String> emptyList());
44  	}
45  
46  	/**
47  	 * Return a SpringExecutable for the PropertySource and annotatedClass passed in
48  	 */
49  	@Deprecated
50  	public static SpringExecutable getSpringExecutable(PropertySource<?> source, Class<?> annotatedClass) {
51  		return getSpringExecutable(SpringExecutable.DEFAULT_SPRING_SERVICE, source, annotatedClass);
52  	}
53  
54  	/**
55  	 * Return a SpringContext that resolves all placeholders from the PropertySource passed in
56  	 */
57  	public static SpringContext getSinglePropertySourceContext(PropertySource<?> source) {
58  		// Set things up such that this PropertySource is the only one registered with Spring
59  		// This PropertySource will be the ONLY thing used to resolve placeholders
60  		PropertySourceContext psc = new PropertySourceContext(source, true);
61  
62  		// Return a Spring context configured with the PropertySourceContext
63  		return new SpringContext(psc);
64  	}
65  
66  }