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

io.quarkus.smallrye.graphql.runtime.spi.QuarkusClassloadingService Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package io.quarkus.smallrye.graphql.runtime.spi;

import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;

import graphql.schema.PropertyDataFetcherHelper;
import io.smallrye.graphql.execution.Classes;
import io.smallrye.graphql.spi.ClassloadingService;

/**
 * Quarkus specific classloading service, that allows
 * hot reloading to work in dev mode.
 */
public class QuarkusClassloadingService implements ClassloadingService {

    /*
     * Ugly Hack
     * In Quarkus dev mode, we receive a classloader to use, when doing hot reload
     * This hack is required because using the TCCL would get an outdated version - the initial one.
     * This is because the worker thread on which the handler is called captures the TCCL at creation time
     * and does not allow updating it.
     *
     * In non dev mode, the TCCL is used.
     *
     * TODO: remove this once the vert.x class loader issues are resolved.
     */
    private static volatile ClassLoader classLoader;

    @Override
    public String getName() {
        return "Quarkus";
    }

    @Override
    public Class loadClass(String className) {
        try {
            if (Classes.isPrimitive(className)) {
                return Classes.getPrimativeClassType(className);
            } else {
                return AccessController.doPrivileged((PrivilegedExceptionAction>) () -> {
                    ClassLoader cl = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader;
                    return loadClass(className, cl);
                });
            }
        } catch (PrivilegedActionException | ClassNotFoundException pae) {
            throw new RuntimeException("Can not load class [" + className + "]", pae);
        }
    }

    public static void setClassLoader(ClassLoader classLoader) {
        QuarkusClassloadingService.classLoader = classLoader;
        PropertyDataFetcherHelper.clearReflectionCache();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy