All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.alogic.remote.cluster.ClusterManagerImpl Maven / Gradle / Ivy
package com.alogic.remote.cluster;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.anysoft.util.Factory;
import com.anysoft.util.IOTools;
import com.anysoft.util.JsonTools;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.XmlElementProperties;
import com.anysoft.util.XmlTools;
/**
* 集群管理器实现
* @author yyduan
* @since 1.6.8.12
*
* @version 1.6.11.16 [20180207 duanyy]
* - 增加scope支持;
*
* @version 1.6.13.31 [20210108 duanyy]
* - 加载sink对象列表时增加enable开关
*/
public class ClusterManagerImpl implements ClusterManager {
/**
* a logger of log4j
*/
protected static final Logger LOG = LoggerFactory.getLogger(ClusterManager.class);
/**
* 缺省的集群类名
*/
protected String dftClusterClazz = StaticCluster.class.getName();
/**
* 缺省集群的id
*/
protected String dftClusterId = "default";
/**
* 集群列表
*/
protected Map clusters = new ConcurrentHashMap();
@Override
public void report(Element xml) {
if (xml != null){
XmlTools.setString(xml,"module",getClass().getName());
XmlTools.setString(xml,"dftClusterClass",dftClusterClazz);
XmlTools.setString(xml,"dftClusterId",dftClusterId);
if (!clusters.isEmpty()){
Document doc = xml.getOwnerDocument();
Cluster[] list = getClusters();
for (Cluster c:list){
Element cluster = doc.createElement("cluster");
c.report(cluster);
xml.appendChild(cluster);
}
}
}
}
@Override
public void report(Map json) {
if (json != null){
JsonTools.setString(json,"module",getClass().getName());
JsonTools.setString(json,"dftClusterClass",dftClusterClazz);
JsonTools.setString(json,"dftClusterId",dftClusterId);
if (!clusters.isEmpty()){
List clusterList = new ArrayList();
Cluster[] list = getClusters();
for (Cluster c:list){
Map map = new HashMap();
c.report(map);
clusterList.add(map);
}
json.put("cluster", clusterList);
}
}
}
@Override
public void configure(Properties p) {
dftClusterClazz = PropertiesConstants.getString(p,"dftClusterClass",dftClusterClazz);
dftClusterId = PropertiesConstants.getString(p,"dftClusterId",dftClusterId);
}
@Override
public void configure(Element root, Properties p) {
Properties props = new XmlElementProperties(root,p);
configure(props);
NodeList nodeList = XmlTools.getNodeListByPath(root, "cluster");
Factory factory = new Factory();
String scope = PropertiesConstants.getString(p, "ketty.scope", "runtime");
for (int i = 0 ;i < nodeList.getLength() ;i ++){
Node n = nodeList.item(i);
if (Node.ELEMENT_NODE != n.getNodeType()){
continue;
}
Element e = (Element)n;
XmlElementProperties itemProps = new XmlElementProperties(e,props);
String itemScope = PropertiesConstants.getString(itemProps,"scope","",true);
if (StringUtils.isNotEmpty(itemScope) && !itemScope.equals(scope)){
continue;
}
boolean enable = PropertiesConstants.getBoolean(itemProps,"enable",true,true);
if (!enable){
continue;
}
try {
Cluster instance = factory.newInstance(e, props, "module", dftClusterClazz);
if (instance != null){
String id = instance.getId();
if (StringUtils.isNotEmpty(id)){
clusters.put(id, instance);
}
}
}catch (Exception ex){
LOG.error("Failed to create cluster.",ex);
}
}
}
@Override
public void close() {
Cluster [] list = getClusters();
for (Cluster c:list){
IOTools.close(c);
}
clusters.clear();
}
@Override
public Cluster getCluster(String id) {
return clusters.get(id);
}
@Override
public Cluster[] getClusters() {
return clusters.values().toArray(new Cluster[clusters.size()]);
}
@Override
public Cluster getDefaultCluster() {
return getCluster(dftClusterId);
}
}