-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support environment variables and system properties in configuration files #716
Merged
guwirth
merged 1 commit into
SonarOpenCommunity:master
from
guwirth:enhancement/environment-variables
Dec 30, 2015
+295
−117
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/CxxSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2010 Neticoa SAS France | ||
* [email protected] | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.plugins.cxx; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Properties; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.sonar.api.config.Settings; | ||
|
||
/** | ||
* CxxSettings supports variables in configuration files | ||
* | ||
* - CxxSettings clones Settings in ctor | ||
* - format for placeholder is ${xxx} - supported are environment variables, | ||
* Java system properties and SonarQube properties | ||
* - backslashes in values from environment variables and Java system properties | ||
* are replaced with slashes to support Windows paths | ||
*/ | ||
public class CxxSettings extends Settings { | ||
|
||
private final HashMap<String, String> vars = new HashMap<>(); | ||
private final Pattern regex = Pattern.compile("\\$\\{(.+?)\\}"); | ||
|
||
/** | ||
* Clone settings. | ||
*/ | ||
public CxxSettings(Settings other) { | ||
super(other); | ||
|
||
Map<String, String> envMap = System.getenv(); | ||
for (Entry<String, String> entry : envMap.entrySet()) { | ||
String key = entry.getKey(); | ||
String value = entry.getValue(); | ||
value = value.replace("\\", "/"); | ||
vars.put(key, value); | ||
} | ||
|
||
Properties props = System.getProperties(); | ||
for (String key : props.stringPropertyNames()) { | ||
String value = props.getProperty(key); | ||
value = value.replace("\\", "/"); | ||
vars.put(key, value); | ||
} | ||
|
||
vars.putAll(other.getProperties()); | ||
} | ||
|
||
@Override | ||
protected String getClearString(String key) { | ||
String value = super.getClearString(key); | ||
if (value != null) { | ||
value = expandProperties(value); | ||
} | ||
return value; | ||
} | ||
|
||
/** | ||
* expand properties in string | ||
*/ | ||
private String expandProperties(String text) { | ||
Matcher m = regex.matcher(text); | ||
String result = text; | ||
while (m.find()) { | ||
String key = m.group(1); | ||
String value = vars.get(key); | ||
if (value != null) { | ||
result = result.replace(m.group(), value); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
protected void doOnSetProperty(String key, String value) { | ||
if (value != null) { | ||
vars.put(key, value); | ||
} | ||
} | ||
|
||
@Override | ||
protected void doOnRemoveProperty(String key) { | ||
vars.remove(key); | ||
} | ||
|
||
@Override | ||
protected void doOnClearProperties() { | ||
vars.clear(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wonder if the internals of Settings will be in proper state when it arrives to the plugin. i would assume core to create its own instance of Settings class, likely when CxxSettings gets into the plugin it comes without any information about the settings.
perhaps this class cannot be used like this, and might explain the issue in server side. Could this be done in a helper class that receives Settings and then returns the value as per you implementation below?