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.spring;
017
018import java.util.Collections;
019import java.util.List;
020
021import org.kuali.common.util.CollectionUtils;
022import org.kuali.common.util.spring.service.PropertySourceContext;
023import org.kuali.common.util.spring.service.SpringContext;
024import org.kuali.common.util.spring.service.SpringService;
025import org.springframework.core.env.PropertySource;
026
027public class SpringExecUtils {
028
029        /**
030         * Return an executable that resolves all placeholder values against <code>source</code>.
031         */
032        public static SpringExecutable getSpringExecutable(SpringService service, PropertySource<?> source, Class<?> annotatedClass, List<String> activeProfiles) {
033                SpringContext context = getSinglePropertySourceContext(source);
034                context.setActiveProfiles(activeProfiles);
035                context.setAnnotatedClasses(CollectionUtils.asList(annotatedClass));
036                return new SpringExecutable(service, context);
037        }
038
039        /**
040         * Return an executable that resolves all placeholder values against <code>source</code>.
041         */
042        public static SpringExecutable getSpringExecutable(SpringService service, PropertySource<?> source, Class<?> annotatedClass) {
043                return getSpringExecutable(service, source, annotatedClass, Collections.<String> emptyList());
044        }
045
046        /**
047         * Return a SpringExecutable for the PropertySource and annotatedClass passed in
048         */
049        @Deprecated
050        public static SpringExecutable getSpringExecutable(PropertySource<?> source, Class<?> annotatedClass) {
051                return getSpringExecutable(SpringExecutable.DEFAULT_SPRING_SERVICE, source, annotatedClass);
052        }
053
054        /**
055         * Return a SpringContext that resolves all placeholders from the PropertySource passed in
056         */
057        public static SpringContext getSinglePropertySourceContext(PropertySource<?> source) {
058                // Set things up such that this PropertySource is the only one registered with Spring
059                // This PropertySource will be the ONLY thing used to resolve placeholders
060                PropertySourceContext psc = new PropertySourceContext(source, true);
061
062                // Return a Spring context configured with the PropertySourceContext
063                return new SpringContext(psc);
064        }
065
066}