com.infusers.core.version.java.InfusersJavaVersionDetails Maven / Gradle / Ivy
package com.infusers.core.version.java;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import com.infusers.core.audit.AuditService;
public class InfusersJavaVersionDetails {
private InfusersJavaVersionDetails() {}
private static InfusersJavaVersionDetails instance;
public static InfusersJavaVersionDetails getInstance() {
if(instance==null) {
instance = new InfusersJavaVersionDetails();
}
return instance;
}
private static final String CLASS_NAME = "InfusersJavaVersionDetails";
@Autowired(required = true)
private AuditService auditService;
final Logger log = LogManager.getLogger(InfusersJavaVersionDetails.class);
public String getJavaVersion() {
String javaVersion = System.getProperty("java.version");
String javaVersionLatest = getLatestJavaVersion();
if(javaVersion!=null && javaVersion.trim().equalsIgnoreCase(javaVersionLatest)) {
return javaVersion;
}
else {
return javaVersion+", Upgrade option: "+javaVersionLatest;
}
}
public String getLatestJavaVersion() {
String latestVersion = "";
try {
String url = "https://www.oracle.com/java/technologies/downloads/";
Document doc = Jsoup.connect(url).get();
Elements downloadLinks = doc.select("[href*='jdk'][href*='linux-aarch64_bin.tar.gz']");
Element latestLink = downloadLinks.first();
if(latestLink!=null) {
String archiveName = latestLink.attr("href");
//System.out.println("latestLink = "+latestLink+" :: archiveName = "+archiveName);
Path archivePath = Paths.get(archiveName);
String regex = "^([^-]+-[^-]+)_.*\\.tar\\.gz$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(archivePath.getFileName().toString());
if (matcher.matches()) {
latestVersion = matcher.group(1);
}
}
}
catch (Exception e) {
//e.printStackTrace();
return "Some issue, found excpetion";
}
//System.out.println("Latest version of Java SE: " + latestVersion);
return latestVersion;
}
// public static void main(String[] args) throws IOException {
// getLatestJavaVersion();
//}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy