Search in sources :

Example 1 with NessieApi

use of org.projectnessie.client.api.NessieApi in project nessie by projectnessie.

the class AbstractNessieApiHolder method createNessieClient.

/**
 * Used to construct {@link NessieApi} instances, for both current (in-tree) and old Nessie
 * versions.
 *
 * <p>Must use {@link AutoCloseable} instead of {@link NessieApi}, because it loads the class via
 * the given class loader, so instances of {@link NessieApi} for <em>old</em> Nessie versions will
 * return a different class.
 */
protected static AutoCloseable createNessieClient(ClassLoader classLoader, ClientKey clientKey) {
    try {
        Class<?> builderClazz = classLoader.loadClass(clientKey.getBuilderClass());
        Object builderInstance = builderClazz.getMethod("builder").invoke(null);
        Method fromConfigMethod = builderInstance.getClass().getMethod("fromConfig", Function.class);
        Function<String, String> getCfg = k -> {
            String v = clientKey.getConfigs().get(k);
            if (v != null) {
                return v;
            }
            return System.getProperty(k);
        };
        builderInstance = fromConfigMethod.invoke(builderInstance, getCfg);
        Class<?> targetClass = classLoader.loadClass(clientKey.getType().getName());
        Method buildMethod = builderInstance.getClass().getMethod("build", Class.class);
        Object apiInstance = buildMethod.invoke(builderInstance, targetClass);
        LOGGER.info("Created Nessie client for version {} for {}", clientKey.getVersion(), getCfg.apply("nessie.uri"));
        return (AutoCloseable) apiInstance;
    } catch (InvocationTargetException e) {
        throw throwUnchecked(e.getTargetException());
    } catch (Exception e) {
        throw throwUnchecked(e);
    }
}
Also used : NessieAPI(org.projectnessie.tools.compatibility.api.NessieAPI) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) CloseableResource(org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource) NessieApi(org.projectnessie.client.api.NessieApi) HashMap(java.util.HashMap) AnnotationSupport.findRepeatableAnnotations(org.junit.platform.commons.support.AnnotationSupport.findRepeatableAnnotations) Field(java.lang.reflect.Field) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Util.throwUnchecked(org.projectnessie.tools.compatibility.internal.Util.throwUnchecked) Function(java.util.function.Function) InvocationTargetException(java.lang.reflect.InvocationTargetException) Util.extensionStore(org.projectnessie.tools.compatibility.internal.Util.extensionStore) Map(java.util.Map) URI(java.net.URI) Method(java.lang.reflect.Method) NessieApiBuilderProperty(org.projectnessie.tools.compatibility.api.NessieApiBuilderProperty) Version(org.projectnessie.tools.compatibility.api.Version) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with NessieApi

use of org.projectnessie.client.api.NessieApi in project nessie by projectnessie.

the class AbstractNessieApiHolder method createClientKey.

private static ClientKey createClientKey(ExtensionContext context, Field field, Version version, Function<ExtensionContext, NessieServer> nessieServerSupplier) {
    Map<String, String> configs = buildApiBuilderConfig(context, field, nessieServerSupplier);
    // This method is only called for fields that are annotated with NessieAPI.
    NessieAPI nessieAPI = field.getAnnotation(NessieAPI.class);
    @SuppressWarnings("unchecked") Class<? extends NessieApi> apiType = (Class<? extends NessieApi>) field.getType();
    ClientKey clientKey = new ClientKey(version, nessieAPI.builderClassName(), apiType, configs);
    return clientKey;
}
Also used : NessieApi(org.projectnessie.client.api.NessieApi) NessieAPI(org.projectnessie.tools.compatibility.api.NessieAPI)

Aggregations

NessieApi (org.projectnessie.client.api.NessieApi)2 NessieAPI (org.projectnessie.tools.compatibility.api.NessieAPI)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Function (java.util.function.Function)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1 CloseableResource (org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource)1 AnnotationSupport.findRepeatableAnnotations (org.junit.platform.commons.support.AnnotationSupport.findRepeatableAnnotations)1 NessieApiBuilderProperty (org.projectnessie.tools.compatibility.api.NessieApiBuilderProperty)1 Version (org.projectnessie.tools.compatibility.api.Version)1 Util.extensionStore (org.projectnessie.tools.compatibility.internal.Util.extensionStore)1 Util.throwUnchecked (org.projectnessie.tools.compatibility.internal.Util.throwUnchecked)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1