WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package tech.powerjob.remote.framework.engine.impl;
 
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import tech.powerjob.remote.framework.actor.Actor;
import tech.powerjob.remote.framework.actor.ActorInfo;
import tech.powerjob.remote.framework.actor.Handler;
import tech.powerjob.remote.framework.actor.HandlerInfo;
import tech.powerjob.remote.framework.base.HandlerLocation;
 
import java.lang.reflect.Method;
import java.util.List;
 
/**
 * load all Actor
 *
 * @author tjq
 * @since 2022/12/31
 */
@Slf4j
class ActorFactory {
 
    static List<ActorInfo> load(List<Object> actorList) {
 
        List<ActorInfo> actorInfos = Lists.newArrayList();
 
        actorList.forEach(actor -> {
            final Class<?> clz = actor.getClass();
            try {
                final Actor anno = clz.getAnnotation(Actor.class);
 
                ActorInfo actorInfo = new ActorInfo().setActor(actor).setAnno(anno);
                actorInfo.setHandlerInfos(loadHandlerInfos4Actor(actorInfo));
 
                actorInfos.add(actorInfo);
            } catch (Throwable t) {
                log.error("[ActorFactory] process Actor[{}] failed!", clz);
                ExceptionUtils.rethrow(t);
            }
        });
 
        return actorInfos;
    }
 
    private static List<HandlerInfo> loadHandlerInfos4Actor(ActorInfo actorInfo) {
        List<HandlerInfo> ret = Lists.newArrayList();
 
        Actor anno = actorInfo.getAnno();
        String rootPath = anno.path();
        Object actor = actorInfo.getActor();
 
        findHandlerMethod(rootPath, actor.getClass(), ret);
        return ret;
    }
 
    private static void findHandlerMethod(String rootPath, Class<?> clz, List<HandlerInfo> result) {
        Method[] declaredMethods = clz.getDeclaredMethods();
        for (Method handlerMethod: declaredMethods) {
            Handler handlerMethodAnnotation = handlerMethod.getAnnotation(Handler.class);
            if (handlerMethodAnnotation == null) {
                continue;
            }
 
            HandlerLocation handlerLocation = new HandlerLocation()
                    .setRootPath(suitPath(rootPath))
                    .setMethodPath(suitPath(handlerMethodAnnotation.path()));
 
            HandlerInfo handlerInfo = new HandlerInfo()
                    .setAnno(handlerMethodAnnotation)
                    .setMethod(handlerMethod)
                    .setLocation(handlerLocation);
            result.add(handlerInfo);
        }
 
        // 递归处理父类
        final Class<?> superclass = clz.getSuperclass();
        if (superclass != null) {
            findHandlerMethod(rootPath, superclass, result);
        }
    }
 
    static String suitPath(String path) {
        if (path.startsWith("/")) {
            return path.replaceFirst("/", "");
        }
        return path;
    }
}