package org.graalvm.component.installer.gds;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.graalvm.component.installer.CommandInput;
import org.graalvm.component.installer.Feedback;
import org.graalvm.component.installer.SoftwareChannel;
import org.graalvm.component.installer.SoftwareChannelSource;
public class GraalChannelFactory implements SoftwareChannel.Factory {
private static final String PROTOCOL_OLDS_PREFIX = "gds:";
private static final String PROTOCOL_HTTTPS_PREFIX = "https:";
private static final Map<String, String> OPTIONS = new HashMap<>();
private MailStorage mailStorage;
private Feedback feedback;
static {
OPTIONS.put(GdsCommands.OPTION_EMAIL_ADDRESS, "s");
OPTIONS.put(GdsCommands.LONG_OPTION_EMAIL_ADDRESS, GdsCommands.OPTION_EMAIL_ADDRESS);
}
private MailStorage initMailStorage(CommandInput input, Feedback output) {
if (mailStorage == null) {
MailStorage ms = new MailStorage(input.getLocalRegistry(), output);
ms.setStorage(input.getGraalHomePath());
mailStorage = ms;
}
return mailStorage;
}
@Override
public SoftwareChannel createChannel(SoftwareChannelSource source, CommandInput input, Feedback output) {
feedback = output;
String urlString = source.getLocationURL();
if (!urlString.startsWith(PROTOCOL_OLDS_PREFIX)) {
return null;
}
String rest = urlString.substring(PROTOCOL_OLDS_PREFIX.length());
URL u;
try {
if (rest.startsWith("http") || rest.startsWith("file:") || rest.startsWith("test:")) {
u = new URL(rest);
} else {
u = new URL(PROTOCOL_HTTTPS_PREFIX + rest);
}
} catch (MalformedURLException ex) {
throw output.failure("YUM_InvalidLocation", ex, urlString, ex.getLocalizedMessage());
}
GraalChannel ch = new GraalChannel(input, output, input.getLocalRegistry());
ch.setEdition(source.getParameter("edition"));
ch.setReleasesIndexURL(u);
ch.setMailStorage(initMailStorage(input, output));
return ch;
}
@Override
public Map<String, String> globalOptions() {
return OPTIONS;
}
@Override
public String globalOptionsHelp() {
return feedback.l10n("GDS_ExtraOptionsHelp");
}
@Override
public void init(CommandInput input, Feedback output) {
feedback = output;
}
}