DUBBO-51 配有多协议时,必须指定缺省协议,否则报错

git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@239 1a56cb94-b969-4eaa-88fa-be21384802f2
This commit is contained in:
william.liangf 2011-11-10 14:06:33 +00:00
parent 41c17d6f8f
commit bafe99e4b3
3 changed files with 61 additions and 43 deletions

View File

@ -105,6 +105,20 @@ public abstract class AbstractConfig implements Serializable {
appendMaps(parameters, config, prefix, true);
}
private static boolean isPrimitive(Class<?> type) {
return type.isPrimitive()
|| type == String.class
|| type == Character.class
|| type == Boolean.class
|| type == Byte.class
|| type == Short.class
|| type == Integer.class
|| type == Long.class
|| type == Float.class
|| type == Double.class
|| type == Object.class;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void appendMaps(Map parameters, Object config, String prefix, boolean attribute) {
if (config == null) {
@ -118,16 +132,7 @@ public abstract class AbstractConfig implements Serializable {
&& ! "getClass".equals(name)
&& Modifier.isPublic(method.getModifiers())
&& method.getParameterTypes().length == 0
&& (method.getReturnType() == String.class
|| method.getReturnType() == Character.class
|| method.getReturnType() == Boolean.class
|| method.getReturnType() == Byte.class
|| method.getReturnType() == Short.class
|| method.getReturnType() == Integer.class
|| method.getReturnType() == Long.class
|| method.getReturnType() == Float.class
|| method.getReturnType() == Double.class
|| method.getReturnType() == Object.class)) {
&& isPrimitive(method.getReturnType())) {
Parameter parameter = method.getAnnotation(Parameter.class);
if (attribute){
if (parameter == null || !parameter.attribute())
@ -271,5 +276,47 @@ public abstract class AbstractConfig implements Serializable {
}
}, "DubboShutdownHook"));
}
@Override
public String toString() {
try {
String tag = getClass().getSimpleName();
if (tag.equals("Config")) {
tag = tag.substring(0, tag.length() - "Config".length());
}
tag = tag.toLowerCase();
StringBuilder buf = new StringBuilder();
buf.append("<dubbo:");
buf.append(tag);
Method[] methods = getClass().getMethods();
for (Method method : methods) {
try {
String name = method.getName();
if ((name.startsWith("get") || name.startsWith("is"))
&& ! "getClass".equals(name)
&& Modifier.isPublic(method.getModifiers())
&& method.getParameterTypes().length == 0
&& isPrimitive(method.getReturnType())) {
int i = name.startsWith("get") ? 3 : 2;
String key = name.substring(i, i + 1).toLowerCase() + name.substring(i + 1);
Object value = method.invoke(this, new Object[0]);
if (value != null) {
buf.append(" ");
buf.append(key);
buf.append("=\"");
buf.append(value);
buf.append("\"");
}
}
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
buf.append(" />");
return buf.toString();
} catch (Throwable t) { // 防御性容错
return super.toString();
}
}
}

View File

@ -140,36 +140,4 @@ public class ApplicationConfig extends AbstractConfig {
this.monitor = new MonitorConfig(monitor);
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder("<dubbo:application");
if (name != null && name.length() > 0) {
buf.append(" name=\"");
buf.append(name);
buf.append("\"");
}
if (owner != null && owner.length() > 0) {
buf.append(" owner=\"");
buf.append(owner);
buf.append("\"");
}
if (organization != null && organization.length() > 0) {
buf.append(" organization=\"");
buf.append(organization);
buf.append("\"");
}
if (architecture != null && architecture.length() > 0) {
buf.append(" architecture=\"");
buf.append(architecture);
buf.append("\"");
}
if (environment != null && environment.length() > 0) {
buf.append(" environment=\"");
buf.append(environment);
buf.append("\"");
}
buf.append(" />");
return buf.toString();
}
}

View File

@ -150,7 +150,10 @@ public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean
if ((getProtocols() == null || getProtocols().size() == 0)
&& (getProvider() == null || getProvider().getProtocols() == null || getProvider().getProtocols().size() == 0)) {
Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null : applicationContext.getBeansOfType(ProtocolConfig.class, false, false);
if (protocolConfigMap != null && protocolConfigMap.size() > 0) {
if (protocolConfigMap != null && protocolConfigMap.size() > 0) {
if (protocolConfigMap.size() > 1) {
throw new IllegalStateException("Found multi-protocols: " + protocolConfigMap.values() + ", You must be set default protocol in: <dubbo:provider protocol=\"dubbo\" />, or set service protocol in: <dubbo:service protocol=\"dubbo\" />");
}
Collection<ProtocolConfig> protocolConfigs = protocolConfigMap.values();
if (protocolConfigs != null && protocolConfigs.size() > 0) {
setProtocols(new ArrayList<ProtocolConfig>(protocolConfigs));