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);
|
}
|
}
|
|
}
|