001/**
002 * Copyright 2005-2016 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.rice.deploy.spring;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.kuali.common.deploy.env.model.DeployEnvironment;
022import org.kuali.common.deploy.env.spring.DefaultDeployEnvironmentConfig;
023import org.kuali.common.deploy.env.spring.DeployEnvironmentConfig;
024import org.kuali.common.deploy.project.DeployProjectConstants;
025import org.kuali.common.jdbc.project.spring.JdbcProjectConfig;
026import org.kuali.common.jdbc.project.spring.JdbcPropertyLocationsConfig;
027import org.kuali.common.util.Assert;
028import org.kuali.common.util.Mode;
029import org.kuali.common.util.project.ProjectService;
030import org.kuali.common.util.project.ProjectUtils;
031import org.kuali.common.util.project.model.Project;
032import org.kuali.common.util.project.model.ProjectIdentifier;
033import org.kuali.common.util.project.spring.AutowiredProjectConfig;
034import org.kuali.common.util.project.spring.ProjectServiceConfig;
035import org.kuali.common.util.properties.Location;
036import org.kuali.common.util.properties.PropertiesLocationService;
037import org.kuali.common.util.properties.PropertiesService;
038import org.kuali.common.util.properties.spring.DefaultPropertiesServiceConfig;
039import org.kuali.common.util.properties.spring.PropertiesLocationServiceConfig;
040import org.kuali.common.util.property.PropertyFormat;
041import org.kuali.common.util.spring.PropertySourceUtils;
042import org.kuali.common.util.spring.service.PropertySourceConfig;
043import org.kuali.rice.deploy.RiceDeployProjectConstants;
044import org.springframework.beans.factory.annotation.Autowired;
045import org.springframework.context.annotation.Bean;
046import org.springframework.context.annotation.Configuration;
047import org.springframework.context.annotation.Import;
048import org.springframework.core.env.PropertySource;
049
050@Configuration
051@Import({ JdbcProjectConfig.class, JdbcPropertyLocationsConfig.class, DefaultPropertiesServiceConfig.class, PropertiesLocationServiceConfig.class, AutowiredProjectConfig.class,
052                DefaultDeployEnvironmentConfig.class, ProjectServiceConfig.class })
053public class DeployPSC implements PropertySourceConfig {
054
055        private static final ProjectIdentifier DEPLOY = DeployProjectConstants.ID;
056
057        @Autowired
058        JdbcPropertyLocationsConfig jdbc;
059
060        @Autowired
061        PropertiesService service;
062
063        @Autowired
064        PropertiesLocationService locationService;
065
066        @Autowired
067        Project project;
068
069        @Autowired
070        ProjectService projectService;
071
072        @Autowired
073        DeployEnvironmentConfig deployEnvConfig;
074
075        @Override
076        @Bean
077        public PropertySource<?> propertySource() {
078
079                DeployEnvironment deployEnv = deployEnvConfig.deployEnvironment();
080
081                // Generic jdbc locations
082                List<Location> jdbcLocations = jdbc.jdbcPropertyLocations();
083
084                // Pull in configuration specific to this branch of Rice
085                Location branchLoc = getOptionalLocation(RiceDeployProjectConstants.ID, "deploy.properties");
086
087                // Extract the group code
088                String groupCode = project.getProperties().getProperty("project.groupId.code");
089                Assert.noBlanks(groupCode);
090
091                // Pull in configuration specific to this Rice application
092                Location appLoc = getOptionalLocation(DEPLOY, groupCode + "/" + project.getArtifactId() + ".properties");
093
094                // Pull in configuration specific to the environment we are deploying to
095                Location envLoc = getOptionalLocation(DEPLOY, groupCode + "/" + deployEnv.getName() + ".properties");
096
097                // Combine them making sure Rice properties go in last
098                List<Location> locations = new ArrayList<Location>();
099                locations.addAll(jdbcLocations);
100                locations.addAll(getKualiDeployLocs());
101                locations.add(branchLoc);
102                locations.add(appLoc);
103                locations.add(envLoc);
104                return PropertySourceUtils.getPropertySource(service, locations);
105        }
106
107        protected Location getOptionalLocation(ProjectIdentifier pid, String filename) {
108                String value = ProjectUtils.getClasspathPrefix(pid) + "/" + filename;
109                Project project = projectService.getProject(pid);
110                String encoding = ProjectUtils.getEncoding(project);
111                return new Location(value, encoding, Mode.INFORM, PropertyFormat.NORMAL, true);
112
113        }
114
115        protected List<Location> getKualiDeployLocs() {
116                List<Location> locs = new ArrayList<Location>();
117                locs.add(getKualiDeployLoc("common.properties"));
118                locs.add(getKualiDeployLoc("appdynamics.properties"));
119                locs.add(getKualiDeployLoc("aws.properties"));
120                locs.add(getKualiDeployLoc("tomcat.properties"));
121                locs.add(getKualiDeployLoc("db.properties"));
122                locs.add(getKualiDeployLoc("rice/common.properties"));
123                locs.add(getKualiDeployLoc("rice/db.properties"));
124                locs.add(getKualiDeployLoc("rice/aws.properties"));
125                locs.add(getKualiDeployLoc("rice/appdynamics.properties"));
126                return locs;
127        }
128
129        protected Location getKualiDeployLoc(String filename) {
130                return locationService.getLocation(DEPLOY, filename);
131        }
132
133}