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