All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.frameworkset.apollo.ApolloPropertiesFilePlugin Maven / Gradle / Ivy

The newest version!
package org.frameworkset.apollo;
/**
 * Copyright 2020 bboss
 * 

* 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. */ import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigChangeListener; import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import org.frameworkset.config.ResetTag; import org.frameworkset.spi.BaseApplicationContext; import org.frameworkset.spi.assemble.GetProperties; import org.frameworkset.spi.assemble.PropertiesContainer; import org.frameworkset.spi.assemble.plugin.PropertiesFilePlugin; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; /** *

Description:

*

*

Copyright (c) 2020

* @date 2020/7/31 11:34 * @author biaoping.yin * @version 1.0 */ public class ApolloPropertiesFilePlugin implements PropertiesFilePlugin { private static Logger logger = LoggerFactory.getLogger(ApolloPropertiesFilePlugin.class); //Properties("properties"), XML("xml"), JSON("json"), YML("yml"), YAML("yaml"); private String[] namespaces; private ConfigChangeListener configChangeListener; private boolean changeReload; @Override public String getFiles(BaseApplicationContext applicationContext,Map extendsAttributes, PropertiesContainer propertiesContainer) { return extendsAttributes.get(PropertiesContainer.apolloNamespaceName); } private String[] parserNamespaces(String namespace){ String[] ns = namespace.split(","); return ns; } private void configProperties(BaseApplicationContext applicationContext,Map extendsAttributes,String ns, ConfigFileFormat configFileFormat,Map datas ){ Config config = null; if(configFileFormat == null) config = ConfigService.getConfig(ns); else // @to fixed. { ConfigFile configFile = ConfigService.getConfigFile(ns, configFileFormat); String content = configFile.getContent(); } // if(!ResetTag.isFromReset()) {//如果是重置热加载, // if (configChangeListener != null) { // config.addChangeListener(configChangeListener); // } else if (changeReload) { // ApplicationContenxtPropertiesListener applicationContenxtPropertiesListener = new ApplicationContenxtPropertiesListener(); // applicationContenxtPropertiesListener.setGetProperties(applicationContext); // config.addChangeListener(applicationContenxtPropertiesListener); // } // } Set pros = config.getPropertyNames(); if(pros == null || pros.size() == 0) return ; for(String pname :pros){ datas.put(pname,config.getProperty(pname,null)); } } @Override public Map getConfigProperties(BaseApplicationContext applicationContext, Map extendsAttributes, PropertiesContainer propertiesContainer) { String namespace = extendsAttributes.get(PropertiesContainer.apolloNamespaceName); if(namespace == null){ throw new IllegalArgumentException("must set apolloNamespace for config element. "); } String _changeReload = extendsAttributes.get("changeReload"); boolean changeReload = _changeReload != null && _changeReload.equals("true"); this.changeReload = changeReload; String _configChangeListener = extendsAttributes.get("configChangeListener"); String _configFileFormat = extendsAttributes.get("configFileFormat"); ConfigFileFormat configFileFormat = null; if(_configFileFormat != null) { configFileFormat = ConfigFileFormat.fromString(_configFileFormat); } ConfigChangeListener configChangeListener = null; if(this.configChangeListener != null && _configChangeListener != null && !_configChangeListener.equals("")){ try { configChangeListener = (ConfigChangeListener) Class.forName(_configChangeListener).newInstance(); if(configChangeListener instanceof PropertiesChangeListener){ PropertiesChangeListener propertiesChangeListener = (PropertiesChangeListener)configChangeListener; propertiesChangeListener.setPropertiesContainer(propertiesContainer); propertiesChangeListener.setApplicationContext(applicationContext); } this.configChangeListener = configChangeListener; } catch (Exception e){ logger.error("Init configChangeListener:"+configChangeListener +" failed:",e); } } Map datas = new LinkedHashMap(); String[] ns = parserNamespaces(namespace); for(String n:ns){ configProperties( applicationContext, extendsAttributes,n, configFileFormat, datas); } namespaces = ns; return datas; } @Override public int getInitType(BaseApplicationContext applicationContext,Map extendsAttributes, PropertiesContainer propertiesContainer) { return PropertiesFilePlugin.INIT_TYPE_OUTMAP; } @Override public void restore(BaseApplicationContext applicationContext,Map extendsAttributes, PropertiesContainer propertiesContainer) { } @Override /** * 第一次初始化完毕后注册监听器 */ public void afterLoaded(GetProperties applicationContext, PropertiesContainer propertiesContainer) { if(!ResetTag.isFromReset()) {//如果是重置热加载,不需重新添加监听器 if(namespaces != null ) { if(configChangeListener == null && changeReload){ ApplicationContenxtPropertiesListener applicationContenxtPropertiesListener = new ApplicationContenxtPropertiesListener(); applicationContenxtPropertiesListener.setGetProperties(applicationContext); configChangeListener = applicationContenxtPropertiesListener; } if(configChangeListener != null) { if (configChangeListener instanceof PropertiesChangeListener) { ((PropertiesChangeListener) configChangeListener).completeLoaded(); } for (String ns : namespaces) { Config config = ConfigService.getConfig(ns); if (config == null) continue; config.addChangeListener(configChangeListener); } } } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy