mirror of
https://github.com/funkemunky/AntiVPN.git
synced 2026-06-03 02:12:20 +00:00
Applying spotless
This commit is contained in:
+184
-180
@@ -35,207 +35,211 @@ import java.util.Set;
|
||||
/**
|
||||
* Classloader that can load a jar from within another jar file.
|
||||
*
|
||||
* <p>The "loader" jar contains the loading code & public API classes
|
||||
* and is class-loaded by the platform.</p>
|
||||
* <p>The "loader" jar contains the loading code & public API classes and is class-loaded by the
|
||||
* platform.
|
||||
*
|
||||
* <p>The inner "plugin" jar contains the plugin itself and is class-loaded
|
||||
* by the loading code & this classloader.</p>
|
||||
* <p>The inner "plugin" jar contains the plugin itself and is class-loaded by the loading code &
|
||||
* this classloader.
|
||||
*/
|
||||
public class JarInJarClassLoader extends URLClassLoader {
|
||||
private static final String[] PARENT_FIRST_PACKAGES = {
|
||||
"java.",
|
||||
"javax.",
|
||||
"jdk.",
|
||||
"sun.",
|
||||
"com.sun.",
|
||||
"org.w3c.",
|
||||
"org.xml.",
|
||||
"org.slf4j.",
|
||||
"com.google.inject.",
|
||||
"javax.inject.",
|
||||
"com.velocitypowered.",
|
||||
"org.bukkit.",
|
||||
"net.md_5.bungee.",
|
||||
"org.spongepowered.",
|
||||
"org.bstats.",
|
||||
"dev.brighten.antivpn.velocity.org.bstats.",
|
||||
"dev.brighten.antivpn.loader."
|
||||
};
|
||||
private static final String[] PARENT_FIRST_PACKAGES = {
|
||||
"java.",
|
||||
"javax.",
|
||||
"jdk.",
|
||||
"sun.",
|
||||
"com.sun.",
|
||||
"org.w3c.",
|
||||
"org.xml.",
|
||||
"org.slf4j.",
|
||||
"com.google.inject.",
|
||||
"javax.inject.",
|
||||
"com.velocitypowered.",
|
||||
"org.bukkit.",
|
||||
"net.md_5.bungee.",
|
||||
"org.spongepowered.",
|
||||
"org.bstats.",
|
||||
"dev.brighten.antivpn.velocity.org.bstats.",
|
||||
"dev.brighten.antivpn.loader."
|
||||
};
|
||||
|
||||
static {
|
||||
ClassLoader.registerAsParallelCapable();
|
||||
static {
|
||||
ClassLoader.registerAsParallelCapable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new jar-in-jar class loader.
|
||||
*
|
||||
* @param loaderClassLoader the loader plugin's classloader (setup and created by the platform)
|
||||
* @param jarResourcePath the path to the jar-in-jar resource within the loader jar
|
||||
* @throws LoadingException if something unexpectedly bad happens
|
||||
*/
|
||||
public JarInJarClassLoader(ClassLoader loaderClassLoader, String... jarResourcePath)
|
||||
throws LoadingException {
|
||||
super(
|
||||
Arrays.stream(jarResourcePath)
|
||||
.map(path -> extractJar(loaderClassLoader, path))
|
||||
.toArray(URL[]::new),
|
||||
loaderClassLoader);
|
||||
}
|
||||
|
||||
public void addJarToClasspath(URL url) {
|
||||
addURL(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
||||
synchronized (getClassLoadingLock(name)) {
|
||||
Class<?> loadedClass = findLoadedClass(name);
|
||||
if (loadedClass == null) {
|
||||
loadedClass =
|
||||
shouldLoadParentFirst(name) ? loadParentThenChild(name) : loadChildThenParent(name);
|
||||
}
|
||||
|
||||
if (resolve) {
|
||||
resolveClass(loadedClass);
|
||||
}
|
||||
|
||||
return loadedClass;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getResource(String name) {
|
||||
URL resource = findResource(name);
|
||||
if (resource != null) {
|
||||
return resource;
|
||||
}
|
||||
return super.getResource(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<URL> getResources(String name) throws IOException {
|
||||
Set<URL> resources = new LinkedHashSet<>();
|
||||
Enumeration<URL> childResources = findResources(name);
|
||||
while (childResources.hasMoreElements()) {
|
||||
resources.add(childResources.nextElement());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new jar-in-jar class loader.
|
||||
*
|
||||
* @param loaderClassLoader the loader plugin's classloader (setup and created by the platform)
|
||||
* @param jarResourcePath the path to the jar-in-jar resource within the loader jar
|
||||
* @throws LoadingException if something unexpectedly bad happens
|
||||
*/
|
||||
public JarInJarClassLoader(ClassLoader loaderClassLoader, String... jarResourcePath) throws LoadingException {
|
||||
super(Arrays.stream(jarResourcePath)
|
||||
.map(path -> extractJar(loaderClassLoader, path))
|
||||
.toArray(URL[]::new), loaderClassLoader);
|
||||
Enumeration<URL> parentResources =
|
||||
getParent() == null ? ClassLoader.getSystemResources(name) : getParent().getResources(name);
|
||||
while (parentResources.hasMoreElements()) {
|
||||
resources.add(parentResources.nextElement());
|
||||
}
|
||||
|
||||
public void addJarToClasspath(URL url) {
|
||||
addURL(url);
|
||||
return Collections.enumeration(resources);
|
||||
}
|
||||
|
||||
private Class<?> loadChildThenParent(String name) throws ClassNotFoundException {
|
||||
try {
|
||||
return findClass(name);
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
return super.loadClass(name, false);
|
||||
}
|
||||
}
|
||||
|
||||
private Class<?> loadParentThenChild(String name) throws ClassNotFoundException {
|
||||
try {
|
||||
return super.loadClass(name, false);
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
return findClass(name);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldLoadParentFirst(String className) {
|
||||
for (String prefix : PARENT_FIRST_PACKAGES) {
|
||||
if (className.startsWith(prefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void deleteJarResource() {
|
||||
URL[] urls = getURLs();
|
||||
if (urls.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
||||
synchronized (getClassLoadingLock(name)) {
|
||||
Class<?> loadedClass = findLoadedClass(name);
|
||||
if (loadedClass == null) {
|
||||
loadedClass = shouldLoadParentFirst(name) ? loadParentThenChild(name) : loadChildThenParent(name);
|
||||
}
|
||||
try {
|
||||
Path path = Paths.get(urls[0].toURI());
|
||||
Files.deleteIfExists(path);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
if (resolve) {
|
||||
resolveClass(loadedClass);
|
||||
}
|
||||
|
||||
return loadedClass;
|
||||
}
|
||||
/**
|
||||
* Creates a new plugin instance.
|
||||
*
|
||||
* @param bootstrapClass the name of the bootstrap plugin class
|
||||
* @param loaderPluginType the type of the loader plugin, the only parameter of the bootstrap
|
||||
* plugin constructor
|
||||
* @param loaderPlugin the loader plugin instance
|
||||
* @param <T> the type of the loader plugin
|
||||
* @return the instantiated bootstrap plugin
|
||||
*/
|
||||
public <T> LoaderBootstrap instantiatePlugin(
|
||||
String bootstrapClass, Class<T> loaderPluginType, T loaderPlugin) throws LoadingException {
|
||||
Class<? extends LoaderBootstrap> plugin;
|
||||
try {
|
||||
plugin = loadClass(bootstrapClass).asSubclass(LoaderBootstrap.class);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new LoadingException("Unable to load bootstrap class", e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getResource(String name) {
|
||||
URL resource = findResource(name);
|
||||
if (resource != null) {
|
||||
return resource;
|
||||
}
|
||||
return super.getResource(name);
|
||||
Constructor<? extends LoaderBootstrap> constructor;
|
||||
try {
|
||||
constructor = plugin.getConstructor(loaderPluginType);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new LoadingException("Unable to get bootstrap constructor", e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<URL> getResources(String name) throws IOException {
|
||||
Set<URL> resources = new LinkedHashSet<>();
|
||||
Enumeration<URL> childResources = findResources(name);
|
||||
while (childResources.hasMoreElements()) {
|
||||
resources.add(childResources.nextElement());
|
||||
}
|
||||
try {
|
||||
return constructor.newInstance(loaderPlugin);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new LoadingException("Unable to create bootstrap plugin instance", e);
|
||||
}
|
||||
}
|
||||
|
||||
Enumeration<URL> parentResources = getParent() == null
|
||||
? ClassLoader.getSystemResources(name)
|
||||
: getParent().getResources(name);
|
||||
while (parentResources.hasMoreElements()) {
|
||||
resources.add(parentResources.nextElement());
|
||||
}
|
||||
|
||||
return Collections.enumeration(resources);
|
||||
/**
|
||||
* Extracts the "jar-in-jar" from the loader plugin into a temporary file, then returns a URL that
|
||||
* can be used by the {@link JarInJarClassLoader}.
|
||||
*
|
||||
* @param loaderClassLoader the classloader for the "host" loader plugin
|
||||
* @param jarResourcePath the inner jar resource path
|
||||
* @return a URL to the extracted file
|
||||
*/
|
||||
private static URL extractJar(ClassLoader loaderClassLoader, String jarResourcePath)
|
||||
throws LoadingException {
|
||||
// get the jar-in-jar resource
|
||||
URL jarInJar = loaderClassLoader.getResource(jarResourcePath);
|
||||
if (jarInJar == null) {
|
||||
throw new LoadingException("Could not locate jar-in-jar");
|
||||
}
|
||||
|
||||
private Class<?> loadChildThenParent(String name) throws ClassNotFoundException {
|
||||
try {
|
||||
return findClass(name);
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
return super.loadClass(name, false);
|
||||
}
|
||||
// create a temporary file
|
||||
// on posix systems; by default, this is only read/writable by the process owner
|
||||
Path path;
|
||||
try {
|
||||
path = Files.createTempFile(jarResourcePath, ".jar.tmp");
|
||||
} catch (IOException e) {
|
||||
throw new LoadingException("Unable to create a temporary file", e);
|
||||
}
|
||||
|
||||
private Class<?> loadParentThenChild(String name) throws ClassNotFoundException {
|
||||
try {
|
||||
return super.loadClass(name, false);
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
return findClass(name);
|
||||
}
|
||||
// mark that the file should be deleted on exit
|
||||
path.toFile().deleteOnExit();
|
||||
|
||||
// copy the jar-in-jar to the temporary file path
|
||||
try (InputStream in = jarInJar.openStream()) {
|
||||
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
throw new LoadingException("Unable to copy jar-in-jar to temporary path", e);
|
||||
}
|
||||
|
||||
private static boolean shouldLoadParentFirst(String className) {
|
||||
for (String prefix : PARENT_FIRST_PACKAGES) {
|
||||
if (className.startsWith(prefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
try {
|
||||
return path.toUri().toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
throw new LoadingException("Unable to get URL from path", e);
|
||||
}
|
||||
|
||||
public void deleteJarResource() {
|
||||
URL[] urls = getURLs();
|
||||
if (urls.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Path path = Paths.get(urls[0].toURI());
|
||||
Files.deleteIfExists(path);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new plugin instance.
|
||||
*
|
||||
* @param bootstrapClass the name of the bootstrap plugin class
|
||||
* @param loaderPluginType the type of the loader plugin, the only parameter of the bootstrap
|
||||
* plugin constructor
|
||||
* @param loaderPlugin the loader plugin instance
|
||||
* @param <T> the type of the loader plugin
|
||||
* @return the instantiated bootstrap plugin
|
||||
*/
|
||||
public <T> LoaderBootstrap instantiatePlugin(String bootstrapClass, Class<T> loaderPluginType, T loaderPlugin) throws LoadingException {
|
||||
Class<? extends LoaderBootstrap> plugin;
|
||||
try {
|
||||
plugin = loadClass(bootstrapClass).asSubclass(LoaderBootstrap.class);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new LoadingException("Unable to load bootstrap class", e);
|
||||
}
|
||||
|
||||
Constructor<? extends LoaderBootstrap> constructor;
|
||||
try {
|
||||
constructor = plugin.getConstructor(loaderPluginType);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new LoadingException("Unable to get bootstrap constructor", e);
|
||||
}
|
||||
|
||||
try {
|
||||
return constructor.newInstance(loaderPlugin);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new LoadingException("Unable to create bootstrap plugin instance", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the "jar-in-jar" from the loader plugin into a temporary file,
|
||||
* then returns a URL that can be used by the {@link JarInJarClassLoader}.
|
||||
*
|
||||
* @param loaderClassLoader the classloader for the "host" loader plugin
|
||||
* @param jarResourcePath the inner jar resource path
|
||||
* @return a URL to the extracted file
|
||||
*/
|
||||
private static URL extractJar(ClassLoader loaderClassLoader, String jarResourcePath) throws LoadingException {
|
||||
// get the jar-in-jar resource
|
||||
URL jarInJar = loaderClassLoader.getResource(jarResourcePath);
|
||||
if (jarInJar == null) {
|
||||
throw new LoadingException("Could not locate jar-in-jar");
|
||||
}
|
||||
|
||||
// create a temporary file
|
||||
// on posix systems; by default, this is only read/writable by the process owner
|
||||
Path path;
|
||||
try {
|
||||
path = Files.createTempFile(jarResourcePath, ".jar.tmp");
|
||||
} catch (IOException e) {
|
||||
throw new LoadingException("Unable to create a temporary file", e);
|
||||
}
|
||||
|
||||
// mark that the file should be deleted on exit
|
||||
path.toFile().deleteOnExit();
|
||||
|
||||
// copy the jar-in-jar to the temporary file path
|
||||
try (InputStream in = jarInJar.openStream()) {
|
||||
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
throw new LoadingException("Unable to copy jar-in-jar to temporary path", e);
|
||||
}
|
||||
|
||||
try {
|
||||
return path.toUri().toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
throw new LoadingException("Unable to get URL from path", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,17 +18,12 @@ package dev.brighten.antivpn.loader;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Minimal bootstrap plugin, called by the loader plugin.
|
||||
*/
|
||||
/** Minimal bootstrap plugin, called by the loader plugin. */
|
||||
public interface LoaderBootstrap {
|
||||
|
||||
void onLoad(File dataFolder);
|
||||
void onLoad(File dataFolder);
|
||||
|
||||
default void onEnable() {
|
||||
}
|
||||
|
||||
default void onDisable() {
|
||||
}
|
||||
default void onEnable() {}
|
||||
|
||||
default void onDisable() {}
|
||||
}
|
||||
|
||||
+7
-10
@@ -16,17 +16,14 @@
|
||||
|
||||
package dev.brighten.antivpn.loader;
|
||||
|
||||
/**
|
||||
* Runtime exception used if there is a problem during loading
|
||||
*/
|
||||
/** Runtime exception used if there is a problem during loading */
|
||||
public class LoadingException extends RuntimeException {
|
||||
|
||||
public LoadingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public LoadingException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
public LoadingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public LoadingException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user