package com.datastax.oss.driver.internal.core;
import com.datastax.oss.driver.api.core.MavenCoordinates;
import com.datastax.oss.driver.api.core.Version;
import edu.umd.cs.findbugs.annotations.NonNull;
import io.netty.buffer.ByteBuf;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultMavenCoordinates implements MavenCoordinates {
private static final Logger LOG = LoggerFactory.getLogger(DefaultMavenCoordinates.class);
public static MavenCoordinates buildFromResourceAndPrint(URL resource) {
MavenCoordinates info = buildFromResource(resource);
LOG.info("{}", info);
return info;
}
public static DefaultMavenCoordinates buildFromResource(URL resource) {
try (InputStreamReader reader =
new InputStreamReader(resource.openStream(), StandardCharsets.UTF_8)) {
Properties props = new Properties();
props.load(reader);
String name = props.getProperty("driver.name");
String groupId = props.getProperty("driver.groupId");
String artifactId = props.getProperty("driver.artifactId");
String version = props.getProperty("driver.version");
if (ByteBuf.class.getPackage().getName().contains("com.datastax.oss.driver.shaded")) {
artifactId += "-shaded";
}
return new DefaultMavenCoordinates(name, groupId, artifactId, Version.parse(version));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private final String name;
private final String groupId;
private final String artifactId;
private final Version version;
public DefaultMavenCoordinates(String name, String groupId, String artifactId, Version version) {
this.name = name;
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
}
@NonNull
@Override
public String getName() {
return name;
}
@NonNull
@Override
public String getGroupId() {
return groupId;
}
@NonNull
@Override
public String getArtifactId() {
return artifactId;
}
@NonNull
@Override
public Version getVersion() {
return version;
}
@Override
public String toString() {
return String.format("%s (%s:%s) version %s", name, groupId, artifactId, version);
}
}