All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.jbang.net.jdkproviders.DefaultJdkProvider Maven / Gradle / Ivy

There is a newer version: 0.121.0
Show newest version
package dev.jbang.net.jdkproviders;

import static dev.jbang.util.JavaUtil.resolveJavaVersionStringFromPath;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import dev.jbang.Settings;
import dev.jbang.net.JdkProvider;

/**
 * This JDK provider returns the "default" JDK if it was set (using
 * jbang jdk default).
 */
public class DefaultJdkProvider implements JdkProvider {
	@Nonnull
	@Override
	public List listInstalled() {
		Path defaultDir = Settings.getCurrentJdkDir();
		if (Files.isDirectory(defaultDir)) {
			Optional version = resolveJavaVersionStringFromPath(defaultDir);
			if (version.isPresent()) {
				String id = "default";
				return Collections.singletonList(createJdk(id, defaultDir, version.get()));
			}
		}
		return Collections.emptyList();
	}

	@Nullable
	@Override
	public Jdk getJdkById(@Nonnull String id) {
		if (id.equals(name())) {
			List l = listInstalled();
			if (!l.isEmpty()) {
				return l.get(0);
			}
		}
		return null;
	}

	@Nullable
	@Override
	public Jdk getJdkByPath(@Nonnull Path jdkPath) {
		List installed = listInstalled();
		Jdk def = !installed.isEmpty() ? installed.get(0) : null;
		return def != null && def.getHome() != null && jdkPath.startsWith(def.getHome()) ? def : null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy