Skip to content

Commit

Permalink
Issue helidon-io#6991 - Blocking DB Client: Interceptors API and init…
Browse files Browse the repository at this point in the history
…ial implementation in JDBC

Signed-off-by: Tomas Kraus <[email protected]>
  • Loading branch information
Tomas-Kraus committed Jul 3, 2023
1 parent 4a7f5f9 commit 407869a
Show file tree
Hide file tree
Showing 25 changed files with 1,105 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*/
package io.helidon.dbclient.common;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import io.helidon.common.GenericType;
import io.helidon.common.mapper.MapperManager;
import io.helidon.config.Config;
import io.helidon.dbclient.DbClientService;
import io.helidon.dbclient.DbMapper;
import io.helidon.dbclient.DbMapperManager;
import io.helidon.dbclient.DbStatements;
Expand All @@ -43,11 +46,13 @@ public abstract class CommonClientBuilder<T extends CommonClientBuilder<T>>
private DbStatements statements;
private MapperManager mapperManager;
private DbMapperManager dbMapperManager;
private final List<DbClientService> clientServices;

/**
* Creates an instance of {@link CommonClientBuilder}.
*/
protected CommonClientBuilder() {
this.clientServices = new LinkedList<>();
}

@Override
Expand Down Expand Up @@ -127,6 +132,12 @@ public T addMapperProvider(DbMapperProvider provider) {
return identity();
}

@Override
public T addService(DbClientService clientService) {
clientServices.add(clientService);
return identity();
}

/**
* Get database URL.
*
Expand Down Expand Up @@ -163,9 +174,15 @@ public DbStatements statements() {
return statements;
}

// List<DbClientService> clientServices() {
// return List.copyOf(clientServices);
// }
/**
* Get configured client services (interceptors).
* List of services is converted to unmodifiable List.
*
* @return client services
*/
public List<DbClientService> clientServices() {
return List.copyOf(clientServices);
}

/**
* Get {@link io.helidon.common.mapper.Mapper} manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package io.helidon.dbclient.common;

import java.util.List;

import io.helidon.common.mapper.MapperManager;
import io.helidon.dbclient.DbClientService;
import io.helidon.dbclient.DbMapperManager;
import io.helidon.dbclient.DbStatements;

Expand All @@ -29,14 +32,14 @@ public class CommonClientContext {

private final DbMapperManager dbMapperManager;
private final MapperManager mapperManager;
//private final List<DbClientService> clientServices;
private final List<DbClientService> clientServices;
private final DbStatements statements;
private final String dbType;

private CommonClientContext(Builder builder) {
this.dbMapperManager = builder.dbMapperManager;
this.mapperManager = builder.mapperManager;
// this.clientServices = builder.clientServices;
this.clientServices = builder.clientServices;
this.statements = builder.statements;
this.dbType = builder.dbType;
}
Expand Down Expand Up @@ -68,6 +71,15 @@ public MapperManager mapperManager() {
return mapperManager;
}

/**
* Configured client services (interceptors).
*
* @return client services
*/
public List<DbClientService> clientServices() {
return clientServices;
}

/**
* Type of this database provider (such as jdbc:mysql, mongoDB etc.).
*
Expand All @@ -81,18 +93,19 @@ public String dbType() {
* Create Helidon database client context.
*
* @param statements configured statements
* @param clientServices configured client services (interceptors)
* @param dbMapperManager configured DB Mapper manager
* @param mapperManager configured mapper manager
* @param dbType type of this database provider
* @return database client context instance
*/
public static CommonClientContext create(DbStatements statements,
/*List<DbClientService> clientServices,*/
List<DbClientService> clientServices,
DbMapperManager dbMapperManager,
MapperManager mapperManager,
String dbType) {
return builder().statements(statements)
// .clientServices(clientServices)
.clientServices(clientServices)
.dbMapperManager(dbMapperManager)
.mapperManager(mapperManager)
.dbType(dbType)
Expand All @@ -115,7 +128,7 @@ public static class Builder implements io.helidon.common.Builder<Builder, Common

private DbMapperManager dbMapperManager;
private MapperManager mapperManager;
//private List<DbClientService> clientServices;
private List<DbClientService> clientServices;
private DbStatements statements;
private String dbType;

Expand Down Expand Up @@ -152,16 +165,16 @@ public Builder mapperManager(MapperManager mapperManager) {
return this;
}

// /**
// * Configure the client services to use.
// *
// * @param clientServices client service list
// * @return updated builder instance
// */
// public Builder clientServices(List<DbClientService> clientServices) {
// this.clientServices = clientServices;
// return me;
// }
/**
* Configure the client services to use.
*
* @param clientServices client service list
* @return updated builder instance
*/
public Builder clientServices(List<DbClientService> clientServices) {
this.clientServices = clientServices;
return this;
}

/**
* Configure the db statements to use.
Expand Down
4 changes: 4 additions & 0 deletions dbclient/dbclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-context</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-mapper</artifactId>
Expand Down
52 changes: 40 additions & 12 deletions dbclient/dbclient/src/main/java/io/helidon/dbclient/DbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
package io.helidon.dbclient;

import java.util.Arrays;
import java.util.List;
import java.util.ServiceLoader;
import java.util.function.Supplier;

import io.helidon.common.HelidonServiceLoader;
import io.helidon.common.mapper.MapperManager;
import io.helidon.config.Config;
import io.helidon.dbclient.spi.DbClientBuilder;
import io.helidon.dbclient.spi.DbClientProvider;
import io.helidon.dbclient.spi.DbClientServiceProvider;
import io.helidon.dbclient.spi.DbMapperProvider;

/**
Expand Down Expand Up @@ -156,6 +161,8 @@ final class Builder implements io.helidon.common.Builder<Builder, DbClient> {
// DbClient configuration
private Config config = Config.empty();

private final HelidonServiceLoader.Builder<DbClientServiceProvider> clientServiceProviders = HelidonServiceLoader.builder(
ServiceLoader.load(DbClientServiceProvider.class));

/**
* Create an instance of Helidon database handler builder.
Expand All @@ -174,18 +181,17 @@ private Builder(DbClientProvider dbClientProvider) {
@Override
public DbClient build() {
// add client services from service loader
// Config servicesConfig = config.get("services");
// List<DbClientServiceProvider> providers = clientServiceProviders.build().asList();
// for (DbClientServiceProvider provider : providers) {
// Config providerConfig = servicesConfig.get(provider.configKey());
// if (!providerConfig.exists()) {
// // this client service is on classpath, yet there is no configuration for it, so it is ignored
// continue;
// }
//
// provider.create(providerConfig)
// .forEach(this::addService);
// }
Config servicesConfig = config.get("services");
List<DbClientServiceProvider> providers = clientServiceProviders.build().asList();
for (DbClientServiceProvider provider : providers) {
Config providerConfig = servicesConfig.get(provider.configKey());
if (!providerConfig.exists()) {
// this client service is on classpath, yet there is no configuration for it, so it is ignored
continue;
}
provider.create(providerConfig)
.forEach(this::addService);
}

return clientBuilder.build();
}
Expand Down Expand Up @@ -238,6 +244,28 @@ public Builder mapperManager(MapperManager manager) {
return this;
}

/**
* Add a client service.
*
* @param clientService clientService to apply
* @return updated builder instance
*/
public Builder addService(DbClientService clientService) {
clientBuilder.addService(clientService);
return this;
}

/**
* Add a client service.
*
* @param clientServiceSupplier supplier of client service
* @return updated builder instance
*/
public Builder addService(Supplier<? extends DbClientService> clientServiceSupplier) {
clientBuilder.addService(clientServiceSupplier.get());
return this;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.dbclient;

/**
* Services can modify the data used to execute a statement as well as
* react on a statement result.
* <p>
* Example of such services: tracing, metrics.
* <p>
* Order of execution of services is based on the order they are registered in a builder, or by their priority when
* loaded from a Java Service loader
*/
@FunctionalInterface
public interface DbClientService {

/**
* Statement execution to be intercepted.
* This method is called before the statement execution starts.
*
* @param context interceptor context
* @return single that completes when this service is finished
*/
DbClientServiceContext statement(DbClientServiceContext context);

}
Loading

0 comments on commit 407869a

Please sign in to comment.