/**
* Copyright 2014-2017 yangming.liu<bytefox@126.com>.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, see <http://www.gnu.org/licenses/>.
*/
package org.bytesoft.bytetcc.supports.springcloud;
import java.util.ArrayList;
import java.util.List;
import org.bytesoft.common.utils.CommonUtils;
import org.bytesoft.compensable.CompensableBeanFactory;
import org.bytesoft.compensable.aware.CompensableBeanFactoryAware;
import org.bytesoft.compensable.aware.CompensableEndpointAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
public class SpringCloudEndpointPostProcessor
implements BeanFactoryPostProcessor, CompensableBeanFactoryAware, EnvironmentAware {
static final Logger logger = LoggerFactory.getLogger(SpringCloudEndpointPostProcessor.class);
private CompensableBeanFactory beanFactory;
private Environment environment;
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
List<BeanDefinition> beanDefList = new ArrayList<BeanDefinition>();
String[] beanNameArray = beanFactory.getBeanDefinitionNames();
for (int i = 0; i < beanNameArray.length; i++) {
String beanName = beanNameArray[i];
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName);
String beanClassName = beanDef.getBeanClassName();
Class<?> beanClass = null;
try {
beanClass = cl.loadClass(beanClassName);
} catch (Exception ex) {
logger.debug("Cannot load class {}, beanId= {}!", beanClassName, beanName, ex);
continue;
}
if (CompensableEndpointAware.class.isAssignableFrom(beanClass)) {
beanDefList.add(beanDef);
}
}
String host = CommonUtils.getInetAddress();
String port = this.environment.getProperty("server.port");
String identifier = String.format("%s:%s", host, port);
for (int i = 0; i < beanDefList.size(); i++) {
BeanDefinition beanDef = beanDefList.get(i);
MutablePropertyValues mpv = beanDef.getPropertyValues();
mpv.addPropertyValue(CompensableEndpointAware.ENDPOINT_FIELD_NAME, identifier);
}
}
public CompensableBeanFactory getBeanFactory() {
return beanFactory;
}
public void setBeanFactory(CompensableBeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
}