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.ArrayList;
020    import java.util.List;
021    import java.util.Properties;
022    
023    import org.kuali.common.util.CollectionUtils;
024    import org.kuali.common.util.execute.Executable;
025    import org.kuali.common.util.execute.ExecutablesExecutable;
026    import org.kuali.common.util.execute.StorePropertiesExecutable;
027    import org.kuali.common.util.execute.StoreRicePropertiesExecutable;
028    import org.kuali.common.util.property.Constants;
029    import org.springframework.beans.factory.annotation.Autowired;
030    import org.springframework.beans.factory.annotation.Value;
031    import org.springframework.context.annotation.Bean;
032    import org.springframework.context.annotation.Configuration;
033    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
034    import org.springframework.core.env.ConfigurableEnvironment;
035    import org.springframework.util.Assert;
036    
037    /**
038     * Create project.properties and embed it inside META-INF for jar's/war's
039     */
040    @Configuration
041    public class MetaInfProjectPropertiesConfig {
042    
043            @Autowired
044            ConfigurableEnvironment env;
045    
046            @Bean
047            public static PropertySourcesPlaceholderConfigurer pspc() {
048                    return new PropertySourcesPlaceholderConfigurer();
049            }
050    
051            @Value(Constants.PROJECT_PROPERTIES_OUTPUTFILE)
052            File outputFile;
053    
054            @Value(Constants.RICE_PROJECT_PROPERTIES_OUTPUTFILE)
055            File riceOutputFile;
056    
057            @Bean
058            public Properties springProperties() {
059                    return SpringUtils.getAllEnumerableProperties(env);
060            }
061    
062            @Bean(initMethod = "execute")
063            public Executable storePropertiesExecutablesExectuable() {
064    
065                    // Extract property values from the environment
066                    String encoding = SpringUtils.getProperty(env, "project.encoding");
067                    String includesCSV = SpringUtils.getProperty(env, "project.metainf.includes");
068                    String excludesCSV = SpringUtils.getProperty(env, "project.metainf.excludes");
069    
070                    // Make sure we are configured right
071                    Assert.hasText(encoding);
072    
073                    // Convert the lists to CSV
074                    List<String> includes = CollectionUtils.getTrimmedListFromCSV(includesCSV);
075                    List<String> excludes = CollectionUtils.getTrimmedListFromCSV(excludesCSV);
076    
077                    // Get the list of all properties spring knows about
078                    Properties properties = springProperties();
079    
080                    // Setup the regular properties file executable
081                    StorePropertiesExecutable spe = new StorePropertiesExecutable();
082                    spe.setEncoding(encoding);
083                    spe.setOutputFile(outputFile);
084                    spe.setProperties(properties);
085                    spe.setIncludes(includes);
086                    spe.setExcludes(excludes);
087    
088                    // Setup the Rice style properties file executable
089                    StoreRicePropertiesExecutable srpe = new StoreRicePropertiesExecutable();
090                    srpe.setEncoding(encoding);
091                    srpe.setOutputFile(riceOutputFile);
092                    srpe.setProperties(properties);
093                    srpe.setIncludes(includes);
094                    srpe.setExcludes(excludes);
095    
096                    // Create an executables list
097                    List<Executable> executables = new ArrayList<Executable>();
098                    executables.add(spe);
099                    executables.add(srpe);
100    
101                    // Return an executable that executes the list
102                    return new ExecutablesExecutable(executables);
103            }
104    
105    }