Skip to content

Commit

Permalink
* 提升版本至0.3.0-RC5
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyangming committed Mar 6, 2017
1 parent cae0a42 commit 0ad3d27
Show file tree
Hide file tree
Showing 14 changed files with 667 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bytejta-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.bytesoft</groupId>
<artifactId>bytejta-parent</artifactId>
<version>0.3.0-RC4</version>
<version>0.3.0-RC5</version>
</parent>
<artifactId>bytejta-core</artifactId>
<packaging>jar</packaging>
Expand Down
4 changes: 2 additions & 2 deletions bytejta-supports/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.bytesoft</groupId>
<artifactId>bytejta-parent</artifactId>
<version>0.3.0-RC4</version>
<version>0.3.0-RC5</version>
</parent>
<artifactId>bytejta-supports</artifactId>
<packaging>jar</packaging>
Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>org.bytesoft</groupId>
<artifactId>bytejta-core</artifactId>
<version>0.3.0-RC4</version>
<version>0.3.0-RC5</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/**
* 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.bytejta.supports.dubbo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bytesoft.bytejta.supports.dubbo.validator.ApplicationConfigValidator;
import org.bytesoft.bytejta.supports.dubbo.validator.ProtocolConfigValidator;
import org.bytesoft.bytejta.supports.dubbo.validator.ProviderConfigValidator;
import org.bytesoft.bytejta.supports.dubbo.validator.ReferenceConfigValidator;
import org.bytesoft.bytejta.supports.dubbo.validator.ServiceConfigValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class DubboConfigPostProcessor implements BeanFactoryPostProcessor {
static final Logger logger = LoggerFactory.getLogger(DubboConfigPostProcessor.class);

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
ClassLoader cl = Thread.currentThread().getContextClassLoader();

List<BeanDefinition> appNameList = new ArrayList<BeanDefinition>();
List<BeanDefinition> providerList = new ArrayList<BeanDefinition>();
List<BeanDefinition> consumerList = new ArrayList<BeanDefinition>();
List<BeanDefinition> protocolList = new ArrayList<BeanDefinition>();
List<BeanDefinition> registryList = new ArrayList<BeanDefinition>();

Map<String, BeanDefinition> serviceMap = new HashMap<String, BeanDefinition>();
Map<String, BeanDefinition> referenceMap = new HashMap<String, BeanDefinition>();

Map<String, Class<?>> clazzMap = new HashMap<String, Class<?>>();

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;
}

clazzMap.put(beanClassName, beanClass);
}

for (int i = 0; i < beanNameArray.length; i++) {
String beanName = beanNameArray[i];
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName);
String beanClassName = beanDef.getBeanClassName();

Class<?> beanClass = clazzMap.get(beanClassName);

if (com.alibaba.dubbo.config.ApplicationConfig.class.equals(beanClass)) {
appNameList.add(beanDef);
} else if (com.alibaba.dubbo.config.ProtocolConfig.class.equals(beanClass)) {
protocolList.add(beanDef);
} else if (com.alibaba.dubbo.config.RegistryConfig.class.equals(beanClass)) {
registryList.add(beanDef);
} else if (com.alibaba.dubbo.config.ProviderConfig.class.equals(beanClass)) {
providerList.add(beanDef);
} else if (com.alibaba.dubbo.config.ConsumerConfig.class.equals(beanClass)) {
consumerList.add(beanDef);
} else if (com.alibaba.dubbo.config.spring.ServiceBean.class.equals(beanClass)) {
MutablePropertyValues mpv = beanDef.getPropertyValues();
PropertyValue group = mpv.getPropertyValue("group");
if (group == null || group.getValue() == null //
|| ("org.bytesoft.bytejta".equals(group.getValue())
|| String.valueOf(group.getValue()).startsWith("org.bytesoft.bytejta-")) == false) {
continue;
}

serviceMap.put(beanName, beanDef);
} else if (com.alibaba.dubbo.config.spring.ReferenceBean.class.equals(beanClass)) {
MutablePropertyValues mpv = beanDef.getPropertyValues();
PropertyValue group = mpv.getPropertyValue("group");
if (group == null || group.getValue() == null //
|| ("org.bytesoft.bytejta".equals(group.getValue())
|| String.valueOf(group.getValue()).startsWith("org.bytesoft.bytejta-")) == false) {
continue;
}

referenceMap.put(beanName, beanDef);
}
}

Set<BeanDefinition> providerSet = new HashSet<BeanDefinition>();
Set<BeanDefinition> protocolSet = new HashSet<BeanDefinition>();
for (Iterator<Map.Entry<String, BeanDefinition>> itr = serviceMap.entrySet().iterator(); itr.hasNext();) {
Map.Entry<String, BeanDefinition> entry = itr.next();
BeanDefinition beanDef = entry.getValue();
MutablePropertyValues mpv = beanDef.getPropertyValues();
PropertyValue provider = mpv.getPropertyValue("provider");
PropertyValue protocol = mpv.getPropertyValue("protocol");

String providerValue = provider == null ? null : String.valueOf(provider.getValue());
if (providerValue == null) {
if (providerList.size() > 0) {
providerSet.add(providerList.get(0));
}
} else if ("N/A".equals(providerValue) == false) {
String[] keyArray = providerValue.split("\\s*,\\s*");
for (int j = 0; j < keyArray.length; j++) {
String key = keyArray[j];
BeanDefinition def = beanFactory.getBeanDefinition(key);
providerSet.add(def);
}
}

String protocolValue = protocol == null ? null : String.valueOf(protocol.getValue());
if (protocolValue == null) {
if (protocolList.size() > 0) {
protocolSet.add(protocolList.get(0));
}
} else if ("N/A".equals(protocolValue) == false) {
String[] keyArray = protocolValue.split("\\s*,\\s*");
for (int i = 0; i < keyArray.length; i++) {
String key = keyArray[i];
BeanDefinition def = beanFactory.getBeanDefinition(key);
protocolSet.add(def);
}
}
}

ApplicationConfigValidator appConfigValidator = new ApplicationConfigValidator();
appConfigValidator.setDefinitionList(appNameList);
appConfigValidator.validate();

if (protocolList.size() == 0) {
throw new FatalBeanException("There is no protocol config specified!");
}

for (Iterator<BeanDefinition> itr = protocolSet.iterator(); itr.hasNext();) {
BeanDefinition beanDef = itr.next();
ProtocolConfigValidator validator = new ProtocolConfigValidator();
validator.setBeanDefinition(beanDef);
validator.validate();
}

for (Iterator<BeanDefinition> itr = providerSet.iterator(); itr.hasNext();) {
BeanDefinition beanDef = itr.next();
ProviderConfigValidator validator = new ProviderConfigValidator();
validator.setBeanDefinition(beanDef);
validator.validate();
}

for (Iterator<Map.Entry<String, BeanDefinition>> itr = serviceMap.entrySet().iterator(); itr.hasNext();) {
Map.Entry<String, BeanDefinition> entry = itr.next();
ServiceConfigValidator validator = new ServiceConfigValidator();
validator.setBeanName(entry.getKey());
validator.setBeanDefinition(entry.getValue());
validator.validate(); // retries, loadbalance, cluster, filter, group
}

for (Iterator<Map.Entry<String, BeanDefinition>> itr = referenceMap.entrySet().iterator(); itr.hasNext();) {
Map.Entry<String, BeanDefinition> entry = itr.next();
ReferenceConfigValidator validator = new ReferenceConfigValidator();
validator.setBeanName(entry.getKey());
validator.setBeanDefinition(entry.getValue());
validator.validate(); // retries, loadbalance, cluster, filter
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2014-2016 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.bytejta.supports.dubbo;

import org.springframework.beans.BeansException;

public interface DubboConfigValidator {

public void validate() throws BeansException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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.bytejta.supports.dubbo.validator;

import java.util.List;

import org.bytesoft.bytejta.supports.dubbo.DubboConfigValidator;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.config.BeanDefinition;

public class ApplicationConfigValidator implements DubboConfigValidator {

private List<BeanDefinition> definitionList;

public void validate() throws BeansException {
if (this.definitionList == null || this.definitionList.isEmpty()) {
throw new FatalBeanException("There is no application name specified!");
} else if (this.definitionList.size() > 1) {
throw new FatalBeanException("There are more than one application name specified!");
}
}

public List<BeanDefinition> getDefinitionList() {
return definitionList;
}

public void setDefinitionList(List<BeanDefinition> definitionList) {
this.definitionList = definitionList;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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.bytejta.supports.dubbo.validator;

import org.bytesoft.bytejta.supports.dubbo.DubboConfigValidator;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;

public class ProtocolConfigValidator implements DubboConfigValidator {

private BeanDefinition beanDefinition;

public void validate() throws BeansException {
MutablePropertyValues mpv = this.beanDefinition.getPropertyValues();
PropertyValue pv = mpv.getPropertyValue("port");
Object value = pv == null ? null : pv.getValue();
if (pv == null || value == null) {
throw new FatalBeanException(
"The value of the attribute 'port' (<dubbo:protocol port='...' />) must be explicitly specified.");
} else if ("-1".equals(value)) {
throw new FatalBeanException(
"The value of the attribute 'port' (<dubbo:protocol port='...' />) must be explicitly specified and not equal to -1.");
}
}

public BeanDefinition getBeanDefinition() {
return beanDefinition;
}

public void setBeanDefinition(BeanDefinition beanDefinition) {
this.beanDefinition = beanDefinition;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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.bytejta.supports.dubbo.validator;

import org.bytesoft.bytejta.supports.dubbo.DubboConfigValidator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;

public class ProviderConfigValidator implements DubboConfigValidator {
private BeanDefinition beanDefinition;

public void validate() throws BeansException {
}

public BeanDefinition getBeanDefinition() {
return beanDefinition;
}

public void setBeanDefinition(BeanDefinition beanDefinition) {
this.beanDefinition = beanDefinition;
}

}
Loading

0 comments on commit 0ad3d27

Please sign in to comment.