diff --git a/api/src/main/java/com/alibaba/nacos/api/config/listener/ConfigChangeParser.java b/api/src/main/java/com/alibaba/nacos/api/config/listener/ConfigChangeParser.java new file mode 100644 index 000000000..9a6f053c0 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/config/listener/ConfigChangeParser.java @@ -0,0 +1,43 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.api.config.listener; + +import java.io.IOException; +import java.util.Map; + +/** + * ConfigChangeParser + * + * @author rushsky518 + */ +public interface ConfigChangeParser { + /** + * judge type + * @param type + * @return + */ + boolean isResponsibleFor(String type); + + /** + * compare old and new data + * @param oldContent + * @param newContent + * @param type + * @return + * @throws IOException + */ + Map doParse(String oldContent, String newContent, String type) throws IOException; +} diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/AbstractConfigChangeParser.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/AbstractConfigChangeParser.java index 1447c83a2..6c8ee6f51 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/AbstractConfigChangeParser.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/AbstractConfigChangeParser.java @@ -15,8 +15,8 @@ */ package com.alibaba.nacos.client.config.impl; -import java.io.IOException; -import java.util.Collections; +import com.alibaba.nacos.api.config.listener.ConfigChangeParser; + import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -26,50 +26,18 @@ import java.util.Map; * * @author rushsky518 */ -public abstract class AbstractConfigChangeParser { +public abstract class AbstractConfigChangeParser implements ConfigChangeParser { private String configType; - private AbstractConfigChangeParser next; - public AbstractConfigChangeParser(String configType) { this.configType = configType; } - public AbstractConfigChangeParser addNext(AbstractConfigChangeParser configChangeParser) { - if (null == this.next) { - this.next = configChangeParser; - } else { - this.next.addNext(configChangeParser); - } - return this; - } - - protected boolean isResponsibleFor(String type) { + @Override + public boolean isResponsibleFor(String type) { return this.configType.equalsIgnoreCase(type); } - public Map parseChangeData(String oldContent, String newContent, String type) throws IOException { - if (isResponsibleFor(type)) { - return this.doParse(oldContent, newContent, type); - } - - if (null != this.next) { - return this.next.parseChangeData(oldContent, newContent, type); - } - - return Collections.emptyMap(); - } - - /** - * parse and compare config data - * @param oldContent - * @param newContent - * @param type - * @return - * @throws IOException - */ - protected abstract Map doParse(String oldContent, String newContent, String type) throws IOException; - protected Map filterChangeData(Map oldMap, Map newMap) { Map result = new HashMap(16); for (Iterator> entryItr = oldMap.entrySet().iterator(); entryItr.hasNext();) { diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java index 23953cb47..5ebabe084 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java @@ -203,7 +203,7 @@ public class CacheData { // compare lastContent and content if (listener instanceof AbstractConfigChangeListener) { - Map data = ConfigChangeHandler.getChangeParserInstance().parseChangeData(listenerWrap.lastContent, content, type); + Map data = ConfigChangeHandler.getInstance().parseChangeData(listenerWrap.lastContent, content, type); ConfigChangeEvent event = new ConfigChangeEvent(data); ((AbstractConfigChangeListener)listener).receiveConfigChange(event); listenerWrap.lastContent = content; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 50b764b0a..278e26e65 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.client.config.impl; import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.config.ConfigType; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.common.GroupKey; @@ -248,6 +249,8 @@ public class ClientWorker { ct[0] = result.content; if (result.headers.containsKey(CONFIG_TYPE)) { ct[1] = result.headers.get(CONFIG_TYPE).get(0); + } else { + ct[1] = ConfigType.TEXT.getType(); } return ct; case HttpURLConnection.HTTP_NOT_FOUND: diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeHandler.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeHandler.java index 1f7de1e81..586646a09 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeHandler.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeHandler.java @@ -15,28 +15,51 @@ */ package com.alibaba.nacos.client.config.impl; +import com.alibaba.nacos.api.config.listener.ConfigChangeParser; + +import java.io.IOException; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.ServiceLoader; +import java.util.Map; + /** * ConfigChangeHandler * * @author rushsky518 */ public class ConfigChangeHandler { + private static class ConfigChangeHandlerHolder { + private final static ConfigChangeHandler INSTANCE = new ConfigChangeHandler(); + } + private ConfigChangeHandler() { - this.configChangeParser = new PropertiesChangeParser("properties").addNext(new YmlChangeParser("yaml")); + this.parserList = new LinkedList(); + + ServiceLoader loader = ServiceLoader.load(ConfigChangeParser.class); + if (loader.iterator().hasNext()) { + this.parserList.add(loader.iterator().next()); + } + + this.parserList.add(new PropertiesChangeParser()); + this.parserList.add(new YmlChangeParser()); } public static ConfigChangeHandler getInstance() { return ConfigChangeHandlerHolder.INSTANCE; } - public static AbstractConfigChangeParser getChangeParserInstance() { - return ConfigChangeHandlerHolder.INSTANCE.configChangeParser; + public Map parseChangeData(String oldContent, String newContent, String type) throws IOException { + for (ConfigChangeParser changeParser: this.parserList) { + if (changeParser.isResponsibleFor(type)) { + return changeParser.doParse(oldContent, newContent, type); + } + } + + return Collections.emptyMap(); } - private static class ConfigChangeHandlerHolder { - private final static ConfigChangeHandler INSTANCE = new ConfigChangeHandler(); - } - - private AbstractConfigChangeParser configChangeParser; + private List parserList; } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java index 5f56bd67b..4d5354869 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java @@ -28,13 +28,12 @@ import java.util.Properties; * @author rushsky518 */ public class PropertiesChangeParser extends AbstractConfigChangeParser { - - public PropertiesChangeParser(String configType) { - super(configType); + public PropertiesChangeParser() { + super("properties"); } @Override - protected Map doParse(String oldContent, String newContent, String type) throws IOException { + public Map doParse(String oldContent, String newContent, String type) throws IOException { Properties oldProps = new Properties(); Properties newProps = new Properties(); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java index 7f361d40c..19eaa2052 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java @@ -25,13 +25,12 @@ import java.util.*; * @author rushsky518 */ public class YmlChangeParser extends AbstractConfigChangeParser { - - public YmlChangeParser(String configType) { - super(configType); + public YmlChangeParser() { + super("yaml"); } @Override - protected Map doParse(String oldContent, String newContent, String type) { + public Map doParse(String oldContent, String newContent, String type) { Map oldMap = Collections.emptyMap(); Map newMap = Collections.emptyMap(); @@ -88,4 +87,3 @@ public class YmlChangeParser extends AbstractConfigChangeParser { } } - diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java index 3ae982e40..885171707 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigServletInner.java @@ -131,9 +131,7 @@ public class ConfigServletInner { } } String configType = cacheItem.getType(); - if (null != configType) { - response.setHeader("Config-Type", configType); - } + response.setHeader("Config-Type", (null != configType) ? configType : "text"); } File file = null; ConfigInfoBase configInfoBase = null;