Search in sources :

Example 1 with EndpointChannelFactory

use of com.palantir.dialogue.EndpointChannelFactory in project conjure-java by palantir.

the class DialogueInterfaceGenerator method generate.

private JavaFile generate(ServiceDefinition def, ClassName className, Function<Optional<Type>, TypeName> returnTypeMapper, StaticFactoryMethodGenerator methodGenerator) {
    TypeSpec.Builder serviceBuilder = TypeSpec.interfaceBuilder(className).addModifiers(Modifier.PUBLIC).addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(DialogueInterfaceGenerator.class)).addAnnotation(AnnotationSpec.builder(DialogueService.class).addMember("value", "$T.Factory.class", className).build());
    serviceBuilder.addType(TypeSpec.classBuilder("Factory").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).addSuperinterface(ParameterizedTypeName.get(ClassName.get(DialogueServiceFactory.class), className)).addMethod(MethodSpec.methodBuilder("create").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(className).addParameter(EndpointChannelFactory.class, "endpointChannelFactory").addParameter(ConjureRuntime.class, "runtime").addStatement("return $T.of($L, $L)", className, "endpointChannelFactory", "runtime").build()).build());
    def.getDocs().ifPresent(docs -> serviceBuilder.addJavadoc("$L", StringUtils.appendIfMissing(docs.get(), "\n")));
    serviceBuilder.addMethods(def.getEndpoints().stream().map(endpoint -> apiMethod(endpoint, returnTypeMapper)).collect(toList()));
    MethodSpec staticFactoryMethod = methodGenerator.generate(def);
    serviceBuilder.addMethod(staticFactoryMethod);
    serviceBuilder.addMethod(MethodSpec.methodBuilder("of").addModifiers(Modifier.STATIC, Modifier.PUBLIC).addJavadoc("Creates an asynchronous/non-blocking client for a $L service.", def.getServiceName().getName()).returns(staticFactoryMethod.returnType).addParameter(Channel.class, StaticFactoryMethodGenerator.CHANNEL).addParameter(ConjureRuntime.class, StaticFactoryMethodGenerator.RUNTIME).addCode(CodeBlock.builder().add("if ($L instanceof $T) { return $L(($T) $L, $L); }\n", StaticFactoryMethodGenerator.CHANNEL, EndpointChannelFactory.class, staticFactoryMethod.name, EndpointChannelFactory.class, StaticFactoryMethodGenerator.CHANNEL, StaticFactoryMethodGenerator.RUNTIME).add("return $L(new $T() { " + "  @$T " + "  public $T endpoint($T endpoint) { " + "    return $L.clients().bind($L, endpoint);" + "  } " + "}, " + "$L);", staticFactoryMethod.name, EndpointChannelFactory.class, Override.class, EndpointChannel.class, Endpoint.class, StaticFactoryMethodGenerator.RUNTIME, StaticFactoryMethodGenerator.CHANNEL, StaticFactoryMethodGenerator.RUNTIME).build()).build());
    return JavaFile.builder(Packages.getPrefixedPackage(def.getServiceName().getPackage(), options.packagePrefix()), serviceBuilder.build()).build();
}
Also used : ConjureRuntime(com.palantir.dialogue.ConjureRuntime) MethodSpec(com.squareup.javapoet.MethodSpec) EndpointChannelFactory(com.palantir.dialogue.EndpointChannelFactory) DialogueServiceFactory(com.palantir.dialogue.DialogueServiceFactory) DialogueService(com.palantir.dialogue.DialogueService) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with EndpointChannelFactory

use of com.palantir.dialogue.EndpointChannelFactory in project dialogue by palantir.

the class ReloadingClientFactoryTest method live_reloading_wrapper_still_uses_the_EndpointChannelFactory_factory.

@Test
void live_reloading_wrapper_still_uses_the_EndpointChannelFactory_factory() {
    LiveReloadingChannel live = new LiveReloadingChannel(Refreshable.only(channel), runtime.clients());
    assertThat(SampleServiceAsync.of((EndpointChannelFactory) live, runtime).getMyAlias()).isNotDone();
    // ensure we use the bind method
    verify(channel, atLeastOnce()).endpoint(any());
}
Also used : LiveReloadingChannel(com.palantir.dialogue.clients.ReloadingClientFactory.LiveReloadingChannel) EndpointChannelFactory(com.palantir.dialogue.EndpointChannelFactory) Test(org.junit.jupiter.api.Test)

Example 3 with EndpointChannelFactory

use of com.palantir.dialogue.EndpointChannelFactory in project dialogue by palantir.

the class Reflection method createFromAnnotation.

private static <T> T createFromAnnotation(Class<T> dialogueInterface, DialogueService dialogueService, Channel channel, ConjureRuntime conjureRuntime) {
    Class<? extends DialogueServiceFactory<?>> serviceFactoryClass = dialogueService.value();
    EndpointChannelFactory factory = endpointChannelFactory(channel);
    Object client;
    try {
        client = serviceFactoryClass.getConstructor().newInstance().create(factory, conjureRuntime);
    } catch (NoSuchMethodException e) {
        throw new SafeIllegalArgumentException("Failed to reflectively construct dialogue client. The service factory class must have a " + "public no-arg constructor.", e, SafeArg.of("dialogueInterface", dialogueInterface), SafeArg.of("serviceFactoryClass", serviceFactoryClass));
    } catch (ReflectiveOperationException e) {
        throw new SafeIllegalArgumentException("Failed to reflectively construct dialogue client.", e, SafeArg.of("dialogueInterface", dialogueInterface), SafeArg.of("serviceFactoryClass", serviceFactoryClass));
    }
    if (dialogueInterface.isInstance(client)) {
        return dialogueInterface.cast(client);
    }
    throw new SafeIllegalArgumentException("Dialogue service factory produced an incompatible service", SafeArg.of("dialogueInterface", dialogueInterface), SafeArg.of("serviceFactoryClass", serviceFactoryClass), SafeArg.of("invalidClientType", client.getClass()), SafeArg.of("invalidClient", client));
}
Also used : EndpointChannelFactory(com.palantir.dialogue.EndpointChannelFactory) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException)

Aggregations

EndpointChannelFactory (com.palantir.dialogue.EndpointChannelFactory)3 ConjureRuntime (com.palantir.dialogue.ConjureRuntime)1 DialogueService (com.palantir.dialogue.DialogueService)1 DialogueServiceFactory (com.palantir.dialogue.DialogueServiceFactory)1 LiveReloadingChannel (com.palantir.dialogue.clients.ReloadingClientFactory.LiveReloadingChannel)1 SafeIllegalArgumentException (com.palantir.logsafe.exceptions.SafeIllegalArgumentException)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Test (org.junit.jupiter.api.Test)1