Skip to content

Instantly share code, notes, and snippets.

@asamaraw
Last active August 29, 2015 14:01
Replace placeholders of the dropwizard yml file with values fetched from azure worker role configuration settings at the dw service startup
public class AzureYamlConfigurator
{
final static Charset ENCODING = StandardCharsets.UTF_8;
String configureYaml(String fileName) throws IOException
{
if (RoleEnvironment.isAvailable())
{
Map<String, String> propMap = RoleEnvironment.getConfigurationSettings();
configureYaml(fileName, propMap);
return fileName + YML_POST_FIX;
}
else
{
return fileName;
}
}
void configureYaml(String fileName, Map<String, String> propMap) throws IOException
{
List<String> lines = new ArrayList<String>();
for(String line : readYamlFile(fileName))
{
lines.add(replacePlaceHolders(line, propMap));
}
writeYamlFile(lines, fileName);
}
String replacePlaceHolders(String text, Map<String, String> props)
{
Scanner scanner = new Scanner(text);
Pattern pattern = Pattern.compile("\\&([a-z].+)");
StringBuffer stringBuffer = new StringBuffer();
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
Matcher matcher = pattern.matcher(line);
while (matcher.find())
{
String key = matcher.group(1);
String replacement = props.get(key);
if (replacement == null)
{
// Not a recognized token that can be replaced. Is that an error? Maybe.
// We can throw an exception, log it, or just put back the original matched expression:
replacement = matcher.group();
}
matcher.appendReplacement(stringBuffer, replacement);
}
matcher.appendTail(stringBuffer);
}
scanner.close();
return stringBuffer.toString();
}
private List<String> readYamlFile(String fileName) throws IOException
{
Path path = Paths.get(fileName);
return Files.readAllLines(path, ENCODING);
}
private void writeYamlFile(List<String> lines, String fileName) throws IOException
{
Path path = Paths.get(fileName + YML_POST_FIX);
Files.write(path, lines, ENCODING);
}
}
public class DropwizardApplication extends Application<AppConfiguration>
{
public static void main(final String[] args)
throws Exception // NOPMD: SignatureDeclareThrowsException
{
args[1] = new AzureYamlConfigurator().configureYaml(args[1]);
new DropwizardApplication().run(args);
}
@Override
public void initialize(final Bootstrap<AppConfiguration> bootstrap)
{
bootstrap.addBundle(new AssetsBundle("/assets", "/"));
bootstrap.addBundle(new AssetsBundle("/assets"));
}
@Override
public String getName()
{
return "iam-provisioning-service";
}
@Override
public void run(final AppConfiguration configuration, final Environment environment)
throws SecurityConfigurationException
{
server:
applicationConnectors:
- type: https
port: 8080
keyStoreType: Windows-MY
certAlias: &ssl.cert.alias
validateCerts: false
adminConnectors:
- type: http
port: 8081
requestLog:
appenders:
- type: console
threshold: OFF
- type: syslog
threshold: OFF
- type: file
currentLogFilename: ./logs/provisioningService-requestLog.log
archivedLogFilenamePattern: ./logs/provisioningService-requestLog-%d.log.gz
archivedFileCount: 5
timeZone: UTC
rootPath: /rest/*
keyStore:
secureCredentialsKeyAlias: &secure.credentials.key.alias
# Logging settings.
logging:
# The default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
level: &log.level
# Uncomment to enable ContextRetrievalFilter to trace every request
# loggers:
# "com.prosrm.iam.provisioning.service.filter.ContextRetrievalFilter": DEBUG
appenders:
- type: file
threshold: &log.level
currentLogFilename: ./logs/provisioningService.log
archivedLogFilenamePattern: ./logs/provisioningService-%d.log.gz
archivedFileCount: 5
timeZone: UTC
logFormat: "|%date{ISO8601,UTC}|%-5level|%mdc{pros-request-id:-na}|%logger{40}|%mdc{pros-request-id}|%message|%n%rootException"
...
<ConfigurationSettings>
...
<Setting name="ssl.cert.alias" value="sslCertAliasValue" />
<Setting name="secure.credentials.key.alias" value="KeyAlias" />
<Setting name="log.level" value="INFO" />
<Setting name="security.config.application.secret.encrypted" value="BlaBla" />
</ConfigurationSettings>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment