P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package foundation.server.config;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
import foundation.route.RouteTable;
import foundation.server.ControllerConfig;
import foundation.server.Initializer;
import foundation.server.InitializerContainer;
import foundation.server.ObjectCreator;
import foundation.server.VirtualPathConfig;
import foundation.translator.Translator;
import foundation.util.CaseInsensitiveSet;
import foundation.util.Util;
 
 
public class ServerAssembleLoader {
 
    protected static Logger logger;
    private static ServerAssembleLoader instance;
    private static InitializerContainer initializerContainer;
    private static List<VirtualPathConfig> virtualPathList;
    private static List<ControllerConfig> controllerList;
    public static Set<String> activeDataSourceList;
 
    static {
        logger = LogManager.getLogger(ServerAssembleLoader.class);
        initializerContainer = InitializerContainer.getInstance();
    }
 
    private ServerAssembleLoader() {
        virtualPathList = new ArrayList<VirtualPathConfig>();
        controllerList = new ArrayList<ControllerConfig>();
        activeDataSourceList = new CaseInsensitiveSet();
    }
 
    public synchronized static ServerAssembleLoader getInstance() {
        if (instance == null) {
            instance = new ServerAssembleLoader();
        }
 
        return instance;
    }
 
    public static void staticLoad() throws Exception {
        ServerAssembleLoader instance = getInstance();
        instance.load();
    }
    
    public void load() throws Exception {
        //1. load configuration file
        String path = Configer.getPath_ServerConfig();
        File file = new File(path);
        loadOneFile(file);
        
        //2. create virtual path
        for (VirtualPathConfig virtualPath: virtualPathList) {
            createOneRoute(virtualPath);
        }
        
        //3. create controller path
        for (ControllerConfig callableConfig: controllerList) {
            createOneRoute(callableConfig);
        }
    }
 
    private static void loadOneFile(File file) throws Exception {
        try {
            logger.debug("load config file:" + file);
            InputStream inputStream = new FileInputStream(file);
 
            try {
                SAXReader reader = new SAXReader();
                reader.setValidation(false);
 
                Document doc = reader.read(inputStream);
                Element root = doc.getRootElement();
 
                loadFreeVisit(root);
                loadRouteConfig(root);
                loadInitializers(root);
                loadActiveDataSource(root);
            } 
            finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        } catch (Exception e) {
            logger.error("can not load dispatch file: " + file);
            logger.error(e);
            throw e;
        }
    }
    
    private static void loadFreeVisit(Element root) throws Exception {
        Iterator<?> iterator = root.elementIterator("freeVisit");
 
        while (iterator.hasNext()) {
            Element element = (Element) iterator.next();
 
            Iterator<?> typeIterator = element.elementIterator("type");
            while (typeIterator.hasNext()) {
                Element elemnet = (Element) typeIterator.next();
                String type = elemnet.getTextTrim();
                RouteTable.addFreeVisitResourceType(type);
            }
 
            Iterator<?> resourceIterator = element.elementIterator("resource");
            while (resourceIterator.hasNext()) {
                Element elemnet = (Element) resourceIterator.next();
                String resource = elemnet.getTextTrim();
                RouteTable.appendFreeVisitResource(resource);
            }
 
            Iterator<?> callIterator = element.elementIterator("call");
            while (callIterator.hasNext()) {
                Element elemnet = (Element) callIterator.next();
                String call = elemnet.getTextTrim();
                RouteTable.appendFreeVisitCall(call);
            }
        }
    }
    
    private static void loadRouteConfig(Element root) throws Exception {
        Iterator<?> iterator = root.elementIterator("routes");
 
        while (iterator.hasNext()) {
            Element element = (Element) iterator.next();
 
            Iterator<?> mappingIterator = element.elementIterator("route");
            while (mappingIterator.hasNext()) {
                Element elemnet = (Element) mappingIterator.next();
 
                String path = elemnet.attributeValue("path");
                String target = elemnet.attributeValue("target");
                String className = elemnet.attributeValue("classname");
 
                if (!Util.isEmpty(className)) {
                    ControllerConfig controllerConfig = new ControllerConfig(path, className);
                    controllerList.add(controllerConfig);
                }
                else if (!Util.isEmpty(target)) {
                    VirtualPathConfig virtualPath = new VirtualPathConfig(path, target, className);
                    virtualPathList.add(virtualPath);
                }
            }
        }
    }
 
    private static void loadInitializers(Element root) throws Exception {
        Iterator<?> iterator = root.elementIterator("initializers");
 
        while (iterator.hasNext()) {
            Element element = (Element) iterator.next();
            Iterator<?> configIterator = element.elementIterator("initializer");
 
            while (configIterator.hasNext()) {
                Element loaderEl = (Element) configIterator.next();
 
                String name = loaderEl.attributeValue("name");
                String classname = loaderEl.attributeValue("classname");
                boolean active = Translator.toBoolean(loaderEl.attributeValue("active"), true);
                
                if (!active) {
                    logger.debug("loader not active, skip: " + name);
                    continue;
                }
                
                Initializer initializer = ObjectCreator.create(classname);
                initializer.setName(name);
                
                initializerContainer.add(initializer);
            }
        }
    }
    
    private static void createOneRoute(VirtualPathConfig virtualPath) throws Exception {
        if (virtualPath.invalid()) {
            return;
        }
 
        RouteTable.appendVirtualPath(virtualPath.getPath(), virtualPath.getTarget());
    }
 
    private static void createOneRoute(ControllerConfig callableConfig) throws Exception {
        if (callableConfig.invalid()) {
            return;
        }
 
        RouteTable.appendCallableClass(callableConfig.getPath(), callableConfig.getClassName());
    }
    
    private static void loadActiveDataSource(Element root) {
        Element parent = root.element("datasources");
        Iterator<?> iterator = parent.elementIterator("datasource");
        
        while (iterator.hasNext()) {
            Element element = (Element) iterator.next();
            String name = element.attributeValue("name");
            boolean active = Translator.toBoolean(element.attributeValue("active"), false);
            
            if (!active) {
                continue;
            }
            
            activeDataSourceList.add(name);
        }
    }
 
}