Commit 2e969ac7 by Nepxion

重构类结构

parent e5a8bd95
...@@ -9,6 +9,8 @@ package com.nepxion.discovery.plugin.configcenter; ...@@ -9,6 +9,8 @@ package com.nepxion.discovery.plugin.configcenter;
* @version 1.0 * @version 1.0
*/ */
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
...@@ -16,8 +18,10 @@ import java.util.List; ...@@ -16,8 +18,10 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute; import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element; import org.dom4j.Element;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -45,6 +49,23 @@ public class ConfigParser extends Dom4JParser { ...@@ -45,6 +49,23 @@ public class ConfigParser extends Dom4JParser {
@Autowired @Autowired
private ReentrantReadWriteLock reentrantReadWriteLock; private ReentrantReadWriteLock reentrantReadWriteLock;
@Override
public void parse(InputStream inputStream) {
try {
String text = IOUtils.toString(inputStream, PluginConstant.ENCODING_UTF_8);
super.parse(text);
} catch (IOException e) {
throw new PluginException(e);
} catch (DocumentException e) {
throw new PluginException(e);
} finally {
if (inputStream != null) {
IOUtils.closeQuietly(inputStream);
}
}
}
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
protected void parseRoot(Element element) { protected void parseRoot(Element element) {
......
...@@ -9,13 +9,10 @@ package com.nepxion.discovery.plugin.configcenter; ...@@ -9,13 +9,10 @@ package com.nepxion.discovery.plugin.configcenter;
* @version 1.0 * @version 1.0
*/ */
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.apache.commons.io.IOUtils;
import org.dom4j.DocumentException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -25,7 +22,6 @@ import com.google.common.eventbus.Subscribe; ...@@ -25,7 +22,6 @@ import com.google.common.eventbus.Subscribe;
import com.nepxion.discovery.plugin.configcenter.constant.ConfigConstant; import com.nepxion.discovery.plugin.configcenter.constant.ConfigConstant;
import com.nepxion.discovery.plugin.configcenter.loader.ConfigLoader; import com.nepxion.discovery.plugin.configcenter.loader.ConfigLoader;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant; import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.nepxion.eventbus.annotation.EventBus; import com.nepxion.eventbus.annotation.EventBus;
import com.nepxion.eventbus.core.Event; import com.nepxion.eventbus.core.Event;
...@@ -70,7 +66,7 @@ public class ConfigSubscriber { ...@@ -70,7 +66,7 @@ public class ConfigSubscriber {
} else { } else {
inputStream = configLoader.getLocalInputStream(); inputStream = configLoader.getLocalInputStream();
} }
parse(inputStream); configParser.parse(inputStream);
} }
@Subscribe @Subscribe
...@@ -92,27 +88,7 @@ public class ConfigSubscriber { ...@@ -92,27 +88,7 @@ public class ConfigSubscriber {
LOG.info("********** Remote config change has been retrieved **********"); LOG.info("********** Remote config change has been retrieved **********");
InputStream inputStream = (InputStream) object; InputStream inputStream = (InputStream) object;
parse(inputStream); configParser.parse(inputStream);
}
}
private void parse(InputStream inputStream) {
if (inputStream == null) {
throw new PluginException("Failed to load " + (remoteConfigEnabled ? "remote" : "local") + " config, no input stream returns");
}
try {
String content = IOUtils.toString(inputStream, PluginConstant.ENCODING_UTF_8);
configParser.parse(content);
} catch (IOException e) {
throw new PluginException(e);
} catch (DocumentException e) {
throw new PluginException(e);
} finally {
if (inputStream != null) {
IOUtils.closeQuietly(inputStream);
}
} }
} }
} }
\ No newline at end of file
...@@ -14,6 +14,7 @@ import java.io.IOException; ...@@ -14,6 +14,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.dom4j.Element; import org.dom4j.Element;
...@@ -24,6 +25,10 @@ public abstract class Dom4JParser { ...@@ -24,6 +25,10 @@ public abstract class Dom4JParser {
private InputStream inputStream; private InputStream inputStream;
public void parse(String text) throws DocumentException { public void parse(String text) throws DocumentException {
if (StringUtils.isEmpty(text)) {
throw new IllegalArgumentException("The text is empty");
}
Document document = Dom4JReader.getDocument(text); Document document = Dom4JReader.getDocument(text);
this.text = text; this.text = text;
...@@ -32,6 +37,10 @@ public abstract class Dom4JParser { ...@@ -32,6 +37,10 @@ public abstract class Dom4JParser {
} }
public void parseFormat(String text) throws DocumentException, UnsupportedEncodingException { public void parseFormat(String text) throws DocumentException, UnsupportedEncodingException {
if (StringUtils.isEmpty(text)) {
throw new IllegalArgumentException("The text is empty");
}
Document document = Dom4JReader.getFormatDocument(text); Document document = Dom4JReader.getFormatDocument(text);
this.text = text; this.text = text;
...@@ -40,6 +49,10 @@ public abstract class Dom4JParser { ...@@ -40,6 +49,10 @@ public abstract class Dom4JParser {
} }
public void parse(File file) throws DocumentException, IOException, UnsupportedEncodingException { public void parse(File file) throws DocumentException, IOException, UnsupportedEncodingException {
if (file == null) {
throw new IllegalArgumentException("The file is null");
}
Document document = Dom4JReader.getDocument(file); Document document = Dom4JReader.getDocument(file);
this.file = file; this.file = file;
...@@ -48,6 +61,10 @@ public abstract class Dom4JParser { ...@@ -48,6 +61,10 @@ public abstract class Dom4JParser {
} }
public void parseFormat(File file) throws DocumentException, IOException, UnsupportedEncodingException { public void parseFormat(File file) throws DocumentException, IOException, UnsupportedEncodingException {
if (file == null) {
throw new IllegalArgumentException("The file is null");
}
Document document = Dom4JReader.getFormatDocument(file); Document document = Dom4JReader.getFormatDocument(file);
this.file = file; this.file = file;
...@@ -56,6 +73,10 @@ public abstract class Dom4JParser { ...@@ -56,6 +73,10 @@ public abstract class Dom4JParser {
} }
public void parse(InputStream inputStream) throws DocumentException, IOException { public void parse(InputStream inputStream) throws DocumentException, IOException {
if (inputStream == null) {
throw new IllegalArgumentException("The input stream is null");
}
Document document = Dom4JReader.getDocument(inputStream); Document document = Dom4JReader.getDocument(inputStream);
this.inputStream = inputStream; this.inputStream = inputStream;
...@@ -64,6 +85,10 @@ public abstract class Dom4JParser { ...@@ -64,6 +85,10 @@ public abstract class Dom4JParser {
} }
public void parseFormat(InputStream inputStream) throws DocumentException, IOException, UnsupportedEncodingException { public void parseFormat(InputStream inputStream) throws DocumentException, IOException, UnsupportedEncodingException {
if (inputStream == null) {
throw new IllegalArgumentException("The input stream is null");
}
Document document = Dom4JReader.getFormatDocument(inputStream); Document document = Dom4JReader.getFormatDocument(inputStream);
this.inputStream = inputStream; this.inputStream = inputStream;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment