Skip to content

Commit

Permalink
Work related to fabric8io#1032
Browse files Browse the repository at this point in the history
- Detects cluster manager from the project classpath and adds -cluster to the command
- Enable JMX metrics registration when vertx-dropwizard-metrics is in the classpath
- If the Infinispan Cluster Manager is used, force IPv4
- Disable async DNS resolution (as it is not able to resolve names exposed by other namespaces)
  • Loading branch information
cescoffier committed Aug 29, 2017
1 parent 3158ecd commit 044d44f
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 1 deletion.
17 changes: 17 additions & 0 deletions core/src/main/java/io/fabric8/maven/core/util/MavenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ public static boolean hasClass(MavenProject project, String ... classNames) {
return false;
}

/**
* Returns true if any of the given resources could be found on the given class loader
*/
public static boolean hasResource(MavenProject project, String... paths) {
URLClassLoader compileClassLoader = getCompileClassLoader(project);
for (String path : paths) {
try {
if (compileClassLoader.getResource(path) != null) {
return true;
}
} catch (Throwable e) {
// ignore
}
}
return false;
}

/**
* Returns true if all the given class names could be found on the given class loader
*/
Expand Down
24 changes: 24 additions & 0 deletions generator/vertx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>io.fabric8</groupId>
Expand Down Expand Up @@ -61,6 +73,18 @@
<artifactId>jmockit</artifactId>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-infinispan</artifactId>
<version>3.4.2</version>
<scope>test</scope>
</dependency>


</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ public class Constants {
static final String VERTX_MAVEN_PLUGIN_GA = "io.fabric8:vertx-maven-plugin";
static final String SHADE_PLUGIN_GA = "org.apache.maven.plugins:maven-shade-plugin";
static final String VERTX_GROUPID = "io.vertx";

static final String VERTX_DROPWIZARD = "vertx-dropwizard-metrics";
static final String VERTX_INFINIPAN = "vertx-infinispan";

static final String CLUSTER_MANAGER_SPI = "META-INF/services/io.vertx.core.spi.cluster.ClusterManager";

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@
* Maven Shader Plugin or if the project use the Vert.x Maven Plugin.
*
* To avoid the issue to write file in the current working directory the `cacheDirBase` is configured to `/tmp`.
*
* When a cluster manager is detected in the classpath, `-cluster` is automatically appended to the command line.
*
* To avoid DNS resolution issue, the async DNS resolver is disabled (falling back to the regular Java resolver)
*
* If vertx-dropwizard-metrics is in the classpath, the metrics are enabled and the JMX export is also enabled.
*/
public class VertxGenerator extends JavaExecGenerator {

public VertxGenerator(GeneratorContext context) {
super(context, "vertx");
}
Expand All @@ -58,9 +64,49 @@ public boolean isApplicable(List<ImageConfiguration> configs) throws MojoExecuti
protected List<String> getExtraJavaOptions() {
List<String> opts = super.getExtraJavaOptions();
opts.add("-Dvertx.cacheDirBase=/tmp");

if (! contains("-Dvertx.disableDnsResolver=", opts)) {
opts.add("-Dvertx.disableDnsResolver=true");
}

if (MavenUtil.hasDependency(getProject(), VERTX_GROUPID, VERTX_DROPWIZARD)) {
opts.add("-Dvertx.metrics.options.enabled=true");
opts.add("-Dvertx.metrics.options.jmxEnabled=true");
}

if (! contains("-Djava.net.preferIPv4Stack", opts) && MavenUtil.hasDependency(getProject(), VERTX_GROUPID, VERTX_INFINIPAN)) {
opts.add("-Djava.net.preferIPv4Stack=true");
}

return opts;
}

@Override
protected Map<String, String> getEnv(boolean prePackagePhase) throws MojoExecutionException {
Map<String, String> map = super.getEnv(prePackagePhase);

String args = map.get("JAVA_ARGS");
if (args == null) {
args = "";
}

if (MavenUtil.hasResource(getProject(), CLUSTER_MANAGER_SPI)) {
if (! args.isEmpty()) {
args += " ";
}
args += "-cluster";
}

if (! args.isEmpty()) {
map.put("JAVA_ARGS", args);
}
return map;
}

private boolean contains(String prefix, List<String> opts) {
return opts.stream().anyMatch(val -> val.startsWith(prefix));
}

@Override
protected boolean isFatJar() throws MojoExecutionException {
return !hasMainClass() && isUsingFatJarPlugin() || super.isFatJar();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package io.fabric8.maven.generator.vertx;

import com.google.common.collect.ImmutableSet;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.generator.api.GeneratorContext;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import mockit.integration.junit4.JMockit;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

/**
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
@RunWith(JMockit.class)
public class VertxGeneratorTest {

@Injectable
Logger logger;

DefaultArtifactHandler handler = new DefaultArtifactHandler("jar");


private final Artifact dropwizard = new DefaultArtifact("io.vertx", "vertx-dropwizard-metrics", "3.4.2", null, "jar", "", null);
private final Artifact core = new DefaultArtifact("io.vertx", "vertx-core", "3.4.2", null, "jar", "", null);
private final Artifact infinispan = new DefaultArtifact("io.vertx", "vertx-infinispan", "3.4.2", null, "jar", "", handler);


@Test
public void testDefaultOptions(@Mocked final MavenProject project) {
new Expectations() {{
project.getBuild().getDirectory(); result = new File("target/tmp").getAbsolutePath();
project.getBuild().getOutputDirectory(); result = new File("target/tmp/target").getAbsolutePath();
}};

GeneratorContext context = new GeneratorContext.Builder()
.logger(logger)
.project(project)
.build();
VertxGenerator generator = new VertxGenerator(context);
List<String> list = generator.getExtraJavaOptions();

assertThat(list).containsOnly("-Dvertx.cacheDirBase=/tmp", "-Dvertx.disableDnsResolver=true");
}

@Test
public void testWithMetrics(@Mocked final MavenProject project) {

new Expectations() {{
project.getBuild().getDirectory(); result = new File("target/tmp").getAbsolutePath();
project.getBuild().getOutputDirectory(); result = new File("target/tmp/target").getAbsolutePath();
project.getArtifacts(); result = ImmutableSet.of(dropwizard, core);
}};

GeneratorContext context = new GeneratorContext.Builder()
.logger(logger)
.project(project)
.build();
VertxGenerator generator = new VertxGenerator(context);
List<String> list = generator.getExtraJavaOptions();

assertThat(list).containsOnly(
// Default entries
"-Dvertx.cacheDirBase=/tmp", "-Dvertx.disableDnsResolver=true",
// Metrics entries
"-Dvertx.metrics.options.enabled=true", "-Dvertx.metrics.options.jmxEnabled=true");
}

@Test
public void testWithInfinispanClusterManager(@Mocked final MavenProject project) throws MojoExecutionException {
new Expectations() {{
project.getBuild().getDirectory(); result = new File("target/tmp").getAbsolutePath();
project.getBuild().getOutputDirectory(); result = new File("target/tmp/target").getAbsolutePath();
project.getArtifacts(); result = ImmutableSet.of(infinispan, core);
}};

GeneratorContext context = new GeneratorContext.Builder()
.logger(logger)
.project(project)
.build();
VertxGenerator generator = new VertxGenerator(context);
Map<String, String> env = generator.getEnv(true);

assertThat(env).contains(entry("JAVA_OPTIONS", "-Dvertx.cacheDirBase=/tmp -Dvertx.disableDnsResolver=true " +
// Force IPv4
"-Djava.net.preferIPv4Stack=true"));
assertThat(env).contains(entry("JAVA_ARGS", "-cluster"));
}


}

0 comments on commit 044d44f

Please sign in to comment.