|
3 | 3 | import com.networknt.aws.lambda.handler.MiddlewareHandler; |
4 | 4 | import com.networknt.config.Config; |
5 | 5 | import com.networknt.rule.*; |
| 6 | +import com.networknt.service.SingletonServiceFactory; |
6 | 7 | import org.slf4j.Logger; |
7 | 8 | import org.slf4j.LoggerFactory; |
8 | 9 |
|
|
11 | 12 |
|
12 | 13 | public abstract class AbstractTransformerMiddleware implements MiddlewareHandler { |
13 | 14 | private static final Logger LOG = LoggerFactory.getLogger(AbstractTransformerMiddleware.class); |
14 | | - static Map<String, Object> endpointRulesMap = new HashMap<>(); |
15 | | - public static Map<String, Rule> rules; |
16 | | - public static RuleEngine ruleEngine; |
17 | 15 |
|
18 | 16 | public AbstractTransformerMiddleware() { |
19 | | - |
20 | | - // load endpointRules |
21 | | - RuleLoaderConfig ruleLoaderConfig = RuleLoaderConfig.load(); |
22 | | - if (ruleLoaderConfig.isEnabled()) { |
23 | | - if (RuleLoaderConfig.RULE_SOURCE_CONFIG_FOLDER.equals(ruleLoaderConfig.getRuleSource())) { |
24 | | - // load the rules for the service from the externalized config folder. The filename is rules.yml |
25 | | - String ruleString = Config.getInstance().getStringFromFile("rules.yml"); |
26 | | - rules = RuleMapper.string2RuleMap(ruleString); |
27 | | - LOG.info("Load YAML rules from config folder with size = {}", rules.size()); |
28 | | - // load the endpoint rule mapping from the rule-loader.yml |
29 | | - endpointRulesMap = ruleLoaderConfig.getEndpointRules(); |
30 | | - } |
31 | | - if (rules != null) { |
32 | | - // create the rule engine with the rule map. |
33 | | - ruleEngine = new RuleEngine(rules, null); |
34 | | - // iterate all action classes to initialize them to ensure that the jar file are deployed and configuration is registered. |
35 | | - // This is to prevent runtime exception and also ensure that the configuration is part of the server info response. |
36 | | - loadPluginClass(); |
37 | | - } |
38 | | - } else { |
39 | | - LOG.error("RuleLoaderConfig is not enabled. Please check the configuration."); |
40 | | - } |
41 | 17 | LOG.info("AbstractTransformerMiddleware is constructed"); |
42 | 18 | } |
43 | 19 |
|
44 | | - public static void loadPluginClass() { |
45 | | - // iterate the rules map to find the action classes. |
46 | | - for (Rule rule : rules.values()) { |
47 | | - for (RuleAction action : rule.getActions()) { |
48 | | - String actionClass = action.getActionClassName(); |
49 | | - loadActionClass(actionClass); |
50 | | - } |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - public static void loadActionClass(String actionClass) { |
55 | | - LOG.debug("load action class {}", actionClass); |
56 | | - try { |
57 | | - IAction ia = (IAction) Class.forName(actionClass).getDeclaredConstructor().newInstance(); |
58 | | - // this happens during the server startup, so the cache must be empty. No need to check. |
59 | | - ruleEngine.actionClassCache.put(actionClass, ia); |
60 | | - } catch (Exception e) { |
61 | | - LOG.error("Exception:", e); |
62 | | - throw new RuntimeException("Could not find rule action class " + actionClass, e); |
63 | | - } |
64 | | - } |
65 | | - |
66 | 20 | public static Map<String, String> convertMapValueToString(Map<String, Object> originalMap) { |
67 | 21 | Map<String, String> convertedMap = new HashMap<>(); |
68 | 22 | for (Map.Entry<String, Object> entry : originalMap.entrySet()) { |
|
0 commit comments