001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.core.config;
021
022import org.apache.commons.configuration.Configuration;
023import org.apache.commons.io.FileUtils;
024import org.apache.commons.io.IOUtils;
025import org.apache.commons.lang.text.StrSubstitutor;
026
027import java.io.File;
028import java.io.FileInputStream;
029import java.io.IOException;
030import java.io.InputStream;
031import java.util.Enumeration;
032import java.util.Map;
033import java.util.Properties;
034
035/**
036 * @since 2.12
037 */
038public final class ConfigurationUtils {
039
040  private ConfigurationUtils() {
041  }
042
043  public static void copyProperties(Properties from, Map<String, String> to) {
044    for (Map.Entry<Object, Object> entry : from.entrySet()) {
045      String key = (String) entry.getKey();
046      to.put(key, entry.getValue().toString());
047    }
048  }
049
050  public static Properties openProperties(File file) throws IOException {
051    FileInputStream input = FileUtils.openInputStream(file);
052    return readInputStream(input);
053  }
054
055  /**
056   * Note that the input stream is closed in this method.
057   */
058  public static Properties readInputStream(InputStream input) throws IOException {
059    try {
060      Properties p = new Properties();
061      p.load(input);
062      return p;
063
064    } finally {
065      IOUtils.closeQuietly(input);
066    }
067  }
068
069  public static Properties interpolateEnvVariables(Properties properties) {
070    return interpolateVariables(properties, System.getenv());
071  }
072
073  public static Properties interpolateVariables(Properties properties, Map<String, String> variables) {
074    Properties result = new Properties();
075    Enumeration keys = properties.keys();
076    while (keys.hasMoreElements()) {
077      String key = (String) keys.nextElement();
078      String value = (String) properties.get(key);
079      String interpolatedValue = StrSubstitutor.replace(value, variables, "${env:", "}");
080      result.setProperty(key, interpolatedValue);
081    }
082    return result;
083  }
084
085  public static void copyToCommonsConfiguration(Map<String, String> input, Configuration commonsConfig) {
086    // update deprecated configuration
087    commonsConfig.clear();
088    for (Map.Entry<String, String> entry : input.entrySet()) {
089      String key = entry.getKey();
090      commonsConfig.setProperty(key, entry.getValue());
091    }
092  }
093}