-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa836fd
commit af2edc6
Showing
12 changed files
with
227 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Apply the java-library plugin to add support for Java Library | ||
plugins { | ||
id 'java' | ||
} | ||
dependencies{ | ||
compile project(":wedpr-core-utils") | ||
} |
95 changes: 95 additions & 0 deletions
95
wedpr-components/hook/src/main/java/com/webank/wedpr/components/hook/UserHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2017-2025 [webank-wedpr] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
|
||
package com.webank.wedpr.components.hook; | ||
|
||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class UserHook { | ||
private static final Logger logger = LoggerFactory.getLogger(UserHook.class); | ||
|
||
public interface UserCallback { | ||
boolean interruptOnException(); | ||
|
||
void onCreated(String user) throws Exception; | ||
|
||
void onUpdated(String user) throws Exception; | ||
|
||
void onDeleted(String user) throws Exception; | ||
} | ||
|
||
public enum Action { | ||
CREATE_USER, | ||
UPDATE_USER, | ||
DELETE_USER | ||
} | ||
|
||
private Map<String, UserCallback> callbacks; | ||
|
||
public synchronized void registerUserCallback(String module, UserCallback callback) { | ||
callbacks.put(module, callback); | ||
} | ||
|
||
private synchronized void triggerCallback(Action action, String user) throws Exception { | ||
if (callbacks.isEmpty()) { | ||
return; | ||
} | ||
for (String module : callbacks.keySet()) { | ||
UserCallback callback = callbacks.get(module); | ||
try { | ||
switch (action) { | ||
case CREATE_USER: | ||
{ | ||
callback.onCreated(user); | ||
continue; | ||
} | ||
case UPDATE_USER: | ||
{ | ||
callback.onUpdated(user); | ||
continue; | ||
} | ||
case DELETE_USER: | ||
{ | ||
callback.onDeleted(user); | ||
continue; | ||
} | ||
default: | ||
continue; | ||
} | ||
} catch (Exception e) { | ||
logger.warn("Trigger callback for module {} failed, reason: ", module, e); | ||
if (callback.interruptOnException()) { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public synchronized void onUserCreated(String user) throws Exception { | ||
triggerCallback(Action.CREATE_USER, user); | ||
} | ||
|
||
public synchronized void onUserUpdated(String user) throws Exception { | ||
triggerCallback(Action.UPDATE_USER, user); | ||
} | ||
|
||
public synchronized void onUserDeleted(String user) throws Exception { | ||
triggerCallback(Action.DELETE_USER, user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.