Skip to content

Commit

Permalink
Replace hard coded dubbo-config-api values and small code optimization (
Browse files Browse the repository at this point in the history
#3108)

* Refactoring to replace hard coded value with constant and some code optiomization

* Fixed UT failure

* Added review comment by Ian and code4wt
  • Loading branch information
khanimteyaz authored and beiwei30 committed Jan 2, 2019
1 parent 1b8d952 commit e172b29
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 107 deletions.
44 changes: 44 additions & 0 deletions dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ public class Constants {

public static final String DOBBO_PROTOCOL = DUBBO;

public static final String ZOOKEEPER_PROTOCOL = "zookeeper";

public static final String PROXY_KEY = "proxy";

public static final String WEIGHT_KEY = "weight";
Expand Down Expand Up @@ -324,6 +326,8 @@ public class Constants {

public static final String EXCHANGER_KEY = "exchanger";

public static final String DISPACTHER_KEY = "dispacther";

public static final String TRANSPORTER_KEY = "transporter";

public static final String SERVER_KEY = "server";
Expand Down Expand Up @@ -769,6 +773,46 @@ public class Constants {
public static final String[] DEFAULT_REGISTER_CONSUMER_KEYS = {APPLICATION_KEY, VERSION_KEY, GROUP_KEY, DUBBO_VERSION_KEY, SPECIFICATION_VERSION_KEY};

public static final String TELNET = "telnet";

/**
* Application name;
*/
public static final String NAME = "name";

/**
* Application owner name;
*/
public static final String OWNER = "owner";

/**
* Running application organization name.
*/
public static final String ORGANIZATION = "organization";

/**
* Application architecture name.
*/
public static final String ARCHITECTURE = "architecture";

/**
* Environment name
*/
public static final String ENVIRONMENT = "environment";

/**
* Test environment key.
*/
public static final String TEST_ENVIRONMENT = "test";

/**
* Development environment key.
*/
public static final String DEVELOPMENT_ENVIRONMENT = "develop";

/**
* Production environment key.
*/
public static final String PRODUCTION_ENVIRONMENT = "product";
/*
* private Constants(){ }
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String getVersion() {
}

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

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

public void setGroup(String group) {
checkKey("group", group);
checkKey(Constants.GROUP_KEY, group);
this.group = group;
}

Expand Down Expand Up @@ -138,7 +138,7 @@ public void setToken(Boolean token) {
}

public void setToken(String token) {
checkName("token", token);
checkName(Constants.TOKEN_KEY, token);
this.token = token;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public String getName() {
}

public void setName(String name) {
checkName("name", name);
checkName(Constants.NAME, name);
this.name = name;
if (id == null || id.length() == 0) {
if (StringUtils.isEmpty(id)) {
id = name;
}
}
Expand All @@ -123,7 +123,7 @@ public String getOwner() {
}

public void setOwner(String owner) {
checkMultiName("owner", owner);
checkMultiName(Constants.OWNER, owner);
this.owner = owner;
}

Expand All @@ -132,7 +132,7 @@ public String getOrganization() {
}

public void setOrganization(String organization) {
checkName("organization", organization);
checkName(Constants.ORGANIZATION, organization);
this.organization = organization;
}

Expand All @@ -141,7 +141,7 @@ public String getArchitecture() {
}

public void setArchitecture(String architecture) {
checkName("architecture", architecture);
checkName(Constants.ARCHITECTURE, architecture);
this.architecture = architecture;
}

Expand All @@ -150,10 +150,18 @@ public String getEnvironment() {
}

public void setEnvironment(String environment) {
checkName("environment", environment);
checkName(Constants.ENVIRONMENT, environment);
if (environment != null) {
if (!("develop".equals(environment) || "test".equals(environment) || "product".equals(environment))) {
throw new IllegalStateException("Unsupported environment: " + environment + ", only support develop/test/product, default is product.");
if (!(Constants.DEVELOPMENT_ENVIRONMENT.equals(environment)
|| Constants.TEST_ENVIRONMENT.equals(environment)
|| Constants.PRODUCTION_ENVIRONMENT.equals(environment))) {

throw new IllegalStateException(String.format("Unsupported environment: %s, only support %s/%s/%s, default is %s.",
environment,
Constants.DEVELOPMENT_ENVIRONMENT,
Constants.TEST_ENVIRONMENT,
Constants.PRODUCTION_ENVIRONMENT,
Constants.PRODUCTION_ENVIRONMENT));
}
}
this.environment = environment;
Expand Down Expand Up @@ -284,4 +292,5 @@ public void setShutwait(String shutwait) {
public boolean isValid() {
return !StringUtils.isEmpty(name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private URL toConfigUrl() {
map.put(Constants.PATH_KEY, ConfigCenterConfig.class.getSimpleName());
// use 'zookeeper' as the default configcenter.
if (StringUtils.isEmpty(map.get(Constants.PROTOCOL_KEY))) {
map.put(Constants.PROTOCOL_KEY, "zookeeper");
map.put(Constants.PROTOCOL_KEY, Constants.ZOOKEEPER_PROTOCOL);
}
return UrlUtils.parseURL(address, map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public String getName() {
public void setName(String name) {
checkMethodName("name", name);
this.name = name;
if (id == null || id.length() == 0) {
if (StringUtils.isEmpty(id)) {
id = name;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.config;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.config.support.Parameter;

import java.util.ArrayList;
Expand Down Expand Up @@ -64,7 +65,7 @@ public String getName() {
}

public void setName(String name) {
checkName("name", name);
checkName(Constants.NAME, name);
this.name = name;
if (id == null || id.length() == 0) {
id = name;
Expand All @@ -85,7 +86,7 @@ public String getOwner() {
}

public void setOwner(String owner) {
checkName("owner", owner);
checkName(Constants.OWNER, owner);
this.owner = owner;
}

Expand All @@ -94,7 +95,7 @@ public String getOrganization() {
}

public void setOrganization(String organization) {
checkName("organization", organization);
checkName(Constants.ORGANIZATION, organization);
this.organization = organization;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.config;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.serialize.Serialization;
import org.apache.dubbo.common.status.StatusChecker;
Expand Down Expand Up @@ -154,7 +155,7 @@ public String getName() {
return name;
}

public void setName(String name) {
public final void setName(String name) {
checkName("name", name);
this.name = name;
this.updateIdIfAbsent(name);
Expand All @@ -166,7 +167,7 @@ public String getHost() {
}

public void setHost(String host) {
checkName("host", host);
checkName(Constants.HOST_KEY, host);
this.host = host;
}

Expand All @@ -175,7 +176,7 @@ public Integer getPort() {
return port;
}

public void setPort(Integer port) {
public final void setPort(Integer port) {
this.port = port;
}

Expand Down Expand Up @@ -205,7 +206,7 @@ public String getThreadpool() {
}

public void setThreadpool(String threadpool) {
checkExtension(ThreadPool.class, "threadpool", threadpool);
checkExtension(ThreadPool.class, Constants.THREADPOOL_KEY, threadpool);
this.threadpool = threadpool;
}

Expand Down Expand Up @@ -254,8 +255,8 @@ public String getCodec() {
}

public void setCodec(String codec) {
if ("dubbo".equals(name)) {
checkMultiExtension(Codec.class, "codec", codec);
if (Constants.DOBBO_PROTOCOL.equals(name)) {
checkMultiExtension(Codec.class, Constants.CODEC_KEY, codec);
}
this.codec = codec;
}
Expand All @@ -265,8 +266,8 @@ public String getSerialization() {
}

public void setSerialization(String serialization) {
if ("dubbo".equals(name)) {
checkMultiExtension(Serialization.class, "serialization", serialization);
if (Constants.DOBBO_PROTOCOL.equals(name)) {
checkMultiExtension(Serialization.class, Constants.SERIALIZATION_KEY, serialization);
}
this.serialization = serialization;
}
Expand Down Expand Up @@ -308,8 +309,8 @@ public String getServer() {
}

public void setServer(String server) {
if ("dubbo".equals(name)) {
checkMultiExtension(Transporter.class, "server", server);
if (Constants.DOBBO_PROTOCOL.equals(name)) {
checkMultiExtension(Transporter.class, Constants.SERVER_KEY, server);
}
this.server = server;
}
Expand All @@ -319,8 +320,8 @@ public String getClient() {
}

public void setClient(String client) {
if ("dubbo".equals(name)) {
checkMultiExtension(Transporter.class, "client", client);
if (Constants.DOBBO_PROTOCOL.equals(name)) {
checkMultiExtension(Transporter.class, Constants.CLIENT_KEY, client);
}
this.client = client;
}
Expand All @@ -338,7 +339,7 @@ public String getTelnet() {
}

public void setTelnet(String telnet) {
checkMultiExtension(TelnetHandler.class, "telnet", telnet);
checkMultiExtension(TelnetHandler.class, Constants.TELNET, telnet);
this.telnet = telnet;
}

Expand Down Expand Up @@ -373,7 +374,7 @@ public String getTransporter() {
}

public void setTransporter(String transporter) {
checkExtension(Transporter.class, "transporter", transporter);
checkExtension(Transporter.class, Constants.TRANSPORTER_KEY, transporter);
this.transporter = transporter;
}

Expand All @@ -382,7 +383,7 @@ public String getExchanger() {
}

public void setExchanger(String exchanger) {
checkExtension(Exchanger.class, "exchanger", exchanger);
checkExtension(Exchanger.class, Constants.EXCHANGER_KEY, exchanger);
this.exchanger = exchanger;
}

Expand Down Expand Up @@ -412,7 +413,7 @@ public String getDispatcher() {
}

public void setDispatcher(String dispatcher) {
checkExtension(Dispatcher.class, "dispacther", dispatcher);
checkExtension(Dispatcher.class, Constants.DISPACTHER_KEY, dispatcher);
this.dispatcher = dispatcher;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public String getThreadpool() {
}

public void setThreadpool(String threadpool) {
checkExtension(ThreadPool.class, "threadpool", threadpool);
checkExtension(ThreadPool.class, Constants.THREADPOOL_KEY, threadpool);
this.threadpool = threadpool;
}

Expand Down Expand Up @@ -258,7 +258,7 @@ public String getTelnet() {
}

public void setTelnet(String telnet) {
checkMultiExtension(TelnetHandler.class, "telnet", telnet);
checkMultiExtension(TelnetHandler.class, Constants.TELNET, telnet);
this.telnet = telnet;
}

Expand Down Expand Up @@ -320,7 +320,7 @@ public String getTransporter() {
}

public void setTransporter(String transporter) {
checkExtension(Transporter.class, "transporter", transporter);
checkExtension(Transporter.class, Constants.TRANSPORTER_KEY, transporter);
this.transporter = transporter;
}

Expand All @@ -329,7 +329,7 @@ public String getExchanger() {
}

public void setExchanger(String exchanger) {
checkExtension(Exchanger.class, "exchanger", exchanger);
checkExtension(Exchanger.class, Constants.EXCHANGER_KEY, exchanger);
this.exchanger = exchanger;
}

Expand Down
Loading

0 comments on commit e172b29

Please sign in to comment.