Skip to content

Commit

Permalink
replace hard coded values with constants and small refactor (apache#3101
Browse files Browse the repository at this point in the history
)

* repalce hard coded values with constants and small refactor

* style issue fixed
  • Loading branch information
khanimteyaz committed Jan 18, 2019
1 parent 5525690 commit a046a4f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ public class Constants {

public static final String PROTOCOL_KEY = "protocol";

public static final String DOBBO_PROTOCOL = DUBBO;

public static final String PROXY_KEY = "proxy";

public static final String WEIGHT_KEY = "weight";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,15 @@ private static boolean isTypeMatch(Class<?> type, String value) {

protected static void checkExtension(Class<?> type, String property, String value) {
checkName(property, value);
if (value != null && value.length() > 0
if (StringUtils.isNotEmpty(value)
&& !ExtensionLoader.getExtensionLoader(type).hasExtension(value)) {
throw new IllegalStateException("No such extension " + value + " for " + property + "/" + type.getName());
}
}

protected static void checkMultiExtension(Class<?> type, String property, String value) {
checkMultiName(property, value);
if (value != null && value.length() > 0) {
if (StringUtils.isNotEmpty(value)) {
String[] values = value.split("\\s*[,]+\\s*");
for (String v : values) {
if (v.startsWith(Constants.REMOVE_VALUE_PREFIX)) {
Expand Down Expand Up @@ -338,7 +338,7 @@ protected static void checkParameterName(Map<String, String> parameters) {
}

protected static void checkProperty(String property, String value, int maxlength, Pattern pattern) {
if (value == null || value.length() == 0) {
if (StringUtils.isEmpty(value)) {
return;
}
if (value.length() > maxlength) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.Version;
import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.common.utils.ConfigUtils;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.ReflectUtils;
Expand Down Expand Up @@ -203,22 +204,14 @@ protected List<URL> loadRegistries(boolean provider) {
appendParameters(registryDataConfigurationMap, registryDataConfig);
for (RegistryConfig config : registries) {
String address = config.getAddress();
if (address == null || address.length() == 0) {
if (StringUtils.isEmpty(address)) {
address = Constants.ANYHOST_VALUE;
}
if (address.length() > 0 && !RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(address)) {
if (!RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(address)) {
Map<String, String> map = new HashMap<String, String>();
appendParameters(map, application);
appendParameters(map, config);
map.put("path", RegistryService.class.getName());
map.put("dubbo", Version.getProtocolVersion());
map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
if (ConfigUtils.getPid() > 0) {
map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));
}
if (!map.containsKey("protocol")) {
map.put("protocol", "dubbo");
}
overrideParameters(map);
List<URL> urls = UrlUtils.parseURLs(address, map);

for (URL url : urls) {
Expand All @@ -241,7 +234,7 @@ protected URL loadMonitor(URL registryURL) {
checkMonitor();
Map<String, String> map = new HashMap<String, String>();
map.put(Constants.INTERFACE_KEY, MonitorService.class.getName());
map.put("dubbo", Version.getProtocolVersion());
map.put(Constants.DOBBO_PROTOCOL, Version.getProtocolVersion());
map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
if (ConfigUtils.getPid() > 0) {
map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));
Expand All @@ -266,17 +259,17 @@ protected URL loadMonitor(URL registryURL) {
if (getExtensionLoader(MonitorFactory.class).hasExtension("logstat")) {
map.put(Constants.PROTOCOL_KEY, "logstat");
} else {
map.put(Constants.PROTOCOL_KEY, "dubbo");
map.put(Constants.PROTOCOL_KEY, Constants.DOBBO_PROTOCOL);
}
}
return UrlUtils.parseURL(address, map);
} else if (Constants.REGISTRY_PROTOCOL.equals(monitor.getProtocol()) && registryURL != null) {
return registryURL.setProtocol("dubbo").addParameter(Constants.PROTOCOL_KEY, "registry").addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map));
return registryURL.setProtocol(Constants.DOBBO_PROTOCOL).addParameter(Constants.PROTOCOL_KEY, Constants.REGISTRY_PROTOCOL).addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map));
}
return null;
}

final private URL loadMetadataReporterURL(boolean provider) {
private URL loadMetadataReporterURL(boolean provider) {
this.checkApplication();
String address = metadataReportConfig.getAddress();
if (address == null || address.length() == 0) {
Expand All @@ -300,9 +293,8 @@ protected MetadataReportService getMetadataReportService() {

protected void checkInterfaceAndMethods(Class<?> interfaceClass, List<MethodConfig> methods) {
// interface cannot be null
if (interfaceClass == null) {
throw new IllegalStateException("interface not allow null!");
}
Assert.notNull(interfaceClass, new IllegalStateException("interface not allow null!"));

// to verify interfaceClass is an interface
if (!interfaceClass.isInterface()) {
throw new IllegalStateException("The interface class " + interfaceClass + " is not a interface!");
Expand All @@ -314,16 +306,11 @@ protected void checkInterfaceAndMethods(Class<?> interfaceClass, List<MethodConf
methodBean.setServiceId(this.getId());
methodBean.refresh();
String methodName = methodBean.getName();
if (methodName == null || methodName.length() == 0) {
if (StringUtils.isEmpty(methodName)) {
throw new IllegalStateException("<dubbo:method> name attribute is required! Please check: <dubbo:service interface=\"" + interfaceClass.getName() + "\" ... ><dubbo:method name=\"\" ... /></<dubbo:reference>");
}
boolean hasMethod = false;
for (java.lang.reflect.Method method : interfaceClass.getMethods()) {
if (method.getName().equals(methodName)) {
hasMethod = true;
break;
}
}

boolean hasMethod = Arrays.stream(interfaceClass.getMethods()).anyMatch(method -> method.getName().equals(methodName));
if (!hasMethod) {
throw new IllegalStateException("The interface " + interfaceClass.getName()
+ " not found method " + methodName);
Expand Down Expand Up @@ -386,6 +373,16 @@ void checkStub(Class<?> interfaceClass) {
}
}

private void overrideParameters(Map<String, String> map) {
map.put(Constants.PATH_KEY, RegistryService.class.getName());
map.put(Constants.DOBBO_PROTOCOL, Version.getProtocolVersion());
map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
if (ConfigUtils.getPid() > 0) {
map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));
}
map.putIfAbsent(Constants.PROTOCOL_KEY, Constants.DOBBO_PROTOCOL);
}

private void convertRegistryIdsToRegistries() {
if (StringUtils.isEmpty(registryIds) && (registries == null || registries.isEmpty())) {
Set<String> configedRegistries = new HashSet<>();
Expand Down Expand Up @@ -466,16 +463,6 @@ public String getLocal() {
return local;
}

/**
* @param local
* @deprecated Replace to <code>setStub(String)</code>
*/
@Deprecated
public void setLocal(String local) {
checkName("local", local);
this.local = local;
}

/**
* @param local
* @deprecated Replace to <code>setStub(Boolean)</code>
Expand All @@ -489,13 +476,18 @@ public void setLocal(Boolean local) {
}
}

public String getStub() {
return stub;
/**
* @param local
* @deprecated Replace to <code>setStub(String)</code>
*/
@Deprecated
public void setLocal(String local) {
checkName("local", local);
this.local = local;
}

public void setStub(String stub) {
checkName("stub", stub);
this.stub = stub;
public String getStub() {
return stub;
}

public void setStub(Boolean stub) {
Expand All @@ -506,6 +498,11 @@ public void setStub(Boolean stub) {
}
}

public void setStub(String stub) {
checkName("stub", stub);
this.stub = stub;
}

public String getCluster() {
return cluster;
}
Expand Down Expand Up @@ -609,14 +606,14 @@ public MonitorConfig getMonitor() {
return monitor;
}

public void setMonitor(MonitorConfig monitor) {
this.monitor = monitor;
}

public void setMonitor(String monitor) {
this.monitor = new MonitorConfig(monitor);
}

public void setMonitor(MonitorConfig monitor) {
this.monitor = monitor;
}

public String getOwner() {
return owner;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String getLoadbalance() {
}

public void setLoadbalance(String loadbalance) {
checkExtension(LoadBalance.class, "loadbalance", loadbalance);
checkExtension(LoadBalance.class, Constants.LOADBALANCE_KEY, loadbalance);
this.loadbalance = loadbalance;
}

Expand Down Expand Up @@ -137,11 +137,11 @@ public void setMock(String mock) {
}

if (mock.startsWith(Constants.RETURN_PREFIX) || mock.startsWith(Constants.THROW_PREFIX + " ")) {
checkLength("mock", mock);
checkLength(Constants.MOCK_KEY, mock);
} else if (mock.startsWith(Constants.FAIL_PREFIX) || mock.startsWith(Constants.FORCE_PREFIX)) {
checkNameHasSymbol("mock", mock);
checkNameHasSymbol(Constants.MOCK_KEY, mock);
} else {
checkName("mock", mock);
checkName(Constants.MOCK_KEY, mock);
}
this.mock = mock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public String getVersion() {
}

public void setVersion(String version) {
checkKey("version", version);
checkKey(Constants.VERSION_KEY, version);
this.version = version;
}

Expand All @@ -194,7 +194,7 @@ public String getGroup() {
}

public void setGroup(String group) {
checkKey("group", group);
checkKey(Constants.GROUP_KEY, group);
this.group = group;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.dubbo.config;

import org.apache.dubbo.common.utils.StringUtils;

/**
* ConsumerConfig
*
Expand Down Expand Up @@ -48,7 +50,7 @@ public void setTimeout(Integer timeout) {
super.setTimeout(timeout);
String rmiTimeout = System.getProperty("sun.rmi.transport.tcp.responseTimeout");
if (timeout != null && timeout > 0
&& (rmiTimeout == null || rmiTimeout.length() == 0)) {
&& (StringUtils.isEmpty(rmiTimeout))) {
System.setProperty("sun.rmi.transport.tcp.responseTimeout", String.valueOf(timeout));
}
}
Expand Down

0 comments on commit a046a4f

Please sign in to comment.