📄 proxyruntimecontext.java
字号:
PoolableObject object = (PoolableObject)pool.borrowObject();
pool.returnObject(object);
}catch(Exception e){
}
}
}
}
private void initAllInitialisableBeans(){
for(Initialisable bean : initialisableList){
try {
bean.init();
} catch (InitialisationException e) {
throw new ConfigurationException("Initialisation Exception",e);
}
}
}
private ProxyServerConfig loadConfig(String configFileName){
DocumentBuilder db;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(false);
db = dbf.newDocumentBuilder();
db.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
if (systemId.endsWith("amoeba.dtd")) {
InputStream in = ProxyRuntimeContext.class.getResourceAsStream("/com/meidusa/amoeba/xml/amoeba.dtd");
if (in == null) {
LogLog.error("Could not find [amoeba.dtd]. Used [" + ProxyRuntimeContext.class.getClassLoader()
+ "] class loader in the search.");
return null;
} else {
return new InputSource(in);
}
} else {
return null;
}
}
});
db.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException exception) {
}
public void error(SAXParseException exception) throws SAXException {
logger.error(exception.getMessage() + " at (" + exception.getLineNumber() + ":" + exception.getColumnNumber() + ")");
throw exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
logger.fatal(exception.getMessage() + " at (" + exception.getLineNumber() + ":" + exception.getColumnNumber() + ")");
throw exception;
}
});
return loadConfigurationFile(configFileName, db);
} catch (Exception e) {
logger.fatal("Could not load configuration file, failing", e);
throw new ConfigurationException("Error loading configuration file " + configFileName, e);
}
}
private ProxyServerConfig loadConfigurationFile(String fileName, DocumentBuilder db) {
Document doc = null;
InputStream is = null;
ProxyServerConfig config = new ProxyServerConfig();
try {
is = new FileInputStream(new File(fileName));
if (is == null) {
throw new Exception("Could not open file " + fileName);
}
doc = db.parse(is);
} catch (Exception e) {
final String s = "Caught exception while loading file " + fileName;
logger.error(s, e);
throw new ConfigurationException(s, e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
logger.error("Unable to close input stream", e);
}
}
}
Element rootElement = doc.getDocumentElement();
NodeList children = rootElement.getChildNodes();
int childSize = children.getLength();
for (int i = 0; i < childSize; i++) {
Node childNode = children.item(i);
if (childNode instanceof Element) {
Element child = (Element) childNode;
final String nodeName = child.getNodeName();
if (nodeName.equals("server")) {
loadServerConfig(child,config);
}else if (nodeName.equals("connectionManagerList")) {
loadConnectionManagers(child,config);
}else if (nodeName.equals("dbServerList")) {
loadServers(child,config);
}else if (nodeName.equals("queryRouter")) {
loadQueryRouter(rootElement,config);
}
}
}
if (logger.isInfoEnabled()) {
logger.info("Loaded Engine configuration from: " + fileName);
}
return config;
}
private void loadQueryRouter(Element current,ProxyServerConfig config){
BeanObjectEntityConfig queryRouter = DocumentUtil.loadBeanConfig(DocumentUtil.getTheOnlyElement(current,"queryRouter"));
config.setQueryRouterConfig(queryRouter);
}
private void loadServers(Element current , ProxyServerConfig config) {
NodeList children = current.getChildNodes();
int childSize = children.getLength();
for (int i = 0; i < childSize; i++) {
Node childNode = children.item(i);
if (childNode instanceof Element) {
Element child = (Element) childNode;
DBServerConfig serverConfig = loadServer(child);
if(serverConfig.isVirtual()){
if(serverConfig.getPoolConfig() != null){
if(StringUtil.isEmpty( serverConfig.getPoolConfig().getClassName())){
serverConfig.getPoolConfig().setClassName(getDefaultVirtualPoolClassName());
}
}
}else{
if(serverConfig.getPoolConfig() != null){
if(StringUtil.isEmpty( serverConfig.getPoolConfig().getClassName())){
serverConfig.getPoolConfig().setClassName(getDefaultRealPoolClassName());
}
}
}
if(serverConfig.getFactoryConfig() != null){
if(StringUtil.isEmpty(serverConfig.getFactoryConfig().getClassName())){
serverConfig.getFactoryConfig().setClassName(getDefaultServerConnectionFactoryClassName());
}
}
config.addServer(serverConfig.getName(), serverConfig);
}
}
}
private DBServerConfig loadServer(Element current){
DBServerConfig serverConfig = new DBServerConfig();
NamedNodeMap nodeMap = current.getAttributes();
Map<String,Object> map = new HashMap<String,Object>();
for(int i=0;i<nodeMap.getLength();i++){
Node node = nodeMap.item(i);
if (node instanceof org.w3c.dom.Attr) {
Attr attr =(Attr)node;
map.put(attr.getName(),attr.getNodeValue());
}
}
ParameterMapping.mappingObject(serverConfig, map);
BeanObjectEntityConfig factory = DocumentUtil.loadBeanConfig(DocumentUtil.getTheOnlyElement(current,"factoryConfig"));
BeanObjectEntityConfig pool = DocumentUtil.loadBeanConfig(DocumentUtil.getTheOnlyElement(current,"poolConfig"));
if(pool != null){
serverConfig.setPoolConfig(pool);
}
if(factory != null){
serverConfig.setFactoryConfig(factory);
}
return serverConfig;
}
private void loadConnectionManagers(Element current, ProxyServerConfig config) {
NodeList children = current.getChildNodes();
int childSize = children.getLength();
for (int i = 0; i < childSize; i++) {
Node childNode = children.item(i);
if (childNode instanceof Element) {
Element child = (Element) childNode;
BeanObjectEntityConfig managerConfig = DocumentUtil.loadBeanConfig(child);
if(StringUtil.isEmpty(managerConfig.getClassName())){
managerConfig.setClassName(getDefaultServerConnectionManagerClassName());
}
config.addManager(managerConfig.getName(), managerConfig);
}
}
}
private void loadServerConfig(Element current,ProxyServerConfig config) {
NodeList children = current.getChildNodes();
int childSize = children.getLength();
Map<String,Object> map = new HashMap<String,Object>();
for (int i = 0; i < childSize; i++) {
Node childNode = children.item(i);
if (childNode instanceof Element) {
Element child = (Element) childNode;
final String nodeName = child.getNodeName();
if (nodeName.equals("property")) {
String key = child.getAttribute("name");
String value = child.getTextContent();
map.put(key, value);
}
}
}
ParameterMapping.mappingObject(config, map);
}
public void appendReport(StringBuilder buffer, long now, long sinceLast,
boolean reset,Level level) {
for(Map.Entry<String, ObjectPool> entry :getPoolMap().entrySet()){
ObjectPool pool = entry.getValue();
String poolName = entry.getKey();
buffer.append("* Server pool=").append(poolName == null?"default pool":poolName).append("\n")
.append(" - pool active Size=").append(pool.getNumActive());
buffer.append(", pool Idle size=").append(pool.getNumIdle()).append("\n");
}
}
public ProxyServerConfig getConfig() {
return config;
}
public QueryRouter getQueryRouter() {
return queryRouter;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -