feat: extract package util

This commit is contained in:
tjq 2022-10-23 11:58:22 +08:00
parent a9936b8dba
commit 33539857f4
3 changed files with 84 additions and 37 deletions

View File

@ -0,0 +1,57 @@
package tech.powerjob.common.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
/**
* Java 语言相关的工具
*
* @author tjq
* @since 2022/10/23
*/
@Slf4j
public class JavaUtils {
/**
* 获取类所在 Jar 包的版本
* @param clz
* @return 包版本
*/
public static String determinePackageVersion(Class<?> clz) {
try {
String implementationVersion = clz.getPackage().getImplementationVersion();
if (implementationVersion != null) {
return implementationVersion;
}
CodeSource codeSource = clz.getProtectionDomain().getCodeSource();
if (codeSource == null) {
return null;
}
URL codeSourceLocation = codeSource.getLocation();
URLConnection connection = codeSourceLocation.openConnection();
if (connection instanceof JarURLConnection) {
return getImplementationVersion(((JarURLConnection) connection).getJarFile());
}
try (JarFile jarFile = new JarFile(new File(codeSourceLocation.toURI()))) {
return getImplementationVersion(jarFile);
}
}
catch (Throwable t) {
log.warn("[JavaUtils] determinePackageVersion for clz[{}] failed, msg: {}", clz.getSimpleName(), t.toString());
}
return null;
}
private static String getImplementationVersion(JarFile jarFile) throws IOException {
return jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
}
}

View File

@ -0,0 +1,25 @@
package tech.powerjob.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.*;
/**
* Java 语言相关的工具测试
*
* @author tjq
* @since 2022/10/23
*/
@Slf4j
class JavaUtilsTest {
@Test
void determinePackageVersion() {
String packageVersion = JavaUtils.determinePackageVersion(LoggerFactory.class);
log.info("[determinePackageVersion] LoggerFactory's package version: {}", packageVersion);
}
}

View File

@ -1,15 +1,7 @@
package tech.powerjob.worker.common; package tech.powerjob.worker.common;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import tech.powerjob.common.utils.JavaUtils;
import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
/** /**
* 获取 Worker 版本便于开发者排查问题 * 获取 Worker 版本便于开发者排查问题
@ -29,36 +21,9 @@ public final class PowerJobWorkerVersion {
*/ */
public static String getVersion() { public static String getVersion() {
if (StringUtils.isEmpty(CACHE)) { if (StringUtils.isEmpty(CACHE)) {
CACHE = determinePowerJobVersion(); CACHE = JavaUtils.determinePackageVersion(PowerJobWorkerVersion.class);
} }
return CACHE; return CACHE;
} }
private static String determinePowerJobVersion() {
String implementationVersion = PowerJobWorkerVersion.class.getPackage().getImplementationVersion();
if (implementationVersion != null) {
return implementationVersion;
}
CodeSource codeSource = PowerJobWorkerVersion.class.getProtectionDomain().getCodeSource();
if (codeSource == null) {
return null;
}
URL codeSourceLocation = codeSource.getLocation();
try {
URLConnection connection = codeSourceLocation.openConnection();
if (connection instanceof JarURLConnection) {
return getImplementationVersion(((JarURLConnection) connection).getJarFile());
}
try (JarFile jarFile = new JarFile(new File(codeSourceLocation.toURI()))) {
return getImplementationVersion(jarFile);
}
}
catch (Exception ex) {
return null;
}
}
private static String getImplementationVersion(JarFile jarFile) throws IOException {
return jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
}
} }