Skip to content

Commit

Permalink
IPROTO-213 Protostream processor must ensure it has the same version …
Browse files Browse the repository at this point in the history
…as protostream core

* use manifest implementation version for comparison
  • Loading branch information
anistor committed May 24, 2021
1 parent f7fe89d commit 5efa416
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 44 deletions.
68 changes: 34 additions & 34 deletions core/src/main/java/org/infinispan/protostream/Version.java
@@ -1,49 +1,38 @@
package org.infinispan.protostream;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.Properties;

/**
* Contains version information about this ProtoStream release.
* Provides version information about this ProtoStream release.
*
* @author anistor@redhat.com
* @since 4.2
*/
public final class Version implements Comparable<Version> {

private static final Version VERSION = getArtifactVersion();
private static final Version VERSION = getVersion(Version.class);

/**
* We try to obtain the Maven pom.properties of the artifact and get the build version from there.
* Try to obtain the version from the manifest.
*/
private static Version getArtifactVersion() {
int major = 0;
int minor = 0;
int micro = 0;
String suffix = null;

InputStream res = Version.class.getResourceAsStream("/META-INF/maven/org.infinispan.protostream/protostream/pom.properties");
if (res != null) {
try {
Properties pomProps = new Properties();
pomProps.load(res);
String version = pomProps.getProperty("version", "0.0.0-UNKNOWN");
String[] versionParts = version.split("[.\\-]");
major = Integer.parseInt(versionParts[0]);
if (versionParts.length > 1) {
minor = Integer.parseInt(versionParts[1]);
}
if (versionParts.length > 2) {
micro = Integer.parseInt(versionParts[2]);
}
if (versionParts.length > 3) {
suffix = versionParts[3];
}
} catch (IOException | NumberFormatException | ArrayIndexOutOfBoundsException e) {
// ignored
}
public static Version getVersion(Class<?> clazz) {
String version = clazz.getPackage().getImplementationVersion();
if (version == null) {
return null;
}

int major;
int minor;
int micro;
String suffix;
try {
String[] versionParts = version.split("[.\\-]");
major = Integer.parseInt(versionParts[0]);
minor = versionParts.length > 1 ? Integer.parseInt(versionParts[1]) : 0;
micro = versionParts.length > 2 ? Integer.parseInt(versionParts[2]) : 0;
suffix = versionParts.length > 3 ? versionParts[3] : null;
} catch (Exception e) {
return null;
}

return new Version(major, minor, micro, suffix);
Expand Down Expand Up @@ -72,7 +61,14 @@ public Version(int major, int minor, int micro, String suffix) {
this.minor = minor;
this.micro = micro;
this.suffix = suffix;
versionString = major + "." + minor + "." + micro + (suffix != null ? "." + suffix : "");
if (suffix == null) {
suffix = "";
} else if ("SNAPSHOT".equals(suffix)) {
suffix = "-SNAPSHOT";
} else {
suffix = '.' + suffix;
}
versionString = major + "." + minor + "." + micro + suffix;
}

public int getMajor() {
Expand Down Expand Up @@ -124,7 +120,11 @@ public int compareTo(Version other) {
if (d == 0) {
d = micro - other.micro;
if (d == 0) {
d = suffix.compareTo(other.suffix);
if (suffix == null) {
d = other.suffix == null ? 0 : -1;
} else {
d = other.suffix == null ? 1 : suffix.compareTo(other.suffix);
}
}
}
}
Expand Down
Expand Up @@ -41,6 +41,7 @@
import org.infinispan.protostream.ProtobufUtil;
import org.infinispan.protostream.SerializationContext;
import org.infinispan.protostream.SerializationContextInitializer;
import org.infinispan.protostream.Version;
import org.infinispan.protostream.WrappedMessage;
import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder;
import org.infinispan.protostream.annotations.ProtoSchemaBuilderException;
Expand All @@ -67,7 +68,7 @@ public final class AutoProtoSchemaBuilderAnnotationProcessor extends AbstractPro
*/
static final String ANNOTATION_NAME = "org.infinispan.protostream.annotations.AutoProtoSchemaBuilder";

private static boolean checkForMinRequiredJava = true;
private static boolean checkRequiredEnv = true;

private final ServiceLoaderFileGenerator serviceLoaderFileGenerator = new ServiceLoaderFileGenerator(SerializationContextInitializer.class);

Expand Down Expand Up @@ -102,6 +103,8 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
messager = processingEnv.getMessager();

generatedFilesWriter = new GeneratedFilesWriter(filer);

ensureRequiredEnv();
}

@Override
Expand Down Expand Up @@ -172,8 +175,6 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
logDebug("AutoProtoSchemaBuilderAnnotationProcessor annotations=%s, rootElements=%s", annotations, roundEnv.getRootElements());
}

ensureMinRequiredJava();

Optional<? extends TypeElement> claimedAnnotation = annotations.stream()
.filter(a -> a.getQualifiedName().contentEquals(ANNOTATION_NAME))
.findAny();
Expand Down Expand Up @@ -208,15 +209,23 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
return claimedAnnotation.isPresent();
}

private void ensureMinRequiredJava() {
if (checkForMinRequiredJava && getJavaMajorVersion() < 9) {
private void ensureRequiredEnv() {
if (checkRequiredEnv) {
// check requirements and complain only once
checkRequiredEnv = false;

// check and complain only once
checkForMinRequiredJava = false;
if (getJavaMajorVersion() < 9) {
reportWarning(null, "Please ensure you use at least Java ver. 9 for compilation in order to avoid various " +
"compiler related bugs from older Java versions that impact the AutoProtoSchemaBuilder annotation " +
"processor (you can still set the output target to 8 or above).");
}

reportWarning(null, "Please ensure you use at least Java ver. 9 for compilation in order to avoid various " +
"compiler related bugs from older Java versions that impact the AutoProtoSchemaBuilder annotation " +
"processor (you can still set the output target to 8 or above).");
Version procVersion = Version.getVersion(AutoProtoSchemaBuilder.class);
if (Version.getVersion().compareTo(procVersion) != 0) {
// protostream core and processor versions must be identical to ensure compatibility
reportWarning(null, "Version mismatch! protostream (%s) and protostream-processor (%s) are expected to be the same version. " +
"Mixing different versions is unsupported and can lead to unintended consequences.", Version.getVersion(), procVersion);
}
}
}

Expand Down

0 comments on commit 5efa416

Please sign in to comment.