Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
Minor cleanup of spigot subproject
Browse files Browse the repository at this point in the history
  • Loading branch information
PiggyPiglet committed Oct 2, 2020
1 parent 4ec8485 commit 3cfc2ab
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 130 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,6 @@ run/

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

# Ignore gradle configuration directory
.gradle/
5 changes: 3 additions & 2 deletions pdm/src/main/java/me/bristermitten/pdm/DependencyLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void loadDependency(@Nullable final File file)
{
return;
}

if (loaded.contains(file))
{
return;
Expand All @@ -46,9 +47,9 @@ public void loadDependency(@Nullable final File file)
ClassLoaderReflection.addURL(classLoader, file.toURI().toURL());
loaded.add(file);
}
catch (MalformedURLException e)
catch (MalformedURLException exception)
{
logger.log(Level.SEVERE, e, () -> "Could not load dependency from file " + file);
logger.log(Level.SEVERE, exception, () -> "Could not load dependency from file " + file);
}
}
}
79 changes: 44 additions & 35 deletions pdm/src/main/java/me/bristermitten/pdm/DependencyManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.bristermitten.pdm;

import me.bristermitten.pdm.repository.SpigotRepository;
import me.bristermitten.pdm.util.FileUtil;
import me.bristermitten.pdm.util.FileUtils;
import me.bristermitten.pdmlibs.artifact.Artifact;
import me.bristermitten.pdmlibs.artifact.ArtifactFactory;
import me.bristermitten.pdmlibs.http.HTTPService;
Expand Down Expand Up @@ -31,14 +31,13 @@ public class DependencyManager

public static final String PDM_DIRECTORY_NAME = "PluginLibraries";

@NotNull
private final PDMSettings settings;
@NotNull private final PDMSettings settings;

private final @NotNull RepositoryManager repositoryManager;
private final @NotNull MavenRepositoryFactory repositoryFactory;
private final @NotNull DependencyLoader loader;
@NotNull private final RepositoryManager repositoryManager;
@NotNull private final MavenRepositoryFactory repositoryFactory;
@NotNull private final DependencyLoader loader;
private final ArtifactFactory artifactFactory = new ArtifactFactory();
private final @NotNull HTTPService httpService;
@NotNull private final HTTPService httpService;

/**
* A Map that caches download tasks for artifacts.
Expand All @@ -48,15 +47,16 @@ public class DependencyManager
*/
private final Map<Artifact, CompletableFuture<File>> downloadsInProgress = new ConcurrentHashMap<>();
private final Logger logger;
private final @NotNull DefaultParseProcess parseProcess;
@NotNull private final DefaultParseProcess parseProcess;
private File pdmDirectory;

public DependencyManager(@NotNull final PDMSettings settings, @NotNull HTTPService httpService)
public DependencyManager(@NotNull final PDMSettings settings, @NotNull final HTTPService httpService)
{
this(settings, PDM_DIRECTORY_NAME, httpService);
}

public DependencyManager(@NotNull final PDMSettings settings, @NotNull String outputDirectoryName, @NotNull HTTPService httpService)
public DependencyManager(@NotNull final PDMSettings settings, @NotNull final String outputDirectoryName,
@NotNull final HTTPService httpService)
{
this.settings = settings;
this.logger = settings.getLoggerSupplier().apply(getClass().getName());
Expand All @@ -65,7 +65,6 @@ public DependencyManager(@NotNull final PDMSettings settings, @NotNull String ou

this.repositoryManager = new RepositoryManager(settings.getLoggerSupplier().apply(RepositoryManager.class.getName()));


this.parseProcess = new DefaultParseProcess(artifactFactory, repositoryManager, httpService);
this.repositoryFactory = new MavenRepositoryFactory(httpService, parseProcess);

Expand All @@ -79,15 +78,16 @@ public void setOutputDirectoryName(@NotNull final String outputDirectoryName)
try
{
this.pdmDirectory = new File(settings.getRootDirectory().getCanonicalFile(), outputDirectoryName).getCanonicalFile();
FileUtil.createDirectoryIfNotPresent(pdmDirectory);
FileUtils.createDirectoryIfNotPresent(pdmDirectory);
}
catch (IOException ex)
catch (IOException exception)
{
throw new IllegalStateException(ex);
throw new IllegalStateException(exception);
}
}

public @NotNull RepositoryManager getRepositoryManager()
@NotNull
public RepositoryManager getRepositoryManager()
{
return repositoryManager;
}
Expand All @@ -99,36 +99,38 @@ SpigotRepository.SPIGOT_ALIAS, new SpigotRepository(httpService, parseProcess)
);
}

public @NotNull ArtifactFactory getArtifactFactory()
@NotNull
public ArtifactFactory getArtifactFactory()
{
return artifactFactory;
}

