Skip to content

Commit 6e9080e

Browse files
authored
feat: add the code and CI
1 parent 8d93f1b commit 6e9080e

File tree

10 files changed

+797
-1
lines changed

10 files changed

+797
-1
lines changed

.github/workflows/ci.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: build
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
services:
10+
postgres:
11+
image: postgres
12+
env:
13+
POSTGRES_USER: postgres
14+
POSTGRES_PASSWORD: postgres
15+
POSTGRES_DB: testdb
16+
ports:
17+
- 5432:5432
18+
options: >-
19+
--health-cmd="pg_isready"
20+
--health-interval=10s
21+
--health-timeout=5s
22+
--health-retries=5
23+
24+
steps:
25+
- name: Checkout
26+
uses: actions/checkout@v2
27+
with:
28+
fetch-depth: 0
29+
30+
- name: Set up JDK 11
31+
uses: actions/setup-java@v1
32+
with:
33+
java-version: 11
34+
server-username: OSSRH_USERNAME
35+
server-password: OSSRH_PASSWORD
36+
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
37+
gpg-passphrase: GPG_PASSPHRASE
38+
39+
- name: Wait for PostgreSQL
40+
run: |
41+
for i in {1..10}; do
42+
nc -z localhost 5432 && echo "PostgreSQL is up!" && break
43+
echo "Waiting for PostgreSQL..." && sleep 3
44+
done
45+
46+
- name: Build with Maven
47+
run: mvn clean test
48+
49+
- name: Set up Node.js
50+
uses: actions/setup-node@v2
51+
with:
52+
node-version: 20
53+
54+
- name: Semantic Release
55+
run: |
56+
npm install -g @conveyal/maven-semantic-release semantic-release
57+
semantic-release --prepare @conveyal/maven-semantic-release --publish @semantic-release/github,@conveyal/maven-semantic-release --verify-conditions @semantic-release/github,@conveyal/maven-semantic-release --verify-release @conveyal/maven-semantic-release
58+
env:
59+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60+
GPG_KEY_NAME: ${{ secrets.GPG_KEY_NAME }}
61+
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
62+
OSSRH_USERNAME: ${{ secrets.OSSRH_JIRA_USERNAME }}
63+
OSSRH_PASSWORD: ${{ secrets.OSSRH_JIRA_PASSWORD }}

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# MacOS
2+
.DS_Store
3+
4+
# Idea
5+
.idea/
6+
17
# Compiled class file
28
*.class
39

