001    /**
002     * Copyright 2004-2012 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.maven.common;
017    
018    import java.io.IOException;
019    import java.io.InputStream;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Properties;
023    
024    import org.apache.commons.io.IOUtils;
025    import org.apache.commons.lang.StringUtils;
026    import org.apache.maven.project.MavenProject;
027    import org.springframework.util.PropertyPlaceholderHelper;
028    
029    public class PropertiesUtils {
030        PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
031        ResourceUtils resourceUtils = new ResourceUtils();
032    
033        /**
034         * Split the string trimming as we go
035         */
036        public static String[] splitAndTrim(String csv, String separator) {
037            String[] tokens = StringUtils.split(csv, separator);
038            for (String token : tokens) {
039                token = token.trim();
040            }
041            return tokens;
042        }
043    
044        public String getResolvedValue(String value, Properties properties) {
045            return helper.replacePlaceholders(value, properties);
046        }
047    
048        public Properties getProperties(List<String> locations) throws IOException {
049            Properties properties = new Properties();
050            for (String location : locations) {
051                properties.putAll(getProperties(location));
052            }
053            return properties;
054        }
055    
056        /**
057         * Return properties as viewed by Maven. ie, project properties get overridden by system properties and environment
058         * properties are prefixed with "env"
059         */
060        public Properties getMavenProperties(MavenProject project) {
061            Properties properties = new Properties();
062            properties.putAll(project.getProperties());
063            properties.putAll(getEnvironmentProperties());
064            properties.putAll(System.getProperties());
065            return properties;
066        }
067    
068        public Properties getEnvironmentProperties() {
069            String prefix = "env.";
070            Map<String, String> env = System.getenv();
071            Properties properties = new Properties();
072            for (Map.Entry<String, String> pair : env.entrySet()) {
073                String key = prefix + pair.getKey();
074                String value = pair.getValue();
075                properties.setProperty(key, value);
076            }
077            return properties;
078        }
079    
080        public Properties getProperties(String location) throws IOException {
081            InputStream in = null;
082            try {
083                in = resourceUtils.getInputStream(location);
084                Properties properties = new Properties();
085                if (isXml(location)) {
086                    properties.loadFromXML(in);
087                } else {
088                    properties.load(in);
089                }
090                return properties;
091            } finally {
092                IOUtils.closeQuietly(in);
093            }
094        }
095    
096        public boolean isXml(String location) {
097            return location.toLowerCase().endsWith(".xml");
098        }
099    }