public @NotNull MavenRepositoryFactory getRepositoryFactory()
@NotNull
public MavenRepositoryFactory getRepositoryFactory()
{
return repositoryFactory;
}

public CompletableFuture<Void> downloadAndLoad(@NotNull Artifact dependency)
@NotNull
public CompletableFuture<Void> downloadAndLoad(@NotNull final Artifact dependency)
{
CompletableFuture<File> downloaded = download(dependency);

return downloaded.thenAccept(loader::loadDependency);
return download(dependency).thenAccept(loader::loadDependency);
}

public CompletableFuture<File> download(@NotNull Artifact dependency)
@NotNull
public CompletableFuture<File> download(@NotNull final Artifact dependency)
{
CompletableFuture<File> inProgress = downloadsInProgress.get(dependency);
final CompletableFuture<File> inProgress = downloadsInProgress.get(dependency);

if (inProgress != null)
{
return inProgress;
}

File file = new File(pdmDirectory, dependency.getJarName());

CompletableFuture<File> downloadingFuture = CompletableFuture.supplyAsync(() -> {

final File file = new File(pdmDirectory, dependency.getJarName());
final CompletableFuture<File> downloadingFuture = CompletableFuture.supplyAsync(() -> {
@Nullable final Repository containingRepo = getRepositoryFor(dependency);

if (containingRepo == null)
{
logger.warning(() -> "No repository found for " + dependency + ", it cannot be downloaded. Other plugins may not function properly.");
Expand All @@ -152,8 +154,8 @@ public CompletableFuture<File> download(@NotNull Artifact dependency)
}

return file;
}).exceptionally(t -> {
logger.log(Level.SEVERE, t, () -> "Could not download " + dependency);
}).exceptionally(throwable -> {
logger.log(Level.SEVERE, throwable, () -> "Could not download " + dependency);
downloadsInProgress.remove(dependency);
return file;
});
Expand All @@ -164,24 +166,31 @@ public CompletableFuture<File> download(@NotNull Artifact dependency)
return downloadingFuture;
}

@NotNull
private Set<CompletableFuture<Void>> downloadTransitiveDependencies(@NotNull final Repository repository, @NotNull final Artifact artifact)
{
logger.fine(() -> "Downloading Transitive Dependencies for " + artifact);

Set<Artifact> transitiveDependencies = artifact.getTransitiveDependencies();

if (transitiveDependencies == null)
{
transitiveDependencies = repository.getTransitiveDependencies(artifact);
artifact.setTransitiveDependencies(transitiveDependencies); //To save potential repeated lookups
}

return transitiveDependencies.stream().map(this::downloadAndLoad).collect(Collectors.toSet());
return transitiveDependencies.stream()
.map(this::downloadAndLoad)
.collect(Collectors.toSet());
}

@Nullable
private Repository getRepositoryFor(@NotNull final Artifact artifact)
{
if (artifact.getRepoAlias() != null)
{
Repository byURL = repositoryManager.getByAlias(artifact.getRepoAlias());
final Repository byURL = repositoryManager.getByAlias(artifact.getRepoAlias());

if (byURL == null)
{
logger.warning(() -> "No repository configured for " + artifact.getRepoAlias());
Expand All @@ -196,14 +205,14 @@ private Repository getRepositoryFor(@NotNull final Artifact artifact)

private void writeToFile(@NotNull final InputStream data, @NotNull final File file)
{
FileUtil.createDirectoryIfNotPresent(pdmDirectory);
FileUtils.createDirectoryIfNotPresent(pdmDirectory);
try
{
FileUtil.writeFrom(file, data);
FileUtils.writeFrom(file, data);
}
catch (IOException e)
catch (IOException exception)
{
logger.log(Level.SEVERE, e, () -> "Could not copy file for " + file);
logger.log(Level.SEVERE, exception, () -> "Could not copy file for " + file);
}
}
}
90 changes: 78 additions & 12 deletions pdm/src/main/java/me/bristermitten/pdm/PDMBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@
/**
* @author AlexL
*/
@SuppressWarnings("UnusedReturnValue")
public final class PDMBuilder
{

public static final String DEPENDENCIES_RESOURCE_NAME = "dependencies.json";
public static final String PLUGIN_CLASS_LOADER_NAME = "org.bukkit.plugin.java.PluginClassLoader";
private Function<String, Logger> loggerFactory = Logger::getLogger;
private @Nullable InputStream dependenciesResource = null;
private @Nullable File rootDirectory = null;
private @Nullable URLClassLoader classLoader = null;
private @Nullable String applicationName = null;
private @Nullable String applicationVersion = null;
@Nullable private InputStream dependenciesResource = null;
@Nullable private File rootDirectory = null;
@Nullable private URLClassLoader classLoader = null;
@Nullable private String applicationName = null;
@Nullable private String applicationVersion = null;
private CacheConfiguration cacheConfiguration = CacheConfiguration.builder().build();

/**
* @deprecated Use one of the static factory methods; the direct replacement for this method is {@link #builder(Plugin)}.
* @param plugin Plugin implementation instance
*/
@Deprecated
public PDMBuilder(@NotNull final Plugin plugin)
{
loggerFactory(clazz -> plugin.getLogger());
Expand All @@ -42,6 +48,11 @@ public PDMBuilder(@NotNull final Plugin plugin)
applicationVersion(plugin.getDescription().getVersion());
}

/**
* @deprecated Use one of the static factory methods; the direct replacement for this method is {@link #builder(Class)}
* @param plugin Plugin implementation class
*/
@Deprecated
public PDMBuilder(@NotNull final Class<? extends Plugin> plugin)
{
Validate.isTrue(PLUGIN_CLASS_LOADER_NAME.equals(plugin.getClassLoader().getClass().getName()), "Plugin must be loaded with a PluginClassLoader");
Expand All @@ -54,48 +65,103 @@ public PDMBuilder(@NotNull final Class<? extends Plugin> plugin)
loggerFactory(clazz -> Logger.getLogger(description.getName()));
}

/**
* @deprecated Do not construct an instance of this class manually.
*/
@Deprecated
public PDMBuilder()
{

}

public @NotNull PDMBuilder loggerFactory(@NotNull Function<String, Logger> loggerFactory)
@NotNull
public static PDMBuilder builder() {
return new PDMBuilder();
}

@NotNull
public static PDMBuilder builder(@NotNull final Plugin plugin) {
return new PDMBuilder()
.loggerFactory(clazz -> plugin.getLogger())
.dependenciesResource(plugin.getResource(DEPENDENCIES_RESOURCE_NAME))
.rootDirectory(plugin.getDataFolder().getParentFile())
.classLoader((URLClassLoader) plugin.getClass().getClassLoader())
.applicationName(plugin.getName())
.applicationVersion(plugin.getDescription().getVersion());
}

@NotNull
public static PDMBuilder builder(@NotNull final Class<? extends Plugin> plugin) {
Validate.isTrue(PLUGIN_CLASS_LOADER_NAME.equals(plugin.getClassLoader().getClass().getName()), "Plugin must be loaded with a PluginClassLoader");

final URLClassLoader classLoader = (URLClassLoader) plugin.getClassLoader();
final PluginDescriptionFile description = Reflection.getFieldValue(classLoader, "description");

return builder()
.classLoader((URLClassLoader) plugin.getClassLoader())
.dependenciesResource(classLoader.getResourceAsStream(DEPENDENCIES_RESOURCE_NAME))
.rootDirectory(new File("./plugins"))
.applicationName(description.getName())
.applicationVersion(description.getVersion())
.loggerFactory(clazz -> Logger.getLogger(description.getName()));
}

@NotNull
public static PluginDependencyManager of(@NotNull final Plugin plugin) {
return builder(plugin)
.build();
}

@NotNull
public static PluginDependencyManager of(@NotNull final Class<? extends Plugin> plugin) {
return builder(plugin)
.build();
}

@NotNull
public PDMBuilder loggerFactory(@NotNull final Function<String, Logger> loggerFactory)
{
this.loggerFactory = loggerFactory;
return this;
}

public @NotNull PDMBuilder dependenciesResource(InputStream dependenciesResource)
@NotNull
public PDMBuilder dependenciesResource(@NotNull final InputStream dependenciesResource)
{
this.dependenciesResource = dependenciesResource;
return this;
}

public @NotNull PDMBuilder rootDirectory(@NotNull File rootDirectory)
@NotNull
public PDMBuilder rootDirectory(@NotNull final File rootDirectory)
{
this.rootDirectory = rootDirectory;
return this;
}

public @NotNull PDMBuilder classLoader(@NotNull URLClassLoader classLoader)
@NotNull
public PDMBuilder classLoader(@NotNull URLClassLoader classLoader)
{
this.classLoader = classLoader;
return this;
}

public @NotNull PDMBuilder applicationName(@NotNull String applicationName)
@NotNull
public PDMBuilder applicationName(@NotNull String applicationName)
{
this.applicationName = applicationName;
return this;
}

public @NotNull PDMBuilder applicationVersion(@NotNull String applicationVersion)
@NotNull
public PDMBuilder applicationVersion(@NotNull String applicationVersion)
{
this.applicationVersion = applicationVersion;
return this;
}

public @NotNull PDMBuilder caching(@NotNull Consumer<CacheConfiguration.Builder> configuration)
@NotNull
public PDMBuilder caching(@NotNull Consumer<CacheConfiguration.Builder> configuration)
{
final CacheConfiguration.Builder builder = CacheConfiguration.builder();
configuration.accept(builder);
Expand Down
Loading

0 comments on commit 3cfc2ab

Please sign in to comment.