Pārlūkot izejas kodu

消息通知接口

mxd 4 gadi atpakaļ
vecāks
revīzija
f7edd5c687

+ 40 - 0
src/main/java/org/ssssssss/magicapi/model/Constants.java

@@ -122,4 +122,44 @@ public class Constants {
 	 */
 	public static int RESPONSE_CODE_INVALID = 0;
 
+	/**
+	 * 通知新增
+	 */
+	public static final int NOTIFY_ACTION_ADD = 1;
+
+	/**
+	 * 通知修改
+	 */
+	public static final int NOTIFY_ACTION_UPDATE = 2;
+
+	/**
+	 * 通知删除
+	 */
+	public static final int NOTIFY_ACTION_DELETE = 3;
+
+	/**
+	 * 通知更新全部
+	 */
+	public static final int NOTIFY_ACTION_ALL = 4;
+
+	/**
+	 * 通知接口刷新
+	 */
+	public static final int NOTIFY_ACTION_API = 1;
+
+	/**
+	 * 通知分组刷新
+	 */
+	public static final int NOTIFY_ACTION_GROUP = 2;
+
+	/**
+	 * 通知函数刷新
+	 */
+	public static final int NOTIFY_ACTION_FUNCTION = 3;
+
+	/**
+	 * 通知数据源刷新
+	 */
+	public static final int NOTIFY_ACTION_DATASOURCE = 4;
+
 }

+ 70 - 0
src/main/java/org/ssssssss/magicapi/model/MagicNotify.java

@@ -0,0 +1,70 @@
+package org.ssssssss.magicapi.model;
+
+public class MagicNotify {
+
+	/**
+	 * 消息来源
+	 */
+	private String from;
+
+	/**
+	 * 对应的id,如接口id、函数id,分组id、数据源id
+	 */
+	private String id;
+
+	/**
+	 * 动作
+	 */
+	private int action = -1;
+
+	/**
+	 * 操作对象,如接口、函数、分组、数据源
+	 */
+	private int type = -1;
+
+	public MagicNotify() {
+	}
+
+	public MagicNotify(String from) {
+		this(from, null, Constants.NOTIFY_ACTION_ALL, -1);
+	}
+
+	public MagicNotify(String from, String id, int action, int type) {
+		this.from = from;
+		this.id = id;
+		this.action = action;
+		this.type = type;
+	}
+
+	public String getFrom() {
+		return from;
+	}
+
+	public void setFrom(String from) {
+		this.from = from;
+	}
+
+	public String getId() {
+		return id;
+	}
+
+	public void setId(String id) {
+		this.id = id;
+	}
+
+	public int getAction() {
+		return action;
+	}
+
+	public void setAction(int action) {
+		this.action = action;
+	}
+
+	public int getType() {
+		return type;
+	}
+
+	public void setType(int type) {
+		this.type = type;
+	}
+}

+ 6 - 0
src/main/java/org/ssssssss/magicapi/provider/MagicAPIService.java

@@ -4,6 +4,7 @@ import org.ssssssss.magicapi.config.MagicModule;
 import org.ssssssss.magicapi.model.ApiInfo;
 import org.ssssssss.magicapi.model.FunctionInfo;
 import org.ssssssss.magicapi.model.Group;
+import org.ssssssss.magicapi.model.MagicNotify;
 
 import java.util.List;
 import java.util.Map;
@@ -127,4 +128,9 @@ public interface MagicAPIService extends MagicModule {
 	 * @param type 分组类型,1 接口分组,2 函数分组
 	 */
 	List<Group> groupList(String type);
+
+	/**
+	 * 处理刷新通知
+	 */
+	boolean processNotify(MagicNotify magicNotify);
 }

+ 10 - 0
src/main/java/org/ssssssss/magicapi/provider/MagicNotifyService.java

@@ -0,0 +1,10 @@
+package org.ssssssss.magicapi.provider;
+
+import org.ssssssss.magicapi.model.MagicNotify;
+
+public interface MagicNotifyService {
+
+	void sendNotify(MagicNotify magicNotify);
+
+	void onNotifyReceived(MagicNotify magicNotify);
+}

+ 1 - 0
src/main/java/org/ssssssss/magicapi/provider/StoreServiceProvider.java

@@ -113,6 +113,7 @@ public abstract class StoreServiceProvider<T extends MagicEntity> {
 			}
 			src.renameTo(dest);
 		}
