Skip to content

Commit

Permalink
Embed git properties in code; improve AFSocket.getVersion, selftest
Browse files Browse the repository at this point in the history
By embedding the actual git commit information and project version into
the Java code, we can make debugging a lot easier, especially in
environments that don't keep the META-INF files around that we relied
upon earlier (e.g., Android).

This relies on latest changes in kohlschutter-parent 1.5.5-SNAPSHOT,
which is required for building junixsocket.
  • Loading branch information
kohlschuetter committed Jul 16, 2023
1 parent a6dcf52 commit b332b9d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* junixsocket
*
* Copyright 2009-2023 Christian Kohlschütter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.newsclub.net.unix;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Git properties that are populated upon build time.
*
* Also see {@code src/main/unresolved-java/.../GitProperties.java}
*
* @author Christian Kohlschütter
*/
final class GitProperties {
private static final Map<String, String> MAP = new LinkedHashMap<>();

static {
MAP.put("git.build.version", "${git.build.version}"); // junixsocket version
MAP.put("git.commit.id.abbrev", "${git.commit.id.abbrev}");
MAP.put("git.commit.id.describe", "${git.commit.id.abbrev}");
MAP.put("git.commit.id.full", "${git.commit.id.full}");
MAP.put("git.commit.time", "${git.commit.time}");
MAP.put("git.dirty", "${git.dirty}");
}

private GitProperties() {
throw new IllegalStateException("No instances");
}

static Map<String, String> getGitProperties() {
return Collections.unmodifiableMap(MAP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ public static void ensureSupported() throws UnsupportedOperationException {
* @see #supports(AFSocketCapability)
*/
public static final String getVersion() {
String v = GitProperties.getGitProperties().get("git.build.version");
if (v != null && !v.startsWith("$")) {
return v;
}

try {
return NativeLibraryLoader.getJunixsocketVersion();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ private List<LibraryCandidate> tryProviderClass(String providerClassname, String

public static String getJunixsocketVersion() throws IOException {
// NOTE: This can't easily be tested from within the junixsocket-common Maven build

String v = GitProperties.getGitProperties().get("git.build.version");
if (v != null && !v.startsWith("$")) {
return v;
}

return getArtifactVersion(AFSocket.class, "junixsocket-common");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.newsclub.net.unix;

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

/**
* Some bridging code that allows junixsocket-selftest to do some in-depth diagnostics.
Expand Down Expand Up @@ -45,4 +46,15 @@ public static Throwable initError() {
public static File tempDir() {
return NativeLibraryLoader.tempDir();
}

/**
* Returns git properties determined upon Maven build time.
*
* For performance reasons, these will not be correctly resolves when developing in Eclipse.
*
* @return The properties.
*/
public static Map<String, String> gitProperties() {
return GitProperties.getGitProperties();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -350,20 +351,25 @@ public void printExplanation() throws IOException {
out.println("and file a new bug report with the output below.");
out.println();
out.println("junixsocket selftest version " + AFUNIXSocket.getVersion());

Map<String, String> gitProperties = new LinkedHashMap<>(retrieveGitProperties());
try (InputStream in = getClass().getResourceAsStream(
"/META-INF/maven/com.kohlschutter.junixsocket/junixsocket-selftest/git.properties")) {
Properties props = new Properties();
if (in != null) {
Properties props = new Properties();
props.load(in);
out.println();
out.println("Git properties:");
out.println();
for (String key : new TreeSet<>(props.stringPropertyNames())) {
out.println(key + ": " + props.getProperty(key));
gitProperties.put(key, props.getProperty(key));
}
}
}
out.println();
out.println("Git properties:");
out.println();
for (Map.Entry<String, String> en : gitProperties.entrySet()) {
out.println(en.getKey() + ": " + en.getValue());
}
out.println();
}

public void dumpSystemProperties() {
Expand Down Expand Up @@ -856,6 +862,16 @@ private static File retrieveTempDir() {
}
}

@SuppressWarnings("unchecked")
private static Map<String, String> retrieveGitProperties() {
try {
Class<?> clazz = Class.forName("org.newsclub.net.unix.SelftestDiagnosticsHelper");
return (Map<String, String>) clazz.getMethod("gitProperties").invoke(null);
} catch (Exception e) {
return Collections.emptyMap();
}
}

private static final class ModuleResult {
private final Result result;
private final TestExecutionSummary summary;
Expand Down

0 comments on commit b332b9d

Please sign in to comment.