package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler; import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType; import net.knarcraft.minecraftserverlauncher.userinterface.GUI; import javax.naming.ConfigurationException; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; /** * This class handles all downloading of .jar files */ public final class JarDownloader { private final String jarDirectory; private final GUI gui; /** * Initializes a jar downloader * * @param gui

The GUI to use for displaying messages

* @param jarDirectory

The directory to download jar files to

*/ public JarDownloader(GUI gui, String jarDirectory) { this.gui = gui; this.jarDirectory = jarDirectory; } /** * Downloads all jars to the program directory. * * @throws IOException On version file failure or folder creation failure */ public void downloadJars() throws IOException { if (!new File(jarDirectory).exists() && !new File(jarDirectory).mkdirs()) { gui.showError("Could not create the Jars folder. Please run the program with admin permissions, or move it to " + "a writable directory."); throw new FileNotFoundException("Unable to create jars folder"); } try { gui.setStatus("Downloading all jars..."); downloadAll(); gui.setStatus("Finished downloading jars"); } catch (FileNotFoundException | ConfigurationException e) { gui.setStatus(e.getMessage()); throw new FileNotFoundException("One or more downloads failed: " + e.getMessage()); } } /** * Downloads jar files for all possible server versions * * @throws IOException

If a jar fails to download

*/ private void downloadAll() throws IOException, ConfigurationException { for (ServerType type : ServerTypeHandler.getServerTypes()) { if (type.getName().equals("Custom")) { continue; } for (String version : type.getVersions()) { gui.setStatus("Downloading " + type.getName() + version + "..."); boolean success = type.downloadJar(jarDirectory, version); if (!success) { throw new FileNotFoundException("Unable to download the jar file for " + type.getName() + version); } } } } }