001    /**
002     * Copyright 2010-2013 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     */
016    package org.kuali.common.util.spring;
017    
018    import java.io.File;
019    import java.util.List;
020    import java.util.Properties;
021    
022    import org.kuali.common.util.CollectionUtils;
023    import org.kuali.common.util.execute.Executable;
024    import org.kuali.common.util.execute.StorePropertiesExecutable;
025    import org.kuali.common.util.property.Constants;
026    import org.springframework.beans.factory.annotation.Autowired;
027    import org.springframework.beans.factory.annotation.Value;
028    import org.springframework.context.annotation.Bean;
029    import org.springframework.context.annotation.Configuration;
030    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
031    import org.springframework.core.env.ConfigurableEnvironment;
032    import org.springframework.util.Assert;
033    
034    /**
035     * Create project.properties and embed it inside META-INF for jar's/war's
036     */
037    @Configuration
038    public class MetaInfProjectPropertiesConfig {
039    
040            @Autowired
041            ConfigurableEnvironment env;
042    
043            @Bean
044            public static PropertySourcesPlaceholderConfigurer pspc() {
045                    return new PropertySourcesPlaceholderConfigurer();
046            }
047    
048            @Value(Constants.PROJECT_PROPERTIES_OUTPUTFILE)
049            File outputFile;
050    
051            @Bean
052            public Properties springProperties() {
053                    return SpringUtils.getAllEnumerableProperties(env);
054            }
055    
056            @Bean(initMethod = "execute")
057            public Executable storePropertiesExecutable() {
058    
059                    // Extract property values from the environment
060                    String encoding = SpringUtils.getProperty(env, "project.encoding");
061                    String includesCSV = SpringUtils.getProperty(env, "project.metainf.includes");
062                    String excludesCSV = SpringUtils.getProperty(env, "project.metainf.excludes");
063    
064                    // Make sure we are configured right
065                    Assert.hasText(encoding);
066    
067                    // Convert the lists to CSV
068                    List<String> includes = CollectionUtils.getTrimmedListFromCSV(includesCSV);
069                    List<String> excludes = CollectionUtils.getTrimmedListFromCSV(excludesCSV);
070    
071                    // Get the list of all properties spring knows about
072                    Properties properties = springProperties();
073    
074                    // Setup the executable
075                    StorePropertiesExecutable spe = new StorePropertiesExecutable();
076                    spe.setEncoding(encoding);
077                    spe.setOutputFile(outputFile);
078                    spe.setProperties(properties);
079                    spe.setIncludes(includes);
080                    spe.setExcludes(excludes);
081                    return spe;
082            }
083    
084    }