+		info.setUpdateTime(System.currentTimeMillis());
 		if (dest.write(serialize(info))) {
 			mappings.put(info.getId(), dest);
 			infos.put(info.getId(), info);

+ 83 - 9
src/main/java/org/ssssssss/magicapi/provider/impl/DefaultMagicAPIService.java

@@ -17,10 +17,7 @@ import org.ssssssss.script.parsing.ast.Expression;
 
 import javax.script.ScriptContext;
 import javax.script.SimpleScriptContext;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Optional;
+import java.util.*;
 import java.util.stream.Collectors;
 
 public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstants {
@@ -39,13 +36,18 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 
 	private final MagicFunctionManager magicFunctionManager;
 
-	public DefaultMagicAPIService(MappingHandlerMapping mappingHandlerMapping, ApiServiceProvider apiServiceProvider, FunctionServiceProvider functionServiceProvider, GroupServiceProvider groupServiceProvider, ResultProvider resultProvider, MagicFunctionManager magicFunctionManager, boolean throwException) {
+	private final MagicNotifyService magicNotifyService;
+
+	private final String instanceId = UUID.randomUUID().toString();
+
+	public DefaultMagicAPIService(MappingHandlerMapping mappingHandlerMapping, ApiServiceProvider apiServiceProvider, FunctionServiceProvider functionServiceProvider, GroupServiceProvider groupServiceProvider, ResultProvider resultProvider, MagicFunctionManager magicFunctionManager, MagicNotifyService magicNotifyService, boolean throwException) {
 		this.mappingHandlerMapping = mappingHandlerMapping;
 		this.apiServiceProvider = apiServiceProvider;
 		this.functionServiceProvider = functionServiceProvider;
 		this.groupServiceProvider = groupServiceProvider;
 		this.resultProvider = resultProvider;
 		this.magicFunctionManager = magicFunctionManager;
+		this.magicNotifyService = magicNotifyService;
 		this.throwException = throwException;
 		MagicResourceLoader.addFunctionLoader((name) -> {
 			int index = name.indexOf(":");
@@ -120,11 +122,12 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 		isTrue(IoUtils.validateFileName(info.getName()), NAME_INVALID);
 		// 验证路径是否有冲突
 		isTrue(!mappingHandlerMapping.hasRegisterMapping(info), REQUEST_PATH_CONFLICT);
+		int action = Constants.NOTIFY_ACTION_UPDATE;
 		if (StringUtils.isBlank(info.getId())) {
 			// 先判断接口是否存在
 			isTrue(!apiServiceProvider.exists(info), API_ALREADY_EXISTS.format(info.getMethod(), info.getPath()));
-
 			isTrue(apiServiceProvider.insert(info), API_SAVE_FAILURE);
+			action = Constants.NOTIFY_ACTION_ADD;
 		} else {
 			// 先判断接口是否存在
 			isTrue(!apiServiceProvider.existsWithoutId(info), API_ALREADY_EXISTS.format(info.getMethod(), info.getPath()));
@@ -138,6 +141,8 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 		}
 		// 注册接口
 		mappingHandlerMapping.registerMapping(info, true);
+		// 通知更新接口
+		magicNotifyService.sendNotify(new MagicNotify(instanceId, info.getId(), action, Constants.NOTIFY_ACTION_API));
 		return info.getId();
 	}
 
@@ -156,6 +161,8 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 		boolean success = apiServiceProvider.delete(id);
 		if (success) {    //删除成功时在取消注册
 			mappingHandlerMapping.unregisterMapping(id, true);
+			// 通知删除接口
+			magicNotifyService.sendNotify(new MagicNotify(instanceId, id, Constants.NOTIFY_ACTION_DELETE, Constants.NOTIFY_ACTION_API));
 		}
 		return success;
 	}
@@ -168,7 +175,12 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 		isTrue(apiServiceProvider.allowMove(id, groupId), NAME_CONFLICT);
 		// 验证路径是否有冲突
 		isTrue(mappingHandlerMapping.move(id, groupId), REQUEST_PATH_CONFLICT);
-		return apiServiceProvider.move(id, groupId);
+		if (apiServiceProvider.move(id, groupId)) {
+			// 通知更新接口
+			magicNotifyService.sendNotify(new MagicNotify(instanceId, id, Constants.NOTIFY_ACTION_UPDATE, Constants.NOTIFY_ACTION_API));
+			return true;
+		}
+		return false;
 	}
 
 	@Override
@@ -178,16 +190,18 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 		notBlank(functionInfo.getPath(), FUNCTION_PATH_REQUIRED);
 		notBlank(functionInfo.getScript(), SCRIPT_REQUIRED);
 		isTrue(!magicFunctionManager.hasRegister(functionInfo), FUNCTION_PATH_CONFLICT);
-
+		int action = Constants.NOTIFY_ACTION_UPDATE;
 		if (StringUtils.isBlank(functionInfo.getId())) {
 			isTrue(!functionServiceProvider.exists(functionInfo), FUNCTION_ALREADY_EXISTS.format(functionInfo.getPath()));
 			isTrue(functionServiceProvider.insert(functionInfo), FUNCTION_SAVE_FAILURE);
+			action = Constants.NOTIFY_ACTION_ADD;
 		} else {
 			isTrue(!functionServiceProvider.existsWithoutId(functionInfo), FUNCTION_ALREADY_EXISTS.format(functionInfo.getPath()));
 			isTrue(functionServiceProvider.update(functionInfo), FUNCTION_SAVE_FAILURE);
 			functionServiceProvider.backup(functionInfo);
 		}
 		magicFunctionManager.register(functionInfo);
+		magicNotifyService.sendNotify(new MagicNotify(instanceId, functionInfo.getId(), action, Constants.NOTIFY_ACTION_FUNCTION));
 		return functionInfo.getId();
 	}
 
@@ -206,6 +220,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 		boolean success = functionServiceProvider.delete(id);
 		if (success) {
 			magicFunctionManager.unregister(id);
+			magicNotifyService.sendNotify(new MagicNotify(instanceId, id, Constants.NOTIFY_ACTION_DELETE, Constants.NOTIFY_ACTION_FUNCTION));
 		}
 		return success;
 	}
@@ -214,7 +229,11 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 	public boolean moveFunction(String id, String groupId) {
 		isTrue(functionServiceProvider.allowMove(id, groupId), NAME_CONFLICT);
 		isTrue(magicFunctionManager.move(id, groupId), FUNCTION_PATH_CONFLICT);
-		return functionServiceProvider.move(id, groupId);
+		if (functionServiceProvider.move(id, groupId)) {
+			magicNotifyService.sendNotify(new MagicNotify(instanceId, id, Constants.NOTIFY_ACTION_UPDATE, Constants.NOTIFY_ACTION_FUNCTION));
+			return true;
+		}
+		return false;
 	}
 
 	@Override
@@ -231,6 +250,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 		} else {
 			magicFunctionManager.loadGroup();
 		}
