Class AbstractPluginsDownloader

java.lang.Object
com.fathzer.plugin.loader.utils.AbstractPluginsDownloader

@IgnoreJRERequirement public abstract class AbstractPluginsDownloader extends Object
A class that downloads plugins from an Internet remote repository to a local folder.
WARNING: This class requires a Java 11+ JVM and is not available in java 8 distribution!
  • Constructor Details

    • AbstractPluginsDownloader

      protected AbstractPluginsDownloader(URI uri, Path localDirectory)
      Constructor.
      Parameters:
      uri - The uri where to load the remote plugin .
      localDirectory - The folder where plugins jar files will be loaded.
  • Method Details

    • getUri

      protected URI getUri()
      Gets the remote plugin repository URI.
      Returns:
      The URI passed to the constructor.
    • getLocalDirectory

      public Path getLocalDirectory()
      Gets the folder where plugins jar files are loaded.
      Returns:
      The Path passed to the constructor.
    • setProxy

      public void setProxy(ProxySettings proxy)
      Sets the proxy used to connect with remote repository.
      Parameters:
      proxy - The proxy (null, which is the default, to use no proxy)
    • setPluginTypeWording

      public void setPluginTypeWording(String wording)
      Sets the wording of plugins.
      This information is used to generate human friendly error messages.
      Parameters:
      wording - The new wording (default is "plugin")
    • getPluginTypeWording

      protected String getPluginTypeWording()
      Gets the wording of plugins.
      This information is used to generate human friendly error messages.
      Returns:
      wording of the plugin (default is "plugin")
    • clean

      public boolean clean() throws IOException
      Deletes all files in local directory.
      Returns:
      true if loacalDirectory existed and is deleted.
      Throws:
      IOException - If something went wrong
    • download

      public Collection<Path> download(String... keys) throws IOException
      Searches for plugin keys in remote repository, then downloads the corresponding jars.
      Parameters:
      keys - The plugin's keys to search
      Returns:
      The paths of files that contains the jars (including the ones for which shouldLoad(URI, Path) returned false
      Throws:
      IOException - If something went wrong
    • getDownloadTarget

      protected Path getDownloadTarget(URI uri)
      Gets the local path where a remote jar should be downloaded.
      Parameters:
      uri - The uri of a remote jar
      Returns:
      a Path. Default value if a file in the local directory passed to the constructor, this the same filename as the uri.
    • shouldLoad

      protected boolean shouldLoad(URI uri, Path path)
      Tests whether a remote jar should be downloaded.
      Parameters:
      uri - The uri of a remote jar
      path - The local path where the file should be downloaded (the one returned by getDownloadTarget(URI)
      Returns:
      true if the file should be loaded (default is the file is loaded if it does not exists in local directory.
    • downloadFile

      protected void downloadFile(URI uri, Path path) throws IOException
      Downloads an URI to a file.
      Parameters:
      uri - The uri to download
      path - The local path where the file should be downloaded (the one returned by getDownloadTarget(URI). There's no guarantee that the directory that contains path is created. If not, this method should create it.
      Throws:
      IOException - if something went wrong
    • getURIMap

      public Map<String,URI> getURIMap() throws IOException
      Gets the content of the remote repository.
      This method gets an input stream from the uri passed to this class constructor, then pass this input stream to getURIMap(InputStream) and return its result.
      Returns:
      A key to uri map.
      Throws:
      IOException - If something went wrong
    • getRepositoryRequestBuilder

      protected java.net.http.HttpRequest.Builder getRepositoryRequestBuilder()
      Gets the builder of the request used to query the repository.
      A sub-class can override this method to, for example, add headers to the request.
      Returns:
      a request builder that build the request.
    • getJarRequestBuilder

      protected java.net.http.HttpRequest.Builder getJarRequestBuilder(URI uri)
      Gets the builder of the request used to download a jar.
      A sub-class can override this method to, for example, add headers to the request.
      Parameters:
      uri - The jar's uri.
      Returns:
      a request builder that build the request.
    • getURIMap

      protected abstract Map<String,URI> getURIMap(InputStream in) throws IOException
      Gets the map that links a plugin key to the URI of a remote jar file from an InputStream
      Parameters:
      in - An input stream on the remote repository URI
      Returns:
      A map.
      Throws:
      IOException - If something went wrong while reading the input stream
    • getHttpClient

      protected java.net.http.HttpClient getHttpClient()
      Build the http client used to connect with the remote repository.
      Returns:
      An HTTPClient