All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bjoernpetersen.jmusicbot.PluginLoader Maven / Gradle / Ivy

There is a newer version: 0.25.0
Show newest version
package com.github.bjoernpetersen.jmusicbot;

import java.io.File;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import javax.annotation.Nonnull;

public final class PluginLoader implements Loggable {

  private static ClassLoader loader;

  @Nonnull
  private final File pluginFolder;
  @Nonnull
  private final Class type;

  public PluginLoader(@Nonnull File pluginFolder, @Nonnull Class type) {
    this.pluginFolder = pluginFolder;
    this.type = type;
  }

  @Nonnull
  public Collection load() {
    if (!pluginFolder.isDirectory()) {
      if (!pluginFolder.exists()) {
        if (!pluginFolder.mkdirs()) {
          logWarning("Could not create plugin folder '%s'", pluginFolder.getPath());
        }
      }
      return Collections.emptyList();
    }

    ClassLoader loader;
    if (PluginLoader.loader != null) {
      loader = PluginLoader.loader;
    } else {
      File[] pluginFiles = pluginFolder.listFiles(path -> path.getName().endsWith(".jar"));
      if (pluginFiles == null) {
        return Collections.emptyList();
      }
      try {
        PluginLoader.loader = loader = createLoader(pluginFiles);
      } catch (MalformedURLException e) {
        throw new UncheckedIOException(e);
      }
    }

    List result = new LinkedList<>();
    try {
      Collection plugins = loadPlugins(loader);
      result.addAll(plugins);
      logInfo(
          "Loaded %d plugins of type '%s' from plugin folder: %s",
          plugins.size(),
          type.getSimpleName(),
          pluginFolder.getName()
      );
    } catch (Exception | Error e) {
      logSevere(e, "Error loading plugins");
    }

    return result;
  }

  @Nonnull
  private ClassLoader createLoader(@Nonnull File[] files) throws MalformedURLException {
    URL[] urls = new URL[files.length];
    for (int i = 0; i < files.length; i++) {
      urls[i] = files[i].toURI().toURL();
    }

    return new URLClassLoader(urls, getClass().getClassLoader());
  }

  @Nonnull
  private Collection loadPlugins(@Nonnull ClassLoader classLoader)
      throws MalformedURLException, ServiceConfigurationError, NoClassDefFoundError {
    ServiceLoader loader = ServiceLoader.load(type, classLoader);

    List result = new LinkedList<>();
    for (T plugin : loader) {
      if (type.isInstance(plugin)) {
        result.add(plugin);
      } else {
        logSevere(
            "Loaded plugin '%s' is not instance of desired type %s",
            plugin,
            type.getSimpleName()
        );
      }
    }
    return result;
  }

  static void reset() {
    loader = null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy