Skip to content

Commit

Permalink
Improve client factories
Browse files Browse the repository at this point in the history
Use the client builders as a parameter to clients to
allow users to configure the builders. When they are
used as parameters, the builder singleton becomes
requires and therefore can be configured with bean
creation event listener.
  • Loading branch information
andriy-dmytruk committed Nov 16, 2023
1 parent 860b15b commit 7418a44
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static Collection<String> parseOciBomArtifacts(String version) {
if (!extraPropertiesExtension.has('ociArtifacts')) {
String ociVersion = libs.oci.bom.get().version
def ociArtifacts = parseOciBomArtifacts(ociVersion)
.stream().filter(artifact -> artifact != "oci-java-sdk-shaded-full").toList()

String ociProjectVersion = new XmlSlurper().parse("checkouts/oci-java-sdk/bmc-bom/pom.xml").version.text()
def ociProjectArtifacts = parseOciBomArtifacts(ociProjectVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ protected AbstractSdkClientFactory(
}

/**
* Builds the client.
* Builds the client based on its builder to make sure user can configure
* required parameters in the builder.
*
* @param clientBuilder The builder for client
* @param authenticationDetailsProvider The authentication details provider
* @return The client to build
*/
protected abstract @NonNull T build(@NonNull AbstractAuthenticationDetailsProvider authenticationDetailsProvider);
protected abstract @NonNull T build(
@NonNull B clientBuilder,
@NonNull AbstractAuthenticationDetailsProvider authenticationDetailsProvider
);

/**
* Set the HTTP provider for this client. This is injected by the application context, in order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,10 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

if (isRxJava2) {
writeRxJava2Clients(e, packageName, simpleName);
} else {
if (isReactor) {
writeReactorClients(e, packageName, simpleName);
} else {
if (isAsync) {
factoryClassNames.add(writeClientFactory(e, packageName, simpleName));
}
}
} else if (isReactor) {
writeReactorClients(e, packageName, simpleName);
} else if (isAsync) {
factoryClassNames.add(writeClientFactory(e, packageName, simpleName));
}
}

Expand Down Expand Up @@ -414,12 +410,13 @@ private String writeClientFactory(Element e, String packageName, String simpleNa
final MethodSpec.Builder buildMethod = MethodSpec.methodBuilder("build");

buildMethod.returns(ClassName.get(packageName, simpleName))
.addParameter(builderType, "clientBuilder")
.addParameter(authProviderType, "authenticationDetailsProvider")
.addAnnotation(Singleton.class)
.addAnnotation(requiresSpec.build())
.addAnnotation(preDestroy.build())
.addModifiers(Modifier.PROTECTED)
.addCode("return builder.build(authenticationDetailsProvider);");
.addCode("return clientBuilder.build(authenticationDetailsProvider);");
if (isBootstrapCompatible) {
buildMethod.addAnnotation(BootstrapContextCompatible.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.micronaut.oraclecloud.database;

import com.oracle.bmc.database.DatabaseAsyncClient;
import com.oracle.bmc.database.DatabaseClient;
import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Singleton;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@MicronautTest
public class DatabaseFactoryTest {

public static final String ENDPOINT = "http://my_endpoint";
public static final String ASYNC_ENDPOINT = "http://my_endpoint_async";
private final DatabaseClient databaseClient;
private final DatabaseAsyncClient databaseAsyncClient;

public DatabaseFactoryTest(
DatabaseClient databaseClient,
DatabaseAsyncClient databaseAsyncClient
) {
this.databaseClient = databaseClient;
this.databaseAsyncClient = databaseAsyncClient;
}

@Test
void testDataBaseClientConfiguration() {
assertEquals(ENDPOINT, databaseClient.getEndpoint());
}

@Test
void testDataBaseAsyncClientConfiguration() {
assertEquals(ASYNC_ENDPOINT, databaseAsyncClient.getEndpoint());
}

@Singleton
static class DatabaseClientBuilderListener
implements BeanCreatedEventListener<DatabaseClient.Builder> {
@Override
public DatabaseClient.Builder onCreated(
@NonNull BeanCreatedEvent<DatabaseClient.Builder> event
) {
DatabaseClient.Builder builder = event.getBean();
builder.endpoint(DatabaseFactoryTest.ENDPOINT);
return builder;
}
}

@Singleton
static class DatabaseAsyncClientBuilderListener
implements BeanCreatedEventListener<DatabaseAsyncClient.Builder> {
@Override
public DatabaseAsyncClient.Builder onCreated(
@NonNull BeanCreatedEvent<DatabaseAsyncClient.Builder> event
) {
DatabaseAsyncClient.Builder builder = event.getBean();
builder.endpoint(DatabaseFactoryTest.ASYNC_ENDPOINT);
return builder;
}
}

}

0 comments on commit 7418a44

Please sign in to comment.