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.config.spring;
017
018import java.util.Properties;
019
020import org.kuali.common.util.maven.MavenConstants;
021import org.kuali.common.util.maven.MavenUtils;
022import org.kuali.common.util.maven.spring.AutowiredMavenProperties;
023import org.kuali.common.util.maven.spring.NoAutowiredMavenProperties;
024import org.kuali.common.util.project.ProjectService;
025import org.kuali.common.util.project.model.Project;
026import org.kuali.common.util.project.spring.ProjectServiceConfig;
027import org.springframework.beans.factory.annotation.Autowired;
028import org.springframework.beans.factory.annotation.Qualifier;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Configuration;
031import org.springframework.context.annotation.Import;
032import org.springframework.util.Assert;
033
034/**
035 * You can use this configuration transparently between the Maven CLI and a normal runtime by injecting <code>projectId</code> into your context.
036 * 
037 * When running from the Maven CLI, <code>projectId</code> is ignored because project properties are pre-injected into the context by spring-maven-plugin.
038 * 
039 * When running in a normal runtime, <code>projectId</code> is used to load a <code>project.properties</code> file.
040 * 
041 * Project Id's are in this format:
042 * 
043 * <pre>
044 *   org.kuali.common:kuali-util
045 * </pre>
046 * 
047 * @deprecated
048 */
049@Configuration
050@Deprecated
051public class ProjectPropertySourceConfig extends BasicPropertySourceConfig {
052
053        private static final String PROJECT_BEAN_NAME = "project.immutable";
054        public static final String PROJECT_ID_BEAN_NAME = "project.id";
055
056        @Autowired
057        @Qualifier(PROJECT_BEAN_NAME)
058        Project project;
059
060        @Override
061        protected Properties getOverrides() {
062                return project.getProperties();
063        }
064
065        /**
066         * @deprecated
067         */
068        @Deprecated
069        @Configuration
070        @NoAutowiredMavenProperties
071        @Import({ ProjectServiceConfig.class })
072        static class RuntimeProjectConfig {
073
074                // Use of this configuration at runtime requires "projectId" wired into the context
075                // Format for "projectId" is [groupId:artifactId], eg "org.kuali.common:kuali-util"
076                @Autowired
077                @Qualifier(PROJECT_ID_BEAN_NAME)
078                String projectId;
079
080                @Autowired
081                ProjectServiceConfig projectServiceConfig;
082
083                @Bean(name = PROJECT_BEAN_NAME)
084                public Project runtimeProjectConfigProject() {
085
086                        // Make sure projectId got wired in correctly
087                        Assert.hasText(projectId, "projectId is blank");
088
089                        // Get a reference to the project service
090                        ProjectService service = projectServiceConfig.projectService();
091
092                        // Use the service to convert the projectId into a Project
093                        return service.getProject("", "");
094                }
095        }
096
097        /**
098         * @deprecated
099         */
100        @Deprecated
101        @Configuration
102        @AutowiredMavenProperties
103        @Import({ ProjectServiceConfig.class })
104        static class BuildProjectConfig {
105
106                // Spring Maven Plugin wires this in for us
107                @Autowired
108                @Qualifier(MavenConstants.PROPERTIES_BEAN_NAME)
109                Properties mavenProperties;
110
111                @Autowired
112                ProjectServiceConfig projectServiceConfig;
113
114                @Bean(name = PROJECT_BEAN_NAME)
115                public Project buildProjectConfigProject() {
116                        // Make sure the maven properties got wired in correctly
117                        Assert.notNull(mavenProperties, "mavenProperties are null");
118
119                        // Get a reference to the project service
120                        ProjectService service = projectServiceConfig.projectService();
121
122                        // Enhance the default set of Maven properties
123                        MavenUtils.augmentProjectProperties(service, mavenProperties);
124
125                        // Use the service to convert the properties into a Project
126                        return service.getProject(mavenProperties);
127                }
128        }
129
130}