Skip to content

Commit

Permalink
fix sonar lint hints; minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcGiffing committed Feb 25, 2024
1 parent a5c79f2 commit 2c38082
Show file tree
Hide file tree
Showing 24 changed files with 412 additions and 426 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

public class Bucket4JConfigurationPredicateNameValidator implements ConstraintValidator<ValidPredicateNames, Bucket4JConfiguration> {

private final Map<FilterMethod, Map<String, ExecutePredicate<?>>> filterPredicates = new HashMap<>();
private final EnumMap<FilterMethod, Map<String, ExecutePredicate<?>>> filterPredicates = new EnumMap<>(FilterMethod.class);

@Autowired
public Bucket4JConfigurationPredicateNameValidator(List<ExecutePredicate<?>> executePredicates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.hazelcast.core.EntryEvent;
import com.hazelcast.map.IMap;
import com.hazelcast.map.listener.EntryUpdatedListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;

/**
Expand All @@ -18,11 +17,11 @@
*/
public class HazelcastCacheListener<K, V> implements EntryUpdatedListener<K, V> {

@Autowired
private ApplicationEventPublisher eventPublisher;

public HazelcastCacheListener(IMap<K, V> map) {
public HazelcastCacheListener(IMap<K, V> map, ApplicationEventPublisher eventPublisher) {
map.addEntryListener(this, true);
this.eventPublisher = eventPublisher;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down Expand Up @@ -48,9 +49,9 @@ public CacheManager<String, Bucket4JConfiguration> configCacheManager() {

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
public HazelcastCacheListener<String, Bucket4JConfiguration> configCacheListener() {
public HazelcastCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
IMap<String, Bucket4JConfiguration> map = hazelcastInstance.getMap(configCacheName);
return new HazelcastCacheListener<>(map);
return new HazelcastCacheListener<>(map, eventPublisher);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.hazelcast.map.IMap;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down Expand Up @@ -49,9 +50,9 @@ public CacheManager<String, Bucket4JConfiguration> configCacheManager() {

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
public HazelcastCacheListener<String, Bucket4JConfiguration> configCacheListener() {
public HazelcastCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
IMap<String, Bucket4JConfiguration> map = hazelcastInstance.getMap(configCacheName);
return new HazelcastCacheListener<>(map);
return new HazelcastCacheListener<>(map, eventPublisher);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import org.apache.ignite.Ignite;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down Expand Up @@ -39,7 +40,7 @@ public CacheManager<String, Bucket4JConfiguration> configCacheManager() {

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
public IgniteCacheListener<String, Bucket4JConfiguration> configCacheListener() {
return new IgniteCacheListener<>(ignite.cache(configCacheName));
public IgniteCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new IgniteCacheListener<>(ignite.cache(configCacheName), eventPublisher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheUpdateEvent;
import org.apache.ignite.IgniteCache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;

import javax.cache.configuration.FactoryBuilder;
Expand All @@ -24,13 +22,13 @@
*/
public class IgniteCacheListener<K,V> implements CacheEntryUpdatedListener<K,V>, Serializable {

@Autowired
private ApplicationEventPublisher eventPublisher;

public IgniteCacheListener(IgniteCache<K,V> cache){
public IgniteCacheListener(IgniteCache<K,V> cache, ApplicationEventPublisher eventPublisher){
cache.registerCacheEntryListener(
new MutableCacheEntryListenerConfiguration<K, V>
(FactoryBuilder.factoryOf(this), null, false, false));
new MutableCacheEntryListenerConfiguration<>
(FactoryBuilder.factoryOf(this), null, false, false));
this.eventPublisher = eventPublisher;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import org.infinispan.manager.CacheContainer;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down Expand Up @@ -39,7 +40,7 @@ public CacheManager<String, Bucket4JConfiguration> configCacheManager() {

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
public InfinispanCacheListener<String, Bucket4JConfiguration> configCacheListener() {
return new InfinispanCacheListener<>(cacheContainer.getCache(configCacheName));
public InfinispanCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new InfinispanCacheListener<>(cacheContainer.getCache(configCacheName), eventPublisher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified;
import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;

/**
* This class is intended to be used as bean.
*
* <p>
* It will listen to changes in the cache, parse them to a {@code CacheUpdateEvent<K, V>}
* and publish the event to the Spring ApplicationEventPublisher.
*
Expand All @@ -21,15 +19,16 @@
@Listener
public class InfinispanCacheListener<K, V> {

@Autowired
private ApplicationEventPublisher eventPublisher;
private ApplicationEventPublisher eventPublisher;

public InfinispanCacheListener(Cache<K, V> cache, ApplicationEventPublisher eventPublisher) {
cache.addListener(this);
this.eventPublisher = eventPublisher;
}

public InfinispanCacheListener(Cache<K, V> cache){
cache.addListener(this);
}
@CacheEntryModified
public void entryModified(CacheEntryModifiedEvent<K, V> event) {
CacheUpdateEvent<K, V> updateEvent = new CacheUpdateEvent<>(event.getKey(),event.getOldValue(), event.getNewValue());
this.eventPublisher.publishEvent(updateEvent);
}
@CacheEntryModified
public void entryModified(CacheEntryModifiedEvent<K, V> event) {
CacheUpdateEvent<K, V> updateEvent = new CacheUpdateEvent<>(event.getKey(), event.getOldValue(), event.getNewValue());
this.eventPublisher.publishEvent(updateEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import com.giffing.bucket4j.spring.boot.starter.config.cache.infinispan.InfinispanCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;

import org.infinispan.manager.CacheContainer;

import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.ApplicationEventPublisher;
Expand Down Expand Up @@ -52,7 +50,7 @@ public CacheManager<String, Bucket4JConfiguration> configCacheManager() {

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
public InfinispanCacheListener<String, Bucket4JConfiguration> configCacheListener() {
return new InfinispanCacheListener<>(cacheContainer.getCache(configCacheName));
public InfinispanCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new InfinispanCacheListener<>(cacheContainer.getCache(configCacheName), eventPublisher);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.giffing.bucket4j.spring.boot.starter.config.cache.jcache;

import com.giffing.bucket4j.spring.boot.starter.config.cache.ProxyManagerWrapper;
import com.giffing.bucket4j.spring.boot.starter.config.cache.AbstractCacheResolverTemplate;
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.context.ConsumptionProbeHolder;
import com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.distributed.proxy.AbstractProxyManager;
import io.github.bucket4j.grid.infinispan.InfinispanProxyManager;
import org.infinispan.Cache;
import org.infinispan.functional.impl.FunctionalMapImpl;
Expand All @@ -13,37 +12,41 @@

/**
* To use Infinispan you need a special bucket4j-infinispan dependency.
*
* <p>
* https://github.com/vladimir-bukhtoyarov/bucket4j/blob/master/doc-pages/infinispan.md
*
* <p>
* Question: Bucket4j already supports JCache since version 1.2. Why it was needed to introduce direct support for Infinispan?
*
* <p>
* Answer: When you want to use Bucket4j together with Infinispan, you must always use bucket4j-infinispan module instead of bucket4j-jcache,
* because Infinispan does not provide mutual exclusion for entry-processors. Any attempt to use Infinispan via bucket4j-jcache will be
* because Infinispan does not provide mutual exclusion for entry-processors. Any attempt to use Infinispan via bucket4j-jcache will be
* failed with UnsupportedOperationException exception at bucket construction time.
*
*/
public class InfinispanJCacheCacheResolver implements SyncCacheResolver {

private final CacheContainer cacheContainer;

public InfinispanJCacheCacheResolver(CacheContainer cacheContainer) {
this.cacheContainer = cacheContainer;
}

public ProxyManagerWrapper resolve(String cacheName) {
Cache<String, byte[]> cache = cacheContainer.getCache(cacheName);
if (cache == null) {
throw new JCacheNotFoundException(cacheName);
}

FunctionalMapImpl<String, byte[]> functionalMap = FunctionalMapImpl.create(cache.getAdvancedCache());
InfinispanProxyManager<String> infinispanProxyManager = new InfinispanProxyManager<>(ReadWriteMapImpl.create(functionalMap));
return (key, numTokens, bucketConfiguration, metricsListener, version, replaceStrategy) -> {
Bucket bucket = infinispanProxyManager.builder()
.withImplicitConfigurationReplacement(version, replaceStrategy)
.build(key, bucketConfiguration).toListenable(metricsListener);
return new ConsumptionProbeHolder(bucket.tryConsumeAndReturnRemaining(numTokens));
};
}
public class InfinispanJCacheCacheResolver extends AbstractCacheResolverTemplate<String> implements SyncCacheResolver {

private final CacheContainer cacheContainer;

public InfinispanJCacheCacheResolver(CacheContainer cacheContainer) {
this.cacheContainer = cacheContainer;
}


@Override
public String castStringToCacheKey(String key) {
return key;
}

@Override
public boolean isAsync() {
return false;
}

@Override
public AbstractProxyManager<String> getProxyManager(String cacheName) {
Cache<String, byte[]> cache = cacheContainer.getCache(cacheName);
if (cache == null) {
throw new JCacheNotFoundException(cacheName);
}
FunctionalMapImpl<String, byte[]> functionalMap = FunctionalMapImpl.create(cache.getAdvancedCache());
return new InfinispanProxyManager<>(ReadWriteMapImpl.create(functionalMap));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.giffing.bucket4j.spring.boot.starter.config.cache.jcache;

import javax.cache.CacheManager;
import javax.cache.Caching;

import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import javax.cache.CacheManager;
import javax.cache.Caching;

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
Expand Down Expand Up @@ -44,8 +43,8 @@ public com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager<String

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
public JCacheCacheListener<String, Bucket4JConfiguration> configCacheListener() {
return new JCacheCacheListener<>(cacheManager.getCache(configCacheName));
public JCacheCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new JCacheCacheListener<>(cacheManager.getCache(configCacheName), eventPublisher);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.giffing.bucket4j.spring.boot.starter.config.cache.jcache;

import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheUpdateEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;

import javax.cache.Cache;
Expand All @@ -15,7 +13,7 @@

/**
* This class is intended to be used as bean.
*
* <p>
* It will listen to changes in the cache, parse them to a {@code CacheUpdateEvent<K, V>}
* and publish the event to the Spring ApplicationEventPublisher.
*
Expand All @@ -24,20 +22,20 @@
*/
public class JCacheCacheListener<K, V> implements CacheEntryUpdatedListener<K, V>, Serializable {

@Autowired
private ApplicationEventPublisher eventPublisher;
private ApplicationEventPublisher eventPublisher;

public JCacheCacheListener(Cache<K,V> cache ){
cache.registerCacheEntryListener(
new MutableCacheEntryListenerConfiguration<K, V>
(FactoryBuilder.factoryOf(this), null, true, false));
}
public JCacheCacheListener(Cache<K, V> cache, ApplicationEventPublisher eventPublisher) {
cache.registerCacheEntryListener(
new MutableCacheEntryListenerConfiguration<>
(FactoryBuilder.factoryOf(this), null, true, false));
this.eventPublisher = eventPublisher;
}

@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends K, ? extends V>> iterable) throws CacheEntryListenerException {
iterable.forEach(event -> {
CacheUpdateEvent<K, V> updateEvent = new CacheUpdateEvent<>(event.getKey(), event.getOldValue(), event.getValue());
eventPublisher.publishEvent(updateEvent);
});
}
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends K, ? extends V>> iterable) throws CacheEntryListenerException {
iterable.forEach(event -> {
CacheUpdateEvent<K, V> updateEvent = new CacheUpdateEvent<>(event.getKey(), event.getOldValue(), event.getValue());
eventPublisher.publishEvent(updateEvent);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import io.github.bucket4j.redis.jedis.cas.JedisBasedProxyManager.JedisBasedProxyManagerBuilder;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
Expand Down Expand Up @@ -40,7 +41,7 @@ public CacheManager<String, Bucket4JConfiguration> configCacheManager() {

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
public JedisCacheListener<String, Bucket4JConfiguration> configCacheListener() {
return new JedisCacheListener<>(jedisPool, configCacheName, String.class, Bucket4JConfiguration.class);
public JedisCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new JedisCacheListener<>(jedisPool, configCacheName, String.class, Bucket4JConfiguration.class, eventPublisher);
}
}
Loading

0 comments on commit 2c38082

Please sign in to comment.