Skip to content

Commit

Permalink
feat: add TxWatcher (#99)
Browse files Browse the repository at this point in the history
* support spring tx

* Revert "support spring tx"

This reverts commit 94dd068.

* feat: add TxWatcher (#98)

* fix: add the new usage to README.md
  • Loading branch information
mouse9527 authored Jun 6, 2024
1 parent a5221b0 commit d1b6524
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ casbin:
#CasbinWatcher notification mode, defaults to use Redis for notification synchronization, temporarily only supports Redis
#After opening Watcher, you need to manually add spring-boot-starter-data-redis dependency.
watcherType: redis
#Watcher to support spring tx, the default is not enable.
#If the mechanism is enabled, When updating policy in a spring transaction, watcher will trigger the update after commit
watcherTxSupport: false
exception:
... See Schedule A for exception settings.
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;

/**
Expand Down Expand Up @@ -71,4 +73,15 @@ public Watcher lettuceRedisWatcher(RedisProperties redisProperties, CasbinProper
throw new CasbinWatcherLettuceTypeUnsupportedException("Unsupported watcher type!");
}
}
}

@Bean
@Primary
@ConditionalOnBean(Watcher.class)
@ConditionalOnProperty(value = "casbin.watcher-tx-support", havingValue = "true")
public Watcher txWatcher(Watcher watcher, Enforcer enforcer) {
TxWatcher txWatcher = new TxWatcher(watcher);
enforcer.setWatcher(txWatcher);
logger.info("TxWatcher proxy watcher: {}", watcher.getClass().getName());
return txWatcher;
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/casbin/spring/boot/autoconfigure/TxWatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.casbin.spring.boot.autoconfigure;

import org.casbin.jcasbin.persist.Watcher;
import org.springframework.transaction.support.TransactionSynchronization;

import java.util.function.Consumer;

import static org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive;
import static org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization;

public class TxWatcher implements Watcher {
private final Watcher watcher;

public TxWatcher(Watcher watcher) {
this.watcher = watcher;
}

@Override
public void setUpdateCallback(Runnable runnable) {
this.watcher.setUpdateCallback(runnable);
}

@Override
public void setUpdateCallback(Consumer<String> consumer) {
this.watcher.setUpdateCallback(consumer);
}

@Override
public void update() {
if (isActualTransactionActive()) {
registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
watcher.update();
}
});
} else {
watcher.update();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public class CasbinProperties {
*/
private CasbinWatcherLettuceRedisType watcherLettuceRedisType = CasbinWatcherLettuceRedisType.NONE;

/**
* Use {@link org.casbin.spring.boot.autoconfigure.TxWatcher} proxy, update after the transaction is committed
*/
private boolean watcherTxSupport = false;

public String getModelContext() {
String text = FileUtils.getFileAsText(model);
if (text == null) {
Expand Down Expand Up @@ -193,4 +198,12 @@ public CasbinWatcherLettuceRedisType getWatcherLettuceRedisType() {
public void setWatcherLettuceRedisType(CasbinWatcherLettuceRedisType watcherLettuceRedisType) {
this.watcherLettuceRedisType = watcherLettuceRedisType;
}
}

public boolean isWatcherTxSupport() {
return watcherTxSupport;
}

public void setWatcherTxSupport(boolean watcherTxSupport) {
this.watcherTxSupport = watcherTxSupport;
}
}

0 comments on commit d1b6524

Please sign in to comment.