Class PluginInstallUtils

java.lang.Object
com.ultikits.ultitools.utils.PluginInstallUtils

public class PluginInstallUtils extends Object
Utility class for plugin installation and management operations. Provides methods to download, install, and uninstall UltiTools plugins from the remote plugin repository.
Since:
6.0.0
Author:
wisdomme
  • Field Details

  • Constructor Details

    • PluginInstallUtils

      public PluginInstallUtils()
  • Method Details

    • getBaseUrl

      static String getBaseUrl()
      Get the base URL for API calls. Uses lazy initialization to avoid requiring UltiTools at class loading time.
      Returns:
      the base URL
    • initializeCatalogueUrl

      @Internal public static void initializeCatalogueUrl()
      Resolves an explicitly configured test URL early enough for its diagnostic to appear at startup.
    • configuredCatalogueUrl

      private static String configuredCatalogueUrl()
    • isLoopbackCatalogueUrl

      private static boolean isLoopbackCatalogueUrl(String value)
    • setBaseUrlForTesting

      static void setBaseUrlForTesting(String url)
      Set a custom base URL for testing purposes. This should only be used in unit tests.
      Parameters:
      url - the custom base URL, or null to reset to default
    • resetBaseUrl

      static void resetBaseUrl()
      Reset the base URL to force re-initialization. This should only be used in unit tests.
    • normalizeIdentifyString

      static String normalizeIdentifyString(String idString)
    • installedJarName

      static String installedJarName(String idString, String version)
    • getPluginList

      public static List<PluginEntity> getPluginList(int page, int pageSize)
      Get plugin list online.
      Parameters:
      page - the page number
      pageSize - the number of items per page
      Returns:
      the plugin list
    • getPluginVersionDownloadLink

      public static String getPluginVersionDownloadLink(String idString, String version)
      Get plugin download link.
      Parameters:
      idString - the plugin ID
      version - the version
      Returns:
      the plugin download link
    • getPluginVersions

      public static List<String> getPluginVersions(String idString)
      Gets all versions this framework can install, across every page of the catalogue.
      Parameters:
      idString - the plugin ID
      Returns:
      the complete plugin version list, newest first as the catalogue lists them
    • lookUpPluginVersions

      @Internal public static VersionLookup lookUpPluginVersions(String idString, List<String> names)
      Internal command lookup that distinguishes an unknown module from a successful empty list. This bridge is not part of the supported plugin API; getPluginVersions(String) keeps its existing empty-list/null contract.
      Parameters:
      idString - the plugin ID
      names - collects version names on success
      Returns:
      FOUND with the module identifier on success, NONE when the versions endpoint answers 404, or FAILED when the lookup establishes nothing
    • getPluginLatestVersion

      public static String getPluginLatestVersion(String idString)
      Get plugin latest version.

      Returns null for a blank id, a module the catalogue does not know, no version this framework can install, and every failure (an error status, an unreadable body, an unreachable catalogue); this contract is unchanged as of 6.3.0. The start-up update check uses lookUpPluginLatestVersion(String) instead, which tells those apart.

      Parameters:
      idString - the plugin ID
      Returns:
      the plugin's latest version, or null
    • lookUpPluginLatestVersion

      @Internal public static VersionLookup lookUpPluginLatestVersion(String idString)
      Looks up a module's latest version for this framework's API level and says whether the lookup established anything (Phase 19, D19-032). VersionLookup.Outcome.FOUND carries the version exactly as getPluginLatestVersion(String) returns it. VersionLookup.Outcome.NONE is a confirmed "nothing to offer": a blank id, the catalogue not knowing the module (plugin/get answers 404), or knowing it with no version this framework can install or no finalized version (latest answers 409 or 404). Every other answer -- another non-2xx status, an error envelope, a missing data, an unreadable body, an unreachable catalogue -- is VersionLookup.Outcome.FAILED.

      An exception thrown while asking propagates, exactly as it does from getPluginLatestVersion(String).

      Parameters:
      idString - the module's identify string
      Returns:
      the lookup's outcome, never null
      Since:
      6.3.0
    • lookUpPluginLatestVersion

      @Internal public static VersionLookup lookUpPluginLatestVersion(String idString, BooleanSupplier stopped)
      lookUpPluginLatestVersion(String), with a stop signal read before each request, including every version page (Phase 19, D19-032). When stopped is true, the lookup is VersionLookup.Outcome.FAILED with the failure "stopped". The start-up update check passes its own stop flag, so a check stopped by the plugin's disable sends no further request.
      Parameters:
      idString - the module's identify string
      stopped - read before the module request and every version-page request
      Returns:
      the lookup's outcome, never null
      Since:
      6.3.0
    • getPluginLatestDownloadLink

      public static String getPluginLatestDownloadLink(String idString)
      Get plugin latest version download link.
      Parameters:
      idString - Plugin identify string
      Returns:
      Download link for latest version
    • getPlugin

      public static PluginEntity getPlugin(String idString)
      Get plugin information by identify string.
      Parameters:
      idString - Plugin identify string
      Returns:
      Plugin entity or null if not found
    • installLatestPlugin

      public static boolean installLatestPlugin(String idString)
      Requests an installation of the latest compatible version.
      Parameters:
      idString - the catalogue module identifier
      Returns:
      whether a new change was recorded for the next start
    • installPlugin

      public static boolean installPlugin(String idString, String version)
      Requests an installation of the specified version.
      Parameters:
      idString - the catalogue module identifier
      version - the catalogue version
      Returns:
      whether a new change was recorded for the next start
    • installReporting

      @Internal public static PluginInstallUtils.PendingResult installReporting(String idString, String versionOrNull)
      Returns:
      the install request, with no command sender (for internal programmatic callers)
    • installReporting

      @Internal public static PluginInstallUtils.PendingResult installReporting(String idString, String versionOrNull, org.bukkit.command.CommandSender sender)
      Queues a verified catalogue module without changing the running modules or their files.
    • installVersion

      private static PluginInstallUtils.PendingResult installVersion(CatalogueClient.Version metadata, PluginEntity module, String requested, int level, org.bukkit.command.CommandSender sender)
    • loadedSnapshot

      private static List<UltiToolsPlugin> loadedSnapshot(PluginManager manager)
    • installResult

    • pendingChanges

      @Internal public static PendingChanges pendingChanges()
      Returns:
      the running framework's journal view
    • transactions

      private static ModuleFileTransactions transactions(UltiTools framework)
    • downloadAndAdmit

      private static PluginInstallUtils.PendingResult downloadAndAdmit(String url, CatalogueClient.Version metadata, String fileName, File folder, String requested, String version, int level)
      Verifies the download, then checks it as an archive and by its declared api-version, and either renames it to fileName or deletes it. No partial file survives this method, whatever happens.
    • failureLine

      static String failureLine(String module, String version, VerifiedDownload.Kind kind)
      The one line an operator sees when a download did not pass its checks: the module, the version and the reason, and that nothing was installed. A withdrawn version, a transport failure and an integrity failure cannot be mistaken for one another; every integrity failure (hash, size, hash header, no usable hash or size) is the catalogue text UtilsKeys.INSTALL_REASON_INTEGRITY_FAILED in the server's language (plan 22-22), and which check failed is on the FINE line beside it. Only short hash prefixes may join it in the local log, never a whole URL.
      Parameters:
      module - the module as the operator asked for it
      version - the version
      kind - why the download was refused
      Returns:
      the line
    • isIntegrityFailure

      private static boolean isIntegrityFailure(VerifiedDownload.Kind kind)
      Parameters:
      kind - why a download was refused
      Returns:
      whether the bytes, the header or the catalogue entry failed the integrity check, as opposed to a version that is not offered or a download that did not complete
    • licenceRequired

      private static boolean licenceRequired(CatalogueClient.Version metadata)
      Returns:
      whether the catalogue offers the version only with a licence
    • foreignDownloadLine

      private static String foreignDownloadLine(String module, String version)
      The line for a download.url the catalogue put outside its own host: refused before anything is requested, because a catalogue answer must never move an executable download.
    • notAvailableLine

      private static String notAvailableLine(String module, String version)
      The line for a version the catalogue lists but does not offer for another reason than a licence (that one is UpmKeys.INSTALL_REFUSED_LICENCE_REQUIRED); it is not an integrity failure.
    • refusalOf

      private static PluginInstallUtils.PendingResult refusalOf(File part, String requested, String version, int level)
      Why a downloaded JAR is not installed, or null to install it: the archive guard the module loader applies (SecurityPolicy.isSafeFileStructure(long, int); the .jar suffix check of SecurityPolicy.isValidModuleJar(File) does not apply to the .part name), then the declared api-version.
    • isSafeArchive

      private static boolean isSafeArchive(File file)
    • moveIntoPlace

      private static void moveIntoPlace(File part, File target) throws IOException
      Renames the checked download to its final name in one step, replacing a file of that name.
      Throws:
      IOException
    • deleteQuietly

      private static void deleteQuietly(File file)
    • updateResult

      @Internal public static PluginInstallUtils.PendingResult updateResult(String requested, ModuleFileTransactions.StageResult staged)
      Converts the shared stage result into update replies in the operator's language.
    • appendPreviousFailure

      private static void appendPreviousFailure(PluginInstallUtils.PendingResult.Builder result, String requested, ModuleFileTransactions.StageResult staged)
    • appendStageReply

      private static void appendStageReply(PluginInstallUtils.PendingResult.Builder result, String requested, ModuleFileTransactions.StageResult staged, boolean install)
    • appendStageFailure

      private static void appendStageFailure(PluginInstallUtils.PendingResult.Builder result, Arg module, ModuleFileTransactions.StageResult staged, boolean install)
    • findPluginJar

      public static File findPluginJar(File pluginsFolder, String identifyString)
      Find the JAR file for a plugin by its identify-string. Scans all JARs in the given directory, reads plugin.yml from each.
      Parameters:
      pluginsFolder - the folder to scan
      identifyString - the identify-string to match
      Returns:
      the matching JAR file, or null if not found
    • stageUpdate

      @Internal public static ModuleFileTransactions.StageResult stageUpdate(String identifyString)
      Stages an update of a loaded module to its latest version (#505).

      Nothing in the modules folder changes: the new JAR is downloaded into the transaction folder beside it and recorded. At the next start the files are swapped before any module loads, and the update is kept only if the module is then seen loaded from the new JAR at the new version; otherwise the old JAR is restored. See ModuleFileTransactions.

      Parameters:
      identifyString - the module's identify-string
      Returns:
      what was staged, or why nothing was
    • stageUpdate

      @Internal public static ModuleFileTransactions.StageResult stageUpdate(String identifyString, org.bukkit.command.CommandSender sender)
      Requests a restart-time update as the supplied command sender.
    • downloadVerified

      static void downloadVerified(String link, CatalogueClient.Version metadata, String fileName, File folder) throws IOException
      The staging download: the same verification the install applies, because a staged update becomes the module that loads at the next start. The link is the version's download.url, used as written; the metadata it is checked against is the version object the caller has just read through the contract's version route, so staging never trusts a hash it did not just fetch.
      Parameters:
      link - the absolute version download URL
      metadata - the version, as the catalogue's version route just answered it
      fileName - the file name to write
      folder - the folder to write it into
      Throws:
      IOException - when the catalogue offers no public download or the file does not verify
    • reasonOf

      static String reasonOf(VerifiedDownload.Kind kind)
      Returns:
      the reason a staged download was refused, as the update reason names it
    • recordedMainClass

      static Function<UltiToolsPlugin, String> recordedMainClass(ModuleJarIndex index)
      The main class the module loader recorded for a loaded module, read from its start-up index (#516): what staging checks other copies of the module against (maintainer follow-up 19). null for a module the index has no record of, so that staging falls back to the old JAR's own plugin.yml main: rather than to a runtime class name no JAR declares.
      Parameters:
      index - the loader's index, or null
      Returns:
      the resolver
    • updatePlugin

      public static boolean updatePlugin(String identifyString)
      Stages an update of a loaded module; see stageUpdate(String).

      As of 6.3.0 this no longer replaces the JAR in place and no longer means "updated": it returns true when the update was staged, to take effect at the next start and to be kept only if the module then loads.

      Parameters:
      identifyString - the plugin identify string
      Returns:
      true if the update was staged
    • probe

      private static PluginInstallUtils.Presence probe(File file)
      What is at file, without following links, distinguishing "not there" from "could not be determined".
      Parameters:
      file - the path to probe
      Returns:
      what is there
    • probeFollowingLinks

      private static PluginInstallUtils.Presence probeFollowingLinks(File file)
      What is at file with links followed: the question "is this one of the files the module loader would open?", which is a different question from "is anything at this path?".

      PluginManager#init opens each entry through new JarFile(file), which follows links, so a link to a real JAR is a file it loads from. The access question keeps NOFOLLOW_LINKS, because that is what separates absence from a denial; this one must match the loader.

      Parameters:
      file - the path to probe
      Returns:
      what is at the end of it
    • probe

      private static PluginInstallUtils.Presence probe(File file, LinkOption... options)
      probe(File) with the link options to use.
      Parameters:
      file - the path to probe
      options - how to treat a symbolic link
      Returns:
      what is there
    • codeSourceJarOf

      static File codeSourceJarOf(Class<?> moduleClass)
      The JAR a module class was loaded from, or null when it was not loaded from one.

      The loaded instance is the authority on which file it came from, whatever that file's plugin.yml says now: the file may have been replaced or edited since the start, and a module registered from code has no plugin.yml entry the loader read at all. So the framework asks the instance, exactly as UltiToolsPlugin itself does when it reads its own embedded resources.

      A code source that is a directory is a development checkout rather than an installed JAR and answers nothing about which file to delete. One that cannot be probed is still named: the module was loaded from it, and a probe that cannot answer is not an answer of "no".

      Parameters:
      moduleClass - the loaded module's class
      Returns:
      the JAR, or null when there is none to name
    • codeSourceLocationOf

      static File codeSourceLocationOf(Class<?> moduleClass)
      Whether which JAR a module was loaded from could be determined at all.

      Three answers through one value, because the difference matters: a file is the JAR the module came from; a directory is an exploded development checkout, which is no JAR at all and says so; and null is "could not be determined" -- a code source a policy refuses to reveal, or a class that has none. Unknown is not "none": while it holds, any entry matched only by a name might be that running module's own JAR.

      Parameters:
      moduleClass - the loaded module's class
      Returns:
      the code source's location, or null when it could not be determined
    • resolveCodeSourceFile

      private static File resolveCodeSourceFile(URL location)
      A code source URL as a file, whatever escaping it carries.
    • readArchive

      private static PluginInstallUtils.ArchiveIdentity readArchive(File file)
      Reads what an archive declares about itself.

      The one place a plugin.yml is opened. Four outcomes, each a positive answer: it declares a name; it opened and declares none (no plugin.yml, or one without a name: key -- which say the same thing, since a module needs no metadata at all); it could not be opened or parsed; or the entry is one the module loader itself would never load.

      Parameters:
      file - the entry
      Returns:
      what it says about itself
    • readDeclaredName

      private static PluginInstallUtils.ArchiveIdentity readDeclaredName(JarFile jarFile, JarEntry entry, File file) throws IOException
      The name a plugin.yml declares, if it declares one.
      Parameters:
      jarFile - the archive
      entry - its plugin.yml
      file - the archive's path, for the log line
      Returns:
      what it says about itself
      Throws:
      IOException - when the entry cannot be read
    • canonicalPathOf

      private static String canonicalPathOf(File file)
      file's canonical path, or its absolute path when it cannot be canonicalised.
    • requestUninstall

      @Internal public static PluginInstallUtils.PendingResult requestUninstall(String name, org.bukkit.command.CommandSender requester)
      Records the JARs a module will lose at the next start; never unloads it during this run.
    • requestUninstall

      static PluginInstallUtils.PendingResult requestUninstall(String name, org.bukkit.command.CommandSender requester, Function<UltiToolsPlugin, File> codeSource)
    • requestUninstallLocked

      private static PluginInstallUtils.PendingResult requestUninstallLocked(String name, org.bukkit.command.CommandSender sender, Function<UltiToolsPlugin, File> codeSource)
    • matchRemovalCandidates

      private static PluginInstallUtils.RemovalCandidates matchRemovalCandidates(File[] entries, PluginInstallUtils.ModuleIdentity identity)
    • queueResolvedRemoval

      private static PluginInstallUtils.PendingResult queueResolvedRemoval(ModuleFileTransactions journal, PluginInstallUtils.ModuleIdentity identity, PluginInstallUtils.RemovalCandidates candidates, String name, org.bukkit.command.CommandSender sender)
    • removalTarget

      private static PluginInstallUtils.RemovalTarget removalTarget(UltiToolsPlugin running, List<File> files)
    • readIdentifyString

      private static String readIdentifyString(File file)
    • chooseTargets

      private static void chooseTargets(String requested, List<UltiToolsPlugin> loaded, List<UltiToolsPlugin> others, Map<UltiToolsPlugin, String> declared)
      Moves the instances the argument names from others into loaded.

      The two namespaces are asked in order, never together. A runtime-name match answers the question outright: if any loaded module answers to the name that was typed, that is the answer and no declared-name lookup runs beside it -- asking both at once made one module's runtime name and another's declared name equally good, and unloaded both.

      Only when nothing answers by runtime name is the other namespace consulted, because naming a module by what its JAR declares must still work (gate 1, BL-01). If more than one module answers that way, the uninstall refuses and names the candidates rather than choosing: this deletes files, and a destructive command with two possible targets must stop.

      Parameters:
      requested - the operator's argument
      loaded - collects the instances it names
      others - every loaded instance; the chosen ones are moved out of it
      declared - what each instance's JAR declares
    • refuseIfAmbiguous

      private static void refuseIfAmbiguous(String requested, List<UltiToolsPlugin> candidates)
      Refuses when more than one module answers to a declared name.
      Parameters:
      requested - the operator's argument
      candidates - the loaded modules whose JARs declare it
    • resolveIdentity

      private static PluginInstallUtils.ModuleIdentity resolveIdentity(String requested, PluginManager pluginManager, Function<UltiToolsPlugin, File> codeSource)
      Resolves the operator's argument into the one identity every later decision reads.

      Argument to loaded instances: an instance matches when its runtime name is the argument, or when the JAR it was loaded from declares that name -- the second half is gate 1's BL-01, where naming a module by what its JAR declares deleted the JAR of a module that was left running.

      Instances to keys: what their own JARs declare, plus the argument. A key another loaded module owns -- its runtime name, or what its own JAR declares -- is dropped, which is gate 1's BL-02: a name lifted off one module's JAR is not safe as a global key. Those modules' JARs are also remembered, because no decision may end in deleting one of them.

      With nothing loaded, the argument is the only key there is, and that is recorded.

      Parameters:
      requested - the operator's argument
      pluginManager - the manager that owns the plugin list
      codeSource - how to ask a loaded module which JAR it came from
      Returns:
      the resolved identity
    • recordedOf

      private static PluginInstallUtils.Recorded recordedOf(PluginManager pluginManager, List<UltiToolsPlugin> targets, List<UltiToolsPlugin> others, Set<String> ownJars)
      What the loader recorded about the targets (#516) and what they report about themselves, read before they are unloaded.
      Parameters:
      pluginManager - the manager whose start-up scan recorded them
      targets - the loaded instances this uninstall targets
      others - every other loaded instance
      ownJars - the canonical paths of the targets' code-source JARs
      Returns:
      the record
    • addNormalized

      private static void addNormalized(Set<String> into, String identifyString)
      Adds identifyString, normalised, unless it is absent or blank.
    • mainClassesOf

      private static Set<String> mainClassesOf(PluginManager pluginManager, List<UltiToolsPlugin> targets, List<UltiToolsPlugin> others)
      The main classes the module loader recorded for the targets, less every one another loaded module also has (#516) -- the same exclusion the name keys get (gate 1, BL-02).
      Parameters:
      pluginManager - the manager whose start-up scan recorded them
      targets - the loaded instances this uninstall targets
      others - every other loaded instance
      Returns:
      the main classes an entry is matched on
    • readLoadedModules

      private static void readLoadedModules(PluginManager pluginManager, Function<UltiToolsPlugin, File> codeSource, List<UltiToolsPlugin> others, Map<UltiToolsPlugin, File> jars, Map<UltiToolsPlugin, String> declared, AtomicBoolean someUnknown)
      Reads every loaded module once: which JAR it came from, and what that JAR declares.

      Read before anything is unloaded: afterwards the instance and its class loader may be gone, and with them the only authoritative answer to "which JAR is this module's". Which of them the argument names is decided afterwards, by chooseTargets(String, List, List, Map).

      Parameters:
      pluginManager - the manager that owns the plugin list
      codeSource - how to ask a loaded module which JAR it came from
      others - collects every loaded instance, before any is chosen as a target
      jars - collects each instance's code-source JAR
      declared - collects what each instance's JAR declares
      someUnknown - set when some instance's code source could not be determined at all
    • refuseIfAnotherModuleSharesTheJar

      private static void refuseIfAnotherModuleSharesTheJar(PluginInstallUtils.ModuleIdentity identity)
    • refuseToTouchAnotherModulesJar

      private static void refuseToTouchAnotherModulesJar(PluginInstallUtils.ModuleIdentity identity, List<File> toDelete)
      The invariant, checked immediately before anything is deleted: no JAR another loaded module was loaded from may be removed, whatever decided to get here.

      Gate 1's BL-02 was a bystander module losing its JAR because a name lifted off the target's JAR became the match key. Resolution no longer produces such a key, and this is the guard at the point of destruction for every other way it could happen. It fires before the first delete, so nothing is half-done when it does.

      Parameters:
      identity - the resolved identity, carrying the other loaded modules' JARs
      toDelete - the entries about to be deleted
    • modulesFolder

      private static File modulesFolder()
      The modules folder install and uninstall act on: the one PluginManager#init scans and the module class loader reads, from the one method that computes it (#517). Before 6.3.0 the scan read System.getProperty("user.dir") + /plugins/UltiTools/plugins while this read the data folder, and the two diverged on a server started from another directory.
      Returns:
      the modules folder
    • classify

      private static PluginInstallUtils.EntryState classify(File file, PluginInstallUtils.ModuleIdentity identity)
      Which PluginInstallUtils.EntryState an entry of the modules folder is in.

      The loaded instance's own code-source JAR is state A whatever it declares -- the module itself said where it came from. Otherwise the archive is read once (readArchive(File)): an entry the module loader would never load, and one declaring a module that is not this one, are state B; one that declares a key this identity owns is state A; and one that says nothing about itself -- would not open, cannot be parsed, carries no plugin.yml or no name: -- is state C.

      Parameters:
      file - the entry
      identity - the resolved identity, whose keys and own JARs decide this
      Returns:
      the state it is in
    • byDeclaredMainClass

      private static PluginInstallUtils.EntryState byDeclaredMainClass(PluginInstallUtils.ModuleIdentity identity)
      An entry whose plugin.yml declares one of the targets' main classes: the module loader loads exactly the class main: names (#548/#549), so after a restart this file loads the module the operator removed (#516). The same caution as a name match applies: while some loaded module's own JAR could not be determined, the entry may be that module's, and is reported.
      Parameters:
      identity - the resolved identity
      Returns:
      the entry's state