Skip to content

Commit

Permalink
Move system property overwriting to where its used
Browse files Browse the repository at this point in the history
Tuned up #161 a bit. Also, added a unit test.
  • Loading branch information
rhuss committed May 13, 2015
1 parent 59ff8e1 commit 23762ae
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
5 changes: 1 addition & 4 deletions src/main/java/org/jolokia/docker/maven/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ public void executeInternal(DockerAccess docker) throws DockerAccessException, M
checkImageWithAutoPull(docker, imageName, getRegistry(imageConfig));

RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
Properties properties = new Properties();
properties.putAll(project.getProperties());
properties.putAll(System.getProperties());
PortMapping mappedPorts = getPortMapping(runConfig, properties);
PortMapping mappedPorts = getPortMapping(runConfig, project.getProperties());

String name = calculateContainerName(imageConfig.getAlias(), runConfig.getNamingStrategy());
ContainerCreateConfig config = createContainerConfig(docker, imageName, runConfig, mappedPorts);
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/org/jolokia/docker/maven/access/PortMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,34 @@ public void updateVariablesWithDynamicPorts(Map<String, Integer> dockerObtainedD
}

// Check for a variable containing a port, return it as integer or <code>null</code> is not found or not a number
private Integer getPortFromVariable(Properties variables, String var) {
// First check system properties, then the variables given
private Integer getPortFromVariableOrSystemProperty(Properties variables, String var) {
String sysProp = System.getProperty(var);
if (sysProp != null) {
return getAsIntOrNull(sysProp);
}
if (variables.containsKey(var)) {
try {
return Integer.parseInt(variables.getProperty(var));
} catch (NumberFormatException exp) {
return null;
}
}

return getAsIntOrNull(variables.getProperty(var));
}
return null;
}

private void mapPorts(String bindToHost, String hPort,String containerPortSpec, Properties variables) {

private Integer getAsIntOrNull(String val) {
try {
return Integer.parseInt(val);
} catch (NumberFormatException exp) {
return null;
}
}

private void mapPorts(String bindToHost, String hPort,String containerPortSpec, Properties variables) {
Integer hostPort;
try {
hostPort = Integer.parseInt(hPort);
} catch (NumberFormatException exp) {
String varName = hPort;
// Port should be dynamically assigned and set to the variable give in hPort
hostPort = getPortFromVariable(variables, varName);
hostPort = getPortFromVariableOrSystemProperty(variables, varName);
if (hostPort != null) {
// hPort: Variable name, hostPort: Port coming from the variable
portVariables.put(varName, hostPort);
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/org/jolokia/docker/maven/access/PortMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,25 @@ public void invalidProtocol() {
}

@Test
public void variableReplacementWitProps() throws MojoExecutionException {
public void variableReplacementWithProps() throws MojoExecutionException {
PortMapping mapping = createPortMapping(p("jolokia.port","50000"),"jolokia.port:8080");
updateDynamicMapping(mapping, 8080, 49900);
mapAndVerifyReplacement(mapping,
"http://localhost:50000/", "http://localhost:${jolokia.port}/");
}

@Test
public void variableReplacementWithSystemPropertyOverwrite() throws MojoExecutionException {
try {
System.setProperty("jolokia.port","99999");
PortMapping mapping = createPortMapping(p("jolokia.port","50000"),"jolokia.port:8080");
mapAndVerifyReplacement(mapping,
"http://localhost:99999/", "http://localhost:${jolokia.port}/");
} finally {
System.getProperties().remove("jolokia.port");
}
}

@Test
public void testValidHostname() {
for (String host : new String[] { "localhost", "127.0.0.1" }){
Expand All @@ -107,7 +119,7 @@ public void invalidMapping2() throws MojoExecutionException {

private void mapAndVerifyReplacement(PortMapping mapping, String... args) {
for (int i = 0; i < args.length; i+=2) {
assertEquals(args[i],mapping.replaceVars(args[i+1]));
assertEquals(args[i],mapping.replaceVars(args[i + 1]));
}
}

Expand Down

0 comments on commit 23762ae

Please sign in to comment.