Skip to content

Commit

Permalink
Reverted some changes and refactored code to resolve Sonar warning
Browse files Browse the repository at this point in the history
  • Loading branch information
radovanradic committed Nov 29, 2024
1 parent 8528515 commit c325e1b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,46 @@ final class OracleClientInfoConnectionCustomizer implements ConnectionCustomizer
public <R> Function<ConnectionStatus<Connection>, R> intercept(Function<ConnectionStatus<Connection>, R> operation) {
return connectionStatus -> {
ConnectionDefinition connectionDefinition = connectionStatus.getDefinition();
// Set client info for connection if Oracle connection after connection is opened
// Set client info for connection if Oracle before issue JDBC call
Map<String, String> connectionClientInfo = getConnectionClientInfo(connectionDefinition);
if (CollectionUtils.isNotEmpty(connectionClientInfo)) {
Connection connection = connectionStatus.getConnection();
LOG.trace("Setting connection tracing info to the Oracle connection");
try {
for (Map.Entry<String, String> additionalInfo : connectionClientInfo.entrySet()) {
String name = additionalInfo.getKey();
String value = additionalInfo.getValue();
connection.setClientInfo(name, value);
}
} catch (SQLClientInfoException e) {
LOG.debug("Failed to set connection tracing info", e);
}
}
applyClientInfo(connectionStatus, connectionClientInfo);
try {
return operation.apply(connectionStatus);
} finally {
// Clear client info for connection if it was Oracle connection and client info was set previously
if (CollectionUtils.isNotEmpty(connectionClientInfo)) {
try {
Connection connection = connectionStatus.getConnection();
for (String key : connectionClientInfo.keySet()) {
connection.setClientInfo(key, null);
}
} catch (SQLClientInfoException e) {
LOG.debug("Failed to clear connection tracing info", e);
}
}
clearClientInfo(connectionStatus, connectionClientInfo);
}

};
}

@Override
public String getName() {
return "Oracle Connection Client Info Customizer";
private void applyClientInfo(@NonNull ConnectionStatus<Connection> connectionStatus, @NonNull Map<String, String> connectionClientInfo) {
if (CollectionUtils.isNotEmpty(connectionClientInfo)) {
Connection connection = connectionStatus.getConnection();
LOG.trace("Setting connection tracing info to the Oracle connection");
try {
for (Map.Entry<String, String> additionalInfo : connectionClientInfo.entrySet()) {
String name = additionalInfo.getKey();
String value = additionalInfo.getValue();
connection.setClientInfo(name, value);
}
} catch (SQLClientInfoException e) {
LOG.debug("Failed to set connection tracing info", e);
}
}
}

private void clearClientInfo(@NonNull ConnectionStatus<Connection> connectionStatus, @NonNull Map<String, String> connectionClientInfo) {
if (CollectionUtils.isNotEmpty(connectionClientInfo)) {
try {
Connection connection = connectionStatus.getConnection();
for (String key : connectionClientInfo.keySet()) {
connection.setClientInfo(key, null);
}
} catch (SQLClientInfoException e) {
LOG.debug("Failed to clear connection tracing info", e);
}
}
}

/**
Expand Down Expand Up @@ -186,7 +189,7 @@ private boolean isOracleConnection(Connection connection) {
if (StringUtils.isNotEmpty(applicationName)) {
clientInfoAttributes.putIfAbsent(ORACLE_CLIENT_ID, applicationName);
}
if (annotationMetadata instanceof MethodInvocationContext methodInvocationContext) {
if (annotationMetadata instanceof MethodInvocationContext<?, ?> methodInvocationContext) {
clientInfoAttributes.putIfAbsent(ORACLE_MODULE,
MODULE_CLASS_MAP.computeIfAbsent(methodInvocationContext.getTarget().getClass(),
clazz -> clazz.getName().replace(INTERCEPTED_SUFFIX, ""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ private <R> R executeWithNewConnection(@NonNull ConnectionDefinition definition,
@NonNull Function<ConnectionStatus<C>, R> callback) {
C connection = openConnection(definition);
DefaultConnectionStatus<C> status = new DefaultConnectionStatus<>(connection, definition, true);

try (PropagatedContext.Scope ignore = PropagatedContext.getOrEmpty()
.plus(new ConnectionPropagatedContextElement<>(this, status))
.propagate()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package io.micronaut.data.connection.support;

import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.naming.Named;
import io.micronaut.core.order.Ordered;
import io.micronaut.data.connection.ConnectionStatus;

Expand All @@ -36,7 +34,7 @@
* @since 4.11
*/
@Experimental
public interface ConnectionCustomizer<C> extends Named, Ordered {
public interface ConnectionCustomizer<C> extends Ordered {

/**
* Intercept the connection operation.
Expand All @@ -45,15 +43,4 @@ public interface ConnectionCustomizer<C> extends Named, Ordered {
* @return the operation callback
*/
<R> Function<ConnectionStatus<C>, R> intercept(Function<ConnectionStatus<C>, R> operation);

/**
* Returns the name of this listener. Used for logging purposes. By default, returns class simple name.
*
* @return the name of this listener
*/
@Override
@NonNull
default String getName() {
return getClass().getSimpleName();
}
}

0 comments on commit c325e1b

Please sign in to comment.