mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: extract package util
This commit is contained in:
parent
a9936b8dba
commit
33539857f4
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -1,15 +1,7 @@
|
||||
package tech.powerjob.worker.common;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
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;
|
||||
import tech.powerjob.common.utils.JavaUtils;
|
||||
|
||||
/**
|
||||
* 获取 Worker 版本,便于开发者排查问题
|
||||
@ -29,36 +21,9 @@ public final class PowerJobWorkerVersion {
|
||||
*/
|
||||
public static String getVersion() {
|
||||
if (StringUtils.isEmpty(CACHE)) {
|
||||
CACHE = determinePowerJobVersion();
|
||||
CACHE = JavaUtils.determinePackageVersion(PowerJobWorkerVersion.class);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user