+		magicNotifyService.sendNotify(new MagicNotify(instanceId, group.getId(), Constants.NOTIFY_ACTION_ADD, Constants.NOTIFY_ACTION_GROUP));
 		return group.getId();
 	}
 
@@ -258,6 +278,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 			functionServiceProvider.reload(group.getId());
 			return true;
 		}
+		magicNotifyService.sendNotify(new MagicNotify(instanceId, group.getId(), Constants.NOTIFY_ACTION_UPDATE, Constants.NOTIFY_ACTION_GROUP));
 		return false;
 	}
 
@@ -291,6 +312,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 				magicFunctionManager.loadGroup();
 			}
 		}
+		magicNotifyService.sendNotify(new MagicNotify(instanceId, groupId, Constants.NOTIFY_ACTION_DELETE, Constants.NOTIFY_ACTION_GROUP));
 		return success;
 	}
 
@@ -299,6 +321,58 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
 		return groupServiceProvider.groupList(type);
 	}
 
+	@Override
+	public boolean processNotify(MagicNotify magicNotify) {
+		if (magicNotify == null || instanceId.equals(magicNotify.getFrom())) {
+			return false;
+		}
+		String id = magicNotify.getId();
+		int action = magicNotify.getAction();
+		switch (magicNotify.getType()) {
+			case Constants.NOTIFY_ACTION_API:
+				return processApiNotify(id, action);
+			case Constants.NOTIFY_ACTION_FUNCTION:
+				return processFunctionNotify(id, action);
+			case Constants.NOTIFY_ACTION_GROUP:
+				return processGroupNotify(id, action);
+		}
+		return false;
+	}
+
+	private boolean processApiNotify(String id, int action) {
+		if(action == Constants.NOTIFY_ACTION_DELETE){
+			mappingHandlerMapping.unregisterMapping(id, true);
+		} else {
+			// 刷新缓存
+			apiServiceProvider.list();
+			ApiInfo info = apiServiceProvider.get(id);
+			if(info != null){
+				mappingHandlerMapping.registerMapping(info, true);
+			}
+		}
+		return true;
+	}
+	private boolean processFunctionNotify(String id, int action) {
+		if(action == Constants.NOTIFY_ACTION_DELETE){
+			magicFunctionManager.unregister(id);
+		} else {
+			// 刷新缓存
+			functionServiceProvider.list();
+			FunctionInfo functionInfo = functionServiceProvider.get(id);
+			if(functionInfo != null){
+				magicFunctionManager.register(functionInfo);
+			}
+		}
+		return true;
+	}
+	private boolean processGroupNotify(String id, int action) {
+		if(action == Constants.NOTIFY_ACTION_ADD){
+			groupServiceProvider.getGroupResource(id);
+		}
+		return true;
+	}
+
+
 	@Override
 	public String getModuleName() {
 		return "magic";