테스트

aion-server 4.8

Gitteol
최고관리자 · 1 · 💬 0 클론/새로받기
 4.8 61f661d · 1 commits 새로받기(Pull)
commons/src/com/aionemu/commons/utils/info/VersionInfo.java
package com.aionemu.commons.utils.info;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.ClassFileFormatVersion;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aionemu.commons.utils.ClassUtils;

/**
 * @author lord_rex, Neon
 */
public class VersionInfo {

	private static final Logger log = LoggerFactory.getLogger(VersionInfo.class);
	public static final VersionInfo commons = new VersionInfo(VersionInfo.class);

	private String source;
	private String revision;
	private String branch;
	private Instant buildDate;
	private ClassFileFormatVersion classFileVersion;

	/**
	 * Constructs a VersionInfo object holding the version information of the specified classes JAR or directory
	 */
	public VersionInfo(Class<?> c) {
		try {
			File sourceFile = new File(c.getProtectionDomain().getCodeSource().getLocation().toURI());
			if (sourceFile.isDirectory()) { // when application is run from IDE
				source = Path.of("").toAbsolutePath().relativize(sourceFile.toPath()).toString();
				File latestFile = findLatestFile(sourceFile);
				buildDate = Instant.ofEpochMilli(latestFile.lastModified());
				classFileVersion = ClassUtils.readClassFileVersion(new FileInputStream(latestFile), latestFile.getPath());
			} else {
				source = sourceFile.getName();
				try (JarFile jarFile = new JarFile(sourceFile)) {
					Attributes attrs = jarFile.getManifest().getMainAttributes();
					revision = attrs.getValue("Revision");
					branch = attrs.getValue("Branch");
					buildDate = Instant.parse(attrs.getValue("Date"));
					JarEntry jarEntry = jarFile.stream().filter(e -> !e.isDirectory() && e.getName().endsWith(".class")).findFirst().get();
					classFileVersion = ClassUtils.readClassFileVersion(jarFile.getInputStream(jarEntry), jarEntry.getName());
				}
			}
		} catch (Exception e) {
			log.error("Could not get version information", e);
		}
	}

	private File findLatestFile(File sourceFile) throws IOException {
		AtomicReference<FileTime> latestChange = new AtomicReference<>();
		return Files.find(sourceFile.toPath(), Integer.MAX_VALUE, (filePath, fileAttr) -> {
			FileTime lastModified = fileAttr.lastModifiedTime();
			return fileAttr.isRegularFile() && filePath.toString().endsWith(".class")
				&& latestChange.updateAndGet(t -> t == null || lastModified.compareTo(t) > 0 ? lastModified : t) == lastModified;
		})
			.reduce((first, second) -> second) // due to the file matcher the last element will be the latest, so no need to call .max()
			.get()
			.toFile();
	}

	public String getSource() {
		return source;
	}

	public String getRevision() {
		return revision;
	}

	public String getBranch() {
		return branch;
	}

	public Instant getBuildDate() {
		return buildDate;
	}

	public ClassFileFormatVersion getClassFileVersion() {
		return classFileVersion;
	}

	public String getBuildInfo(ZoneId timeZoneId) {
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").withZone(timeZoneId);
		String buildInfo = revision == null ? "" : "revision " + revision + (branch == null ? " " : " (" + branch + ") ");
		buildInfo += "built on " + dateTimeFormatter.format(buildDate);
		if (classFileVersion != null)
			buildInfo += " for Java " + classFileVersion.runtimeVersion().feature();
		return buildInfo;
	}

	@Override
	public String toString() {
		return toString(ZoneId.systemDefault());
	}

	public String toString(ZoneId timeZoneId) {
		return toString(timeZoneId, 0);
	}

	private String toString(ZoneId timeZoneId, int sourceLeftPadToWidth) {
		String sourceLeftPadded = sourceLeftPadToWidth > source.length() ? String.format("%" + sourceLeftPadToWidth + "s", source) : source;
		return sourceLeftPadded + " " + getBuildInfo(timeZoneId);
	}

	public static void logAll(Class<?> c) {
		logAll(new VersionInfo(c), ZoneId.systemDefault());
	}

	public static void logAll(VersionInfo versionInfo, ZoneId timeZoneId) {
		int maxSourceLength = Math.max(VersionInfo.commons.source.length(), versionInfo.source.length());
		log.info(VersionInfo.commons.toString(timeZoneId, maxSourceLength));
		log.info(versionInfo.toString(timeZoneId, maxSourceLength));
	}
}

📎 첨부파일

댓글 작성 권한이 없습니다.
🏆 포인트 랭킹 TOP 10
순위 닉네임 포인트
1 no_profile 타키야겐지쪽지보내기 자기소개 아이디로 검색 전체게시물 102,949
2 no_profile 동가리쪽지보내기 자기소개 아이디로 검색 전체게시물 63,733
3 no_profile 라프텔쪽지보내기 자기소개 아이디로 검색 전체게시물 51,771
4 no_profile 불멸의행복쪽지보내기 자기소개 아이디로 검색 전체게시물 36,923
5 서번트쪽지보내기 자기소개 아이디로 검색 전체게시물 35,011
6 no_profile 닥터스쪽지보내기 자기소개 아이디로 검색 전체게시물 29,470
7 no_profile 검은고양이쪽지보내기 자기소개 아이디로 검색 전체게시물 29,077
8 no_profile Revolution쪽지보내기 자기소개 아이디로 검색 전체게시물 28,199
9 no_profile 보거스쪽지보내기 자기소개 아이디로 검색 전체게시물 26,731
10 no_profile 호롤롤로쪽지보내기 자기소개 아이디로 검색 전체게시물 17,020
알림 0