001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 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 */
020 package org.sonar.core.config;
021
022 import org.apache.commons.configuration.Configuration;
023 import org.apache.commons.io.FileUtils;
024 import org.apache.commons.io.IOUtils;
025 import org.apache.commons.lang.text.StrSubstitutor;
026 import org.sonar.api.database.DatabaseSession;
027 import org.sonar.api.database.configuration.Property;
028 import org.sonar.api.database.model.ResourceModel;
029 import org.sonar.jpa.session.DatabaseSessionFactory;
030
031 import java.io.File;
032 import java.io.FileInputStream;
033 import java.io.IOException;
034 import java.io.InputStream;
035 import java.util.*;
036
037 /**
038 * @since 2.12
039 */
040 public final class ConfigurationUtils {
041
042 private ConfigurationUtils() {
043 }
044
045 public static void copyProperties(Properties from, Map<String, String> to) {
046 for (Map.Entry<Object, Object> entry : from.entrySet()) {
047 String key = (String) entry.getKey();
048 to.put(key, entry.getValue().toString());
049 }
050 }
051
052 public static Properties openProperties(File file) throws IOException {
053 FileInputStream input = FileUtils.openInputStream(file);
054 return readInputStream(input);
055 }
056
057 /**
058 * Note that the input stream is closed in this method.
059 */
060 public static Properties readInputStream(InputStream input) throws IOException {
061 try {
062 Properties p = new Properties();
063 p.load(input);
064 return p;
065
066 } finally {
067 IOUtils.closeQuietly(input);
068 }
069 }
070
071 public static Properties interpolateEnvVariables(Properties properties) {
072 return interpolateVariables(properties, System.getenv());
073 }
074
075 public static Properties interpolateVariables(Properties properties, Map<String, String> variables) {
076 Properties result = new Properties();
077 Enumeration keys = properties.keys();
078 while (keys.hasMoreElements()) {
079 String key = (String) keys.nextElement();
080 String value = (String) properties.get(key);
081 String interpolatedValue = StrSubstitutor.replace(value, variables, "${env:", "}");
082 result.setProperty(key, interpolatedValue);
083 }
084 return result;
085 }
086
087 public static List<Property> getProjectProperties(DatabaseSessionFactory dbFactory, String moduleKey) {
088 DatabaseSession session = prepareDbSession(dbFactory);
089 ResourceModel resource = session.getSingleResult(ResourceModel.class, "key", moduleKey);
090 if (resource != null) {
091 return session
092 .createQuery("from " + Property.class.getSimpleName() + " p where p.resourceId=:resourceId and p.userId is null")
093 .setParameter("resourceId", resource.getId())
094 .getResultList();
095
096 }
097 return Collections.emptyList();
098 }
099
100 public static List<Property> getGeneralProperties(DatabaseSessionFactory dbFactory) {
101 DatabaseSession session = prepareDbSession(dbFactory);
102 return session
103 .createQuery("from " + Property.class.getSimpleName() + " p where p.resourceId is null and p.userId is null")
104 .getResultList();
105
106 }
107
108 private static DatabaseSession prepareDbSession(DatabaseSessionFactory dbFactory) {
109 DatabaseSession session = dbFactory.getSession();
110 // Ugly workaround before the move to myBatis
111 // Session is not up-to-date when Ruby on Rails inserts new rows in its own transaction. Seems like
112 // Hibernate keeps a cache...
113 session.commit();
114 return session;
115 }
116
117 public static void copyToCommonsConfiguration(Map<String,String> input, Configuration commonsConfig) {
118 // update deprecated configuration
119 commonsConfig.clear();
120 for (Map.Entry<String, String> entry : input.entrySet()) {
121 String key = entry.getKey();
122 commonsConfig.setProperty(key, entry.getValue());
123 }
124 }
125 }