README.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,91 @@
1-
# jcasbin-postgres-watcher
1+
# jcasbin-postgres-watcher
2+
3+
[![GitHub Actions](https://github.com/jcasbin/jcasbin-postgres-watcher/actions/workflows/ci.yml/badge.svg)](https://github.com/jcasbin/jcasbin-postgres-watcher/actions/workflows/ci.yml)
4+
![License](https://img.shields.io/github/license/jcasbin/jcasbin-postgres-watcher)
5+
[![Javadoc](https://javadoc.io/badge2/org.casbin/jcasbin-postgres-watcher/javadoc.svg)](https://javadoc.io/doc/org.casbin/jcasbin-postgres-watcher)
6+
[![Maven Central](https://img.shields.io/maven-central/v/org.casbin/jcasbin-postgres-watcher.svg)](https://mvnrepository.com/artifact/org.casbin/jcasbin-postgres-watcher/latest)
7+
[![Release](https://img.shields.io/github/release/jcasbin/jcasbin-postgres-watcher.svg)](https://github.com/jcasbin/jcasbin-postgres-watcher/releases/latest)
8+
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)
9+
10+
jCasbin PostgreSQL Watcher is a [PostgreSQL](https://www.postgresql.org/) watcher for [jCasbin](https://github.com/casbin/jcasbin).
11+
12+
## Installation
13+
14+
**For Maven**
15+
16+
```
17+
<dependency>
18+
<groupId>org.casbin</groupId>
19+
<artifactId>jcasbin-postgres-watcher</artifactId>
20+
<version>1.0.0</version>
21+
</dependency>
22+
```
23+
24+
## Simple Example
25+
26+
if you have two casbin instances A and B
27+
28+
**A:** **Producer**
29+
30+
```java
31+
// Initialize PostgreSQL Watcher
32+
String channel = "casbin_channel";
33+
JCasbinPostgresWatcher watcher = new JCasbinPostgresWatcher(
34+
"jdbc:postgresql://localhost:5432/your_db",
35+
"postgres",
36+
"your_password",
37+
channel
38+
);
39+
// Support for advanced configuration with WatcherConfig
40+
// WatcherConfig config = new WatcherConfig();
41+
// config.setChannel(channel);
42+
// config.setVerbose(true);
43+
// config.setLocalId("instance-1");
44+
// JCasbinPostgresWatcher watcher = new JCasbinPostgresWatcher(url, user, password, config);
45+
46+
Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
47+
enforcer.setWatcher(watcher);
48+
49+
// The following code is not necessary and generally does not need to be written unless you understand what you want to do
50+
/*
51+
Runnable updateCallback = () -> {
52+
// Custom behavior
53+
};
54+
watcher.setUpdateCallback(updateCallback);
55+
*/
56+
57+
// Modify policy, it will notify B
58+
enforcer.addPolicy(...);
59+
60+
// Using WatcherEx specific methods for fine-grained policy updates
61+
// Add a policy
62+
enforcer.addPolicy(...);
63+
watcher.updateForAddPolicy(...);
64+
65+
```
66+
67+
**B:** **Consumer**
68+
69+
````Java
70+
// Initialize PostgreSQL Watcher with same channel
71+
String channel = "casbin_channel";
72+
JCasbinPostgresWatcher watcher = new JCasbinPostgresWatcher(
73+
"jdbc:postgresql://localhost:5432/your_db",
74+
"postgres",
75+
"your_password",
76+
channel
77+
);
78+
79+
Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
80+
enforcer.setWatcher(watcher);
81+
// B set watcher and subscribe to the same channel, then it will receive the notification of A, and then call LoadPolicy to reload policy
82+
````
83+
84+
## Getting Help
85+
86+
- [jCasbin](https://github.com/casbin/jCasbin)
87+
- [pgjdbc](https://github.com/pgjdbc/pgjdbc)
88+
89+
## License
90+
91+
This project is under Apache 2.0 License. See the [LICENSE](https://github.com/jcasbin/redis-watcher/blob/master/LICENSE) file for the full license text.

maven-settings.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<settings>
2+
<servers>
3+
<server>
4+
<id>ossrh</id>
5+
<username>${OSSRH_USERNAME}</username>
6+
<password>${OSSRH_PASSWORD}</password>
7+
</server>
8+
</servers>
9+
<profiles>
10+
<profile>
11+
<id>ossrh</id>
12+
<activation>
13+
<activeByDefault>true</activeByDefault>
14+
</activation>
15+
<properties>
16+
<gpg.executable>gpg</gpg.executable>
17+
<gpg.keyname>${GPG_KEY_NAME}</gpg.keyname>
18+
<gpg.passphrase>${GPG_PASSPHRASE}</gpg.passphrase>
19+
</properties>
20+
</profile>
21+
</profiles>
22+
</settings>

pom.xml

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.casbin</groupId>
8+
<artifactId>jcasbin-postgres-watcher</artifactId>
9+
<version>1.0.0</version>
10+
11+
<properties>
12+
<maven.compiler.release>11</maven.compiler.release>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
</properties>
15+
16+
<name>Postgres watcher for jCasbin</name>
17+
<description>Postgres Watcher is a Postgres watcher for jCasbin</description>
18+
<url>https://github.com/jcasbin/jcasbin-postgres-watcher</url>
19+
<inceptionYear>2025</inceptionYear>
20+
21+
<licenses>
22+
<license>
23+
<name>The Apache Software License, Version 2.0</name>
24+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
25+
<distribution>repo</distribution>
26+
</license>
27+
</licenses>
28+
29+
<developers>
30+
<developer>
31+
<name>D0000M</name>
32+
<email>[email protected]</email>
33+
<url>https://github.com/D0000M</url>
34+
</developer>
35+
</developers>
36+
37+
<scm>
38+
<url>https://github.com/jcasbin/jcasbin-postgres-watcher</url>
39+
<developerConnection>scm:git:https://github.com/jcasbin/jcasbin-postgres-watcher.git</developerConnection>
40+
<connection>scm:[email protected]:jcasbin/jcasbin-postgres-watcher.git</connection>
41+
</scm>
42+
43+
<distributionManagement>
44+
<snapshotRepository>
45+
<id>ossrh</id>
46+
<url>https://central.sonatype.com</url>
47+
</snapshotRepository>
48+
</distributionManagement>
49+
50+
51+
<dependencies>
52+
<!-- jCasbin -->
53+
<dependency>
54+
<groupId>org.casbin</groupId>
55+
<artifactId>jcasbin</artifactId>
56+
<version>1.82.0</version>
57+
</dependency>
58+
<!-- PostgreSQL JDBC -->
59+
<dependency>
60+
<groupId>org.postgresql</groupId>
61+
<artifactId>postgresql</artifactId>
62+
<version>42.7.7</version>
63+
</dependency>
64+
<!-- Jackson for JSON serialization -->
65+
<dependency>
66+
<groupId>com.fasterxml.jackson.core</groupId>
67+
<artifactId>jackson-databind</artifactId>
68+
<version>2.17.1</version>
69+
</dependency>
70+
<!-- JUnit -->
71+
<dependency>
72+
<groupId>org.junit.jupiter</groupId>
73+
<artifactId>junit-jupiter</artifactId>
74+
<version>5.10.2</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.slf4j</groupId>
79+
<artifactId>slf4j-simple</artifactId>
80+
<version>2.0.16</version>
81+
<scope>test</scope>
82+
</dependency>
83+
</dependencies>
84+
85+
<build>
86+
<plugins>
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-gpg-plugin</artifactId>
90+
<version>1.5</version>
91+
<executions>
92+
<execution>
93+
<id>sign-artifacts</id>
94+
<phase>verify</phase>
95+
<goals>
96+
<goal>sign</goal>
97+
</goals>
98+
</execution>
99+
</executions>
100+
<configuration>
101+
<!-- Prevent gpg from using pinentry programs -->
102+
<gpgArguments>
103+
<arg>--pinentry-mode</arg>
104+
<arg>loopback</arg>
105+
</gpgArguments>
106+
</configuration>
107+
</plugin>
108+
<plugin>
109+
<!-- Include zipped source code in releases -->
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-source-plugin</artifactId>
112+
<executions>
113+
<execution>
114+
<id>attach-sources</id>
115+
<goals>
116+
<goal>jar-no-fork</goal>
117+
</goals>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
<plugin>
122+
<!-- Allow attaching Javadoc during releases -->
123+
<groupId>org.apache.maven.plugins</groupId>
124+
<artifactId>maven-javadoc-plugin</artifactId>
125+
<version>2.10.4</version>
126+
<configuration>
127+
<source>11</source>
128+
<detectJavaApiLink>false</detectJavaApiLink>
129+
<!-- Turn off Java 8 strict Javadoc checking -->
130+
<additionalparam>-Xdoclint:none</additionalparam>
131+
<tags>
132+
<tag>
133+
<name>notnull</name>
134+
<placement>a</placement>
135+
<head>Not null</head>
136+
</tag>
137+
<tag>
138+
<name>default</name>
139+
<placement>a</placement>
140+
<head>Default:</head>
141+
</tag>
142+
</tags>
143+
</configuration>
144+
<executions>
145+
<!-- Compress Javadoc into JAR and include that JAR when deploying. -->
146+
<execution>
147+
<id>attach-javadocs</id>
148+
<goals>
149+
<goal>jar</goal>
150+
</goals>
151+
</execution>
152+
</executions>
153+
</plugin>
154+
<plugin>
155+
<!-- Automatically close and deploy from OSSRH -->
156+
<groupId>org.sonatype.central</groupId>
157+
<artifactId>central-publishing-maven-plugin</artifactId>
158+
<version>0.5.0</version>
159+
<extensions>true</extensions>
160+
<configuration>
161+
<publishingServerId>ossrh</publishingServerId>
162+
<!-- Release versions will be synced to Maven Central automatically. -->
163+
<autoPublish>true</autoPublish>
164+
</configuration>
165+
</plugin>
166+
</plugins>
167+
</build>
168+
169+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2025 The casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package org.casbin.config;
16+
17+
/**
18+
* Watcher Configuration Class
19+
*/
20+
public class WatcherConfig {
21+
private String channel = "casbin_postgres_watcher";
22+
private boolean verbose = false;
23+
private String localId = java.util.UUID.randomUUID().toString();
24+
25+
public String getChannel() { return channel; }
26+
public void setChannel(String channel) { this.channel = channel; }
27+
28+
public boolean isVerbose() { return verbose; }
29+
public void setVerbose(boolean verbose) { this.verbose = verbose; }
30+
31+
public String getLocalId() { return localId; }
32+
public void setLocalId(String localId) { this.localId = localId; }
33+
}

0 commit comments

Comments
 (0)