/*
 * Copyright 1999-2020 Alibaba Group Holding Ltd.
 *
 * 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.alibaba.nacos.api.remote;

import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.api.remote.response.Response;
import com.google.common.collect.Lists;
import org.reflections.Reflections;

import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * payload regitry,include all request and response.
 *
 * @author liuzunfei
 * @version $Id: PayloadRegistry.java, v 0.1 2020年09月01日 10:56 AM liuzunfei Exp $
 */

public class PayloadRegistry {
    
    private static final Map<String, Class> REGISTRY_REQUEST = new HashMap<String, Class>();
    
    static boolean inited = false;
    
    public static void init() {
        scan();
    }
    
    private static synchronized void scan() {
        if (inited) {
            return;
        }
        List<String> scanPackage = Lists.newArrayList("com.alibaba.nacos.api.naming.remote.request",
                "com.alibaba.nacos.api.naming.remote.response", "com.alibaba.nacos.api.config.remote.request",
                "com.alibaba.nacos.api.config.remote.response", "com.alibaba.nacos.api.remote.request",
                "com.alibaba.nacos.api.remote.response", "com.alibaba.nacos.naming.cluster.remote.request",
                "com.alibaba.nacos.naming.cluster.remote.response");
        for (String pkg : scanPackage) {
            Reflections reflections = new Reflections(pkg);
            Set<Class<? extends Request>> subTypesRequest = reflections.getSubTypesOf(Request.class);
            Set<Class<? extends Response>> subTypesOfResponse = reflections.getSubTypesOf(Response.class);
            for (Class clazz : subTypesRequest) {
                register(clazz.getName(), clazz);
            }
            for (Class clazz : subTypesOfResponse) {
                register(clazz.getName(), clazz);
            }
        }
        inited = true;
        
    }
    
    static void register(String type, Class clazz) {
        if (Modifier.isAbstract(clazz.getModifiers())) {
            return;
        }
        if (Modifier.isInterface(clazz.getModifiers())) {
            return;
        }
        if (REGISTRY_REQUEST.containsKey(type)) {
            throw new RuntimeException(String.format("Fail to register, type:%s ,clazz:%s ", type, clazz.getName()));
        }
        REGISTRY_REQUEST.put(type, clazz);
    }
    
    public static Class getClassByType(String type) {
        return REGISTRY_REQUEST.get(type);
    }
}

@@ -1454,32 +1454,41 @@
 turn;%0A        %7D%0A
+        %0A
         List%3CStr
@@ -1492,17 +1492,24 @@
 String%3E 
-s
+requestS
 canPacka
@@ -1622,357 +1622,136 @@
 api.
-naming.remote.response%22, %22com.alibaba.nacos.api.config.remote.request%22,%0A                %22com.alibaba.nacos.api.config.remote.response%22, %22com.alibaba.nacos.api.remote.request%22,%0A                %22com.alibaba.nacos.api.remote.response%22, %22com.alibaba.nacos.naming.cluster.remote.request%22,%0A                %22com.alibaba.nacos.naming.cluster.remote.response
+config.remote.request%22, %22com.alibaba.nacos.api.remote.request%22,%0A                %22com.alibaba.nacos.naming.cluster.remote.request
 %22);%0A
@@ -1776,17 +1776,24 @@
 g pkg : 
-s
+requestS
 canPacka
@@ -1964,115 +1964,8 @@
 s);%0A
-            Set%3CClass%3C? extends Response%3E%3E subTypesOfResponse = reflections.getSubTypesOf(Response.class);%0A
     
@@ -2066,32 +2066,551 @@
 ;%0A            %7D%0A
+        %7D%0A        %0A        List%3CString%3E responseScanPackage = Lists.newArrayList(%22com.alibaba.nacos.api.naming.remote.response%22,%0A                %22com.alibaba.nacos.api.config.remote.response%22, %22com.alibaba.nacos.api.remote.response%22,%0A                %22com.alibaba.nacos.naming.cluster.remote.response%22);%0A        for (String pkg : responseScanPackage) %7B%0A            Reflections reflections = new Reflections(pkg);%0A            Set%3CClass%3C? extends Response%3E%3E subTypesOfResponse = reflections.getSubTypesOf(Response.class);%0A
             for 
@@ -2712,32 +2712,41 @@
     %7D%0A        %7D%0A
+        %0A
         inited =
@@ -2760,18 +2760,14 @@
     
-    
+%7D
 %0A    
-%7D
 %0A   

Optimize PayloadRegistry#init (#4505)