package org.graalvm.component.installer;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.graalvm.component.installer.ComponentCatalog.DownloadInterceptor;
import org.graalvm.component.installer.model.ComponentInfo;
import org.graalvm.component.installer.remote.CatalogIterable;
import org.graalvm.component.installer.remote.FileDownloader;
import org.graalvm.component.installer.remote.RemoteComponentParam;
abstract class AbstractIterable implements ComponentIterable {
protected final CommandInput input;
protected final Feedback feedback;
private boolean verifyJars;
private CommandInput.CatalogFactory remoteFactory;
private ComponentCatalog remoteCatalog;
protected AbstractIterable(CommandInput input, Feedback feedback) {
this.input = input;
this.feedback = feedback;
}
public boolean isVerifyJars() {
return verifyJars;
}
@Override
public void setVerifyJars(boolean verifyJars) {
this.verifyJars = verifyJars;
}
public void setCatalogFactory(CommandInput.CatalogFactory cFactory) {
this.remoteFactory = cFactory;
}
private ComponentCatalog getRemoteContents() {
if (remoteCatalog != null) {
return remoteCatalog;
}
if (remoteFactory != null) {
remoteCatalog = remoteFactory.createComponentCatalog(input);
} else {
remoteCatalog = new NullCatalog();
}
return remoteCatalog;
}
@Override
public ComponentIterable matchVersion(Version.Match m) {
return this;
}
@Override
public ComponentIterable allowIncompatible() {
return this;
}
@Override
public ComponentParam createParam(String cmdString, ComponentInfo info) {
RemoteComponentParam param = new CatalogIterable.CatalogItemParam(
getRemoteContents().getDownloadInterceptor(),
info,
info.getName(),
cmdString,
feedback,
input.optValue(Commands.OPTION_NO_DOWNLOAD_PROGRESS) == null);
param.setVerifyJars(verifyJars);
return param;
}
private static class NullCatalog implements ComponentCatalog, DownloadInterceptor {
@Override
public boolean isAllowDistUpdate() {
return false;
}
@Override
public ComponentInfo findComponentMatch(String id, Version.Match vmatch, boolean localOnly, boolean exact) {
return null;
}
@Override
public Set<String> findDependencies(ComponentInfo start, boolean closure, Boolean installed, Set<ComponentInfo> result) {
return new HashSet<>(start.getDependencies());
}
@Override
public FileDownloader processDownloader(ComponentInfo info, FileDownloader dn) {
return dn;
}
@Override
public DownloadInterceptor getDownloadInterceptor() {
return this;
}
@Override
public void setAllowDistUpdate(boolean distUpgrade) {
}
@Override
public ComponentInfo findComponentMatch(String id, Version.Match vm, boolean exact) {
return null;
}
@Override
public String shortenComponentId(ComponentInfo info) {
return info.getId();
}
@Override
public Collection<String> getComponentIDs() {
return Collections.emptyList();
}
@Override
public Collection<ComponentInfo> loadComponents(String id, Version.Match selector, boolean filelist) {
return Collections.emptySet();
}
@Override
public boolean isRemoteEnabled() {
return false;
}
}
}