Skip to content

Commit

Permalink
refactor: Consolidate exception handling, logging, and JavaDocs
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Vitale <[email protected]>
  • Loading branch information
ThomasVitale committed Mar 24, 2024
1 parent 852173e commit e6f4682
Show file tree
Hide file tree
Showing 45 changed files with 56 additions and 130 deletions.
2 changes: 1 addition & 1 deletion arconia-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ publishing {
mavenJava(MavenPublication) {
pom {
name = "Arconia Core"
description = "Core interfaces and classes used by the Arconia framework."
description = "Arconia Core."
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
/**
* An implementation of {@link TenantKeyGenerator} that generates cache keys combining the
* current tenant identifier with the given method and parameters.
*
* @author Thomas Vitale
*/
public final class DefaultTenantKeyGenerator implements TenantKeyGenerator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
/**
* Cache key generator. Used for creating a tenant-aware key based on the given method
* (used as context) and its parameters.
*
* @author Thomas Vitale
*/
@FunctionalInterface
public interface TenantKeyGenerator extends KeyGenerator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@

/**
* A shared, thread-local store for the current tenant.
*
* @author Thomas Vitale
*/
public final class TenantContextHolder {

private static final Logger log = LoggerFactory.getLogger(TenantContextHolder.class);
private static final Logger logger = LoggerFactory.getLogger(TenantContextHolder.class);

private static final ThreadLocal<String> tenantIdentifier = new ThreadLocal<>();

private TenantContextHolder() {
}

public static void setTenantIdentifier(String tenant) {
Assert.hasText(tenant, "tenant cannot be empty");
log.trace("Setting current tenant to: {}", tenant);
Assert.hasText(tenant, "tenant cannot be null or empty");
logger.debug("Setting current tenant to: {}", tenant);
tenantIdentifier.set(tenant);
}

Expand All @@ -42,7 +40,7 @@ public static String getRequiredTenantIdentifier() {
}

public static void clear() {
log.trace("Clearing current tenant");
logger.debug("Clearing current tenant");
tenantIdentifier.remove();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
/**
* A {@link TenantEventListener} that sets/clears the tenant identifier from the current
* context on the {@link TenantContextHolder}.
*
* @author Thomas Vitale
*/
public final class HolderTenantContextEventListener implements TenantEventListener {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.arconia.core.multitenancy.context.events;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.util.Assert;

Expand All @@ -9,12 +11,12 @@
/**
* A {@link TenantEventListener} that sets/clears the tenant identifier from the current
* context on the SLF4J's {@link MDC}.
*
* @author Thomas Vitale
*/
public final class MdcTenantContextEventListener implements TenantEventListener {

public static final String DEFAULT_TENANT_IDENTIFIER_KEY = "tenantId";
private static final Logger logger = LoggerFactory.getLogger(MdcTenantContextEventListener.class);

private static final String DEFAULT_TENANT_IDENTIFIER_KEY = "tenantId";

private final String tenantIdentifierKey;

Expand All @@ -23,16 +25,18 @@ public MdcTenantContextEventListener() {
}

public MdcTenantContextEventListener(String tenantIdentifierKey) {
Assert.hasText(tenantIdentifierKey, "tenantIdentifierKey cannot be empty");
Assert.hasText(tenantIdentifierKey, "tenantIdentifierKey cannot be null or empty");
this.tenantIdentifierKey = tenantIdentifierKey;
}

@Override
public void onApplicationEvent(TenantEvent tenantEvent) {
if (tenantEvent instanceof TenantContextAttachedEvent event) {
logger.trace("Setting current tenant in MDC to: {}", event.getTenantIdentifier());
MDC.put(tenantIdentifierKey, event.getTenantIdentifier());
}
else if (tenantEvent instanceof TenantContextClosedEvent) {
logger.trace("Removing current tenant from MDC to");
MDC.remove(tenantIdentifierKey);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.micrometer.common.KeyValue;
import io.micrometer.observation.Observation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import io.arconia.core.multitenancy.events.TenantEvent;
Expand All @@ -11,14 +13,14 @@
/**
* A {@link TenantEventListener} that sets the tenant identifier from the current context
* on an existing {@link Observation}.
*
* @author Thomas Vitale
*/
public class ObservationTenantContextEventListener implements TenantEventListener {

public static final Cardinality DEFAULT_CARDINALITY = Cardinality.HIGH;
private static final Logger logger = LoggerFactory.getLogger(ObservationTenantContextEventListener.class);

public static final String DEFAULT_TENANT_IDENTIFIER_KEY = "tenant.id";
protected static final Cardinality DEFAULT_CARDINALITY = Cardinality.HIGH;

protected static final String DEFAULT_TENANT_IDENTIFIER_KEY = "tenant.id";

private final Cardinality cardinality;

Expand All @@ -29,7 +31,7 @@ public ObservationTenantContextEventListener() {
}

public ObservationTenantContextEventListener(String tenantIdentifierKey, Cardinality cardinality) {
Assert.hasText(tenantIdentifierKey, "tenantIdentifierKey cannot be empty");
Assert.hasText(tenantIdentifierKey, "tenantIdentifierKey cannot be null or empty");
Assert.notNull(cardinality, "cardinality cannot be null");
this.tenantIdentifierKey = tenantIdentifierKey;
this.cardinality = cardinality;
Expand All @@ -47,6 +49,8 @@ private void onTenantContextAttached(TenantContextAttachedEvent event) {
return;
}

logger.trace("Enhancing current observation with tenant context for: {}", event.getTenantIdentifier());

switch (cardinality) {
case LOW -> event.getObservationContext()
.addLowCardinalityKeyValue(KeyValue.of(tenantIdentifierKey, event.getTenantIdentifier()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
/**
* A {@link TenantEvent} which indicates a tenant has been attached to the current
* context.
*
* @author Thomas Vitale
*/
public final class TenantContextAttachedEvent extends TenantEvent {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
/**
* A {@link TenantEvent} which indicates the context for the current tenant has been
* closed.
*
* @author Thomas Vitale
*/
public final class TenantContextClosedEvent extends TenantEvent {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

/**
* Strategy to use a fixed value as the current tenant, regardless of the source context.
*
* @author Thomas Vitale
*/
public final class FixedTenantResolver implements TenantResolver<Object> {

Expand All @@ -19,7 +17,7 @@ public FixedTenantResolver() {
}

public FixedTenantResolver(String tenantIdentifier) {
Assert.hasText(tenantIdentifier, "tenantIdentifier cannot be empty");
Assert.hasText(tenantIdentifier, "tenantIdentifier cannot be null or empty");
this.fixedTenantIdentifier = tenantIdentifier;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

/**
* Strategy used to resolve the current tenant from a given source context.
*
* @author Thomas Vitale
*/
@FunctionalInterface
public interface TenantResolver<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
/**
* An implementation of {@link TenantEventPublisher} that uses Spring's event publishing
* support.
*
* @author Thomas Vitale
*/
public class DefaultTenantEventPublisher implements TenantEventPublisher {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@

/**
* Abstract superclass for all tenant-related events.
*
* @author Thomas Vitale
*/
public abstract class TenantEvent extends ApplicationEvent {

private final String tenantIdentifier;

public TenantEvent(String tenantIdentifier, Object source) {
super(source);
Assert.hasText(tenantIdentifier, "tenantIdentifier cannot be empty");
Assert.hasText(tenantIdentifier, "tenantIdentifier cannot be null or empty");
this.tenantIdentifier = tenantIdentifier;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

/**
* A listener for {@link TenantEvent}s.
*
* @author Thomas Vitale
*/
@FunctionalInterface
public interface TenantEventListener extends ApplicationListener<TenantEvent> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

/**
* A contract for publishing {@link TenantEvent}s.
*
* @author Thomas Vitale
*/
@FunctionalInterface
public interface TenantEventPublisher {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

/**
* Thrown when no tenant information is found in a given context.
*
* @author Thomas Vitale
*/
public class TenantNotFoundException extends IllegalStateException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

/**
* Thrown when an error occurred during the tenant resolution process.
*
* @author Thomas Vitale
*/
public class TenantResolutionException extends IllegalStateException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@

/**
* Unit tests for {@link DefaultTenantKeyGenerator}.
*
* @author Thomas Vitale
*/
class DefaultTenantKeyGeneratorTests {

DefaultTenantKeyGenerator keyGenerator = new DefaultTenantKeyGenerator();
private final DefaultTenantKeyGenerator keyGenerator = new DefaultTenantKeyGenerator();

@Test
void whenTenantContextDefinedThenGenerateCacheKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

/**
* Unit tests for {@link TenantContextHolder}.
*
* @author Thomas Vitale
*/
class TenantContextHolderTests {

Expand All @@ -25,14 +23,14 @@ void setValidTenantContext() {
void setNullTenantContext() {
assertThatThrownBy(() -> TenantContextHolder.setTenantIdentifier(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("tenant cannot be empty");
.hasMessageContaining("tenant cannot be null or empty");
}

@Test
void setEmptyTenantContext() {
assertThatThrownBy(() -> TenantContextHolder.setTenantIdentifier(""))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("tenant cannot be empty");
.hasMessageContaining("tenant cannot be null or empty");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

/**
* Unit tests for {@link HolderTenantContextEventListener}.
*
* @author Thomas Vitale
*/
class HolderTenantContextEventListenerTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@

/**
* Unit tests for {@link MdcTenantContextEventListener}.
*
* @author Thomas Vitale
*/
class MdcTenantContextEventListenerTests {

@Test
void whenNullCustomValueThenThrow() {
assertThatThrownBy(() -> new MdcTenantContextEventListener(null)).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("tenantIdentifierKey cannot be empty");
.hasMessageContaining("tenantIdentifierKey cannot be null or empty");
}

@Test
void whenEmptyCustomValueThenThrow() {
assertThatThrownBy(() -> new MdcTenantContextEventListener("")).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("tenantIdentifierKey cannot be empty");
.hasMessageContaining("tenantIdentifierKey cannot be null or empty");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

/**
* Unit tests for {@link ObservationTenantContextEventListener}.
*
* @author Thomas Vitale
*/
class ObservationTenantContextEventListenerTests {

Expand All @@ -27,15 +25,15 @@ void whenEmptyTenantKeyThenThrow() {
assertThatThrownBy(() -> new ObservationTenantContextEventListener("",
ObservationTenantContextEventListener.Cardinality.HIGH))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("tenantIdentifierKey cannot be empty");
.hasMessageContaining("tenantIdentifierKey cannot be null or empty");
}

@Test
void whenNullTenantKeyThenThrow() {
assertThatThrownBy(() -> new ObservationTenantContextEventListener(null,
ObservationTenantContextEventListener.Cardinality.HIGH))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("tenantIdentifierKey cannot be empty");
.hasMessageContaining("tenantIdentifierKey cannot be null or empty");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@

/**
* Unit tests for {@link FixedTenantResolver}.
*
* @author Thomas Vitale
*/
class FixedTenantResolverTests {

@Test
void whenNullCustomValueThenThrow() {
assertThatThrownBy(() -> new FixedTenantResolver(null)).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("tenantIdentifier cannot be empty");
.hasMessageContaining("tenantIdentifier cannot be null or empty");
}

@Test
void whenEmptyCustomValueThenThrow() {
assertThatThrownBy(() -> new FixedTenantResolver("")).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("tenantIdentifier cannot be empty");
.hasMessageContaining("tenantIdentifier cannot be null or empty");
}

@Test
Expand Down
Loading

0 comments on commit e6f4682

Please sign in to comment.